Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

remove some evals from callback conditionals

  • Loading branch information...
commit a63a964a5d1ed02cf0df1b1a33a96ed2a9fa987b 1 parent ebf2113
@tenderlove tenderlove authored
View
5 activemodel/lib/active_model/callbacks.rb
@@ -135,7 +135,10 @@ def _define_after_model_callback(klass, callback) #:nodoc:
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
View
4 activemodel/lib/active_model/validations.rb
@@ -142,7 +142,9 @@ def validate(*args, &block)
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)
View
10 activesupport/lib/active_support/callbacks.rb
@@ -94,6 +94,15 @@ def run_callbacks(kind, &block)
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 @@ def make_lambda(filter)
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|
Please sign in to comment.
Something went wrong with that request. Please try again.