aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-07-23 04:14:59 +0900
committerRyuta Kamizono <kamipo@gmail.com>2018-07-23 04:19:37 +0900
commita4398e412c042b297039cc216f6536b0c048bd9c (patch)
treea7eab8ca30ba593d86480b058331a5279b111364 /activemodel/lib/active_model
parent97321956e6c2d2bae1bf6f76fce5bfb909ab58b0 (diff)
parentdfb0e4b3dc94860e6d484385d31fd399d33dac92 (diff)
downloadrails-a4398e412c042b297039cc216f6536b0c048bd9c.tar.gz
rails-a4398e412c042b297039cc216f6536b0c048bd9c.tar.bz2
rails-a4398e412c042b297039cc216f6536b0c048bd9c.zip
Merge pull request #30919 from seanlinsley/17622-before_save_strict_arguments
Add strict argument checking to ActiveRecord callbacks
Diffstat (limited to 'activemodel/lib/active_model')
-rw-r--r--activemodel/lib/active_model/callbacks.rb16
1 files changed, 9 insertions, 7 deletions
diff --git a/activemodel/lib/active_model/callbacks.rb b/activemodel/lib/active_model/callbacks.rb
index 8fa9680cb1..fde3381df2 100644
--- a/activemodel/lib/active_model/callbacks.rb
+++ b/activemodel/lib/active_model/callbacks.rb
@@ -127,26 +127,28 @@ module ActiveModel
private
def _define_before_model_callback(klass, callback)
- klass.define_singleton_method("before_#{callback}") do |*args, &block|
- set_callback(:"#{callback}", :before, *args, &block)
+ klass.define_singleton_method("before_#{callback}") do |*args, **options, &block|
+ options.assert_valid_keys(:if, :unless, :prepend)
+ set_callback(:"#{callback}", :before, *args, options, &block)
end
end
def _define_around_model_callback(klass, callback)
- klass.define_singleton_method("around_#{callback}") do |*args, &block|
- set_callback(:"#{callback}", :around, *args, &block)
+ klass.define_singleton_method("around_#{callback}") do |*args, **options, &block|
+ options.assert_valid_keys(:if, :unless, :prepend)
+ set_callback(:"#{callback}", :around, *args, options, &block)
end
end
def _define_after_model_callback(klass, callback)
- klass.define_singleton_method("after_#{callback}") do |*args, &block|
- options = args.extract_options!
+ klass.define_singleton_method("after_#{callback}") do |*args, **options, &block|
+ options.assert_valid_keys(:if, :unless, :prepend)
options[:prepend] = true
conditional = ActiveSupport::Callbacks::Conditionals::Value.new { |v|
v != false
}
options[:if] = Array(options[:if]) << conditional
- set_callback(:"#{callback}", :after, *(args << options), &block)
+ set_callback(:"#{callback}", :after, *args, options, &block)
end
end
end