Allow selection of crop display order (popularity or alphabetical)

Had some trouble with tests; apparently the counter_cache behaves
differently when it starts counting from 0, which meant that some tests
started failing unless you do @crop.reload to refresh the
plantings_count.  Weird, but oh well.
This commit is contained in:
Skud
2014-07-18 18:37:43 +10:00
parent aeb14021f3
commit a0fc8802b2
5 changed files with 23 additions and 6 deletions

View File

@@ -7,11 +7,13 @@ class CropsController < ApplicationController
# GET /crops
# GET /crops.json
def index
if params[:sort] = 'popular'
@crops = Crop.popular.includes(:scientific_names, {:plantings => :photos}).paginate(:page => params[:page])
else
@sort = params[:sort]
if @sort == 'alpha'
# alphabetical order
@crops = Crop.includes(:scientific_names, {:plantings => :photos}).paginate(:page => params[:page])
else
# default to sorting by popularity
@crops = Crop.popular.includes(:scientific_names, {:plantings => :photos}).paginate(:page => params[:page])
end
respond_to do |format|

View File

@@ -5,6 +5,11 @@
View any crop page to see which of our members have planted it and find
information on how to grow it yourself.
= form_tag(crops_path, :method => :get, :class => 'form-inline') do
= label_tag :sort, "Sort by:", :class => 'control-label'
= select_tag "sort", options_for_select({"Popularity" => 'popular', "Alphabetical" => 'alpha'}, @sort || 'popular')
= submit_tag "Show", :class => 'btn btn-primary'
%div.pagination
= page_entries_info @crops, :model => "crops"
= will_paginate @crops

View File

@@ -5,6 +5,7 @@ describe "crops/_popover" do
@tomato = FactoryGirl.create(:tomato)
@sn = FactoryGirl.create(:solanum_lycopersicum, :crop => @tomato)
@planting = FactoryGirl.create(:planting, :crop => @tomato)
@tomato.reload # to pick up latest plantings_count
render :partial => 'crops/popover', :locals => { :crop => @tomato }
end

View File

@@ -14,6 +14,14 @@ describe "crops/index" do
assign(:crops, crops)
end
it "has a form for sorting by" do
render
assert_select "form"
assert_select "select#sort"
assert_select "option[value=alpha]"
assert_select "option[value=popular]"
end
it "renders a list of crops" do
render
assert_select "a", :text => @maize.name

View File

@@ -19,7 +19,7 @@ describe "crops/show" do
render
end
it 'shows 4 photos across the top of the page' do
it 'shows 3 photos across the top of the page' do
assert_select "div.thumbnail>a>img", :count => 3
end
@@ -131,12 +131,13 @@ describe "crops/show" do
:garden => @garden,
:crop => @crop
)
@crop.reload # to pick up latest plantings_count
end
it "links to people who are growing this crop" do
render
rendered.should contain /member\d+/
rendered.should contain "Springfield Community Garden"
rendered.should contain @owner.login_name
rendered.should contain @garden.name
end
it "shows photos where available" do