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

Print out a meaningful error when ActiveRecord::ReadOnlyRecord is raised

Currently, there is no messages which get printed out. Convoluted system
may have hooks that create other objects in which case we only fail with
no messages. This commit changes this information allowing you to know
which object is the one that actually raised the error.
This commit is contained in:
Franky W 2014-11-06 09:07:29 -08:00
parent 9a517b5a0b
commit d424ded6fd
2 changed files with 8 additions and 5 deletions

View file

@ -188,7 +188,7 @@ module ActiveRecord
# and <tt>destroy</tt> returns +false+. See # and <tt>destroy</tt> returns +false+. See
# ActiveRecord::Callbacks for further details. # ActiveRecord::Callbacks for further details.
def destroy def destroy
raise ReadOnlyRecord if readonly? raise ReadOnlyRecord, "#{self.class} is marked as readonly" if readonly?
destroy_associations destroy_associations
destroy_row if persisted? destroy_row if persisted?
@destroyed = true @destroyed = true
@ -519,7 +519,7 @@ module ActiveRecord
end end
def create_or_update def create_or_update
raise ReadOnlyRecord if readonly? raise ReadOnlyRecord, "#{self.class} is marked as readonly" if readonly?
result = new_record? ? _create_record : _update_record result = new_record? ? _create_record : _update_record
result != false result != false
end end

View file

@ -22,9 +22,12 @@ class ReadOnlyTest < ActiveRecord::TestCase
assert !dev.save assert !dev.save
dev.name = 'Forbidden.' dev.name = 'Forbidden.'
end end
assert_raise(ActiveRecord::ReadOnlyRecord) { dev.save } e = assert_raise(ActiveRecord::ReadOnlyRecord) { dev.save }
assert_raise(ActiveRecord::ReadOnlyRecord) { dev.save! } assert_equal "Developer is marked as readonly", e.message
assert_raise(ActiveRecord::ReadOnlyRecord) { dev.destroy } e = assert_raise(ActiveRecord::ReadOnlyRecord) { dev.save! }
assert_equal "Developer is marked as readonly", e.message
e = assert_raise(ActiveRecord::ReadOnlyRecord) { dev.destroy }
assert_equal "Developer is marked as readonly", e.message
end end