Commit d4a1d71f
Changed files (5)
app
models
services
spec
models
app/models/comment.rb
@@ -25,6 +25,10 @@ class Comment < ActiveRecord::Base
c
end
+ def self.create_for(user,creation,body)
+ self.build_from(creation, user.id, body)
+ end
+
#helper method to check if a comment has children
def has_children?
self.children.size > 0
app/models/user.rb
@@ -35,5 +35,8 @@ class User < ActiveRecord::Base
def owns(creation)
creation.user == self
end
-end
+ def comment_on(creation, comment)
+ Comment.create_for(self, creation, comment)
+ end
+end
app/services/comment_on_creation_command.rb
@@ -4,7 +4,6 @@ class CommentOnCreationCommand
end
def run(params)
comment = @current_user.comment_on(Creation.find(params[:creation_id]), params[:comment][:body])
- #comment = Comment.build_from(creation, @current_user.id, params[:comment][:body])
comment.save
end
end
spec/models/comment_spec.rb
@@ -0,0 +1,18 @@
+require_relative '../../app/models/comment.rb'
+
+describe Comment do
+ describe "when creating a new comment" do
+ it "should return a comment with the correct body" do
+ @result.body.should == "blah"
+ end
+ it "should track the user id" do
+ @result.user_id.should == user.id
+ end
+ before(:each) do
+ @result = Comment.create_for(user, creation, 'blah')
+ end
+
+ let(:user) { fake }
+ let(:creation) { Creation.new }
+ end
+end
spec/models/user_spec.rb
@@ -2,13 +2,13 @@
describe User do
#describe "when a user already likes a creation" do
- #it "should not let the user like it again" do
- #creation = FactoryGirl.create(:creation)
- #user = FactoryGirl.create(:user)
- #user.add_favorite(creation)
- #user.add_favorite(creation)
- #creation.favorites.length.should eq(1)
- #end
+ #it "should not let the user like it again" do
+ #creation = FactoryGirl.create(:creation)
+ #user = FactoryGirl.create(:user)
+ #user.add_favorite(creation)
+ #user.add_favorite(creation)
+ #creation.favorites.length.should eq(1)
+ #end
#end
describe "when a website url is supplied" do
describe "when the url is valid" do
@@ -36,4 +36,17 @@ describe User do
end
end
end
+ describe "when commenting on a creation" do
+ let(:comment) { fake }
+ let(:creation) { fake }
+ let(:sut) { User.new }
+
+ it "should create a new comment" do
+ @result.should == comment
+ end
+ before(:each) do
+ Comment.stub(:create_for).with(sut, creation, "cool cake").and_return(comment)
+ @result = sut.comment_on(creation, "cool cake")
+ end
+ end
end