Class: StateValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/auxilium/validators/state_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/auxilium/validators/state_validator.rb', line 4

def validate_each(record, attribute, value)
  return if value.nil? && options[:allow_nil]

  correct_states = options[:only] || []
  wrong_states = options[:except] || []
  state_method = options.fetch(:state, :state)
  current_state = attribute == state_method ? value : value&.send(state_method)
  return if current_state.nil? && options[:allow_nil]

  if (wrong_states.present? && wrong_states.include?(current_state)) || (correct_states.present? && correct_states.exclude?(current_state))
    record.errors.add(attribute, I18n.t(attribute == state_method ? 'errors.messages.not_an_allowed_state' : 'errors.messages.not_in_an_allowed_state'))
  end
end