modified Planting.interesting to have some options re: howmany and photos

This commit is contained in:
Skud
2014-09-12 13:20:41 +10:00
parent 09bef3f9d8
commit 3cf2a50a34
2 changed files with 55 additions and 30 deletions

View File

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

View File

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