From 473202df83690d1b6ab08e6166a483109f8c769f Mon Sep 17 00:00:00 2001
From: Jeremy Kemper <jeremy@bitsweat.net>
Date: Fri, 16 Nov 2007 20:31:24 +0000
Subject: 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
---
 activerecord/CHANGELOG                               |  2 ++
 activerecord/lib/active_record/locking/optimistic.rb |  2 +-
 activerecord/test/locking_test.rb                    | 16 ++++++++++++++++
 3 files changed, 19 insertions(+), 1 deletion(-)

(limited to 'activerecord')

diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index e9c6884c7d..7a9b1e11af 100644
--- a/activerecord/CHANGELOG
+++ b/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]
diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb
index d8f9dee372..ecd6a2b258 100644
--- a/activerecord/lib/active_record/locking/optimistic.rb
+++ b/activerecord/lib/active_record/locking/optimistic.rb
@@ -76,7 +76,7 @@ module ActiveRecord
           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
diff --git a/activerecord/test/locking_test.rb b/activerecord/test/locking_test.rb
index 5b23aab515..b0d6f8417f 100644
--- a/activerecord/test/locking_test.rb
+++ b/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 @@ class OptimisticLockingTest < Test::Unit::TestCase
     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|
-- 
cgit v1.2.3