Use comparison validation to validate dates
The `validates_comparison_of` method was added in Rails 7.0. We aren't changing the `date_range` validation in polls yet because it's a bit complex; we'll do it in the next commit.
This commit is contained in:
@@ -54,7 +54,35 @@ class Legislation::Process < ApplicationRecord
|
||||
validates :"#{phase_name}_end_date", presence: true, if: enabled_attribute
|
||||
end
|
||||
|
||||
validate :valid_date_ranges
|
||||
validates :end_date,
|
||||
comparison: {
|
||||
greater_than_or_equal_to: :start_date,
|
||||
message: :invalid_date_range
|
||||
},
|
||||
allow_blank: true,
|
||||
if: -> { start_date }
|
||||
validates :debate_end_date,
|
||||
comparison: {
|
||||
greater_than_or_equal_to: :debate_start_date,
|
||||
message: :invalid_date_range
|
||||
},
|
||||
allow_blank: true,
|
||||
if: -> { debate_start_date }
|
||||
validates :draft_end_date,
|
||||
comparison: {
|
||||
greater_than_or_equal_to: :draft_start_date,
|
||||
message: :invalid_date_range
|
||||
},
|
||||
allow_blank: true,
|
||||
if: -> { draft_start_date }
|
||||
validates :allegations_end_date,
|
||||
comparison: {
|
||||
greater_than_or_equal_to: :allegations_start_date,
|
||||
message: :invalid_date_range
|
||||
},
|
||||
allow_blank: true,
|
||||
if: -> { allegations_start_date }
|
||||
|
||||
validates :background_color, format: { allow_blank: true, with: ->(*) { CSS_HEX_COLOR }}
|
||||
validates :font_color, format: { allow_blank: true, with: ->(*) { CSS_HEX_COLOR }}
|
||||
|
||||
@@ -140,22 +168,4 @@ class Legislation::Process < ApplicationRecord
|
||||
def self.search(terms)
|
||||
pg_search(terms)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def valid_date_ranges
|
||||
if end_date && start_date && end_date < start_date
|
||||
errors.add(:end_date, :invalid_date_range)
|
||||
end
|
||||
if debate_end_date && debate_start_date && debate_end_date < debate_start_date
|
||||
errors.add(:debate_end_date, :invalid_date_range)
|
||||
end
|
||||
if draft_end_date && draft_start_date && draft_end_date < draft_start_date
|
||||
errors.add(:draft_end_date, :invalid_date_range)
|
||||
end
|
||||
if allegations_end_date && allegations_start_date &&
|
||||
allegations_end_date < allegations_start_date
|
||||
errors.add(:allegations_end_date, :invalid_date_range)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -34,9 +34,22 @@ class Poll < ApplicationRecord
|
||||
|
||||
validates_translation :name, presence: true
|
||||
validate :date_range
|
||||
validate :start_date_is_not_past_date, on: :create
|
||||
validates :starts_at,
|
||||
comparison: {
|
||||
greater_than_or_equal_to: ->(*) { Time.current },
|
||||
message: ->(*) { I18n.t("errors.messages.past_date") }
|
||||
},
|
||||
allow_blank: true,
|
||||
on: :create
|
||||
validates :ends_at,
|
||||
comparison: {
|
||||
greater_than_or_equal_to: ->(*) { Time.current },
|
||||
message: ->(*) { I18n.t("errors.messages.past_date") }
|
||||
},
|
||||
on: :update,
|
||||
if: -> { will_save_change_to_ends_at? }
|
||||
|
||||
validate :start_date_change, on: :update
|
||||
validate :end_date_is_not_past_date, on: :update
|
||||
validate :end_date_change, on: :update
|
||||
validate :only_one_active, unless: :public?
|
||||
|
||||
@@ -168,12 +181,6 @@ class Poll < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def start_date_is_not_past_date
|
||||
if starts_at.present? && starts_at < Time.current
|
||||
errors.add(:starts_at, I18n.t("errors.messages.past_date"))
|
||||
end
|
||||
end
|
||||
|
||||
def start_date_change
|
||||
if will_save_change_to_starts_at?
|
||||
if starts_at_in_database < Time.current
|
||||
@@ -184,12 +191,6 @@ class Poll < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def end_date_is_not_past_date
|
||||
if will_save_change_to_ends_at? && ends_at < Time.current
|
||||
errors.add(:ends_at, I18n.t("errors.messages.past_date"))
|
||||
end
|
||||
end
|
||||
|
||||
def end_date_change
|
||||
if will_save_change_to_ends_at? && ends_at_in_database < Time.current
|
||||
errors.add(:ends_at, I18n.t("errors.messages.cannot_change_date.poll_ended"))
|
||||
|
||||
Reference in New Issue
Block a user