Commit fd68d454

mo khan <mo@mokhan.ca>
2014-11-01 04:52:02
add tools to cake edit page.
1 parent 857cb13
Changed files (4)
app
assets
javascripts
backbone
templates
views
controllers
services
app/assets/javascripts/backbone/templates/cakes/edit.jst.ejs
@@ -43,6 +43,17 @@
           <input name="cake_tags" type="text" id="cake_tags" value="<%= tags %>" class="input-xxlarge" autocomplete="off" />
           </div>
         </div>
+        <div class="control-group">
+          <label for="cake_tools" class="control-label">Tools</label>
+          <div class="controls">
+          <% if (typeof(cake.tools) === "string") { %>
+            <% tools = _.map(cake.tools.split(','), function(x){ return x; }) %>
+          <% } else { %>
+            <% tools = _.map(cake.tools, function(x){ return x.name; }) %>
+          <% } %>
+          <input name="cake_tools" type="text" id="cake_tools" value="<%= tools %>" class="input-xxlarge" autocomplete="off" />
+          </div>
+        </div>
         <div class="form-actions">
           <button id='save-button' type="submit" class="btn btn-primary">Publish</button>
           <a href="#cakes/<%= cake.id %>" class="btn">Cancel</a>
app/assets/javascripts/backbone/views/cakes/edit_view.js.coffee
@@ -10,6 +10,7 @@ class CakeSide.Views.Cakes.EditView extends Marionette.CompositeView
     description: "#cake_story"
     category: "#cake_category_id"
     tags: "#cake_tags"
+    tools: "#cake_tools"
     save_button: '#save-button'
 
   modelEvents:
@@ -37,6 +38,7 @@ class CakeSide.Views.Cakes.EditView extends Marionette.CompositeView
   onRender: ->
     @$("#cake_category_id").val(@model.category_id())
     @ui.tags.tagit({ availableTags: ALL_TAGS })
+    @ui.tools.tagit({ availableTags: ALL_TOOLS })
     @disableSaveButton()
 
   savedSuccessfully: (cake) =>
@@ -55,6 +57,7 @@ class CakeSide.Views.Cakes.EditView extends Marionette.CompositeView
     @model.set('story', @ui.description.val())
     @model.set('category_id', @ui.category.val())
     @model.set('tags', @ui.tags.val())
+    @model.set('tools', @ui.tools.val())
     @model.isValid()
 
   displayError: (model, error) ->
app/controllers/api/v1/cakes_controller.rb
@@ -24,15 +24,14 @@ module Api
       end
 
       def update
-        UpdateCakeCommand.new(self).run(params[:id], params[:cake][:tags], cake_params)
-      end
-
-      def update_cake_succeeded(cake)
-        respond_with(@cake = cake)
-      end
-
-      def update_cake_failed(cake)
-        respond_with(@cake = cake)
+        @cake = current_user.creations.find(params[:id])
+        current_user.tag(@cake, with: params[:cake][:tags], on: :tags)
+        current_user.tag(@cake, with: params[:cake][:tools], on: :tools)
+        if @cake.update(cake_params.reject { |key, value| key == "tags" || key == "tools" })
+          respond_with @cake
+        else
+          respond_with @cake
+        end
       end
 
       def destroy
app/services/application/update_cake_command.rb
@@ -1,18 +0,0 @@
-class UpdateCakeCommand
-  def initialize(context)
-    @context = context
-  end
-
-  def run(id, tags, creation_params)
-    current_user = @context.current_user
-
-    cake = current_user.creations.find(id)
-    current_user.tag(cake, with: tags, on: :tags)
-
-    if cake.update(creation_params.reject { |key, value| key == "tags" })
-      @context.update_cake_succeeded(cake)
-    else
-      @context.update_cake_failed(cake)
-    end
-  end
-end