Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
98bd208f87 | |||
823ff5efbd | |||
9fa5ebb212 |
@ -2,20 +2,45 @@
|
|||||||
|
|
||||||
module ValidatesAssociatedWithContext
|
module ValidatesAssociatedWithContext
|
||||||
class AssociatedWithContextValidator < ActiveModel::EachValidator # :nodoc:
|
class AssociatedWithContextValidator < ActiveModel::EachValidator # :nodoc:
|
||||||
attr_reader :context, :inherit_context
|
attr_reader :inherit_context, :bubble_messages
|
||||||
|
|
||||||
def initialize(options)
|
def initialize(options)
|
||||||
@context = options.delete(:context)
|
@context = options.delete(:context)
|
||||||
@inherit_context = options.delete(:inherit_context)
|
@inherit_context = options.delete(:inherit_context)
|
||||||
|
@bubble_messages = options.delete(:bubble_messages)
|
||||||
|
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_each(record, attribute, value)
|
def context(record)
|
||||||
validation_context = inherit_context ? record.validation_context : context
|
return record.validation_context if inherit_context
|
||||||
|
|
||||||
if Array(value).reject { |r| valid_object?(r, validation_context) }.any?
|
if @context.respond_to?(:call)
|
||||||
record.errors.add(attribute, :invalid, **options.merge(value: value))
|
@context.call record
|
||||||
|
else
|
||||||
|
@context
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate_each(record, attribute, value)
|
||||||
|
validation_context = context(record)
|
||||||
|
|
||||||
|
if bubble_messages
|
||||||
|
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.full_message, **options.merge(value: r))
|
||||||
|
end
|
||||||
|
else
|
||||||
|
record.errors.add(attribute, :invalid, **options.merge(value: r))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if Array(value).reject { |r| valid_object?(r, validation_context) }.any?
|
||||||
|
record.errors.add(attribute, :invalid, **options.merge(value: value))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module ValidatesAssociatedWithContext
|
module ValidatesAssociatedWithContext
|
||||||
VERSION = "0.1.1"
|
VERSION = "0.2.0"
|
||||||
end
|
end
|
||||||
|
@ -30,8 +30,8 @@ Gem::Specification.new do |spec|
|
|||||||
spec.require_paths = ["lib"]
|
spec.require_paths = ["lib"]
|
||||||
|
|
||||||
# Uncomment to register a new dependency of your gem
|
# Uncomment to register a new dependency of your gem
|
||||||
spec.add_dependency "activemodel", "~> 7.0"
|
spec.add_dependency "activemodel", ">= 7.0", "< 9.0"
|
||||||
spec.add_dependency "activerecord", "~> 7.0"
|
spec.add_dependency "activerecord", ">= 7.0", "< 9.0"
|
||||||
|
|
||||||
# For more information and examples about making a new gem, check out our
|
# For more information and examples about making a new gem, check out our
|
||||||
# guide at: https://bundler.io/guides/creating_gem.html
|
# guide at: https://bundler.io/guides/creating_gem.html
|
||||||
|
Loading…
x
Reference in New Issue
Block a user