Permalink
Browse files

Extract common logic between one and belongs_to into SingleAssociation

Closes #345
  • Loading branch information...
1 parent 1279269 commit 887ddcdf372502d186f477980a7ad8b7fb7199be @bkeepers bkeepers committed Nov 7, 2011
View
1 lib/mongo_mapper.rb
@@ -62,6 +62,7 @@ module Associations
autoload :Collection, 'mongo_mapper/plugins/associations/collection'
autoload :EmbeddedCollection, 'mongo_mapper/plugins/associations/embedded_collection'
autoload :ManyAssociation, 'mongo_mapper/plugins/associations/many_association'
+ autoload :SingleAssociation, 'mongo_mapper/plugins/associations/single_association'
autoload :BelongsToAssociation, 'mongo_mapper/plugins/associations/belongs_to_association'
autoload :OneAssociation, 'mongo_mapper/plugins/associations/one_association'
autoload :ManyDocumentsProxy, 'mongo_mapper/plugins/associations/many_documents_proxy'
View
39 lib/mongo_mapper/plugins/associations/belongs_to_association.rb
@@ -2,8 +2,7 @@
module MongoMapper
module Plugins
module Associations
- class BelongsToAssociation < Base
-
+ class BelongsToAssociation < SingleAssociation
def embeddable?
false
end
@@ -14,41 +13,7 @@ def proxy_class
def setup(model)
model.key foreign_key, ObjectId unless model.key?(foreign_key)
-
- model.associations_module.module_eval <<-end_eval
- def #{name}
- proxy = get_proxy(associations[#{name.inspect}])
- proxy.nil? ? nil : proxy
- end
-
- def #{name}=(value)
- association = associations[#{name.inspect}]
- proxy = get_proxy(association)
-
- if proxy.nil? || proxy.target != value
- proxy = build_proxy(association)
- end
-
- proxy.replace(value)
- value
- end
-
- def #{name}?
- get_proxy(associations[#{name.inspect}]).present?
- end
-
- def build_#{name}(attrs={})
- get_proxy(associations[#{name.inspect}]).build(attrs)
- end
-
- def create_#{name}(attrs={})
- get_proxy(associations[#{name.inspect}]).create(attrs)
- end
-
- def create_#{name}!(attrs={})
- get_proxy(associations[#{name.inspect}]).create!(attrs)
- end
- end_eval
+ super
end
def autosave?
View
8 lib/mongo_mapper/plugins/associations/one_association.rb
@@ -2,7 +2,7 @@
module MongoMapper
module Plugins
module Associations
- class OneAssociation < BelongsToAssociation
+ class OneAssociation < SingleAssociation
def embeddable?
klass.embeddable?
end
@@ -17,17 +17,17 @@ def proxy_class
OneProxy
end
end
-
+
def setup(model)
super
-
+
association = self
options = self.options
model.before_destroy do
if !association.embeddable?
proxy = self.get_proxy(association)
-
+
unless proxy.nil?
case options[:dependent]
when :destroy then proxy.destroy
View
45 lib/mongo_mapper/plugins/associations/single_association.rb
@@ -0,0 +1,45 @@
+# encoding: UTF-8
+module MongoMapper
+ module Plugins
+ module Associations
+ class SingleAssociation < Base
+ def setup(model)
+ model.associations_module.module_eval <<-end_eval
+ def #{name}
+ proxy = get_proxy(associations[#{name.inspect}])
+ proxy.nil? ? nil : proxy
+ end
+
+ def #{name}=(value)
+ association = associations[#{name.inspect}]
+ proxy = get_proxy(association)
+
+ if proxy.nil? || proxy.target != value
+ proxy = build_proxy(association)
+ end
+
+ proxy.replace(value)
+ value
+ end
+
+ def #{name}?
+ get_proxy(associations[#{name.inspect}]).present?
+ end
+
+ def build_#{name}(attrs={})
+ get_proxy(associations[#{name.inspect}]).build(attrs)
+ end
+
+ def create_#{name}(attrs={})
+ get_proxy(associations[#{name.inspect}]).create(attrs)
+ end
+
+ def create_#{name}!(attrs={})
+ get_proxy(associations[#{name.inspect}]).create!(attrs)
+ end
+ end_eval
+ end
+ end
+ end
+ end
+end
View
6 test/functional/associations/test_one_as_proxy.rb
@@ -17,6 +17,12 @@ def setup
nil.should === @post_class.new.author
end
+ should "not define any keys" do
+ count = @post_class.keys.length
+ @post_class.one :author, :class => @author_class
+ @post_class.keys.length.should == count
+ end
+
should "allow assignment of associated document using a hash" do
@post_class.one :author, :as => :authorable, :class => @author_class

0 comments on commit 887ddcd

Please sign in to comment.