mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-02-20 07:38:13 -05:00
refactored 'interesting plantings'
This commit is contained in:
@@ -53,10 +53,12 @@ class Planting < ActiveRecord::Base
|
||||
"#{owner.login_name}-#{garden}-#{crop}".downcase.gsub(' ', '-')
|
||||
end
|
||||
|
||||
# location = owner + garden, i.e. "Skud's backyard"
|
||||
def location
|
||||
return "#{garden.owner.login_name}'s #{garden}"
|
||||
end
|
||||
|
||||
# stringify as "beet in Skud's backyard" or similar
|
||||
def to_s
|
||||
self.crop_system_name + " in " + self.location
|
||||
end
|
||||
@@ -64,4 +66,23 @@ class Planting < ActiveRecord::Base
|
||||
def default_photo
|
||||
return photos.first
|
||||
end
|
||||
|
||||
# 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=10)
|
||||
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.photos.present? # skip those that don't have photos
|
||||
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)
|
||||
end
|
||||
|
||||
return interesting_plantings
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,29 +6,12 @@
|
||||
- next unless c.photos.present?
|
||||
- count += 1
|
||||
.span3{:style => 'margin:0px; padding: 3px'}
|
||||
= render :partial => 'crops/image_with_popover.html.haml', :locals => { :crop => c }
|
||||
= render :partial => 'crops/image_with_popover', :locals => { :crop => c }
|
||||
- break if count == 12
|
||||
|
||||
.span6
|
||||
%h2 Recently planted
|
||||
- count = 0
|
||||
- Planting.all.each do |p|
|
||||
- next unless p.photos.present?
|
||||
- count += 1
|
||||
.row
|
||||
.span2{:style => 'padding-bottom: 6px'}
|
||||
= render :partial => 'plantings/image_with_popover.html.haml', :locals => { :planting => p }
|
||||
.span10
|
||||
= link_to p.crop, p.crop
|
||||
in
|
||||
= succeed "'s" do
|
||||
= link_to p.owner, p.owner
|
||||
= link_to p.garden, p.garden
|
||||
%br/
|
||||
%small
|
||||
%i
|
||||
= p.owner.location
|
||||
- break if count == 4
|
||||
= render :partial => 'plantings/list', :locals => { :plantings => Planting.interesting(4) }
|
||||
|
||||
.row-fluid
|
||||
.span12
|
||||
|
||||
@@ -1,2 +1,14 @@
|
||||
|
||||
|
||||
- plantings.each do |p|
|
||||
.row
|
||||
.span2{:style => 'padding-bottom: 6px'}
|
||||
= render :partial => 'plantings/image_with_popover', :locals => { :planting => p }
|
||||
.span10
|
||||
= link_to p.crop, p.crop
|
||||
in
|
||||
= succeed "'s" do
|
||||
= link_to p.owner, p.owner
|
||||
= link_to p.garden, p.garden
|
||||
%br/
|
||||
%small
|
||||
%i
|
||||
= p.owner.location
|
||||
|
||||
@@ -148,4 +148,80 @@ describe Planting do
|
||||
end
|
||||
end
|
||||
|
||||
context 'interesting crops' do
|
||||
it 'picks up interesting plantings' do
|
||||
# plantings have members created implicitly for them
|
||||
# each member is different, hence these are all interesting
|
||||
@planting1 = FactoryGirl.create(:planting, :created_at => 5.days.ago)
|
||||
@planting2 = FactoryGirl.create(:planting, :created_at => 4.days.ago)
|
||||
@planting3 = FactoryGirl.create(:planting, :created_at => 3.days.ago)
|
||||
@planting4 = FactoryGirl.create(:planting, :created_at => 2.days.ago)
|
||||
|
||||
# plantings need photos to be interesting
|
||||
@photo = FactoryGirl.create(:photo)
|
||||
[@planting1, @planting2, @planting3, @planting4].each do |p|
|
||||
p.photos << @photo
|
||||
p.save
|
||||
end
|
||||
|
||||
Planting.interesting.should eq [
|
||||
@planting4,
|
||||
@planting3,
|
||||
@planting2,
|
||||
@planting1
|
||||
]
|
||||
end
|
||||
|
||||
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)
|
||||
|
||||
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,
|
||||
:garden_id => @planting1.garden.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
|
||||
|
||||
it 'only gives you as many as you ask for' do
|
||||
# plantings have members created implicitly for them
|
||||
# each member is different, hence these are all interesting
|
||||
@planting1 = FactoryGirl.create(:planting, :created_at => 5.days.ago)
|
||||
@planting2 = FactoryGirl.create(:planting, :created_at => 4.days.ago)
|
||||
@planting3 = FactoryGirl.create(:planting, :created_at => 3.days.ago)
|
||||
@planting4 = FactoryGirl.create(:planting, :created_at => 2.days.ago)
|
||||
|
||||
# plantings need photos to be interesting
|
||||
@photo = FactoryGirl.create(:photo)
|
||||
[@planting1, @planting2, @planting3, @planting4].each do |p|
|
||||
p.photos << @photo
|
||||
p.save
|
||||
end
|
||||
|
||||
Planting.interesting(2).length.should == 2
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user