diff --git a/app/models/crop.rb b/app/models/crop.rb index 12c397613..b425028fb 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -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 diff --git a/app/views/crops/_grown_for.html.haml b/app/views/crops/_grown_for.html.haml new file mode 100644 index 000000000..555371a23 --- /dev/null +++ b/app/views/crops/_grown_for.html.haml @@ -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(", ") diff --git a/app/views/crops/_planting_advice.html.haml b/app/views/crops/_planting_advice.html.haml index 4a44502e7..bdd3ceb4d 100644 --- a/app/views/crops/_planting_advice.html.haml +++ b/app/views/crops/_planting_advice.html.haml @@ -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(", ") diff --git a/app/views/crops/show.html.haml b/app/views/crops/show.html.haml index 9789eaefc..5dc254616 100644 --- a/app/views/crops/show.html.haml +++ b/app/views/crops/show.html.haml @@ -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 diff --git a/spec/helpers/plant_parts_helper_spec.rb b/spec/helpers/plant_parts_helper_spec.rb deleted file mode 100644 index bae3d4ba7..000000000 --- a/spec/helpers/plant_parts_helper_spec.rb +++ /dev/null @@ -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 diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index 531145d96..aa8e20fce 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -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 diff --git a/spec/views/crops/_grown_for.html.haml_spec.rb b/spec/views/crops/_grown_for.html.haml_spec.rb new file mode 100644 index 000000000..3950c1d32 --- /dev/null +++ b/spec/views/crops/_grown_for.html.haml_spec.rb @@ -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 diff --git a/spec/views/crops/_planting_advice.html.haml_spec.rb b/spec/views/crops/_planting_advice.html.haml_spec.rb new file mode 100644 index 000000000..f92fe2bd7 --- /dev/null +++ b/spec/views/crops/_planting_advice.html.haml_spec.rb @@ -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 diff --git a/spec/views/crops/show.html.haml_spec.rb b/spec/views/crops/show.html.haml_spec.rb index 34462bbda..508d76839 100644 --- a/spec/views/crops/show.html.haml_spec.rb +++ b/spec/views/crops/show.html.haml_spec.rb @@ -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+/