From 88e8e3a59e1a3223902025f8ea9a5018fb2e8f59 Mon Sep 17 00:00:00 2001 From: Gabrielle DeWitt Date: Sat, 4 Apr 2015 11:51:24 -0700 Subject: [PATCH 1/3] Added Helper for See Who's Planted Link Text Quantity and planted_from are not required to add a planting. This change handles the cases where one or both attributes are missing. Fixes #633 --- app/helpers/plantings_helper.rb | 15 ++++++ app/views/crops/_plantings.html.haml | 2 +- spec/helpers/plantings_helper_spec.rb | 78 +++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 app/helpers/plantings_helper.rb create mode 100644 spec/helpers/plantings_helper_spec.rb diff --git a/app/helpers/plantings_helper.rb b/app/helpers/plantings_helper.rb new file mode 100644 index 000000000..6478c8d5f --- /dev/null +++ b/app/helpers/plantings_helper.rb @@ -0,0 +1,15 @@ +module PlantingsHelper + + def display_planting(planting) + if planting.quantity.to_i > 0 && planting.planted_from.present? + return "#{planting.owner} planted #{pluralize(planting.quantity, planting.planted_from)}." + elsif planting.quantity.to_i > 0 + return "#{planting.owner} planted #{pluralize(planting.quantity, 'unit')}." + elsif planting.planted_from.present? + return "#{planting.owner} planted #{planting.planted_from.pluralize}." + else + return "#{planting.owner}." + end + end + +end \ No newline at end of file diff --git a/app/views/crops/_plantings.html.haml b/app/views/crops/_plantings.html.haml index 621a6d125..5018d99a3 100644 --- a/app/views/crops/_plantings.html.haml +++ b/app/views/crops/_plantings.html.haml @@ -6,7 +6,7 @@ %ul - crop.plantings.take(3).each do |planting| %li - = link_to "#{planting.owner} planted #{planting.quantity} #{planting.planted_from}.", planting_path(planting) + = link_to display_planting(planting), planting_path(planting) = render :partial => 'members/location', :locals => { :member => planting.owner } %small = distance_of_time_in_words(planting.created_at, Time.zone.now) diff --git a/spec/helpers/plantings_helper_spec.rb b/spec/helpers/plantings_helper_spec.rb new file mode 100644 index 000000000..127585810 --- /dev/null +++ b/spec/helpers/plantings_helper_spec.rb @@ -0,0 +1,78 @@ +require 'rails_helper' + +describe PlantingsHelper do + describe "display_planting" do + + before(:each) do + @member = FactoryGirl.build(:member, + :login_name => 'crop_lady' + ) + end + + it "neither quantity nor planted from provided" do + planting = FactoryGirl.build(:planting, + :quantity => nil, + :planted_from => '', + :owner => @member + ) + result = helper.display_planting(planting) + result.should eq "crop_lady." + end + + it "quantity not provided" do + planting = FactoryGirl.build(:planting, + :quantity => nil, + :planted_from => 'seed', + :owner => @member + ) + result = helper.display_planting(planting) + result.should eq "crop_lady planted seeds." + end + + context "multiple quantity" do + it "planted_from not provided" do + planting = FactoryGirl.build(:planting, + :quantity => 10, + :planted_from => '', + :owner => @member + ) + result = helper.display_planting(planting) + result.should eq "crop_lady planted 10 units." + end + + it "planted_from provided" do + planting = FactoryGirl.build(:planting, + :quantity => 5, + :planted_from => 'seed', + :owner => @member + ) + result = helper.display_planting(planting) + result.should eq "crop_lady planted 5 seeds." + end + end + + context "single quantity" do + it "planted_from not provided" do + planting = FactoryGirl.build(:planting, + :quantity => 1, + :planted_from => '', + :owner => @member + ) + result = helper.display_planting(planting) + result.should eq "crop_lady planted 1 unit." + end + + it "planted_from provided" do + planting = FactoryGirl.build(:planting, + :quantity => 1, + :planted_from => 'seed', + :owner => @member + ) + result = helper.display_planting(planting) + result.should eq "crop_lady planted 1 seed." + end + + end + + end +end \ No newline at end of file From b7d7f6896ef1fd73cc1440b63c781ad8790dea70 Mon Sep 17 00:00:00 2001 From: Gabrielle DeWitt Date: Sat, 4 Apr 2015 11:54:49 -0700 Subject: [PATCH 2/3] Updated Helper Spec to Match Coding Standards Replaced before and should with let and expect, respectively. Issue #633 --- spec/helpers/plantings_helper_spec.rb | 46 ++++++++++++--------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/spec/helpers/plantings_helper_spec.rb b/spec/helpers/plantings_helper_spec.rb index 127585810..4cb2c63a7 100644 --- a/spec/helpers/plantings_helper_spec.rb +++ b/spec/helpers/plantings_helper_spec.rb @@ -3,73 +3,69 @@ require 'rails_helper' describe PlantingsHelper do describe "display_planting" do - before(:each) do - @member = FactoryGirl.build(:member, - :login_name => 'crop_lady' - ) - end + let!(:member) { FactoryGirl.build(:member, :login_name => 'crop_lady') } - it "neither quantity nor planted from provided" do + it "does not have a quantity nor a planted from value provided" do planting = FactoryGirl.build(:planting, :quantity => nil, :planted_from => '', - :owner => @member + :owner => member ) result = helper.display_planting(planting) - result.should eq "crop_lady." + expect(result).to eq "crop_lady." end - it "quantity not provided" do + it "does not have a quantity provided" do planting = FactoryGirl.build(:planting, :quantity => nil, :planted_from => 'seed', - :owner => @member + :owner => member ) result = helper.display_planting(planting) - result.should eq "crop_lady planted seeds." + expect(result).to eq "crop_lady planted seeds." end - context "multiple quantity" do - it "planted_from not provided" do + context "when quantity is greater than 1" do + it "does not have a planted from value provided" do planting = FactoryGirl.build(:planting, :quantity => 10, :planted_from => '', - :owner => @member + :owner => member ) result = helper.display_planting(planting) - result.should eq "crop_lady planted 10 units." + expect(result).to eq "crop_lady planted 10 units." end - it "planted_from provided" do + it "does have a planted from value provided" do planting = FactoryGirl.build(:planting, :quantity => 5, :planted_from => 'seed', - :owner => @member + :owner => member ) result = helper.display_planting(planting) - result.should eq "crop_lady planted 5 seeds." + expect(result).to eq "crop_lady planted 5 seeds." end end - context "single quantity" do - it "planted_from not provided" do + context "when quantity is 1" do + it "does not have a planted from value provided" do planting = FactoryGirl.build(:planting, :quantity => 1, :planted_from => '', - :owner => @member + :owner => member ) result = helper.display_planting(planting) - result.should eq "crop_lady planted 1 unit." + expect(result).to eq "crop_lady planted 1 unit." end - it "planted_from provided" do + it "does have a planted from value provided" do planting = FactoryGirl.build(:planting, :quantity => 1, :planted_from => 'seed', - :owner => @member + :owner => member ) result = helper.display_planting(planting) - result.should eq "crop_lady planted 1 seed." + expect(result).to eq "crop_lady planted 1 seed." end end From 81d2f9829e580e9b57c37b42776c727cdf4e893a Mon Sep 17 00:00:00 2001 From: Gabrielle DeWitt Date: Sat, 4 Apr 2015 12:00:08 -0700 Subject: [PATCH 3/3] Updated Contributors --- CONTRIBUTORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index ad76d09fa..845ddfc01 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -54,4 +54,5 @@ submit the change with your pull request. - Rocky Jaiswal / [rocky-jaiswal](https://github.com/rocky-jaiswal) - Robert Landreaux / [robertlandreaux](https://github.com/robertlandreaux) - Savant Krishna / [sksavant](https://github.com/sksavant) +- Gabrielle DeWitt / [gabrielle27](https://github.com/gabrielle27)