From 10f5cc0d3bb205abd640e838a6261872f59352d0 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Mon, 15 Jan 2018 20:30:45 +0100 Subject: [PATCH] Add phases relation at Budget model, as well as current_phase helper method --- app/models/budget.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/models/budget.rb b/app/models/budget.rb index bf64c0fd6..56b774a41 100644 --- a/app/models/budget.rb +++ b/app/models/budget.rb @@ -14,6 +14,7 @@ class Budget < ActiveRecord::Base has_many :ballots, dependent: :destroy has_many :groups, dependent: :destroy has_many :headings, through: :groups + has_many :phases, class_name: Budget::Phase before_validation :sanitize_descriptions @@ -26,15 +27,22 @@ class Budget < ActiveRecord::Base scope :balloting, -> { where(phase: "balloting") } scope :reviewing_ballots, -> { where(phase: "reviewing_ballots") } scope :finished, -> { where(phase: "finished") } - scope :current, -> { where.not(phase: "finished") } + def current_phase + phases.send(phase) + end + def description description_for_phase(phase) end def description_for_phase(phase) - send("description_#{phase}").try(:html_safe) + if phases.exists? && phases.send(phase).description.present? + phases.send(phase).description + else + send("description_#{phase}").try(:html_safe) + end end def self.title_max_length