diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 000000000..0a31a4604 --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -0,0 +1,14 @@ +class CommentsController < ApplicationController + before_filter :authenticate_user! + + def create + target = Post.first(:id => params[:comment][:post_id]) + text = params[:comment][:text] + if current_user.comment text, :on => target + render :text => "Woo!" + else + render :text => "Boo!" + end + end + +end \ No newline at end of file diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb new file mode 100644 index 000000000..a94d176ad --- /dev/null +++ b/app/helpers/comments_helper.rb @@ -0,0 +1,9 @@ +module CommentsHelper + def target + + end + + def text + params[:comment][:text] + end +end diff --git a/app/models/comment.rb b/app/models/comment.rb new file mode 100644 index 000000000..1029e4bc3 --- /dev/null +++ b/app/models/comment.rb @@ -0,0 +1,24 @@ +class Comment + include MongoMapper::Document + include ROXML + xml_accessor :text + + + key :text, String + key :target, String + timestamps! + + key :post_id, ObjectId + belongs_to :post, :class_name => "Post" + + key :person_id, ObjectId + belongs_to :person, :class_name => "Person" + + + + def ==(other) + (self.message == other.message) && (self.person.email == other.person.email) + end + +end + diff --git a/app/models/post.rb b/app/models/post.rb index f4122e525..6a450ebac 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -8,6 +8,10 @@ class Post key :person_id, ObjectId belongs_to :person, :class_name => 'Person' + + + many :comments, :class_name => 'Comment', :foreign_key => :post_id + timestamps! diff --git a/app/models/user.rb b/app/models/user.rb index c0c5ff464..c2b03629e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,8 +8,14 @@ class User < Person devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable - before_validation :do_bad_things + + def comment(text, options = {}) + raise "Comment on what, motherfucker?" unless options[:on] + Comment.new(:person_id => self.id, :text => text, :post => options[:on]).save + end + + before_validation :do_bad_things def do_bad_things self.password_confirmation = self.password end diff --git a/app/views/comments/_comment.html.haml b/app/views/comments/_comment.html.haml new file mode 100644 index 000000000..9914fcb7a --- /dev/null +++ b/app/views/comments/_comment.html.haml @@ -0,0 +1,4 @@ +%li.comment + = comment.text + \--- + = comment.person.real_name \ No newline at end of file diff --git a/app/views/comments/_new_comment.html.haml b/app/views/comments/_new_comment.html.haml new file mode 100644 index 000000000..0480c968f --- /dev/null +++ b/app/views/comments/_new_comment.html.haml @@ -0,0 +1,7 @@ += form_for Comment.new, :remote => true do |f| + = f.error_messages + %p + /= f.label :message + = f.text_field :text, :value => "dislike!" + = f.hidden_field :post_id, :value => post.id + = f.submit 'comment', :class => 'button' \ No newline at end of file diff --git a/app/views/status_messages/_status_message.html.haml b/app/views/status_messages/_status_message.html.haml index b27d8fb00..0eb914faf 100644 --- a/app/views/status_messages/_status_message.html.haml +++ b/app/views/status_messages/_status_message.html.haml @@ -4,6 +4,12 @@ = post.message %div.time = link_to "#{time_ago_in_words(post.updated_at)} ago", status_message_path(post) + %div.comments + = render "comments/new_comment", :post => post + %ul.comment_set + - for comment in post.comments + = render "comments/comment", :comment => comment + - if mine?(post) = link_to 'Destroy', status_message_path(post), :confirm => 'Are you sure?', :method => :delete diff --git a/config/routes.rb b/config/routes.rb index ec7eab734..c021120ae 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,9 @@ Diaspora::Application.routes.draw do |map| resources :bookmarks resources :friends resources :status_messages + resources :comments + + #routes for devise, not really sure you will need to mess with this in the future, lets put default, #non mutable stuff in anohter file diff --git a/spec/models/comments_spec.rb b/spec/models/comments_spec.rb new file mode 100644 index 000000000..47aa7a39e --- /dev/null +++ b/spec/models/comments_spec.rb @@ -0,0 +1,26 @@ +require 'spec_helper' + +describe Comment do + describe "user" do + before do + @user = Factory.create :user + end + it "should be able to comment on his own status" do + status = Factory.create(:status_message, :person => @user) + status.comments.should == [] + + @user.comment "Yeah, it was great", :on => status + StatusMessage.first.comments.first.text.should == "Yeah, it was great" + end + + it "should be able to comment on a friend's status" do + friend = Factory.create :friend + status = Factory.create(:status_message, :person => @friend) + @user.comment "sup dog", :on => status + + StatusMessage.first.comments.first.text.should == "sup dog" + StatusMessage.first.comments.first.person.should == @user + end + + end +end \ No newline at end of file