From 44bffec390befff2fc90f43333f0b16d5a725729 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Tue, 13 Jul 2010 08:35:52 -0400 Subject: [PATCH] update_attribute should not update readonly attributes [#5106 state:resolved] --- activerecord/lib/active_record/persistence.rb | 1 + activerecord/test/cases/base_test.rb | 8 +++++++- activerecord/test/fixtures/minivans.yml | 1 + activerecord/test/models/minivan.rb | 6 ++++-- activerecord/test/schema/schema.rb | 1 + 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index e53cc5e..ad22f1c 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -105,6 +105,7 @@ 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/base_test.rb b/activerecord/test/cases/base_test.rb index a4cf512..3a1b6e8 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -18,6 +18,7 @@ require 'models/minimalistic' require 'models/warehouse_thing' require 'models/parrot' require 'models/loose_person' +require 'models/minivan' require 'rexml/document' require 'active_support/core_ext/exception' @@ -45,7 +46,7 @@ end class Booleantest < ActiveRecord::Base; end class BasicsTest < 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_table_exists assert !NonExistentTable.table_exists? @@ -940,6 +941,11 @@ class BasicsTest < ActiveRecord::TestCase assert_not_equal updated_at, developer.updated_at 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_attributes topic = Topic.find(1) assert !topic.approved? diff --git a/activerecord/test/fixtures/minivans.yml b/activerecord/test/fixtures/minivans.yml index e7a2ab7..a3022e0 100644 --- a/activerecord/test/fixtures/minivans.yml +++ b/activerecord/test/fixtures/minivans.yml @@ -1,4 +1,5 @@ cool_first: minivan_id: m1 name: my_minivan + color: blue speedometer_id: s1 diff --git a/activerecord/test/models/minivan.rb b/activerecord/test/models/minivan.rb index c753319..5af6f82 100644 --- a/activerecord/test/models/minivan.rb +++ b/activerecord/test/models/minivan.rb @@ -1,6 +1,8 @@ class Minivan < ActiveRecord::Base set_primary_key :minivan_id - + + attr_readonly :color + belongs_to :speedometer has_one :dashboard, :through => :speedometer -end \ No newline at end of file +end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 641726b..c69541f 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -299,6 +299,7 @@ ActiveRecord::Schema.define do create_table :minivans, :force => true, :id => false do |t| t.string :minivan_id t.string :name + t.string :color t.string :speedometer_id end -- 1.6.5.2