mirror of
https://github.com/opf/openproject.git
synced 2026-06-14 03:30:14 +00:00
DRY up Color model validations
This commit is contained in:
+2
-3
@@ -38,9 +38,8 @@ class Color < ApplicationRecord
|
||||
dependent: :nullify
|
||||
|
||||
validates :name, :hexcode, presence: true
|
||||
|
||||
validates :name, length: { maximum: 255, unless: lambda { |e| e.name.blank? } }
|
||||
validates :hexcode, format: { with: /\A#[0-9A-F]{6}\z/, unless: lambda { |e| e.hexcode.blank? } }
|
||||
validates :name, length: { maximum: 255 }
|
||||
validates :hexcode, format: { with: RGB_HEX_FORMAT, allow_blank: true }
|
||||
|
||||
normalizes :hexcode, with: ::Colors::HexColor::Normalizer
|
||||
end
|
||||
|
||||
@@ -30,6 +30,9 @@
|
||||
|
||||
module Colors
|
||||
module HexColor
|
||||
RGB_HEX_FORMAT = /\A#[0-9A-F]{6}\z/
|
||||
private_constant :RGB_HEX_FORMAT
|
||||
|
||||
##
|
||||
# Get the fill style for this color.
|
||||
# If the color is light, use a dark font.
|
||||
|
||||
@@ -43,9 +43,9 @@ class DesignColor < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
validates :variable, :hexcode, presence: true
|
||||
validates :variable, uniqueness: true
|
||||
validates :hexcode, :variable, presence: true
|
||||
validates :hexcode, format: { with: /\A#[0-9A-F]{6}\z/, unless: lambda { |e| e.hexcode.blank? } }
|
||||
validates :hexcode, format: { with: RGB_HEX_FORMAT, allow_blank: true }
|
||||
|
||||
normalizes :hexcode, with: ::Colors::HexColor::Normalizer
|
||||
|
||||
|
||||
Reference in New Issue
Block a user