From 026d10ca88a1c68d172ab760354c2ef88e69ae5f Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Sun, 18 Jul 2010 07:54:48 -0400 Subject: [PATCH] update_attribute should not update readonly attributes [#5106 state:resolved] --- activerecord/lib/active_record/persistence.rb | 2 ++ activerecord/test/cases/persistence_test.rb | 8 +++++++- activerecord/test/fixtures/minivans.yml | 1 + activerecord/test/models/minivan.rb | 5 ++++- activerecord/test/schema/schema.rb | 1 + 5 files changed, 15 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 7ec443c..e2d92c8 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -105,6 +105,8 @@ module ActiveRecord # Updates a single attribute and saves the record without going through the normal validation procedure # or callbacks. This is especially useful for boolean flags on existing records. def update_attribute(name, value) + raise ActiveRecordError, "#{name.to_s} is marked as readonly" if self.class.readonly_attributes.include? name.to_s + changes = record_update_timestamps || {} if name diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb index 4ea5df0..54fe991 100644 --- a/activerecord/test/cases/persistence_test.rb +++ b/activerecord/test/cases/persistence_test.rb @@ -17,13 +17,14 @@ require 'models/comment' require 'models/minimalistic' require 'models/warehouse_thing' require 'models/parrot' +require 'models/minivan' require 'models/loose_person' require 'rexml/document' require 'active_support/core_ext/exception' class PersistencesTest < ActiveRecord::TestCase - fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things', :authors, :categorizations, :categories, :posts + fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things', :authors, :categorizations, :categories, :posts, :minivans def test_create topic = Topic.new @@ -220,6 +221,11 @@ class PersistencesTest < ActiveRecord::TestCase assert !Topic.find(1).approved? end + def test_update_attribute_for_readonly_attribute + minivan = Minivan.find('m1') + assert_raises(ActiveRecord::ActiveRecordError) { minivan.update_attribute(:color, 'black') } + end + def test_update_attribute_with_one_changed_and_one_updated t = Topic.order('id').limit(1).first title, author_name = t.title, t.author_name diff --git a/activerecord/test/fixtures/minivans.yml b/activerecord/test/fixtures/minivans.yml index e7a2ab7..f1224a4 100644 --- a/activerecord/test/fixtures/minivans.yml +++ b/activerecord/test/fixtures/minivans.yml @@ -2,3 +2,4 @@ cool_first: minivan_id: m1 name: my_minivan speedometer_id: s1 + color: blue diff --git a/activerecord/test/models/minivan.rb b/activerecord/test/models/minivan.rb index c753319..602438d 100644 --- a/activerecord/test/models/minivan.rb +++ b/activerecord/test/models/minivan.rb @@ -3,4 +3,7 @@ class Minivan < ActiveRecord::Base belongs_to :speedometer has_one :dashboard, :through => :speedometer -end \ No newline at end of file + + attr_readonly :color + +end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 641726b..702d54a 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -300,6 +300,7 @@ ActiveRecord::Schema.define do t.string :minivan_id t.string :name t.string :speedometer_id + t.string :color end create_table :minimalistics, :force => true do |t| -- 1.6.5.2