Automatically set plantings to finished when a garden is marked inactive

This commit is contained in:
Skud
2014-08-30 10:51:33 +10:00
parent 170d6b474e
commit af2d0e22ee
3 changed files with 33 additions and 1 deletions

View File

@@ -14,6 +14,7 @@ class Garden < ActiveRecord::Base
geocoded_by :location
after_validation :geocode
after_validation :empty_unwanted_geocodes
after_save :mark_inactive_garden_plantings_as_finished
default_scope order("lower(name) asc")
scope :active, where(:active => true)
@@ -74,4 +75,15 @@ class Garden < ActiveRecord::Base
name
end
# When you mark a garden as inactive, all the plantings in it should be
# marked as finished. This automates that.
def mark_inactive_garden_plantings_as_finished
if (active == false)
plantings.current.each do |p|
p.finished = true
p.save
end
end
end
end

View File

@@ -8,7 +8,9 @@
- if can? :edit, @garden
- if @garden.active
= link_to "Plant something", new_planting_path(:garden_id => @garden.id), :class => 'btn btn-primary'
= link_to "Mark as inactive", garden_path(@garden, :garden => {:active => 0}), :method => :put, :class => 'btn btn-default'
= link_to "Mark as inactive", garden_path(@garden, :garden => {:active => 0}), |
:method => :put, :class => 'btn btn-default', |
data: { confirm: 'All plantings associated with this garden will be marked as finished. Are you sure?' }
- else
= link_to "Mark as active", garden_path(@garden, :garden => {:active => 1}), :method => :put, :class => 'btn btn-default'
= link_to 'Edit garden', edit_garden_path(@garden), :class => 'btn btn-default'

View File

@@ -165,4 +165,22 @@ describe Garden do
end
end
it "marks plantings as finished when garden is inactive" do
garden = FactoryGirl.create(:garden)
p1 = FactoryGirl.create(:planting, :garden => garden)
p2 = FactoryGirl.create(:planting, :garden => garden)
p1.finished.should eq false
p2.finished.should eq false
garden.active = false
garden.save
p1.reload
p1.finished.should eq true
p2.reload
p2.finished.should eq true
end
end