Commit a03ff0d3

mo khan <mo@mokhan.ca>
2015-01-28 04:56:41
collapse create cake command.
1 parent 63a52f3
Changed files (3)
app
controllers
services
spec
controllers
app/controllers/api/v1/cakes_controller.rb
@@ -10,15 +10,11 @@ module Api
       end
 
       def create
-        CreateCakeCommand.new(self).run(cake_params, params[:cake][:tags])
-      end
-
-      def create_cake_succeeded(cake)
-        @cake = cake
-      end
-
-      def create_cake_failed(cake)
-        @cake = cake
+        category = Category.find(cake_params[:category_id])
+        @cake = current_user.create_cake(name: cake_params[:name], category: category)
+        if @cake.save
+          PublishToTwitterJob.set(wait_until: 1.hour.from_now).perform_later(@cake)
+        end
       end
 
       def update
app/services/application/create_cake_command.rb
@@ -1,18 +0,0 @@
-class CreateCakeCommand
-  def initialize(context, current_user = context.current_user)
-    @context = context
-    @current_user = current_user
-  end
-
-  def run(attributes, tags)
-    cake = @current_user.create_cake(name: attributes[:name], category: Category.find(attributes[:category_id]))
-    @current_user.tag(cake, with: tags, on: :tags)
-
-    if cake.save
-      PublishToTwitterJob.set(wait_until: 1.hour.from_now).perform_later(cake)
-      @context.create_cake_succeeded(cake)
-    else
-      @context.create_cake_failed(cake)
-    end
-  end
-end
spec/controllers/api/v1/cakes_controller_spec.rb
@@ -46,9 +46,11 @@ describe Api::V1::CakesController do
 
       it 'creates a new project' do
         xhr :post, :create, cake: { name: 'new-cake', category_id: category.id }
+
         expect(Creation.count).to eql(1)
         expect(Creation.first.name).to eql('new-cake')
         expect(Creation.first.category).to eql(category)
+        expect(Creation.first.user).to eql(user)
       end
     end