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