Commit f4ffca56

mo k <mo@mokhan.ca>
2012-06-10 00:22:17
refactor comment controller to save a new comment.
1 parent 412ad05
Changed files (2)
app/controllers/comments_controller.rb
@@ -5,17 +5,13 @@ class CommentsController < ApplicationController
     @comment = Comment.new
   end
 
-  #def create
-    #creation = Creation.find(params[:creation_id])
-    #comment = Comment.build_from(creation, current_user.id, params[:comment][:body])
-    #comment.save
-    #flash[:notice] = params[:comment][:body]
-    #redirect_to :controller => 'creations', :action => 'show', :id => params[:creation_id]
-  #end
   def create
-    CommentOnCreationCommand.new(current_user).run(params)
-
+    command_for(CommentOnCreationCommand).run(params)
     flash[:notice] = params[:comment][:body]
     redirect_to :controller => 'creations', :action => 'show', :id => params[:creation_id]
   end
+
+  def command_for(command)
+    command.new(current_user)
+  end
 end
spec/controllers/comments_controller_spec.rb
@@ -6,36 +6,27 @@ describe CommentsController do
   let(:sut) { CommentsController.new }
 
   describe 'when commenting on a creation' do
-    let(:comment) { fake }
+    let(:flash_hash) { {} }
+    let(:payload){ {:creation_id=>88, :comment => {:body => 'blah'}} }
+    let(:command){ fake }
+
+    before(:each) do
+      sut.stub(:params).and_return(payload)
+      sut.stub(:flash).and_return(flash_hash)
+      sut.stub(:redirect_to).and_return(nil)
+      sut.stub(:command_for).with(CommentOnCreationCommand).and_return(command)
+
+      sut.create
+    end
 
     it "should save the new comment" do
-      comment.should have_received(:save)
+      command.should have_received(:run, payload)
     end
     it "should display a message indicated that the comment was saved" do
-      @flash_hash[:notice].should_not be_nil
-    end
-    it "should not display an errors" do
-      @flash_hash[:error].should be_nil
+      flash_hash[:notice].should_not be_nil
     end
     it "should redirect to the creation#show page" do
      
     end
-    before(:each) do
-      creation = fake
-      user = fake
-      @flash_hash = {}
-
-      comment.stub(:save).and_return(true)
-      user.stub(:id).and_return(8)
-      sut.stub(:params).and_return({:creation_id=>88, :comment => {:body => 'blah'}})
-      sut.stub(:current_user).and_return(user)
-      sut.stub(:flash).and_return(@flash_hash)
-      sut.stub(:redirect_to).and_return(nil)
-      sut.stub(:creation_path).with(creation).and_return('url')
-      Creation.stub(:find).with(88).and_return(creation)
-      Comment.stub(:build_from).with(creation, 8, 'blah').and_return(comment)
-
-      sut.create
-    end
   end
 end