From a0fc8802b236c460604ee6a6c008ee6c6dd4d2fd Mon Sep 17 00:00:00 2001 From: Skud Date: Fri, 18 Jul 2014 18:37:43 +1000 Subject: [PATCH] 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. --- app/controllers/crops_controller.rb | 8 +++++--- app/views/crops/index.html.haml | 5 +++++ spec/views/crops/_popover.html.haml_spec.rb | 1 + spec/views/crops/index.html.haml_spec.rb | 8 ++++++++ spec/views/crops/show.html.haml_spec.rb | 7 ++++--- 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 5c9b280fe..6f8fa6917 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -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| diff --git a/app/views/crops/index.html.haml b/app/views/crops/index.html.haml index 2431bc9b6..a384efaf7 100644 --- a/app/views/crops/index.html.haml +++ b/app/views/crops/index.html.haml @@ -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 diff --git a/spec/views/crops/_popover.html.haml_spec.rb b/spec/views/crops/_popover.html.haml_spec.rb index 4cbe7a677..ad07f8ada 100644 --- a/spec/views/crops/_popover.html.haml_spec.rb +++ b/spec/views/crops/_popover.html.haml_spec.rb @@ -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 diff --git a/spec/views/crops/index.html.haml_spec.rb b/spec/views/crops/index.html.haml_spec.rb index cd43b7c1b..ca0ecc39b 100644 --- a/spec/views/crops/index.html.haml_spec.rb +++ b/spec/views/crops/index.html.haml_spec.rb @@ -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 diff --git a/spec/views/crops/show.html.haml_spec.rb b/spec/views/crops/show.html.haml_spec.rb index 1e0aaa528..6e8250b27 100644 --- a/spec/views/crops/show.html.haml_spec.rb +++ b/spec/views/crops/show.html.haml_spec.rb @@ -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