Create all Phases after a Budget creation
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user