From 4a19b3dea650351aa20d0cad64bf2d5608023a33 Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Fri, 12 Dec 2014 14:47:38 -0800 Subject: [PATCH] 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 --- activemodel/lib/active_model/validations.rb | 2 +- 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 @@ def validates_each(*attr_names, &block) 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 @@ def test_invalid_options_to_validate 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 -- GitLab