Commit 51a0707f

mo khan <mo@mokhan.ca>
2015-01-29 03:08:37
load distinct cakes.
1 parent 28e85b4
Changed files (5)
app/controllers/cakes_controller.rb
@@ -5,7 +5,7 @@ class CakesController < ApplicationController
   end
 
   def index
-    @creations = @repository.search_with(params).page(page).per(per_page)
+    @cakes = @repository.search_with(params).page(page).per(per_page)
   end
 
   def show
app/models/creation/repository.rb
@@ -19,7 +19,7 @@ class Creation
     end
 
     def search_with(params)
-      all_matching(search_filters_for(params))
+      all_matching(search_filters_for(params)).distinct
     end
 
     private
app/views/cakes/_index.html.erb
@@ -1,12 +1,12 @@
 <div class="row-fluid">
-  <% @creations.each_slice(6).each do |batch| %>
+  <% @cakes.each_slice(6).each do |batch| %>
     <ul class='thumbnails'>
-      <% batch.each do |creation| %>
-        <% cache creation do %>
+      <% batch.each do |cake| %>
+        <% cache cake do %>
           <li class="span2">
             <div class="thumbnail">
-              <%= link_to cake_path(creation) do %>
-                <%= image_tag creation.primary_image.url_for(:thumb), alt: creation.name %>
+              <%= link_to cake_path(cake) do %>
+                <%= image_tag cake.primary_image.url_for(:thumb), alt: cake.name %>
               <% end %>
             </div>
           </li>
@@ -16,5 +16,5 @@
   <% end %>
 </div>
 <div id='more-button-row' class="row-fluid">
-  <%= link_to_next_page @creations, 'More...', params: { cache: false, sort: params[:sort], q: params[:q] }, remote: true, class: 'btn pull-right more-button hidden', data: { disable_with: 'loading...' } %>
+  <%= link_to_next_page @cakes, 'More...', params: { cache: false, sort: params[:sort], q: params[:q] }, remote: true, class: 'btn pull-right more-button hidden', data: { disable_with: 'loading...' } %>
 </div>
app/views/cakes/index.html.erb
@@ -33,5 +33,5 @@
 </div>
 
 <div id='pagination-row' class="row-fluid">
-  <%= render "shared/paging", items: @creations %>
+  <%= render "shared/paging", items: @cakes %>
 </div>
spec/controllers/cakes_controller_spec.rb
@@ -2,7 +2,6 @@ require 'rails_helper'
 
 describe CakesController do
   let(:user) { create(:user) }
-  let(:cake) { create(:cake, user: user) }
 
   before(:each) do
     photo = 'spec/fixtures/images/example.png'
@@ -10,14 +9,44 @@ describe CakesController do
   end
 
   describe "#index" do
-    before { get :index }
+    let!(:cakes) { create(:category, slug: "cakes") }
+    let!(:cookies) { create(:category, slug: "cookies") }
+    let!(:cake) { create(:published_cake, name: "cake", category: cakes) }
+    let!(:cookie) do
+      create(:published_cake, name: "cookie", category: cookies)
+    end
+
+    let!(:unpublished_cake) do
+      create(:cake, name: "unpublished", category: cakes)
+    end
 
-    it "should display all creations" do
-      expect(assigns(:creations)).to include(cake)
+    it "returns all published cakes" do
+      get :index
+      expect(assigns(:cakes)).to match_array([cake, cookie])
+    end
+
+    it "returns all cakes in the category" do
+      get :index, category: cookie.category.slug
+      expect(assigns(:cakes)).to match_array([cookie])
+    end
+
+    it "returns all cakes matching the search query" do
+      get :index, q: cake.name[0..2]
+      expect(assigns(:cakes)).to match_array([cake])
+    end
+
+    it "returns all cakes tagged with the tag" do
+      cake.tag_list = "cakes"
+      cake.save!
+
+      get :index, tags: "cakes"
+      expect(assigns(:cakes)).to match_array([cake])
     end
   end
 
   describe "#show" do
+    let(:cake) { create(:cake, user: user) }
+
     it "loads the cake" do
       get :show, id: cake.id
       expect(assigns(:creation)).to eql(cake)