aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSean Linsley <code@seanlinsley.com>2017-10-18 10:29:27 -0500
committerSean Linsley <code@seanlinsley.com>2018-07-22 12:51:47 -0500
commitdfb0e4b3dc94860e6d484385d31fd399d33dac92 (patch)
treef22a5dbdd3a1af63920001749a038629167148d0
parent97321956e6c2d2bae1bf6f76fce5bfb909ab58b0 (diff)
downloadrails-dfb0e4b3dc94860e6d484385d31fd399d33dac92.tar.gz
rails-dfb0e4b3dc94860e6d484385d31fd399d33dac92.tar.bz2
rails-dfb0e4b3dc94860e6d484385d31fd399d33dac92.zip
add strict argument checking to ActiveRecord callbacks
This ends up adding it to all save-related callbacks defined in `ActiveRecord::DefineCallbacks`, including e.g. `after_create`. Which should be fine: they didn't support `:on` in the first place.
-rw-r--r--activemodel/lib/active_model/callbacks.rb16
-rw-r--r--activerecord/test/cases/callbacks_test.rb23
2 files changed, 32 insertions, 7 deletions
diff --git a/activemodel/lib/active_model/callbacks.rb b/activemodel/lib/active_model/callbacks.rb
index 8fa9680cb1..d612743c44 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
diff --git a/activerecord/test/cases/callbacks_test.rb b/activerecord/test/cases/callbacks_test.rb
index b9ba51c730..e10a7b34c8 100644
--- a/activerecord/test/cases/callbacks_test.rb
+++ b/activerecord/test/cases/callbacks_test.rb
@@ -476,4 +476,27 @@ class CallbacksTest < ActiveRecord::TestCase
child.save
assert child.after_save_called
end
+
+ def test_on_isnt_allowed
+ exception = assert_raises ArgumentError do
+ Class.new(ActiveRecord::Base) do
+ before_save(on: :create) {}
+ end
+ end
+ assert_equal "Unknown key: :on. Valid keys are: :if, :unless, :prepend", exception.message
+
+ exception = assert_raises ArgumentError do
+ Class.new(ActiveRecord::Base) do
+ around_save(on: :create) {}
+ end
+ end
+ assert_equal "Unknown key: :on. Valid keys are: :if, :unless, :prepend", exception.message
+
+ exception = assert_raises ArgumentError do
+ Class.new(ActiveRecord::Base) do
+ after_save(on: :create) {}
+ end
+ end
+ assert_equal "Unknown key: :on. Valid keys are: :if, :unless, :prepend", exception.message
+ end
end