Create all Phases after a Budget creation

This commit is contained in:
Bertocq
2018-01-15 20:33:07 +01:00
parent 10f5cc0d3b
commit 59fb0b562c
2 changed files with 78 additions and 12 deletions

View File

@@ -18,6 +18,8 @@ class Budget < ActiveRecord::Base
before_validation :sanitize_descriptions
after_create :generate_phases
scope :drafting, -> { where(phase: "drafting") }
scope :accepting, -> { where(phase: "accepting") }
scope :reviewing, -> { where(phase: "reviewing") }
@@ -156,5 +158,17 @@ class Budget < ActiveRecord::Base
send("description_#{phase}=", sanitized)
end
end
def generate_phases
Budget::Phase::PHASE_KINDS.each do |phase|
Budget::Phase.create(
budget: self,
kind: phase,
prev_phase: phases&.last,
starts_at: phases&.last&.ends_at || Date.current,
ends_at: (phases&.last&.ends_at || Date.current) + 1.month
)
end
end
end