Show aggregate 'grown from' data on crop page

This commit is contained in:
Skud committed 2013-11-06 11:21:08 +11:00
1 parent 838f3c6e36
commit 60eb4c7296
9 files changed
+169 -59

No files matched your search

+15
View File
@@ -8,6 +8,7 @@ class Crop < ActiveRecord::Base
has_many :photos, :through => :plantings
has_many :seeds
has_many :harvests
has_many :plant_parts, :through => :harvests, :uniq => :true
belongs_to :creator, :class_name => 'Member'
belongs_to :parent, :class_name => 'Crop'
@@ -78,6 +79,20 @@ class Crop < ActiveRecord::Base
return planted_from
end
# crop.popular_plant_parts
# returns a hash of most harvested plant parts (fruit, seed, etc)
# key: plant part (eg. 'fruit')
# value: count of how many times it's been used by harvests
def popular_plant_parts
popular_plant_parts = Hash.new(0)
harvests.each do |h|
if h.plant_part
popular_plant_parts[h.plant_part] += 1
end
end
return popular_plant_parts
end
def interesting?
min_plantings = 3 # needs this many plantings to be interesting
min_photos = 3 # needs this many photos to be interesting
+5
View File
@@ -0,0 +1,5 @@
- if ! crop.harvests.empty?
%p
%strong Grown for:
- popular_plant_parts = crop.popular_plant_parts.sort_by {|s, freq| freq }.reverse
!= popular_plant_parts.map {|p, freq| link_to(p, p) + " (#{freq})" }.join(", ")
+2 -2
View File
@@ -1,10 +1,10 @@
- if crop.planted_from.length > 0
%p
Plant from:
%strong Plant from:
- planted_from = crop.planted_from.sort_by {|s, freq| freq }.reverse
= planted_from.map {|s, freq| "#{s} (#{freq})" }.join(", ")
- if crop.sunniness.length > 0
%p
Plant in:
%strong Plant in:
- sunniness = crop.sunniness.sort_by {|s, freq| freq }.reverse
= sunniness.map {|s, freq| "#{s} (#{freq})" }.join(", ")
+2
View File
@@ -6,6 +6,8 @@
= render :partial => 'varieties', :locals => { :crop => @crop }
= render :partial => 'grown_for', :locals => { :crop => @crop }
= render :partial => 'planting_advice', :locals => { :crop => @crop }
%p
-15
View File
@@ -1,15 +0,0 @@
require 'spec_helper'
# Specs in this file have access to a helper object that includes
# the PlantPartsHelper. For example:
#
# describe PlantPartsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# helper.concat_strings("this","that").should == "this that"
# end
# end
# end
describe PlantPartsHelper do
pending "add some examples to (or delete) #{__FILE__}"
end
+65
View File
@@ -157,6 +157,41 @@ describe Crop do
end
end
context 'popular plant parts' do
before(:each) do
@crop = FactoryGirl.create(:tomato)
end
it 'returns a hash of plant_part values' do
@crop.popular_plant_parts.should be_an_instance_of Hash
end
it 'counts each plant_part value' do
@fruit = FactoryGirl.create(:plant_part)
@seed = FactoryGirl.create(:plant_part)
@root = FactoryGirl.create(:plant_part)
@bulb = FactoryGirl.create(:plant_part)
@harvest1 = FactoryGirl.create(:harvest,
:crop => @crop,
:plant_part => @fruit
)
@harvest2 = FactoryGirl.create(:harvest,
:crop => @crop,
:plant_part => @fruit
)
@harvest3 = FactoryGirl.create(:harvest,
:crop => @crop,
:plant_part => @seed
)
@harvest4 = FactoryGirl.create(:harvest,
:crop => @crop,
:plant_part => @root
)
@crop.popular_plant_parts.should == { @fruit => 2, @seed => 1, @root => 1 }
end
end
context 'interesting' do
it 'lists interesting crops' do
# first, a couple of candidate crops
@@ -243,4 +278,34 @@ describe Crop do
end
end
it 'has plant_parts' do
@maize = FactoryGirl.create(:maize)
@pp1 = FactoryGirl.create(:plant_part)
@pp2 = FactoryGirl.create(:plant_part)
@h1 = FactoryGirl.create(:harvest,
:crop => @maize,
:plant_part => @pp1
)
@h2 = FactoryGirl.create(:harvest,
:crop => @maize,
:plant_part => @pp2
)
@maize.plant_parts.should include @pp1
@maize.plant_parts.should include @pp2
end
it "doesn't dupliate plant_parts" do
@maize = FactoryGirl.create(:maize)
@pp1 = FactoryGirl.create(:plant_part)
@h1 = FactoryGirl.create(:harvest,
:crop => @maize,
:plant_part => @pp1
)
@h2 = FactoryGirl.create(:harvest,
:crop => @maize,
:plant_part => @pp1
)
@maize.plant_parts.should eq [@pp1]
end
end
@@ -0,0 +1,18 @@
require 'spec_helper'
describe "crops/_grown_for" do
before(:each) do
@crop = FactoryGirl.create(:crop)
@pp = FactoryGirl.create(:plant_part)
@harvest = FactoryGirl.create(:harvest,
:crop => @crop,
:plant_part => @pp
)
end
it 'shows plant parts' do
render :partial => 'crops/grown_for', :locals => { :crop => @crop }
rendered.should contain @pp.name
assert_select "a", :href => plant_part_path(@pp)
end
end
@@ -0,0 +1,62 @@
require 'spec_helper'
describe "crops/_planting_advice" do
before(:each) do
@owner = FactoryGirl.create(:member)
@crop = FactoryGirl.create(:crop)
@garden = FactoryGirl.create(:garden, :owner => @owner)
@planting = FactoryGirl.create(:planting,
:garden => @garden,
:crop => @crop
)
end
context "sunniness" do
it "doesn't show sunniness if none are set" do
render :partial => 'crops/planting_advice', :locals => { :crop => @crop }
rendered.should_not contain "Plant in:"
end
it "shows sunniness frequencies" do
FactoryGirl.create(:sunny_planting, :crop => @crop)
render :partial => 'crops/planting_advice', :locals => { :crop => @crop }
rendered.should contain "Plant in:"
rendered.should contain "sun (1)"
end
it "shows multiple sunniness frequencies" do
FactoryGirl.create(:sunny_planting, :crop => @crop)
FactoryGirl.create(:sunny_planting, :crop => @crop)
FactoryGirl.create(:shady_planting, :crop => @crop)
render :partial => 'crops/planting_advice', :locals => { :crop => @crop }
rendered.should contain "Plant in:"
rendered.should contain "sun (2), shade (1)"
end
end
context "planted from" do
it "doesn't show planted_from if none are set" do
render :partial => 'crops/planting_advice', :locals => { :crop => @crop }
rendered.should_not contain "Plant from:"
end
it "shows planted_from frequencies" do
FactoryGirl.create(:seed_planting, :crop => @crop)
render :partial => 'crops/planting_advice', :locals => { :crop => @crop }
rendered.should contain "Plant from:"
rendered.should contain "seed (1)"
end
it "shows multiple planted_from frequencies" do
FactoryGirl.create(:seed_planting, :crop => @crop)
FactoryGirl.create(:seed_planting, :crop => @crop)
FactoryGirl.create(:cutting_planting, :crop => @crop)
render :partial => 'crops/planting_advice', :locals => { :crop => @crop }
rendered.should contain "Plant from:"
rendered.should contain "seed (2), cutting (1)"
end
end
end
-42
View File
@@ -94,48 +94,6 @@ describe "crops/show" do
)
end
it "doesn't show sunniness if none are set" do
render
rendered.should_not contain "Plant in:"
end
it "shows sunniness frequencies" do
FactoryGirl.create(:sunny_planting, :crop => @crop)
render
rendered.should contain "Plant in:"
rendered.should contain "sun (1)"
end
it "shows multiple sunniness frequencies" do
FactoryGirl.create(:sunny_planting, :crop => @crop)
FactoryGirl.create(:sunny_planting, :crop => @crop)
FactoryGirl.create(:shady_planting, :crop => @crop)
render
rendered.should contain "Plant in:"
rendered.should contain "sun (2), shade (1)"
end
it "doesn't show planted_from if none are set" do
render
rendered.should_not contain "Plant from:"
end
it "shows planted_from frequencies" do
FactoryGirl.create(:seed_planting, :crop => @crop)
render
rendered.should contain "Plant from:"
rendered.should contain "seed (1)"
end
it "shows multiple planted_from frequencies" do
FactoryGirl.create(:seed_planting, :crop => @crop)
FactoryGirl.create(:seed_planting, :crop => @crop)
FactoryGirl.create(:cutting_planting, :crop => @crop)
render
rendered.should contain "Plant from:"
rendered.should contain "seed (2), cutting (1)"
end
it "links to people who are growing this crop" do
render
rendered.should contain /member\d+/