Class: ValidatesAssociatedWithContext::AssociatedWithContextValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- ValidatesAssociatedWithContext::AssociatedWithContextValidator
- Defined in:
- lib/validates_associated_with_context/associated_with_context.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#bubble_messages ⇒ Object
readonly
Returns the value of attribute bubble_messages.
-
#inherit_context ⇒ Object
readonly
Returns the value of attribute inherit_context.
Instance Method Summary collapse
- #context(record) ⇒ Object
-
#initialize(options) ⇒ AssociatedWithContextValidator
constructor
A new instance of AssociatedWithContextValidator.
- #validate_each(record, attribute, value) ⇒ Object
Constructor Details
#initialize(options) ⇒ AssociatedWithContextValidator
Returns a new instance of AssociatedWithContextValidator.
7 8 9 10 11 12 13 |
# File 'lib/validates_associated_with_context/associated_with_context.rb', line 7 def initialize() @context = .delete(:context) @inherit_context = .delete(:inherit_context) @bubble_messages = .delete(:bubble_messages) super end |
Instance Attribute Details
#bubble_messages ⇒ Object (readonly)
Returns the value of attribute bubble_messages.
5 6 7 |
# File 'lib/validates_associated_with_context/associated_with_context.rb', line 5 def @bubble_messages end |
#inherit_context ⇒ Object (readonly)
Returns the value of attribute inherit_context.
5 6 7 |
# File 'lib/validates_associated_with_context/associated_with_context.rb', line 5 def inherit_context @inherit_context end |
Instance Method Details
#context(record) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/validates_associated_with_context/associated_with_context.rb', line 15 def context(record) return record.validation_context if inherit_context if @context.respond_to?(:call) @context.call record else @context end end |
#validate_each(record, attribute, value) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/validates_associated_with_context/associated_with_context.rb', line 25 def validate_each(record, attribute, value) validation_context = context(record) if Array(value).each do |r| next if valid_object?(r, validation_context) if r.errors.any? r.errors.each do |error| record.errors.add(attribute, error., **.merge(value: r)) end else record.errors.add(attribute, :invalid, **.merge(value: r)) end end else if Array(value).reject { |r| valid_object?(r, validation_context) }.any? record.errors.add(attribute, :invalid, **.merge(value: value)) end end end |