Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added 'embedded_in' call to declare and define relationship to owning…

… document
  • Loading branch information...
commit 9513ccb21f962d8bf07c1a50ccc53d4898c913c3 1 parent bada687
@magnetised magnetised authored jnunemaker committed
View
14 lib/mongo_mapper/embedded_document.rb
@@ -1,12 +1,12 @@
module MongoMapper
module EmbeddedDocument
extend DescendantAppends
-
+
def self.included(model)
model.class_eval do
include InstanceMethods
extend ClassMethods
-
+
extend Plugins
plugin Plugins::Associations
plugin Plugins::Clone
@@ -21,7 +21,7 @@ def self.included(model)
attr_accessor :_root_document, :_owner
end
-
+
super
end
@@ -29,6 +29,12 @@ module ClassMethods
def embeddable?
true
end
+
+ def embedded_in(owner_name)
+ define_method(owner_name) do
+ self._owner
+ end
+ end
end
module InstanceMethods
@@ -38,7 +44,7 @@ def save(options={})
end
result
end
-
+
def save!(options={})
if result = _root_document.try(:save!, options)
@new = false
View
14 test/functional/associations/test_many_embedded_proxy.rb
@@ -15,6 +15,7 @@ def setup
@pet_class = EDoc do
key :name, String
end
+ @pet_class.embedded_in :person
@person_class = EDoc do
key :name, String
end
@@ -169,6 +170,19 @@ def setup
doc.people.first._owner.should == doc
doc.people.first.pets.first._owner.should == doc.people.first
end
+
+ should "create embedded_in relationship for embedded docs" do
+ doc = @klass.new
+ meg = @person_class.new(:name => 'Meg')
+ pet = @pet_class.new(:name => 'Sparky', :species => 'Dog')
+
+ doc.people << meg
+ meg.pets << pet
+ doc.save
+
+ doc.reload
+ doc.people.first.pets.first.person.should == doc.people.first
+ end
end
should "allow finding by id" do
Please sign in to comment.
Something went wrong with that request. Please try again.