1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #16210 from sonnym/assert_valid_keys_in_validate

Check for valid options in validate method
This commit is contained in:
Matthew Draper 2014-07-18 07:07:03 +09:30
commit e213b37fc1
3 changed files with 19 additions and 0 deletions

View file

@ -1,3 +1,9 @@
* Validate options passed to `ActiveModel::Validations.validate`.
Preventing, in many cases, the simple mistake of using `validate` instead of `validates`.
*Sonny Michaud*
* Deprecate `reset_#{attribute}` in favor of `restore_#{attribute}`.
These methods may cause confusion with the `reset_changes` that behaves differently

View file

@ -141,6 +141,11 @@ module ActiveModel
# value.
def validate(*args, &block)
options = args.extract_options!
if args.all? { |arg| arg.is_a?(Symbol) }
options.assert_valid_keys(%i(on if unless))
end
if options.key?(:on)
options = options.dup
options[:if] = Array(options[:if])
@ -148,6 +153,7 @@ module ActiveModel
Array(options[:on]).include?(o.validation_context)
}
end
args << options
set_callback(:validate, *args, &block)
end

View file

@ -167,6 +167,13 @@ class ValidationsTest < ActiveModel::TestCase
end
end
def test_invalid_options_to_validate
assert_raises(ArgumentError) do
# A common mistake -- we meant to call 'validates'
Topic.validate :title, presence: true
end
end
def test_errors_conversions
Topic.validates_presence_of %w(title content)
t = Topic.new