diff options
-rw-r--r-- | activerecord/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/dirty.rb | 24 | ||||
-rw-r--r-- | activerecord/lib/active_record/timestamp.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/autosave_association_test.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/dirty_test.rb | 44 |
5 files changed, 53 insertions, 23 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index ed5d0eebdf..ffd19a5334 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -112,6 +112,10 @@ app processes (so long as the code in those processes doesn't contain any references to the removed column). + The `partial_updates` configuration option is now renamed to + `partial_writes` to reflect the fact that it now impacts both inserts + and updates. + *Jon Leighton* * Allow before and after validations to take an array of lifecycle events diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb index 3417e3af17..59f209cec8 100644 --- a/activerecord/lib/active_record/attribute_methods/dirty.rb +++ b/activerecord/lib/active_record/attribute_methods/dirty.rb @@ -1,9 +1,10 @@ require 'active_support/core_ext/module/attribute_accessors' +require 'active_support/deprecation' module ActiveRecord ActiveSupport.on_load(:active_record_config) do - mattr_accessor :partial_updates, instance_accessor: false - self.partial_updates = true + mattr_accessor :partial_writes, instance_accessor: false + self.partial_writes = true end module AttributeMethods @@ -17,7 +18,18 @@ module ActiveRecord raise "You cannot include Dirty after Timestamp" end - config_attribute :partial_updates + config_attribute :partial_writes + + def self.partial_updates=(v); self.partial_writes = v; end + def self.partial_updates?; partial_writes?; end + def self.partial_updates; partial_writes; end + + ActiveSupport::Deprecation.deprecate_methods( + singleton_class, + :partial_updates= => :partial_writes=, + :partial_updates? => :partial_writes?, + :partial_updates => :partial_writes + ) end # Attempts to +save+ the record and clears changed attributes if successful. @@ -64,16 +76,16 @@ module ActiveRecord end def update(*) - partial_updates? ? super(keys_for_partial_update) : super + partial_writes? ? super(keys_for_partial_write) : super end def create(*) - partial_updates? ? super(keys_for_partial_update) : super + partial_writes? ? super(keys_for_partial_write) : super end # Serialized attributes should always be written in case they've been # changed in place. - def keys_for_partial_update + def keys_for_partial_write changed | (attributes.keys & self.class.serialized_attributes.keys) end diff --git a/activerecord/lib/active_record/timestamp.rb b/activerecord/lib/active_record/timestamp.rb index dd08a6f4f5..ec4588f601 100644 --- a/activerecord/lib/active_record/timestamp.rb +++ b/activerecord/lib/active_record/timestamp.rb @@ -75,7 +75,7 @@ module ActiveRecord end def should_record_timestamps? - self.record_timestamps && (!partial_updates? || changed? || (attributes.keys & self.class.serialized_attributes.keys).present?) + self.record_timestamps && (!partial_writes? || changed? || (attributes.keys & self.class.serialized_attributes.keys).present?) end def timestamp_attributes_for_create_in_model diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index 4afd33f273..16ce150396 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -144,7 +144,7 @@ class TestDefaultAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCas firm = Firm.first firm.account = Account.first - assert_queries(Firm.partial_updates? ? 0 : 1) { firm.save! } + assert_queries(Firm.partial_writes? ? 0 : 1) { firm.save! } firm = Firm.first.dup firm.account = Account.first diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index 7334514f9a..40f1dbccde 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -311,12 +311,12 @@ class DirtyTest < ActiveRecord::TestCase pirate = Pirate.new(:catchphrase => 'foo') old_updated_on = 1.hour.ago.beginning_of_day - with_partial_updates Pirate, false do + with_partial_writes Pirate, false do assert_queries(2) { 2.times { pirate.save! } } Pirate.where(id: pirate.id).update_all(:updated_on => old_updated_on) end - with_partial_updates Pirate, true do + with_partial_writes Pirate, true do assert_queries(0) { 2.times { pirate.save! } } assert_equal old_updated_on, pirate.reload.updated_on @@ -329,12 +329,12 @@ class DirtyTest < ActiveRecord::TestCase person = Person.new(:first_name => 'foo') old_lock_version = 1 - with_partial_updates Person, false do + with_partial_writes Person, false do assert_queries(2) { 2.times { person.save! } } Person.where(id: person.id).update_all(:first_name => 'baz') end - with_partial_updates Person, true do + with_partial_writes Person, true do assert_queries(0) { 2.times { person.save! } } assert_equal old_lock_version, person.reload.lock_version @@ -408,8 +408,8 @@ class DirtyTest < ActiveRecord::TestCase assert !pirate.catchphrase_changed? end - def test_save_should_store_serialized_attributes_even_with_partial_updates - with_partial_updates(Topic) do + def test_save_should_store_serialized_attributes_even_with_partial_writes + with_partial_writes(Topic) do topic = Topic.create!(:content => {:a => "a"}) topic.content[:b] = "b" #assert topic.changed? # Known bug, will fail @@ -421,7 +421,7 @@ class DirtyTest < ActiveRecord::TestCase end def test_save_always_should_update_timestamps_when_serialized_attributes_are_present - with_partial_updates(Topic) do + with_partial_writes(Topic) do topic = Topic.create!(:content => {:a => "a"}) topic.save! @@ -434,8 +434,8 @@ class DirtyTest < ActiveRecord::TestCase end end - def test_save_should_not_save_serialized_attribute_with_partial_updates_if_not_present - with_partial_updates(Topic) do + def test_save_should_not_save_serialized_attribute_with_partial_writes_if_not_present + with_partial_writes(Topic) do Topic.create!(:author_name => 'Bill', :content => {:a => "a"}) topic = Topic.select('id, author_name').first topic.update_columns author_name: 'John' @@ -552,7 +552,7 @@ class DirtyTest < ActiveRecord::TestCase end test "partial insert" do - with_partial_updates Person do + with_partial_writes Person do jon = nil assert_sql(/first_name/i) do jon = Person.create! first_name: 'Jon' @@ -568,20 +568,34 @@ class DirtyTest < ActiveRecord::TestCase end test "partial insert with empty values" do - with_partial_updates Aircraft do + with_partial_writes Aircraft do a = Aircraft.create! a.reload assert_not_nil a.id end end + test "partial_updates config attribute is deprecated" do + klass = Class.new(ActiveRecord::Base) + + assert klass.partial_writes? + assert_deprecated { assert klass.partial_updates? } + assert_deprecated { assert klass.partial_updates } + + assert_deprecated { klass.partial_updates = false } + + assert !klass.partial_writes? + assert_deprecated { assert !klass.partial_updates? } + assert_deprecated { assert !klass.partial_updates } + end + private - def with_partial_updates(klass, on = true) - old = klass.partial_updates? - klass.partial_updates = on + def with_partial_writes(klass, on = true) + old = klass.partial_writes? + klass.partial_writes = on yield ensure - klass.partial_updates = old + klass.partial_writes = old end def check_pirate_after_save_failure(pirate) |