Permalink
Browse files

Inherit connection and database name.

Subclasses were not getting these before. Only collection name was
inherited.

Fixes #420 and #424
  • Loading branch information...
1 parent 5106f80 commit 6c1cb7c41a3ad1ce415a2428de902a33656df9c6 @jnunemaker jnunemaker committed Sep 21, 2012
Showing with 36 additions and 2 deletions.
  1. +10 −1 lib/mongo_mapper/plugins/persistence.rb
  2. +0 −1 lib/mongo_mapper/plugins/sci.rb
  3. +26 −0 test/functional/test_sci.rb
@@ -5,6 +5,15 @@ module Persistence
extend ActiveSupport::Concern
module ClassMethods
+ def inherited(subclass)
+ unless subclass.embeddable?
+ subclass.connection(connection)
+ subclass.set_database_name(database_name)
+ subclass.set_collection_name(collection_name)
+ end
+ super
+ end
+
def connection(mongo_connection=nil)
assert_supported
if mongo_connection.nil?
@@ -66,4 +75,4 @@ def database
end
end
end
-end
+end
@@ -12,7 +12,6 @@ module ClassMethods
def inherited(subclass)
key :_type, String unless key?(:_type)
subclass.instance_variable_set("@single_collection_inherited", true)
- subclass.set_collection_name(collection_name) unless subclass.embeddable?
subclass.single_collection_parent = self
super
end
@@ -32,6 +32,32 @@ class ::DocGrandGrandSon < ::DocGrandSon; end
DocParent.key?(:_type).should be_true
end
+ should "use the same connection in the subclass" do
+ parent_class = Class.new do
+ include MongoMapper::Document
+ connection Mongo::Connection.new
+ end
+
+ child_class = Class.new(parent_class) do
+ include MongoMapper::Document
+ end
+
+ child_class.connection.should == child_class.connection
+ end
+
+ should "use the same database in the subclass" do
+ parent_class = Class.new do
+ include MongoMapper::Document
+ set_database_name 'something'
+ end
+
+ child_class = Class.new(parent_class) do
+ include MongoMapper::Document
+ end
+
+ child_class.database.name.should == 'something'
+ end
+
should "use the same collection in the subclass" do
DocDaughter.collection.name.should == DocParent.collection.name
end

0 comments on commit 6c1cb7c

Please sign in to comment.