Add phases relation at Budget model, as well as current_phase helper method

This commit is contained in:
Bertocq
2018-01-15 20:30:45 +01:00
parent ca3d759d9f
commit 10f5cc0d3b

View File

@@ -14,6 +14,7 @@ class Budget < ActiveRecord::Base
has_many :ballots, dependent: :destroy has_many :ballots, dependent: :destroy
has_many :groups, dependent: :destroy has_many :groups, dependent: :destroy
has_many :headings, through: :groups has_many :headings, through: :groups
has_many :phases, class_name: Budget::Phase
before_validation :sanitize_descriptions before_validation :sanitize_descriptions
@@ -26,16 +27,23 @@ class Budget < ActiveRecord::Base
scope :balloting, -> { where(phase: "balloting") } scope :balloting, -> { where(phase: "balloting") }
scope :reviewing_ballots, -> { where(phase: "reviewing_ballots") } scope :reviewing_ballots, -> { where(phase: "reviewing_ballots") }
scope :finished, -> { where(phase: "finished") } scope :finished, -> { where(phase: "finished") }
scope :current, -> { where.not(phase: "finished") } scope :current, -> { where.not(phase: "finished") }
def current_phase
phases.send(phase)
end
def description def description
description_for_phase(phase) description_for_phase(phase)
end end
def description_for_phase(phase) def description_for_phase(phase)
if phases.exists? && phases.send(phase).description.present?
phases.send(phase).description
else
send("description_#{phase}").try(:html_safe) send("description_#{phase}").try(:html_safe)
end end
end
def self.title_max_length def self.title_max_length
80 80