Skip to content

Commit

Permalink
Use #persisted? instead of #new? internally
Browse files Browse the repository at this point in the history
ActiveModel::Lint requires the presence of #persisted?, but not #new?. This allows MM to play nicer with other ActiveModel ORMs (belongs_to should JustWork now with ActiveRecord models).
  • Loading branch information
bkeepers committed Mar 20, 2011
1 parent 1571721 commit b8f5cab
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 18 deletions.
Expand Up @@ -5,7 +5,7 @@ module Associations
class BelongsToPolymorphicProxy < Proxy
def replace(doc)
if doc
doc.save if doc.new?
doc.save unless doc.persisted?
id, type = doc.id, doc.class.name
end

Expand Down
2 changes: 1 addition & 1 deletion lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb
Expand Up @@ -5,7 +5,7 @@ module Associations
class BelongsToProxy < Proxy
def replace(doc)
if doc
doc.save if doc.new?
doc.save if !doc.persisted?
id = doc.id
end

Expand Down
8 changes: 4 additions & 4 deletions lib/mongo_mapper/plugins/associations/in_array_proxy.rb
Expand Up @@ -60,7 +60,7 @@ def nullify

def create(attrs={})
doc = klass.create(attrs)
unless doc.new?
if doc.persisted?
ids << doc.id
proxy_owner.save
reset
Expand All @@ -70,7 +70,7 @@ def create(attrs={})

def create!(attrs={})
doc = klass.create!(attrs)
unless doc.new?
if doc.persisted?
ids << doc.id
proxy_owner.save
reset
Expand All @@ -80,7 +80,7 @@ def create!(attrs={})

def <<(*docs)
flatten_deeper(docs).each do |doc|
doc.save if doc.new?
doc.save unless doc.persisted?
unless ids.include?(doc.id)
ids << doc.id
end
Expand All @@ -92,7 +92,7 @@ def <<(*docs)

def replace(docs)
doc_ids = docs.map do |doc|
doc.save if doc.new?
doc.save unless doc.persisted?
doc.id
end
ids.replace(doc_ids.uniq)
Expand Down
Expand Up @@ -88,7 +88,7 @@ def find_target
end

def ensure_owner_saved
proxy_owner.save if proxy_owner.new?
proxy_owner.save unless proxy_owner.persisted?
end

def prepare(doc)
Expand Down
6 changes: 3 additions & 3 deletions lib/mongo_mapper/plugins/associations/one_proxy.rb
Expand Up @@ -19,7 +19,7 @@ def replace(doc)
load_target

if !target.nil? && target != doc
if options[:dependent] && !target.new?
if options[:dependent] && target.persisted?
case options[:dependent]
when :delete
target.delete
Expand All @@ -35,10 +35,10 @@ def replace(doc)
if doc.nil?
target.update_attributes(foreign_key => nil) unless target.nil?
else
proxy_owner.save if proxy_owner.new?
proxy_owner.save unless proxy_owner.persisted?
doc = klass.new(doc) unless doc.is_a?(klass)
doc[foreign_key] = proxy_owner.id
doc.save if doc.new?
doc.save unless doc.persisted?
loaded
@target = doc
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo_mapper/plugins/associations/proxy.rb
Expand Up @@ -113,7 +113,7 @@ def method_missing(method, *args, &block)
def load_target
unless loaded?
if @target.is_a?(Array) && @target.any?
@target = find_target + @target.find_all { |record| record.new? }
@target = find_target + @target.find_all { |record| !record.persisted? }
else
@target = find_target
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo_mapper/plugins/caching.rb
Expand Up @@ -7,7 +7,7 @@ module Caching
module InstanceMethods
def cache_key(*suffixes)
cache_key = case
when new?
when !persisted?
"#{self.class.name}/new"
when timestamp = self[:updated_at]
"#{self.class.name}/#{id}-#{timestamp.to_s(:number)}"
Expand Down
5 changes: 2 additions & 3 deletions lib/mongo_mapper/plugins/querying.rb
Expand Up @@ -150,13 +150,12 @@ def destroy
end

def delete
@_destroyed = true
self.class.delete(id) unless new?
self.class.delete(id).tap { @_destroyed = true } if persisted?
end

private
def create_or_update(options={})
result = new? ? create(options) : update(options)
result = persisted? ? update(options) : create(options)
result != false
end

Expand Down
2 changes: 1 addition & 1 deletion lib/mongo_mapper/plugins/timestamps.rb
Expand Up @@ -15,7 +15,7 @@ def timestamps!
module InstanceMethods
def update_timestamps
now = Time.now.utc
self[:created_at] = now if new? && !created_at?
self[:created_at] = now if !persisted? && !created_at?
self[:updated_at] = now
end
end
Expand Down
5 changes: 3 additions & 2 deletions test/functional/test_caching.rb
Expand Up @@ -8,14 +8,15 @@ class CachingTest < Test::Unit::TestCase
plugin MongoMapper::Plugins::Caching
end
@klass.stubs(:name).returns('Post')
@klass.any_instance.stubs(:persisted?).returns(true)
@klass.any_instance.stubs(:[]).returns(nil)
@klass.any_instance.stubs(:[]=).returns(nil)
end

context "new" do
setup do
@doc = @klass.new
@doc.stubs(:new?).returns(true)
@doc.stubs(:persisted?).returns(false)
end

should "be class/new" do
Expand All @@ -35,7 +36,7 @@ class CachingTest < Test::Unit::TestCase
setup do
@object_id = BSON::ObjectId.new
@doc = @klass.new
@doc.stubs(:new?).returns(false)
@doc.stubs(:persisted).returns(true)
@doc.stubs(:id).returns(@object_id)
end

Expand Down

0 comments on commit b8f5cab

Please sign in to comment.