From 7d624fd9ca2cf3fa52a2519e0b70b98c4dac916d Mon Sep 17 00:00:00 2001 From: Skud Date: Sun, 6 Jul 2014 21:21:51 +1000 Subject: [PATCH 01/14] Attempting to get scientific name editing on one page (doesn't work yet) --- app/models/crop.rb | 4 ++++ app/views/crops/_form.html.haml | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/app/models/crop.rb b/app/models/crop.rb index b5340c71b..5f2682432 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -4,6 +4,10 @@ class Crop < ActiveRecord::Base attr_accessible :en_wikipedia_url, :name, :parent_id, :creator_id has_many :scientific_names + accepts_nested_attributes_for :scientific_names, + :allow_destroy => true, + :reject_if => :all_blank + has_many :plantings has_many :photos, :through => :plantings has_many :seeds diff --git a/app/views/crops/_form.html.haml b/app/views/crops/_form.html.haml index 1ff75ce04..91cbe6c22 100644 --- a/app/views/crops/_form.html.haml +++ b/app/views/crops/_form.html.haml @@ -27,5 +27,13 @@ .controls = collection_select(:crop, :parent_id, Crop.all, :id, :name, {:include_blank => true}) %span.help-inline Optional. For setting up crop hierarchies for varieties etc. + .control-group + = fields_for :scientific_names do |sn| + = sn.label :scientific_name, "Scientific name", :class => 'control-label' + .controls + = sn.text_field :scientific_name + - if sn.object && sn.object.persisted? + = sn.check_box :_destroy + = sn.label :_destroy, "Delete" .form-actions = f.submit 'Save', :class => 'btn btn-primary' From 8b45a1759c9bd13d22058f41eff501d81e894bc8 Mon Sep 17 00:00:00 2001 From: Taylor Griffin Date: Sun, 13 Jul 2014 17:57:55 +1000 Subject: [PATCH 02/14] got scientific names working on the crop page but view tests are failing --- CONTRIBUTORS.md | 1 + app/controllers/crops_controller.rb | 6 +++++- app/models/crop.rb | 2 +- app/views/crops/_form.html.haml | 12 ++++++++---- spec/views/crops/new.html.haml_spec.rb | 6 ++++++ 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 2014f5e11..561eb6968 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -38,3 +38,4 @@ submit the change with your pull request. - Yaw Boakye / [yawboakye](https://github.com/yawboakye) - Ryan Clark / [IAMRYO](https://github.com/IAMRYO) - Marty Hines / [martyhines](https://github.com/martyhines) +- Taylor Griffin / [tygriffin](https://github.com/tygriffin) diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 3bb020dda..0c6f1ce71 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -74,7 +74,9 @@ class CropsController < ApplicationController # GET /crops/new.json def new @crop = Crop.new - + 3.times do + @crop.scientific_names.build + end respond_to do |format| format.html # new.html.haml format.json { render json: @crop } @@ -90,8 +92,10 @@ class CropsController < ApplicationController # POST /crops.json def create params[:crop][:creator_id] = current_member.id + puts params.to_yaml @crop = Crop.new(params[:crop]) + respond_to do |format| if @crop.save format.html { redirect_to @crop, notice: 'Crop was successfully created.' } diff --git a/app/models/crop.rb b/app/models/crop.rb index 5f2682432..05d0b1721 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -1,7 +1,7 @@ class Crop < ActiveRecord::Base extend FriendlyId friendly_id :name, use: :slugged - attr_accessible :en_wikipedia_url, :name, :parent_id, :creator_id + attr_accessible :en_wikipedia_url, :name, :parent_id, :creator_id, :scientific_names_attributes has_many :scientific_names accepts_nested_attributes_for :scientific_names, diff --git a/app/views/crops/_form.html.haml b/app/views/crops/_form.html.haml index 91cbe6c22..8556a45ff 100644 --- a/app/views/crops/_form.html.haml +++ b/app/views/crops/_form.html.haml @@ -27,13 +27,17 @@ .controls = collection_select(:crop, :parent_id, Crop.all, :id, :name, {:include_blank => true}) %span.help-inline Optional. For setting up crop hierarchies for varieties etc. - .control-group - = fields_for :scientific_names do |sn| + %p + %span.help-block + You may enter up to 3 scientific names for a crop. Most crops will have only one. + = f.fields_for :scientific_names do |sn| + .control-group = sn.label :scientific_name, "Scientific name", :class => 'control-label' .controls = sn.text_field :scientific_name - if sn.object && sn.object.persisted? - = sn.check_box :_destroy - = sn.label :_destroy, "Delete" + %label.checkbox + = sn.check_box :_destroy + = sn.label :_destroy, "Delete" .form-actions = f.submit 'Save', :class => 'btn btn-primary' diff --git a/spec/views/crops/new.html.haml_spec.rb b/spec/views/crops/new.html.haml_spec.rb index f1ac82827..66de6013b 100644 --- a/spec/views/crops/new.html.haml_spec.rb +++ b/spec/views/crops/new.html.haml_spec.rb @@ -22,4 +22,10 @@ describe "crops/new" do assert_select "a[href^=http://wiki.growstuff.org]", "crop wrangling guide" end + it "shows three fields for scientific_name" do + assert_select "input#crop_scientific_names_attributes_0_scientific_name" + assert_select "input#crop_scientific_names_attributes_1_scientific_name" + assert_select "input#crop_scientific_names_attributes_2_scientific_name" + end + end From ded464ad4c96996119f0aece6658a89a5ed9f93d Mon Sep 17 00:00:00 2001 From: Taylor Griffin Date: Sun, 13 Jul 2014 18:00:46 +1000 Subject: [PATCH 03/14] removed call to for debugging --- app/controllers/crops_controller.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 0c6f1ce71..e5f49e0d4 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -92,7 +92,6 @@ class CropsController < ApplicationController # POST /crops.json def create params[:crop][:creator_id] = current_member.id - puts params.to_yaml @crop = Crop.new(params[:crop]) From 82598e75f84bae0f6b9bd77756deb83f30f5ec5c Mon Sep 17 00:00:00 2001 From: Taylor Griffin Date: Tue, 15 Jul 2014 22:57:07 +1000 Subject: [PATCH 04/14] fix redirect after reset password bug --- app/controllers/passwords_controller.rb | 7 +++++++ config/routes.rb | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 app/controllers/passwords_controller.rb diff --git a/app/controllers/passwords_controller.rb b/app/controllers/passwords_controller.rb new file mode 100644 index 000000000..7a31ffddd --- /dev/null +++ b/app/controllers/passwords_controller.rb @@ -0,0 +1,7 @@ +class PasswordsController < Devise::PasswordsController + +protected + def after_resetting_password_path_for(resource) + root_path + end +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 9f066fa47..360ed2477 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,7 @@ Growstuff::Application.routes.draw do resources :plant_parts - devise_for :members, :controllers => { :registrations => "registrations" } + devise_for :members, :controllers => { :registrations => "registrations", :passwords => "passwords" } resources :members resources :photos From 90c43336ca4bfc0e4bdc32e3c90dcda883f5a8c0 Mon Sep 17 00:00:00 2001 From: Skud Date: Fri, 18 Jul 2014 16:34:04 +1000 Subject: [PATCH 05/14] fixed broken brand link in navbar the "float:none" was causing trouble; removing it doesn't seem to change the display at all. --- .../stylesheets/bootstrap_and_overrides.css.less | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/app/assets/stylesheets/bootstrap_and_overrides.css.less b/app/assets/stylesheets/bootstrap_and_overrides.css.less index 50c40ed6f..e3cc35f00 100644 --- a/app/assets/stylesheets/bootstrap_and_overrides.css.less +++ b/app/assets/stylesheets/bootstrap_and_overrides.css.less @@ -188,7 +188,7 @@ p.stats { display:block; } } -} +} li.crop-hierarchy { @@ -196,16 +196,15 @@ li.crop-hierarchy { } .navbar-inner { - border-width: 0px !important; + border-width: 0px !important; } .navbar-bottom { - margin: 40px 0px 0px 0px !important; + margin: 40px 0px 0px 0px !important; } // navbar centering .navbar .nav { - float: none; display: inline-block; *display: inline; /* ie7 fix */ @@ -226,9 +225,9 @@ li.crop-hierarchy { .navbar-inner { border: none !important; background-image: none !important; - -webkit-border-radius: 0px; + -webkit-border-radius: 0px; -moz-border-radius: 0px; - border-radius: 0px; + border-radius: 0px; } .navbar-bottom .navbar-inner { @@ -236,11 +235,11 @@ li.crop-hierarchy { } .navbar-bottom .container{ - padding: 20px 0px 40px 0px; + padding: 20px 0px 40px 0px; } .navbar-search{ - padding-bottom: 7px; + padding-bottom: 7px; } // Overrides applying only to mobile view. This must be at the end of the overrides file. From 48bc0194197d34360a784d55a7a8e34e8ec38163 Mon Sep 17 00:00:00 2001 From: Skud Date: Fri, 18 Jul 2014 16:45:42 +1000 Subject: [PATCH 06/14] Make centering specific to footer --- app/assets/stylesheets/bootstrap_and_overrides.css.less | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/assets/stylesheets/bootstrap_and_overrides.css.less b/app/assets/stylesheets/bootstrap_and_overrides.css.less index e3cc35f00..1713e6f67 100644 --- a/app/assets/stylesheets/bootstrap_and_overrides.css.less +++ b/app/assets/stylesheets/bootstrap_and_overrides.css.less @@ -204,7 +204,8 @@ li.crop-hierarchy { } // navbar centering -.navbar .nav { +footer .navbar .nav { + float: none; display: inline-block; *display: inline; /* ie7 fix */ From e63b2fb4728963d710af59e75a251273df93a845 Mon Sep 17 00:00:00 2001 From: Skud Date: Fri, 18 Jul 2014 17:27:25 +1000 Subject: [PATCH 07/14] Set up sci names in spec file so fields are shown --- spec/views/crops/new.html.haml_spec.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/spec/views/crops/new.html.haml_spec.rb b/spec/views/crops/new.html.haml_spec.rb index 66de6013b..0aa443f74 100644 --- a/spec/views/crops/new.html.haml_spec.rb +++ b/spec/views/crops/new.html.haml_spec.rb @@ -2,7 +2,11 @@ require 'spec_helper' describe "crops/new" do before(:each) do - assign(:crop, FactoryGirl.create(:maize)) + @crop = FactoryGirl.create(:maize) + 3.times do + @crop.scientific_names.build + end + assign(:crop, @crop) @member = FactoryGirl.create(:crop_wrangling_member) sign_in @member controller.stub(:current_user) { @member } @@ -23,9 +27,9 @@ describe "crops/new" do end it "shows three fields for scientific_name" do - assert_select "input#crop_scientific_names_attributes_0_scientific_name" - assert_select "input#crop_scientific_names_attributes_1_scientific_name" - assert_select "input#crop_scientific_names_attributes_2_scientific_name" + assert_select "input#crop_scientific_names_attributes_0_scientific_name", :count => 1 + assert_select "input#crop_scientific_names_attributes_1_scientific_name", :count => 1 + assert_select "input#crop_scientific_names_attributes_2_scientific_name", :count => 1 end end From 7184a0d5d5bd2b279b4154503958d688407e4352 Mon Sep 17 00:00:00 2001 From: Skud Date: Fri, 18 Jul 2014 17:29:10 +1000 Subject: [PATCH 08/14] Added scientific name field tests for edit form --- spec/views/crops/edit.html.haml_spec.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/spec/views/crops/edit.html.haml_spec.rb b/spec/views/crops/edit.html.haml_spec.rb index 9cf5fd469..3db9c52b0 100644 --- a/spec/views/crops/edit.html.haml_spec.rb +++ b/spec/views/crops/edit.html.haml_spec.rb @@ -5,7 +5,11 @@ describe "crops/edit" do controller.stub(:current_user) { FactoryGirl.create(:crop_wrangling_member) } - @crop = assign(:crop, FactoryGirl.create(:maize)) + @crop = FactoryGirl.create(:maize) + 3.times do + @crop.scientific_names.build + end + assign(:crop, @crop) render end @@ -19,4 +23,10 @@ describe "crops/edit" do assert_select "input#crop_en_wikipedia_url", :name => "crop[en_wikipedia_url]" end end + + it "shows three fields for scientific_name" do + assert_select "input#crop_scientific_names_attributes_0_scientific_name", :count => 1 + assert_select "input#crop_scientific_names_attributes_1_scientific_name", :count => 1 + assert_select "input#crop_scientific_names_attributes_2_scientific_name", :count => 1 + end end From c9857bf1c890b2454335ca4c61d79a3ca64e2068 Mon Sep 17 00:00:00 2001 From: Skud Date: Fri, 18 Jul 2014 17:38:15 +1000 Subject: [PATCH 09/14] popular scope for crops --- app/models/crop.rb | 1 + spec/models/crop_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/app/models/crop.rb b/app/models/crop.rb index ba2b8acaa..577a5024a 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -17,6 +17,7 @@ class Crop < ActiveRecord::Base default_scope order("lower(name) asc") scope :recent, reorder("created_at desc") scope :toplevel, where(:parent_id => nil) + scope :popular, reorder("plantings_count desc") scope :randomized, reorder('random()') # ok on sqlite and psql, but not on mysql validates :en_wikipedia_url, diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index c89328904..60acfdf26 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -52,6 +52,28 @@ describe Crop do end end + context 'popularity' do + before (:each) do + @tomato = FactoryGirl.create(:tomato) + @maize = FactoryGirl.create(:maize) + (1..10).each do + FactoryGirl.create(:planting, :crop => @maize) + end + (1..3).each do + FactoryGirl.create(:planting, :crop => @tomato) + end + end + + it "sorts by most plantings" do + Crop.popular.first.should eq @maize + (1..10).each do + FactoryGirl.create(:planting, :crop => @tomato) + end + Crop.popular.first.should eq @tomato + end + + end + it 'finds a default scientific name' do @crop = FactoryGirl.create(:tomato) @crop.default_scientific_name.should eq nil From 573e555bcfa243114d6bcbd6ccb614fb0684bc0d Mon Sep 17 00:00:00 2001 From: Skud Date: Fri, 18 Jul 2014 17:52:11 +1000 Subject: [PATCH 10/14] Added unpopular scope to crop --- app/controllers/crops_controller.rb | 2 +- app/models/crop.rb | 3 ++- spec/models/crop_spec.rb | 5 +++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 3bb020dda..37efacd42 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -7,7 +7,7 @@ class CropsController < ApplicationController # GET /crops # GET /crops.json def index - @crops = Crop.includes(:scientific_names, {:plantings => :photos}).paginate(:page => params[:page]) + @crops = Crop.popular.includes(:scientific_names, {:plantings => :photos}).paginate(:page => params[:page]) respond_to do |format| format.html diff --git a/app/models/crop.rb b/app/models/crop.rb index 577a5024a..e61b66948 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -17,7 +17,8 @@ class Crop < ActiveRecord::Base default_scope order("lower(name) asc") scope :recent, reorder("created_at desc") scope :toplevel, where(:parent_id => nil) - scope :popular, reorder("plantings_count desc") + scope :popular, where("plantings_count > 0").reorder("plantings_count desc, lower(name) asc") + scope :unpopular, where(:plantings_count => nil) scope :randomized, reorder('random()') # ok on sqlite and psql, but not on mysql validates :en_wikipedia_url, diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index 60acfdf26..e08897ae8 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -56,6 +56,7 @@ describe Crop do before (:each) do @tomato = FactoryGirl.create(:tomato) @maize = FactoryGirl.create(:maize) + @cucumber = FactoryGirl.create(:crop, :name => 'cucumber') (1..10).each do FactoryGirl.create(:planting, :crop => @maize) end @@ -72,6 +73,10 @@ describe Crop do Crop.popular.first.should eq @tomato end + it "finds unpopular crops" do + Crop.unpopular.should eq [@cucumber] + end + end it 'finds a default scientific name' do From 8223025d35568fb4409f9cf64ef90510b788b46b Mon Sep 17 00:00:00 2001 From: Skud Date: Fri, 18 Jul 2014 18:06:36 +1000 Subject: [PATCH 11/14] Set default plantings_count to 0 not nil --- app/controllers/crops_controller.rb | 7 ++++++- app/models/crop.rb | 2 +- ...8075753_default_plantings_count_to_zero.rb | 9 +++++++++ db/schema.rb | 20 +++++++++---------- lib/tasks/growstuff.rake | 13 +++++++++++- script/deploy-tasks.sh | 10 ++-------- 6 files changed, 40 insertions(+), 21 deletions(-) create mode 100644 db/migrate/20140718075753_default_plantings_count_to_zero.rb diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 37efacd42..5c9b280fe 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -7,7 +7,12 @@ class CropsController < ApplicationController # GET /crops # GET /crops.json def index - @crops = Crop.popular.includes(:scientific_names, {:plantings => :photos}).paginate(:page => params[:page]) + if params[:sort] = 'popular' + @crops = Crop.popular.includes(:scientific_names, {:plantings => :photos}).paginate(:page => params[:page]) + else + # alphabetical order + @crops = Crop.includes(:scientific_names, {:plantings => :photos}).paginate(:page => params[:page]) + end respond_to do |format| format.html diff --git a/app/models/crop.rb b/app/models/crop.rb index e61b66948..7631f4645 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -17,7 +17,7 @@ class Crop < ActiveRecord::Base default_scope order("lower(name) asc") scope :recent, reorder("created_at desc") scope :toplevel, where(:parent_id => nil) - scope :popular, where("plantings_count > 0").reorder("plantings_count desc, lower(name) asc") + scope :popular, reorder("plantings_count desc, lower(name) asc") scope :unpopular, where(:plantings_count => nil) scope :randomized, reorder('random()') # ok on sqlite and psql, but not on mysql diff --git a/db/migrate/20140718075753_default_plantings_count_to_zero.rb b/db/migrate/20140718075753_default_plantings_count_to_zero.rb new file mode 100644 index 000000000..608821fbc --- /dev/null +++ b/db/migrate/20140718075753_default_plantings_count_to_zero.rb @@ -0,0 +1,9 @@ +class DefaultPlantingsCountToZero < ActiveRecord::Migration + def up + change_column :crops, :plantings_count, :integer, :default => 0 + end + + def down + change_column :crops, :plantings_count, :integer, :default => nil + end +end diff --git a/db/schema.rb b/db/schema.rb index 44ba8697c..489a9e6b6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20131031000655) do +ActiveRecord::Schema.define(:version => 20140718075753) do create_table "account_types", :force => true do |t| t.string "name", :null => false @@ -51,13 +51,13 @@ ActiveRecord::Schema.define(:version => 20131031000655) do end create_table "crops", :force => true do |t| - t.string "name", :null => false + t.string "name", :null => false t.string "en_wikipedia_url" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.string "slug" t.integer "parent_id" - t.integer "plantings_count" + t.integer "plantings_count", :default => 0 t.integer "creator_id" end @@ -242,11 +242,11 @@ ActiveRecord::Schema.define(:version => 20131031000655) do add_index "posts", ["slug"], :name => "index_updates_on_slug", :unique => true create_table "products", :force => true do |t| - t.string "name", :null => false - t.text "description", :limit => 255, :null => false - t.integer "min_price", :null => false - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.string "name", :null => false + t.text "description", :null => false + t.integer "min_price", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.integer "account_type_id" t.integer "paid_months" t.integer "recommended_price" diff --git a/lib/tasks/growstuff.rake b/lib/tasks/growstuff.rake index 91814aff8..f4f7a0003 100644 --- a/lib/tasks/growstuff.rake +++ b/lib/tasks/growstuff.rake @@ -235,6 +235,17 @@ namespace :growstuff do PlantPart.find_or_create_by_name!(pp) end end - end + + desc "July 2014: set planting_count to 0 by default, not nil" + task :zero_plantings_count => :environment do + Crop.find_each do |c| + if c.plantings_count.nil? + c.plantings_count = 0 + c.save + end + end + end + + end # end oneoff section end diff --git a/script/deploy-tasks.sh b/script/deploy-tasks.sh index 08dd2b7ac..c471b566e 100755 --- a/script/deploy-tasks.sh +++ b/script/deploy-tasks.sh @@ -10,11 +10,5 @@ # echo "YYYY-MM-DD - do something or other" # rake growstuff:oneoff:something -echo "2013-10-24 - fix zeroed geolocations" -rake growstuff:depopulate_null_island - -echo "2013-10-24 - initialize garden locations" -rake growstuff:oneoff:initialize_garden_locations - -echo "2013-10-31 - import plant parts" -rake growstuff:oneoff:import_plant_parts +echo "2013-07-18 - zero crop plantings_count" +rake growstuff:oneoff:zero_plantings_count From aeb14021f36200334c39cfc5daa27e33ed24bb35 Mon Sep 17 00:00:00 2001 From: Skud Date: Fri, 18 Jul 2014 18:17:37 +1000 Subject: [PATCH 12/14] Removed unpopular scope (guess it really was unpopular) --- app/models/crop.rb | 1 - spec/models/crop_spec.rb | 4 ---- 2 files changed, 5 deletions(-) diff --git a/app/models/crop.rb b/app/models/crop.rb index 7631f4645..211a56146 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -18,7 +18,6 @@ class Crop < ActiveRecord::Base scope :recent, reorder("created_at desc") scope :toplevel, where(:parent_id => nil) scope :popular, reorder("plantings_count desc, lower(name) asc") - scope :unpopular, where(:plantings_count => nil) scope :randomized, reorder('random()') # ok on sqlite and psql, but not on mysql validates :en_wikipedia_url, diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index e08897ae8..c56cd7942 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -73,10 +73,6 @@ describe Crop do Crop.popular.first.should eq @tomato end - it "finds unpopular crops" do - Crop.unpopular.should eq [@cucumber] - end - end it 'finds a default scientific name' do From a0fc8802b236c460604ee6a6c008ee6c6dd4d2fd Mon Sep 17 00:00:00 2001 From: Skud Date: Fri, 18 Jul 2014 18:37:43 +1000 Subject: [PATCH 13/14] 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 From 846a2c89c5141b5ab3aa0efbf0bba9388d935f95 Mon Sep 17 00:00:00 2001 From: Skud Date: Sun, 20 Jul 2014 19:29:06 +1000 Subject: [PATCH 14/14] Tweaked FactoryGirl to use create_list --- spec/models/crop_spec.rb | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index c56cd7942..1741faf74 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -57,19 +57,13 @@ describe Crop do @tomato = FactoryGirl.create(:tomato) @maize = FactoryGirl.create(:maize) @cucumber = FactoryGirl.create(:crop, :name => 'cucumber') - (1..10).each do - FactoryGirl.create(:planting, :crop => @maize) - end - (1..3).each do - FactoryGirl.create(:planting, :crop => @tomato) - end + FactoryGirl.create_list(:planting, 10, :crop => @maize) + FactoryGirl.create_list(:planting, 3, :crop => @tomato) end it "sorts by most plantings" do Crop.popular.first.should eq @maize - (1..10).each do - FactoryGirl.create(:planting, :crop => @tomato) - end + FactoryGirl.create_list(:planting, 10, :crop => @tomato) Crop.popular.first.should eq @tomato end