diff --git a/app/models/crop.rb b/app/models/crop.rb index acb04d6d5..06b504274 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -43,4 +43,24 @@ class Crop < ActiveRecord::Base return photos.first end + def sunniness + sunniness = Hash.new(0) + plantings.each do |p| + if !p.sunniness.blank? + sunniness[p.sunniness] += 1 + end + end + return sunniness + end + + def planted_from + planted_from = Hash.new(0) + plantings.each do |p| + if !p.planted_from.blank? + planted_from[p.planted_from] += 1 + end + end + return planted_from + end + end diff --git a/app/views/crops/show.html.haml b/app/views/crops/show.html.haml index f9a08a076..5183041b1 100644 --- a/app/views/crops/show.html.haml +++ b/app/views/crops/show.html.haml @@ -14,6 +14,16 @@ = succeed ":" do = @crop.system_name != @crop.varieties.map{ |c| link_to c, c }.join(", ") + - if @crop.planted_from.length > 0 + %p + 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: + - sunniness = @crop.sunniness.sort_by {|s, freq| freq }.reverse + = sunniness.map {|s, freq| "#{s} (#{freq})" }.join(", ") - if @crop.plantings_count > 0 %p diff --git a/spec/factories/planting.rb b/spec/factories/planting.rb index 963b7a2b0..58f67f653 100644 --- a/spec/factories/planting.rb +++ b/spec/factories/planting.rb @@ -5,7 +5,29 @@ FactoryGirl.define do planted_at Date.today quantity 33 description "This is a *really* good plant." - sunniness 'sun' - planted_from 'seed' + + factory :seed_planting do + planted_from 'seed' + end + + factory :seedling_planting do + planted_from 'seedling' + end + + factory :cutting_planting do + planted_from 'cutting' + end + + factory :sunny_planting do + sunniness 'sun' + end + + factory :semi_shady_planting do + sunniness 'semi-shade' + end + + factory :shady_planting do + sunniness 'shade' + end end end diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index 3184d236e..6c66d2399 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -79,4 +79,64 @@ describe Crop do @crop.default_photo.should be_an_instance_of Photo end end + + context 'sunniness' do + before(:each) do + @crop = FactoryGirl.create(:tomato) + end + + it 'returns a hash of sunniness values' do + planting1 = FactoryGirl.create(:sunny_planting, :crop => @crop) + planting2 = FactoryGirl.create(:sunny_planting, :crop => @crop) + planting3 = FactoryGirl.create(:semi_shady_planting, :crop => @crop) + planting4 = FactoryGirl.create(:shady_planting, :crop => @crop) + @crop.sunniness.should be_an_instance_of Hash + end + + it 'counts each sunniness value' do + planting1 = FactoryGirl.create(:sunny_planting, :crop => @crop) + planting2 = FactoryGirl.create(:sunny_planting, :crop => @crop) + planting3 = FactoryGirl.create(:semi_shady_planting, :crop => @crop) + planting4 = FactoryGirl.create(:shady_planting, :crop => @crop) + @crop.sunniness.should == { 'sun' => 2, 'shade' => 1, 'semi-shade' => 1 } + end + + it 'ignores unused sunniness values' do + planting1 = FactoryGirl.create(:sunny_planting, :crop => @crop) + planting2 = FactoryGirl.create(:sunny_planting, :crop => @crop) + planting3 = FactoryGirl.create(:semi_shady_planting, :crop => @crop) + @crop.sunniness.should == { 'sun' => 2, 'semi-shade' => 1 } + end + end + + context 'planted_from' do + before(:each) do + @crop = FactoryGirl.create(:tomato) + end + + it 'returns a hash of sunniness values' do + planting1 = FactoryGirl.create(:seed_planting, :crop => @crop) + planting2 = FactoryGirl.create(:seed_planting, :crop => @crop) + planting3 = FactoryGirl.create(:seedling_planting, :crop => @crop) + planting4 = FactoryGirl.create(:cutting_planting, :crop => @crop) + @crop.planted_from.should be_an_instance_of Hash + end + + it 'counts each planted_from value' do + planting1 = FactoryGirl.create(:seed_planting, :crop => @crop) + planting2 = FactoryGirl.create(:seed_planting, :crop => @crop) + planting3 = FactoryGirl.create(:seedling_planting, :crop => @crop) + planting4 = FactoryGirl.create(:cutting_planting, :crop => @crop) + @crop.planted_from.should == { 'seed' => 2, 'seedling' => 1, 'cutting' => 1 } + end + + it 'ignores unused planted_from values' do + planting1 = FactoryGirl.create(:seed_planting, :crop => @crop) + planting2 = FactoryGirl.create(:seed_planting, :crop => @crop) + planting3 = FactoryGirl.create(:seedling_planting, :crop => @crop) + @crop.planted_from.should == { 'seed' => 2, 'seedling' => 1 } + end + end + + end diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index ca2d4e02d..692c53284 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -50,6 +50,10 @@ describe Planting do end context 'sunniness' do + before(:each) do + @planting = FactoryGirl.create(:sunny_planting) + end + it 'should have a sunniness value' do @planting.sunniness.should eq 'sun' end @@ -70,6 +74,7 @@ describe Planting do context 'planted from' do it 'should have a planted_from value' do + @planting = FactoryGirl.create(:seed_planting) @planting.planted_from.should eq 'seed' end diff --git a/spec/views/crops/show.html.haml_spec.rb b/spec/views/crops/show.html.haml_spec.rb index b1bfe26da..f59d77b9a 100644 --- a/spec/views/crops/show.html.haml_spec.rb +++ b/spec/views/crops/show.html.haml_spec.rb @@ -6,15 +6,7 @@ describe "crops/show" do @crop = FactoryGirl.create(:maize, :scientific_names => [ FactoryGirl.create(:zea_mays) ] ) - @owner = FactoryGirl.create(:member) - @garden = FactoryGirl.create(:garden, :owner => @owner) - @planting = FactoryGirl.create(:planting, - :garden => @garden, - :crop => @crop - ) - assign(:crop, @crop) - end it "shows the wikipedia URL" do @@ -38,18 +30,71 @@ describe "crops/show" do assert_select("a[href=#{new_planting_path}?crop_id=#{@crop.id}]") end - it "links to people who are growing this crop" do - render - rendered.should contain /member\d+/ - rendered.should contain "Springfield Community Garden" - end + context "has plantings" do + before(:each) do + @owner = FactoryGirl.create(:member) + @garden = FactoryGirl.create(:garden, :owner => @owner) + @planting = FactoryGirl.create(:planting, + :garden => @garden, + :crop => @crop + ) + 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+/ + rendered.should contain "Springfield Community Garden" + end + + it "shows photos where available" do + @photo = FactoryGirl.create(:photo) + @planting.photos << @photo + render + assert_select "img", :src => @photo.thumbnail_url + end - it "shows photos where available" do - @planting = FactoryGirl.create(:planting, :crop => @crop) - @photo = FactoryGirl.create(:photo) - @planting.photos << @photo - render - assert_select "img", :src => @photo.thumbnail_url end context 'varieties' do diff --git a/spec/views/plantings/show.html.haml_spec.rb b/spec/views/plantings/show.html.haml_spec.rb index ddd8db657..3cc735a86 100644 --- a/spec/views/plantings/show.html.haml_spec.rb +++ b/spec/views/plantings/show.html.haml_spec.rb @@ -16,6 +16,11 @@ describe "plantings/show" do end context 'sunniness' do + before(:each) do + @p = assign(:planting, + FactoryGirl.create(:sunny_planting) + ) + end it "shows the sunniness" do render @@ -33,6 +38,10 @@ describe "plantings/show" do end context 'planted from' do + before(:each) do + @p = assign(:planting, FactoryGirl.create(:seed_planting)) + end + it "shows planted_from" do render rendered.should contain 'Planted from:'