diff --git a/app/models/planting.rb b/app/models/planting.rb index b848d3490..78dcc82b2 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -78,15 +78,15 @@ class Planting < ActiveRecord::Base # return a list of interesting plantings, for the homepage etc. # we can't do this via a scope (as far as we know) so sadly we have to # do it this way. - def Planting.interesting - howmany = 12 # max amount to collect - + def Planting.interesting(howmany=12, require_photo=true) interesting_plantings = Array.new seen_owners = Hash.new(false) # keep track of which owners we've seen already Planting.all.each do |p| break if interesting_plantings.count == howmany # got enough yet? - next unless p.interesting? # skip those that don't have photos + if require_photo + next unless p.photos.present? # skip those without photos, if required + end next if seen_owners[p.owner] # skip if we already have one from this owner seen_owners[p.owner] = true # we've seen this owner interesting_plantings.push(p) diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index eae16cbe5..f791bf5af 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -153,7 +153,7 @@ describe Planting do end end - context 'interesting crops' do + context 'interesting plantings' do it 'picks up interesting plantings' do # plantings have members created implicitly for them # each member is different, hence these are all interesting @@ -177,38 +177,63 @@ describe Planting do ] end - it 'ignores plantings without photos' do - # first, an interesting planting - @planting = FactoryGirl.create(:planting) - @planting.photos << FactoryGirl.create(:photo) - @planting.save + context "default arguments" do + it 'ignores plantings without photos' do + # first, an interesting planting + @planting = FactoryGirl.create(:planting) + @planting.photos << FactoryGirl.create(:photo) + @planting.save - # this one doesn't have a photo - @boring_planting = FactoryGirl.create(:planting) + # this one doesn't have a photo + @boring_planting = FactoryGirl.create(:planting) - Planting.interesting.should include @planting - Planting.interesting.should_not include @boring_planting + Planting.interesting.should include @planting + Planting.interesting.should_not include @boring_planting + end + + it 'ignores plantings with the same owner' do + # this planting is older + @planting1 = FactoryGirl.create(:planting, :created_at => 1.day.ago) + @planting1.photos << FactoryGirl.create(:photo) + @planting1.save + + # this one is newer, and has the same owner, through the garden + @planting2 = FactoryGirl.create(:planting, + :created_at => 1.minute.ago, + :owner_id => @planting1.owner.id + ) + @planting2.photos << FactoryGirl.create(:photo) + @planting2.save + + # result: the newer one is interesting, the older one isn't + Planting.interesting.should include @planting2 + Planting.interesting.should_not include @planting1 + end end - it 'ignores plantings with the same owner' do - # this planting is older - @planting1 = FactoryGirl.create(:planting, :created_at => 1.day.ago) - @planting1.photos << FactoryGirl.create(:photo) - @planting1.save + context "with require_photo = false" do + it "returns plantings without photos" do + # first, a planting with a photo + @planting = FactoryGirl.create(:planting) + @planting.photos << FactoryGirl.create(:photo) + @planting.save - # this one is newer, and has the same owner, through the garden - @planting2 = FactoryGirl.create(:planting, - :created_at => 1.minute.ago, - :owner_id => @planting1.owner.id - ) - @planting2.photos << FactoryGirl.create(:photo) - @planting2.save + # this one doesn't have a photo + @boring_planting = FactoryGirl.create(:planting) - # result: the newer one is interesting, the older one isn't - Planting.interesting.should include @planting2 - Planting.interesting.should_not include @planting1 + interesting = Planting.interesting(10, false) + interesting.should include @planting + interesting.should include @boring_planting + end end - end + context "with howmany argument" do + it "only returns the number asked for" do + @plantings = FactoryGirl.create_list(:planting, 10) + Planting.interesting(3, false).length.should eq 3 + end + end + + end # interesting plantings end