Class: SystemSettings::ListOfStringsValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/models/system_settings/list_of_strings_validator.rb

Constant Summary collapse

NON_WHITESPACE_REGEXP =
/[^[:space:]]/.freeze

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attr_name, value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'app/models/system_settings/list_of_strings_validator.rb', line 7

def validate_each(record, attr_name, value)
  came_from_user = :"#{attr_name}_came_from_user?"

  raw_value = record.read_attribute_before_type_cast(attr_name) if record.respond_to?(came_from_user) && record.public_send(came_from_user)
  raw_value ||= value

  raw_value = value if record_attribute_changed_in_place?(record, attr_name)

  record.errors.add(attr_name, :not_a_list_of_strings) unless matches_list_of_strings_regexp?(raw_value)
end