From 1f06652a57e727700c3a673dc1f86e3b1e07ce1f Mon Sep 17 00:00:00 2001 From: David Chelimsky Date: Sun, 7 Nov 2010 08:05:18 -0600 Subject: use persisted? instead of new_record? wherever possible - persisted? is the API defined in ActiveModel - makes it easier for extension libraries to conform to ActiveModel APIs without concern for whether the extended object is specifically ActiveRecord [#5927 state:committed] Signed-off-by: Santiago Pastorino --- activerecord/lib/active_record/persistence.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'activerecord/lib/active_record/persistence.rb') diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 707c1a05be..f905b7026b 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -4,7 +4,8 @@ module ActiveRecord # Returns true if this object hasn't been saved yet -- that is, a record # for the object doesn't exist in the data store yet; otherwise, returns false. def new_record? - @new_record + @persisted = false unless defined?(@persisted) + !@persisted end # Returns true if this object has been destroyed, otherwise returns false. @@ -15,7 +16,8 @@ module ActiveRecord # Returns if the record is persisted, i.e. it's not a new record and it was # not destroyed. def persisted? - !(new_record? || destroyed?) + @persisted = false unless defined?(@persisted) + !!@persisted && !destroyed? end # Saves the model. @@ -94,7 +96,7 @@ module ActiveRecord became = klass.new became.instance_variable_set("@attributes", @attributes) became.instance_variable_set("@attributes_cache", @attributes_cache) - became.instance_variable_set("@new_record", new_record?) + became.instance_variable_set("@persisted", persisted?) became.instance_variable_set("@destroyed", destroyed?) became end @@ -240,7 +242,7 @@ module ActiveRecord private def create_or_update raise ReadOnlyRecord if readonly? - result = new_record? ? create : update + result = persisted? ? update : create result != false end @@ -269,7 +271,7 @@ module ActiveRecord self.id ||= new_id - @new_record = false + @persisted = true id end -- cgit v1.2.3 From 75dfd95618310a9522a73d8126b35351c8189042 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 9 Nov 2010 16:00:02 -0200 Subject: Don't check if persisted is defined just initialize it properly --- activerecord/lib/active_record/persistence.rb | 2 -- 1 file changed, 2 deletions(-) (limited to 'activerecord/lib/active_record/persistence.rb') diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index f905b7026b..390e09d826 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -4,7 +4,6 @@ module ActiveRecord # Returns true if this object hasn't been saved yet -- that is, a record # for the object doesn't exist in the data store yet; otherwise, returns false. def new_record? - @persisted = false unless defined?(@persisted) !@persisted end @@ -16,7 +15,6 @@ module ActiveRecord # Returns if the record is persisted, i.e. it's not a new record and it was # not destroyed. def persisted? - @persisted = false unless defined?(@persisted) !!@persisted && !destroyed? end -- cgit v1.2.3 From 88a559d4148789474c6dbd216725c69fa18200a9 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 9 Nov 2010 16:53:03 -0200 Subject: Double negation of an already boolean value produces the same result --- activerecord/lib/active_record/persistence.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/persistence.rb') diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 390e09d826..00d49dd0f6 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -15,7 +15,7 @@ module ActiveRecord # Returns if the record is persisted, i.e. it's not a new record and it was # not destroyed. def persisted? - !!@persisted && !destroyed? + @persisted && !destroyed? end # Saves the model. -- cgit v1.2.3 From de2933e1a062f0752512eb0ec60f7217f4890f8c Mon Sep 17 00:00:00 2001 From: Franck Verrot Date: Fri, 12 Nov 2010 01:37:12 +0800 Subject: STI type is now updated when calling AR::Base.becomes on subclasses [#5953 state:resolved] --- activerecord/lib/active_record/persistence.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/lib/active_record/persistence.rb') diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 00d49dd0f6..594a2214bb 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -96,6 +96,7 @@ module ActiveRecord became.instance_variable_set("@attributes_cache", @attributes_cache) became.instance_variable_set("@persisted", persisted?) became.instance_variable_set("@destroyed", destroyed?) + became.type = klass.name unless self.class.descends_from_active_record? became end -- cgit v1.2.3