Class: Intermodal::Validators::AccountValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/intermodal/validators/account_validator.rb

Overview

Validates the associated resource is owned by the same account Examples:

validates parent_id, account: true validates parent_id, account: { through: association }

Instance Method Summary collapse

Instance Method Details

#messageObject



19
20
21
# File 'lib/intermodal/validators/account_validator.rb', line 19

def message
  options[:message] || 'must belong to the same account'
end

#validate_each(record, attribute, value) ⇒ Object



12
13
14
15
16
17
# File 'lib/intermodal/validators/account_validator.rb', line 12

def validate_each(record, attribute, value)
  return if value.nil?
  association_name = options[:through] || attribute.to_s.gsub(/_id/, '')
  return if record.send(association_name). == record.
  record.errors[attribute] << message
end