mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add AR::Base#validate! method
Acts same as valid? but raises AR::RecordInvalid exception if validation fails
This commit is contained in:
parent
8a6b8c8a79
commit
0fa4c95d91
2 changed files with 31 additions and 1 deletions
|
@ -54,7 +54,7 @@ module ActiveRecord
|
||||||
# Attempts to save the record just like Base#save but will raise a +RecordInvalid+
|
# Attempts to save the record just like Base#save but will raise a +RecordInvalid+
|
||||||
# exception instead of returning +false+ if the record is not valid.
|
# exception instead of returning +false+ if the record is not valid.
|
||||||
def save!(options={})
|
def save!(options={})
|
||||||
perform_validations(options) ? super : raise(RecordInvalid.new(self))
|
perform_validations(options) ? super : raise_record_invalid
|
||||||
end
|
end
|
||||||
|
|
||||||
# Runs all the validations within the specified context. Returns +true+ if
|
# Runs all the validations within the specified context. Returns +true+ if
|
||||||
|
@ -75,8 +75,24 @@ module ActiveRecord
|
||||||
|
|
||||||
alias_method :validate, :valid?
|
alias_method :validate, :valid?
|
||||||
|
|
||||||
|
# Runs all the validations within the specified context. Returns +true+ if
|
||||||
|
# no errors are found, raises +RecordInvalid+ otherwise.
|
||||||
|
#
|
||||||
|
# If the argument is +false+ (default is +nil+), the context is set to <tt>:create</tt> if
|
||||||
|
# <tt>new_record?</tt> is +true+, and to <tt>:update</tt> if it is not.
|
||||||
|
#
|
||||||
|
# Validations with no <tt>:on</tt> option will run no matter the context. Validations with
|
||||||
|
# some <tt>:on</tt> option will only run in the specified context.
|
||||||
|
def validate!(context = nil)
|
||||||
|
valid?(context) || raise_record_invalid
|
||||||
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
|
def raise_record_invalid
|
||||||
|
raise(RecordInvalid.new(self))
|
||||||
|
end
|
||||||
|
|
||||||
def perform_validations(options={}) # :nodoc:
|
def perform_validations(options={}) # :nodoc:
|
||||||
options[:validate] == false || valid?(options[:context])
|
options[:validate] == false || valid?(options[:context])
|
||||||
end
|
end
|
||||||
|
|
|
@ -78,6 +78,20 @@ class ValidationsTest < ActiveRecord::TestCase
|
||||||
assert_equal r, invalid.record
|
assert_equal r, invalid.record
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_validate_with_bang
|
||||||
|
assert_raise(ActiveRecord::RecordInvalid) do
|
||||||
|
WrongReply.new.validate!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_validate_with_bang_and_context
|
||||||
|
assert_raise(ActiveRecord::RecordInvalid) do
|
||||||
|
WrongReply.new.validate!(:special_case)
|
||||||
|
end
|
||||||
|
r = WrongReply.new(:title => "Valid title", :author_name => "secret", :content => "Good")
|
||||||
|
assert r.validate!(:special_case)
|
||||||
|
end
|
||||||
|
|
||||||
def test_exception_on_create_bang_many
|
def test_exception_on_create_bang_many
|
||||||
assert_raise(ActiveRecord::RecordInvalid) do
|
assert_raise(ActiveRecord::RecordInvalid) do
|
||||||
WrongReply.create!([ { "title" => "OK" }, { "title" => "Wrong Create" }])
|
WrongReply.create!([ { "title" => "OK" }, { "title" => "Wrong Create" }])
|
||||||
|
|
Loading…
Reference in a new issue