Commit 4887ba54

mo <mo.khan@gmail.com>
2017-09-24 19:53:09
render categories as backbone collection in html page.
1 parent dc702a8
app/assets/javascripts/initializers/auto_view_setup.js.coffee
@@ -7,9 +7,11 @@ class csx.AutoViewSetup extends csx.Behaviour
   execute: ->
     for element in $('[data-autoview]')
       $element = $(element)
-      @install($element) unless $element.data('autoview-setup-complete')
+      @install($element)
 
   install: (element) ->
+    return if element.data('autoview-setup-complete')
+
     viewName = element.data('autoview')
     if viewName.indexOf("-") > 0
       csx.AutoView.install(element)
app/assets/javascripts/views/my/cakes/edit_view.js.coffee
@@ -68,7 +68,7 @@ class csx.Views.My.Cakes.EditView extends Marionette.CompositeView
   serializeData: ->
     {
       cake: @model.toJSON(),
-      categories: csx.Application.request('CategoriesRepository').toJSON(),
+      categories: csx.Categories.toJSON(),
     }
 
   launchAddPhoto: ->
app/assets/javascripts/views/my/cakes/new_view.js.coffee
@@ -57,5 +57,5 @@ class csx.Views.My.Cakes.NewView extends Marionette.ItemView
   serializeData: ->
     {
       cake: @model.toJSON(),
-      categories: csx.Application.request('CategoriesRepository').toJSON(),
+      categories: csx.Categories.toJSON(),
     }
app/assets/javascripts/cakeside.js.coffee
@@ -38,13 +38,10 @@ window.csx =
         Backbone.history.start()
 
     @cakes = new csx.Collections.CakesCollection()
-    @categories = new csx.Collections.CategoriesCollection()
     @tutorials = new csx.Collections.TutorialsCollection()
 
     csx.Application.reqres.setHandler 'CakesRepository', =>
       @cakes
-    csx.Application.reqres.setHandler 'CategoriesRepository', =>
-      @categories
     @photos_cache = {}
     csx.Application.reqres.setHandler 'PhotosRepository', (cake_id) =>
       if @photos_cache[cake_id]
@@ -59,7 +56,6 @@ window.csx =
     csx.Application.reqres.setHandler 'ProfilesRepository', =>
       @profiles ||= new csx.Collections.ProfilesCollection()
 
-    @categories.fetch(reset: true)
     @tutorials.fetch(reset: true)
     @cakes.fetch(reset: true).done ->
       csx.Application.start()
app/controllers/application_controller.rb
@@ -4,8 +4,8 @@ class ApplicationController < ActionController::Base
   # For APIs, you may want to use :null_session instead.
   protect_from_forgery with: :exception
   before_action :load_header
-  #before_action :extend_session_cookie
-  helper_method :current_user, :user_signed_in?
+  before_action :extend_session_cookie
+  helper_method :current_user, :current_user?
   rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
 
   def user_session(session_key = session[:raphael])
@@ -16,8 +16,8 @@ class ApplicationController < ActionController::Base
     user_session.try(:user)
   end
 
-  def user_signed_in?
-    current_user
+  def current_user?
+    current_user.present?
   end
 
   private
@@ -31,7 +31,7 @@ class ApplicationController < ActionController::Base
   end
 
   def extend_session_cookie
-    session[:raphael] = user_session.access(request) if user_signed_in?
+    session[:raphael] = user_session.access(request) if current_user?
   end
 
   def record_not_found
app/controllers/sessions_controller.rb
@@ -1,6 +1,6 @@
 class SessionsController < ApplicationController
   def new
-    redirect_to my_dashboard_path if user_signed_in?
+    redirect_to my_dashboard_path if current_user?
     @session = UserSession.new
   end
 
app/views/cakes/_show.html.erb
@@ -2,7 +2,7 @@
   <div class="col">
     <h1><%= link_to @creation.name, cake_path(@creation) %></h1>
     <p>By <%= link_to @creation.user.name, profile_path(@creation.user) %></p>
-    <% if user_signed_in? && current_user != @creation.user %>
+    <% if current_user? && current_user != @creation.user %>
       <% if current_user.already_likes(@creation) %>
         <i class="fa fa-star fa-4x float-right"></i>
       <% else %>
app/views/cakes/index.html.erb
@@ -2,7 +2,7 @@
 <% provide(:search_path, cakes_path) %>
 <% provide(:container_class, 'container') %>
 
-<% unless user_signed_in? %>
+<% unless current_user? %>
 <div class="row jumbotron">
   <div class="col text-center">
     <%= image_tag "cakeside-logo.png", alt: "CakeSide", class: "img-fluid" %>
app/views/layouts/_header.html.erb
@@ -15,7 +15,7 @@
         <li class="nav-item <%= "active" if params["controller"] == "tutorials" %>">
           <%= link_to "Tutorials", tutorials_path(q: params[:q]), class: 'nav-link' %>
         </li>
-        <% if user_signed_in? %>
+        <% if current_user? %>
         <li class="nav-item dropdown">
           <%= link_to my_dashboard_path, id: 'navbarDropdownMenuLink', class: 'nav-link dropdown-toggle', data: { toggle: 'dropdown' }, 'aria-haspopup'=>"true", 'aria-expanded'=>"false" do %>
             <%= current_user.name %>
app/views/my/kitchens/show.html.erb
@@ -1,6 +1,9 @@
 <% provide(:title, "Kitchen") -%>
 <%= content_for :javascript do -%>
   <%= javascript_tag do %>
+    csx.Categories = new Backbone.Collection(<%= raw @categories.to_json %>);
+
+    <%# backbone_collection_for @categories %>
     var ALL_TAGS = <%= raw @tags.pluck(:name) %>;
     csx.initialize({ access_token: '<%= current_user.authentication_token %>' });
   <% end %>
spec/support/authentication.rb
@@ -3,7 +3,7 @@ module Authentication
     new_session = UserSession.new
     allow(controller).to receive(:user_session).and_return(new_session)
     allow(controller).to receive(:current_user).and_return(user)
-    allow(controller).to receive(:user_signed_in?).and_return(true)
+    allow(controller).to receive(:current_user?).and_return(true)
   end
 
   def api_login(user)