diff --git a/app/models/planting.rb b/app/models/planting.rb index 270a827d2..e5955f04f 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 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 + def planting_slug "#{owner.login_name}-#{garden}-#{crop}".downcase.gsub(' ', '-') end diff --git a/app/views/gardens/_form.html.haml b/app/views/gardens/_form.html.haml index dc0c2c6a1..f5cd7c429 100644 --- a/app/views/gardens/_form.html.haml +++ b/app/views/gardens/_form.html.haml @@ -39,7 +39,8 @@ = f.label :active, '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 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/features/plantings/planting_a_crop_spec.rb b/spec/features/plantings/planting_a_crop_spec.rb index 46d293dec..faa4438e8 100644 --- a/spec/features/plantings/planting_a_crop_spec.rb +++ b/spec/features/plantings/planting_a_crop_spec.rb @@ -42,6 +42,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 13b910d22..5248e0b0a 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -229,6 +229,24 @@ 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 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(:planting, :finished_at => '2013-01-01', :planted_at => nil) + @f.should be_valid + end + + end + end end