mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
remove some evals from callback conditionals
This commit is contained in:
parent
ebf2113a05
commit
a63a964a5d
3 changed files with 17 additions and 2 deletions
|
@ -135,7 +135,10 @@ module ActiveModel
|
|||
klass.define_singleton_method("after_#{callback}") do |*args, &block|
|
||||
options = args.extract_options!
|
||||
options[:prepend] = true
|
||||
options[:if] = Array(options[:if]) << "value != false"
|
||||
conditional = ActiveSupport::Callbacks::Conditionals::Value.new { |v|
|
||||
v != false
|
||||
}
|
||||
options[:if] = Array(options[:if]) << conditional
|
||||
set_callback(:"#{callback}", :after, *(args << options), &block)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -142,7 +142,9 @@ module ActiveModel
|
|||
if options.key?(:on)
|
||||
options = options.dup
|
||||
options[:if] = Array(options[:if])
|
||||
options[:if].unshift("validation_context == :#{options[:on]}")
|
||||
options[:if].unshift lambda { |o|
|
||||
o.validation_context == options[:on]
|
||||
}
|
||||
end
|
||||
args << options
|
||||
set_callback(:validate, *args, &block)
|
||||
|
|
|
@ -94,6 +94,15 @@ module ActiveSupport
|
|||
def halted_callback_hook(filter)
|
||||
end
|
||||
|
||||
module Conditionals # :nodoc:
|
||||
class Value
|
||||
def initialize(&block)
|
||||
@block = block
|
||||
end
|
||||
def call(target, value); @block.call(value); end
|
||||
end
|
||||
end
|
||||
|
||||
module Filters
|
||||
Environment = Struct.new(:target, :halted, :value, :run_block)
|
||||
|
||||
|
@ -415,6 +424,7 @@ module ActiveSupport
|
|||
when String
|
||||
l = eval "lambda { |value| #{filter} }"
|
||||
lambda { |target, value| target.instance_exec(value, &l) }
|
||||
when Conditionals::Value then filter
|
||||
when ::Proc
|
||||
if filter.arity > 1
|
||||
return lambda { |target, _, &block|
|
||||
|
|
Loading…
Reference in a new issue