Commit c2e5237d

mo khan <mo@mokhan.ca>
2014-07-20 16:14:46
save a new tutorial and their tags.
1 parent 8604e24
Changed files (8)
app
assets
controllers
views
api
spec
app/assets/javascripts/backbone/templates/tutorials/new.jst.ejs
@@ -13,7 +13,7 @@
         <div class="control-group">
           <label for="tags" class="control-label">Tags</label>
             <div class="controls">
-              <input type="text" id="tags" name="tutorial_tags" value="" class="input-xxlarge" autocomplete="off" />
+              <input type="text" id="tutorial_tags" name="tags" value="" class="input-xxlarge" autocomplete="off" />
               <p class="help-block">
                 Note: help people find this tutorial by adding some keyword tags
               </p>
app/assets/javascripts/backbone/templates/tutorials/tutorial.jst.ejs
@@ -1,7 +1,17 @@
 <div class="thumbnail">
-  <a href="#tutorials/<%= id %>"><img src="<%= image_url %>" /></a>
+  <a href="#tutorials/<%= id %>">
+    <img src="<%= image_url %>" />
+  </a>
   <div class="caption">
-    <h3><a href="#tutorials/<%= id %>"><%= heading %></a></h3>
+    <h3><a href="#tutorials/<%= id %>"><%= heading %></a> <small><%= displayDate(created_at) %></small></h3>
+    <p>
+      <% _.each(tags, function(tag) { %> 
+        <a href="">
+          <span class="label"><%= tag.name %></span>
+        </a>
+      <% }); %>
+    </p>
     <p><%= description %></p>
+    <p><a href="#tutorials/<%= id %>" class="btn">View</a></p>
   </div>
 </div>
app/assets/javascripts/backbone/views/cakes/edit_view.js.coffee
@@ -36,7 +36,7 @@ class CakeSide.Views.Cakes.EditView extends Marionette.CompositeView
 
   onRender: ->
     @$("#cake_category_id").val(@model.category_id())
-    @$('#cake_tags').tagit({ availableTags: ALL_TAGS })
+    @ui.tags.tagit({ availableTags: ALL_TAGS })
     @disableSaveButton()
 
   savedSuccessfully: (cake) =>
app/assets/javascripts/backbone/views/tutorials/new_view.js.coffee
@@ -6,6 +6,7 @@ class CakeSide.Views.Tutorials.NewView extends Marionette.ItemView
     url_group: '#url-group'
     save_button: '#save-button'
     preview: '#preview-panel'
+    tags: '#tutorial_tags'
 
   modelEvents:
     'invalid': 'displayError'
@@ -34,6 +35,7 @@ class CakeSide.Views.Tutorials.NewView extends Marionette.ItemView
       author_url: attributes.provider_url
 
   onRender: ->
+    @ui.tags.tagit({ availableTags: ALL_TAGS })
     @model.isValid()
 
   displayError: (model, error) ->
@@ -46,6 +48,7 @@ class CakeSide.Views.Tutorials.NewView extends Marionette.ItemView
   save: (event) ->
     event.preventDefault()
     event.stopPropagation()
+    @model.set('tags', @ui.tags.val())
     @ui.save_button.attr('disabled', 'disabled')
     @collection.create(@model,
       success: @savedSuccessfully
app/assets/javascripts/backbone/views/tutorials/tutorial_view.js.coffee
@@ -4,3 +4,7 @@ class CakeSide.Views.Tutorials.TutorialView extends Marionette.ItemView
   template: JST['backbone/templates/tutorials/tutorial']
   tagName: 'li'
   className: 'span3'
+
+  templateHelpers:
+    displayDate: (date) ->
+      new Date(date).toLocaleDateString()
app/controllers/api/v1/tutorials_controller.rb
@@ -8,7 +8,9 @@ module Api
       end
 
       def create
-        respond_with(@tutorial = current_user.tutorials.create!(tutorial_params))
+        @tutorial = current_user.tutorials.create!(tutorial_params)
+        current_user.tag(@tutorial, with: params[:tutorial][:tags], on: :tags)
+        respond_with(@tutorial)
       end
 
       private
app/views/api/v1/tutorials/_tutorial.json.jbuilder
@@ -5,3 +5,8 @@ json.url tutorial.url
 json.image_url tutorial.image_url
 json.author tutorial.author
 json.author_url tutorial.author_url
+json.created_at tutorial.created_at
+json.updated_at tutorial.updated_at
+json.tags tutorial.tags do |tag|
+  json.name tag.name
+end
spec/controllers/api/v1/tutorials_controller_spec.rb
@@ -26,6 +26,7 @@ describe Api::V1::TutorialsController do
         image_url: "https://abs.twimg.com/a/1405611403/img/t1/front_page/exp_wc2014_gen_laurenlemon.jpg",
         heading: "Twitter",
         description: "Connect with your friends - and other fascinating people. Get in-the-moment updates on the things that interest you. And watch events unfold, in real time, from every angle.",
+        tags: "cake,cookie",
       }
       post :create, tutorial: attributes, format: :json
 
@@ -33,6 +34,9 @@ describe Api::V1::TutorialsController do
       expect(assigns(:tutorial).url).to eql(attributes[:url])
       expect(assigns(:tutorial).description).to eql(attributes[:description])
       expect(assigns(:tutorial).heading).to eql(attributes[:heading])
+      expect(assigns(:tutorial).tags.count).to eql(2)
+      expect(assigns(:tutorial).tags.first.name).to eql('cake')
+      expect(assigns(:tutorial).tags.last.name).to eql('cookie')
     end
   end
 end