diff options
author | Godfrey Chan <godfreykfc@gmail.com> | 2014-12-12 14:47:38 -0800 |
---|---|---|
committer | Godfrey Chan <godfreykfc@gmail.com> | 2014-12-12 14:51:02 -0800 |
commit | 4a19b3dea650351aa20d0cad64bf2d5608023a33 (patch) | |
tree | fcc14500b033b84245090674e8fd67da1227e037 /activemodel | |
parent | 83534e5678a0f648e0cba767e3279714b3f774ce (diff) | |
download | rails-4a19b3dea650351aa20d0cad64bf2d5608023a33.tar.gz rails-4a19b3dea650351aa20d0cad64bf2d5608023a33.tar.bz2 rails-4a19b3dea650351aa20d0cad64bf2d5608023a33.zip |
Pass through the `prepend` option to `AS::Callback`
I'm not sure what's the use case for this, but apparently it broke some apps.
Since it was not the intended result from #16210 I fixed it to not raise an
exception anymore. However, I didn't add documentation for it because I don't
know if this should be officially supported without knowing how it's meant to
be used.
In general, validations should be side-effect-free (other than adding to the
error message to `@errors`). Order-dependent validations seems like a bad idea.
Fixes #18002
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/lib/active_model/validations.rb | 2 | ||||
-rw-r--r-- | activemodel/test/cases/validations_test.rb | 38 |
2 files changed, 38 insertions, 2 deletions
diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index c1e344b215..6a2668b8f7 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -87,7 +87,7 @@ module ActiveModel validates_with BlockValidator, _merge_attributes(attr_names), &block end - VALID_OPTIONS_FOR_VALIDATE = [:on, :if, :unless].freeze + VALID_OPTIONS_FOR_VALIDATE = [:on, :if, :unless, :prepend].freeze # Adds a validation method or block to the class. This is useful when # overriding the +validate+ instance method becomes too unwieldy and diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb index 98e0266d7e..cef66f3c0d 100644 --- a/activemodel/test/cases/validations_test.rb +++ b/activemodel/test/cases/validations_test.rb @@ -172,7 +172,43 @@ class ValidationsTest < ActiveModel::TestCase Topic.validate :title, presence: true end message = 'Unknown key: :presence. Valid keys are: :on, :if, :unless. Perhaps you meant to call `validates` instead of `validate`?' - assert_equal message, error.message + assert_includes error.message, "Unknown key: :presence" + assert_includes error.message, "Perhaps you meant to call `validates` instead of `validate`?" + end + + def test_callback_options_to_validate + klass = Class.new(Topic) do + attr_reader :call_sequence + + def initialize(*) + super + @call_sequence = [] + end + + private + def validator_a + @call_sequence << :a + end + + def validator_b + @call_sequence << :b + end + + def validator_c + @call_sequence << :c + end + end + + assert_nothing_raised do + klass.validate :validator_a, if: ->{ true } + klass.validate :validator_b, prepend: true + klass.validate :validator_c, unless: ->{ true } + end + + t = klass.new + + assert_predicate t, :valid? + assert_equal [:b, :a], t.call_sequence end def test_errors_conversions |