Class: ContractValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/tram/validators/contract_validator.rb

Overview

Validates attribute by calling given type with the attribute’s value

Examples:

Checks that AdminPolicy.new(user).valid?

validates :user, contract: { policy: AdminPolicy, nested_keys: true }

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/tram/validators/contract_validator.rb', line 7

def validate_each(record, attribute, value)
  policy = options[:policy]
  return unless policy

  source = policy.new(value)
  return if source.valid?

  key = "contract_#{policy.name.underscore}"
  Tram::Validators.copy_errors(source, record, attribute, key, value, options)
end