From 03235f3952cb893f099081f7fb88d1e93c225d11 Mon Sep 17 00:00:00 2001 From: Skud Date: Fri, 12 Sep 2014 07:43:41 +1000 Subject: [PATCH 1/3] Add warning about plantings being marked as finished --- app/views/gardens/_form.html.haml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/views/gardens/_form.html.haml b/app/views/gardens/_form.html.haml index 6313762c4..8a19edb2c 100644 --- a/app/views/gardens/_form.html.haml +++ b/app/views/gardens/_form.html.haml @@ -39,7 +39,8 @@ = f.label 'Active? ', :class => 'control-label col-md-2' .col-md-8 = f.check_box :active - You can mark a garden as inactive if you no longer use it. + You can mark a garden as inactive if you no longer use it. Note: + this will mark all plantings in the garden as "finished". .form-group .form-actions.col-md-offset-2.col-md-8 From 85b5ac12cad18f7293342a23bab69892da895c04 Mon Sep 17 00:00:00 2001 From: Skud Date: Fri, 12 Sep 2014 07:44:07 +1000 Subject: [PATCH 2/3] added validation for finished_at (must be after planted_at) --- app/models/planting.rb | 8 ++++++++ spec/factories/planting.rb | 1 + spec/models/planting_spec.rb | 14 ++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/app/models/planting.rb b/app/models/planting.rb index 270a827d2..9585d0914 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -56,6 +56,14 @@ class Planting < ActiveRecord::Base :allow_nil => true, :allow_blank => true + validate :finished_must_be_after_planted + + # check that any finished_at date occurs after planted_at + def finished_must_be_after_planted + return unless planted_at && finished_at # only check if we have both + errors.add(:finished_at, "must be after the planting date") unless planted_at < finished_at + end + def planting_slug "#{owner.login_name}-#{garden}-#{crop}".downcase.gsub(' ', '-') end diff --git a/spec/factories/planting.rb b/spec/factories/planting.rb index 5d229d545..60b4842e8 100644 --- a/spec/factories/planting.rb +++ b/spec/factories/planting.rb @@ -33,6 +33,7 @@ FactoryGirl.define do factory :finished_planting do finished true + planted_at '2014-07-30' finished_at '2014-08-30' end end diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index 13b910d22..6a93d3750 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -229,6 +229,20 @@ describe Planting do Planting.current.should_not include @f end + context "finished date validation" do + it 'requires finished date after planting date' do + @f = FactoryGirl.build(:finished_planting, :planted_at => + '2014-01-01', :finished_at => '2013-01-01') + @f.should_not be_valid + end + + it 'allows just the finished date' do + @f = FactoryGirl.build(:finished_planting, :finished_at => '2013-01-01') + @f.should be_valid + end + + end + end end From 374987488fada6277be2caf64fd4840828e6b714 Mon Sep 17 00:00:00 2001 From: Skud Date: Fri, 12 Sep 2014 08:30:44 +1000 Subject: [PATCH 3/3] Fixed broken tests. --- app/models/planting.rb | 2 +- spec/features/planting_a_crop_spec.rb | 1 + spec/models/planting_spec.rb | 6 +++++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/models/planting.rb b/app/models/planting.rb index 9585d0914..e5955f04f 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -60,7 +60,7 @@ class Planting < ActiveRecord::Base # check that any finished_at date occurs after planted_at def finished_must_be_after_planted - return unless planted_at && finished_at # only check if we have both + return unless planted_at and finished_at # only check if we have both errors.add(:finished_at, "must be after the planting date") unless planted_at < finished_at end diff --git a/spec/features/planting_a_crop_spec.rb b/spec/features/planting_a_crop_spec.rb index 52226aeee..12d0fc82a 100644 --- a/spec/features/planting_a_crop_spec.rb +++ b/spec/features/planting_a_crop_spec.rb @@ -30,6 +30,7 @@ feature "Planting a crop", :js => true do fill_autocomplete "crop", :with => "m" select_from_autocomplete "maize" within "form#new_planting" do + fill_in "When?", :with => '2014-07-01' check 'Mark as finished' fill_in "Finished date", :with => '2014-08-30' click_button "Save" diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index 6a93d3750..5248e0b0a 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -236,8 +236,12 @@ describe Planting do @f.should_not be_valid end + it 'allows just the planted date' do + @f = FactoryGirl.build(:planting, :planted_at => '2013-01-01', :finished_at => nil) + @f.should be_valid + end it 'allows just the finished date' do - @f = FactoryGirl.build(:finished_planting, :finished_at => '2013-01-01') + @f = FactoryGirl.build(:planting, :finished_at => '2013-01-01', :planted_at => nil) @f.should be_valid end