class ActiveModel::Validations::OwnershipValidator

Constants

WITH_OPTIONS

Public Instance Methods

check_validity!() click to toggle source
# File lib/validators/validates_ownership_of.rb, line 22
def check_validity!
  raise ArgumentError, ":with is required" unless options.key?(:with)
  raise ArgumentError, ":with option must be a string or a symbol" unless WITH_OPTIONS.include?(options[:with].class.name)
end
validate_each(record, attribute, value) click to toggle source
# File lib/validators/validates_ownership_of.rb, line 8
def validate_each(record, attribute, value)
  owner = record.send(options[:with])
  actual_owner = value ? value.send(options[:with]) : nil

  return unless value
  return if owner == actual_owner

  record.errors.add(
    attribute,
    :invalid_owner,
    message: options[:message]
  )
end