aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-06-27 14:49:43 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-06-27 14:53:59 -0300
commita87573d9e6610c72a93d4cc778e7703ca6d964e2 (patch)
treed69cdb78dee44aee415ce7c1cf88e6cb10bd56a5 /activerecord
parent35a9f7a6353ea12dd3103366e84ccc90123c3583 (diff)
parent0fa4c95d914d9f04553642f5181ee7c2cf4c6add (diff)
downloadrails-a87573d9e6610c72a93d4cc778e7703ca6d964e2.tar.gz
rails-a87573d9e6610c72a93d4cc778e7703ca6d964e2.tar.bz2
rails-a87573d9e6610c72a93d4cc778e7703ca6d964e2.zip
Merge pull request #8639 from bogdan/valid-with-bang
Add AR::Base#valid! method
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md5
-rw-r--r--activerecord/lib/active_record/validations.rb18
-rw-r--r--activerecord/test/cases/validations_test.rb14
3 files changed, 36 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index ccd7629373..8eabeee304 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,8 @@
+* Add `ActiveRecord::Base#validate!` that raises `RecordInvalid` if the record
+ is invalid.
+
+ *Bogdan Gusiev*, *Marc Schütz*
+
* Support for adding and removing foreign keys. Foreign keys are now
a part of `schema.rb`. This is supported by Mysql2Adapter, MysqlAdapter
and PostgreSQLAdapter.
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index 9999624fcf..b4b33804de 100644
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -54,7 +54,7 @@ module ActiveRecord
# 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.
def save!(options={})
- perform_validations(options) ? super : raise(RecordInvalid.new(self))
+ perform_validations(options) ? super : raise_record_invalid
end
# Runs all the validations within the specified context. Returns +true+ if
@@ -75,8 +75,24 @@ module ActiveRecord
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
+ def raise_record_invalid
+ raise(RecordInvalid.new(self))
+ end
+
def perform_validations(options={}) # :nodoc:
options[:validate] == false || valid?(options[:context])
end
diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb
index a6e1dc72e5..55804f9576 100644
--- a/activerecord/test/cases/validations_test.rb
+++ b/activerecord/test/cases/validations_test.rb
@@ -78,6 +78,20 @@ class ValidationsTest < ActiveRecord::TestCase
assert_equal r, invalid.record
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
assert_raise(ActiveRecord::RecordInvalid) do
WrongReply.create!([ { "title" => "OK" }, { "title" => "Wrong Create" }])