aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/persistence.rb
diff options
context:
space:
mode:
authorDavid Chelimsky <dchelimsky@gmail.com>2010-11-07 08:05:18 -0600
committerSantiago Pastorino <santiago@wyeworks.com>2010-11-09 13:54:04 -0200
commit1f06652a57e727700c3a673dc1f86e3b1e07ce1f (patch)
tree6ab5165939c2fcae615bd586ea0254e352f6715a /activerecord/lib/active_record/persistence.rb
parentf57b5197b3215d9dd66196e960ef5d78b7a62de1 (diff)
downloadrails-1f06652a57e727700c3a673dc1f86e3b1e07ce1f.tar.gz
rails-1f06652a57e727700c3a673dc1f86e3b1e07ce1f.tar.bz2
rails-1f06652a57e727700c3a673dc1f86e3b1e07ce1f.zip
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 <santiago@wyeworks.com>
Diffstat (limited to 'activerecord/lib/active_record/persistence.rb')
-rw-r--r--activerecord/lib/active_record/persistence.rb12
1 files changed, 7 insertions, 5 deletions
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