Refactor hexadecimal color validation

This commit is contained in:
decabeza
2019-01-30 13:17:04 +01:00
parent 865dca85bf
commit 41f9ef167d

View File

@@ -22,6 +22,8 @@ class Legislation::Process < ActiveRecord::Base
PHASES_AND_PUBLICATIONS = %i[homepage_phase draft_phase debate_phase allegations_phase
proposals_phase draft_publication result_publication].freeze
CSS_HEX_COLOR = /\A#?(?:[A-F0-9]{3}){1,2}\z/i
has_many :draft_versions, -> { order(:id) }, class_name: 'Legislation::DraftVersion',
foreign_key: 'legislation_process_id',
dependent: :destroy
@@ -44,8 +46,8 @@ class Legislation::Process < ActiveRecord::Base
validates :allegations_end_date, presence: true, if: :allegations_start_date?
validates :proposals_phase_end_date, presence: true, if: :proposals_phase_start_date?
validate :valid_date_ranges
validates :background_color, format: { allow_blank: true, with: /\A#?(?:[A-F0-9]{3}){1,2}\z/i }
validates :font_color, format: { allow_blank: true, with: /\A#?(?:[A-F0-9]{3}){1,2}\z/i }
validates :background_color, format: { allow_blank: true, with: CSS_HEX_COLOR }
validates :font_color, format: { allow_blank: true, with: CSS_HEX_COLOR }
scope :open, -> { where("start_date <= ? and end_date >= ?", Date.current, Date.current)
.order('id DESC') }