Commit 88c307ad

mo khan <mo@mokhan.ca>
2014-05-14 04:14:09
extract remove cake command.
1 parent 303cd1b
Changed files (3)
app/controllers/creations_controller.rb
@@ -51,7 +51,7 @@ class CreationsController < ApplicationController
   end
 
   def destroy
-    current_user.creations.find(params[:id]).destroy
+    RemoveCakeCommand.new(self).run(params[:id])
     redirect_to(creations_url)
   end
 
app/services/commands/remove_cake_command.rb
@@ -0,0 +1,10 @@
+class RemoveCakeCommand
+  def initialize(context, current_user = context.current_user)
+    @context = context
+    @current_user = current_user
+  end
+
+  def run(creation_id)
+    @current_user.creations.find(creation_id).destroy
+  end
+end
app/services/create_cake_command.rb
@@ -1,13 +1,13 @@
 class CreateCakeCommand
-  def initialize(context)
+  def initialize(context, current_user = context.current_user)
     @context = context
+    @current_user = current_user
   end
 
   def run(creation_attributes, category_id, tags)
-    current_user = @context.current_user
-    creation = current_user.creations.create(creation_attributes)
+    creation = @current_user.creations.create(creation_attributes)
     creation.categories << Category.find(category_id) if category_id
-    current_user.tag(creation, with: tags, on: :tags)
+    @current_user.tag(creation, with: tags, on: :tags)
 
     if creation.save
       @context.create_cake_succeeded(creation)