Skip to content

Commit

Permalink
Added embedded document new? method back in. Not perfect but I have a…
Browse files Browse the repository at this point in the history
… plan and it is good enough for now.
  • Loading branch information
jnunemaker committed Oct 2, 2009
1 parent 3228afa commit d377019
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
7 changes: 7 additions & 0 deletions lib/mongomapper/embedded_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,16 @@ def initialize(attrs={})
if self.class.embeddable?
if read_attribute(:_id).blank?
write_attribute :_id, Mongo::ObjectID.new.to_s
@new_document = true
else
@new_document = false
end
end
end

def new?
!!@new_document
end

def attributes=(attrs)
return if attrs.blank?
Expand Down
2 changes: 2 additions & 0 deletions lib/mongomapper/rails_compatibility/embedded_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module EmbeddedDocument
def self.included(model)
model.class_eval do
extend ClassMethods

alias_method :new_record?, :new?
end

class << model
Expand Down
8 changes: 4 additions & 4 deletions test/functional/test_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,20 +158,20 @@ def setup
end
end

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

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

should_eventually "not be new when document is read back" do
should "not be new when document is read back" do
address = Address.new(:city => 'South Bend', :state => 'IN')
doc = @document.new(:foo => address)
doc.save
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_rails_compatibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SecondItem < Item
SecondItem.column_names.sort.should == ['_id', 'for_all', 'second_only']
end

should_eventually "alias new to new_record?" do
should "alias new to new_record?" do
instance = Item.new
instance.new_record?.should == instance.new?
end
Expand Down

0 comments on commit d377019

Please sign in to comment.