Skip to content

Commit

Permalink
Added support for persisted? and to_key to work with rails 3 edge. Ha…
Browse files Browse the repository at this point in the history
…d to refactor how embedded documents know if they are new or not a bit.
  • Loading branch information
jnunemaker committed Mar 26, 2010
1 parent 53f939c commit 11bbcb8
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 70 deletions.
8 changes: 4 additions & 4 deletions lib/mongo_mapper/document.rb
Expand Up @@ -315,12 +315,12 @@ def delete
self.class.delete(id) unless new?
end

def destroyed?
@_destroyed == true
def new?
@new
end

def persisted?
!new? && !destroyed?
def destroyed?
@_destroyed == true
end

def reload
Expand Down
6 changes: 5 additions & 1 deletion lib/mongo_mapper/embedded_document.rb
Expand Up @@ -39,7 +39,11 @@ def embedded_in(owner_name)

module InstanceMethods
def destroyed?
_root_document.try(:destroyed?)
!!_root_document.try(:destroyed?)
end

def new?
_root_document.try(:new?) || @new
end

def save(options={})
Expand Down
8 changes: 4 additions & 4 deletions lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb
Expand Up @@ -11,10 +11,10 @@ def replace(values)

private
def find_target
(@_values || []).map do |v|
child = klass.load(v)
assign_references(child)
child
(@_values || []).map do |attrs|
klass.load(attrs).tap do |child|
assign_references(child)
end
end
end
end
Expand Down
34 changes: 15 additions & 19 deletions lib/mongo_mapper/plugins/keys.rb
Expand Up @@ -146,15 +146,9 @@ def create_validations_for(key)

module InstanceMethods
def initialize(attrs={}, from_database=false)
unless attrs.nil?
provided_keys = attrs.keys.map { |k| k.to_s }
unless provided_keys.include?('_id') || provided_keys.include?('id')
write_key :_id, Mongo::ObjectID.new
end
end

assign_type_if_present

default_id_value(attrs)
assign_type

if from_database
@new = false
self.attributes = attrs
Expand All @@ -164,8 +158,8 @@ def initialize(attrs={}, from_database=false)
end
end

def new?
@new
def persisted?
!new? && !destroyed?
end

def attributes=(attrs)
Expand All @@ -175,9 +169,6 @@ def attributes=(attrs)
writer_method = "#{name}="

if respond_to?(writer_method)
if writer_method == '_root_document='
puts "_root_document= #{value.inspect}"
end
self.send(writer_method, value)
else
self[name.to_s] = value
Expand Down Expand Up @@ -238,28 +229,33 @@ def []=(name, value)
write_key(name, value)
end

# @api public
def keys
self.class.keys
end

# @api private?
def key_names
keys.keys
end

# @api private?
def non_embedded_keys
keys.values.select { |key| !key.embeddable? }
end

# @api private?
def embedded_keys
keys.values.select { |key| key.embeddable? }
end

private
def assign_type_if_present
def default_id_value(attrs)
unless attrs.nil?
provided_keys = attrs.keys.map { |k| k.to_s }
unless provided_keys.include?('_id') || provided_keys.include?('id')
write_key :_id, Mongo::ObjectID.new
end
end
end

def assign_type
self._type = self.class.name if respond_to?(:_type=)
end

Expand Down
6 changes: 5 additions & 1 deletion lib/mongo_mapper/plugins/rails.rb
Expand Up @@ -7,13 +7,17 @@ def self.configure(model)

module InstanceMethods
def to_param
id.to_s
id.to_s if persisted?
end

def to_model
self
end

def to_key
[id] if persisted?
end

def new_record?
new?
end
Expand Down
2 changes: 2 additions & 0 deletions test/active_model_lint_test.rb
@@ -1,4 +1,6 @@
require 'test_helper'
# For testing against edge rails also.
# $:.unshift '/Users/jnunemaker/dev/ruby/rails/activemodel/lib'
require 'active_model'
require 'models'

Expand Down
63 changes: 54 additions & 9 deletions test/functional/test_embedded_document.rb
Expand Up @@ -53,31 +53,54 @@ def setup
doc1.reload.message.class.should be(Enter)
end

context "new?" do
context "new? (embedded key)" do
setup do
@klass.key :foo, @address_class
end

should "be new until document is saved" do
should "be true until document is saved" do
address = @address_class.new(:city => 'South Bend', :state => 'IN')
doc = @klass.new(:foo => address)
address.new?.should == true
address.new?.should be_true
end

should "not be new after document is saved" do
should "be false after document is saved" do
address = @address_class.new(:city => 'South Bend', :state => 'IN')
doc = @klass.new(:foo => address)
doc.save
doc.foo.new?.should == false
doc.foo.new?.should be_false
end

should "not be new when document is read back" do
should "be false when loaded from database" do
address = @address_class.new(:city => 'South Bend', :state => 'IN')
doc = @klass.new(:foo => address)
doc.save

doc = doc.reload
doc.foo.new?.should == false
doc.reload
doc.foo.new?.should be_false
end
end

context "new? (embedded association)" do
setup do
@doc = @klass.new(:pets => [{:name => 'poo bear'}])
end

should "be true until document is saved" do
@doc.should be_new
@doc.pets.first.should be_new
end

should "be false after document is saved" do
@doc.save
@doc.pets.first.should_not be_new
end

should "be false when loaded from database" do
@doc.save
@doc.pets.first.should_not be_new
@doc.reload
@doc.pets.first.should_not be_new
end
end

Expand All @@ -90,14 +113,36 @@ def setup
@doc.should_not be_destroyed
@doc.pets.first.should_not be_destroyed
end

should "be true if root document is destroyed" do
@doc.destroy
@doc.should be_destroyed
@doc.pets.first.should be_destroyed
end
end

context "#persisted?" do
setup do
@doc = @klass.new(:name => 'persisted doc', :pets => [@pet_klass.new(:name => 'persisted pet')])
end

should_eventually "be false if new" do
@doc.pets.first.should_not be_persisted
end

should "be false if destroyed" do
@doc.save
@doc.destroy
@doc.pets.first.should be_destroyed
@doc.pets.first.should_not be_persisted
end

should "be true if not new or destroyed" do
@doc.save
@doc.pets.first.should be_persisted
end
end

should "be able to save" do
person = @klass.create

Expand Down
6 changes: 0 additions & 6 deletions test/unit/test_document.rb
Expand Up @@ -105,12 +105,6 @@ class Post
@document.new._id.should be_instance_of(Mongo::ObjectID)
end

should "have to_param that is string representation of id" do
doc = @document.new(:id => Mongo::ObjectID.new)
doc.to_param.should == doc.id.to_s
doc.to_param.should be_instance_of(String)
end

should "have access to logger" do
doc = @document.new
doc.logger.should == @document.logger
Expand Down
8 changes: 1 addition & 7 deletions test/unit/test_embedded_document.rb
Expand Up @@ -11,7 +11,7 @@ def other_child=(value)
end

class EmbeddedDocumentTest < Test::Unit::TestCase
context "" do
context "EmbeddedDocuments" do
setup do
class ::Grandparent
include MongoMapper::EmbeddedDocument
Expand Down Expand Up @@ -221,12 +221,6 @@ class ::OtherChild < ::Parent
end
end

should "have to_param that is string representation of id" do
doc = @document.new
doc.to_param.should == doc.id.to_s
doc.to_param.should be_instance_of(String)
end

should "have access to class logger" do
doc = @document.new
doc.logger.should == @document.logger
Expand Down

0 comments on commit 11bbcb8

Please sign in to comment.