Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

attr_readonly behaves well with optimistic locking. Closes #10188.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8156 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 473202df83690d1b6ab08e6166a483109f8c769f 1 parent 32602ac
@jeremy jeremy authored
View
2  activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* attr_readonly behaves well with optimistic locking. #10188 [Nick Bugajski]
+
* Base#to_xml supports the nil="true" attribute like Hash#to_xml. #8268 [Catfish]
* Change plings to the more conventional quotes in the documentation. Closes #10104 [danger]
View
2  activerecord/lib/active_record/locking/optimistic.rb
@@ -76,7 +76,7 @@ def update_with_lock #:nodoc:
begin
affected_rows = connection.update(<<-end_sql, "#{self.class.name} Update with optimistic locking")
UPDATE #{self.class.table_name}
- SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false))}
+ SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false, false))}
WHERE #{self.class.primary_key} = #{quote_value(id)}
AND #{self.class.quoted_locking_column} = #{quote_value(previous_value)}
end_sql
View
16 activerecord/test/locking_test.rb
@@ -10,6 +10,10 @@ class LockWithCustomColumnWithoutDefault < ActiveRecord::Base
set_locking_column :custom_lock_version
end
+class ReadonlyFirstNamePerson < Person
+ attr_readonly :first_name
+end
+
class OptimisticLockingTest < Test::Unit::TestCase
fixtures :people, :legacy_things
@@ -94,6 +98,18 @@ def test_lock_with_custom_column_without_default_sets_version_to_zero
assert_equal 0, t1.custom_lock_version
end
+ def test_readonly_attributes
+ assert_equal [ :first_name ], ReadonlyFirstNamePerson.readonly_attributes
+
+ p = ReadonlyFirstNamePerson.create(:first_name => "unchangeable name")
+ p.reload
+ assert_equal "unchangeable name", p.first_name
+
+ p.update_attributes(:first_name => "changed name")
+ p.reload
+ assert_equal "unchangeable name", p.first_name
+ end
+
{ :lock_version => Person, :custom_lock_version => LegacyThing }.each do |name, model|
define_method("test_increment_counter_updates_#{name}") do
counter_test model, 1 do |id|
Please sign in to comment.
Something went wrong with that request. Please try again.