Validate date ranges for legislation process

Only validates start and end dates for the whole process or a phase, not overlap between phases, as they are permitted, at least for now.
This commit is contained in:
Amaia Castro
2017-01-04 18:13:28 +01:00
parent b6b33ff85b
commit bc1905e5d9
4 changed files with 61 additions and 0 deletions

View File

@@ -18,6 +18,7 @@ class Legislation::Process < ActiveRecord::Base
validates :allegations_start_date, presence: true
validates :allegations_end_date, presence: true
validates :final_publication_date, presence: true
validate :valid_date_ranges
scope :open, -> { where("start_date <= ? and end_date >= ?", Date.current, Date.current).order('id DESC') }
scope :next, -> { where("start_date > ?", Date.current).order('id DESC') }
@@ -57,4 +58,13 @@ class Legislation::Process < ActiveRecord::Base
def total_comments
questions.map(&:comments_count).sum
end
private
def valid_date_ranges
errors.add(:end_date, :invalid_date_range) if end_date < start_date
errors.add(:debate_end_date, :invalid_date_range) if debate_end_date < debate_start_date
errors.add(:allegations_end_date, :invalid_date_range) if allegations_end_date < allegations_start_date
end
end