Permalink
Browse files

Merge pull request #1957 from i0rek/set_database

Set database name when inheriting.
  • Loading branch information...
2 parents 39f32f9 + 99e98e4 commit f6cc071b8fda24bdc2394540d6d4f034825a0040 @durran durran committed Apr 27, 2012
Showing with 39 additions and 4 deletions.
  1. +5 −0 lib/mongoid/multi_database.rb
  2. +34 −4 spec/unit/mongoid/multi_database_spec.rb
View
5 lib/mongoid/multi_database.rb
@@ -26,6 +26,11 @@ def database; @database end
def set_database(name)
@database = name.to_s
end
+
+ def inherited(subclass)
+ super
+ subclass.set_database(database.dup) if database
+ end
end
end
end
View
38 spec/unit/mongoid/multi_database_spec.rb
@@ -1,19 +1,49 @@
require "spec_helper"
describe Mongoid::MultiDatabase do
- before do
- @klass = Class.new do
+ let(:klass) do
+ Class.new do
include Mongoid::Document
end
end
describe ".database" do
before do
- @klass.set_database :secondary
+ klass.set_database :secondary
end
it "sets the database key on the class" do
- @klass.database.should == "secondary"
+ klass.database.should eq("secondary")
+ end
+ end
+
+ describe ".inherited" do
+ let(:child) do
+ Class.new do
+ include Mongoid::Document
+ end
+ end
+
+ context "when database" do
+
+ before do
+ klass.set_database("secondary")
+ klass.inherited(child)
+ end
+
+ it "sets the parents database" do
+ child.database.should eq("secondary")
+ end
+ end
+
+ context "when no database" do
+ before do
+ klass.inherited(child)
+ end
+
+ it "does nothing" do
+ child.database.should be_nil
+ end
end
end
end

0 comments on commit f6cc071

Please sign in to comment.