Class: ActiveModel::Validations::OwnershipValidator
- Inherits:
-
EachValidator
- Object
- EachValidator
- ActiveModel::Validations::OwnershipValidator
- Defined in:
- lib/validators/validates_ownership_of.rb
Constant Summary collapse
- WITH_OPTIONS =
%w[String Symbol].freeze
Instance Method Summary collapse
Instance Method Details
#check_validity! ⇒ Object
22 23 24 25 |
# File 'lib/validators/validates_ownership_of.rb', line 22 def check_validity! raise ArgumentError, ":with is required" unless .key?(:with) raise ArgumentError, ":with option must be a string or a symbol" unless WITH_OPTIONS.include?([:with].class.name) end |
#validate_each(record, attribute, value) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/validators/validates_ownership_of.rb', line 8 def validate_each(record, attribute, value) owner = record.send([:with]) actual_owner = value ? value.send([:with]) : nil return unless value return if owner == actual_owner record.errors.add( attribute, :invalid_owner, message: [:message] ) end |