Added unpopular scope to crop

This commit is contained in:
Skud
2014-07-18 17:52:11 +10:00
parent c9857bf1c8
commit 573e555bcf
3 changed files with 8 additions and 2 deletions

View File

@@ -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

View File

@@ -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,

View File

@@ -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