Permalink
Browse files

Mingo.db is nil when not connected

  • Loading branch information...
1 parent fd37f38 commit 8be69b2c41e3ba6873eeff278a4561d8f5863074 @mislav committed Aug 11, 2011
Showing with 21 additions and 2 deletions.
  1. +5 −1 lib/mingo/connection.rb
  2. +16 −1 spec/mingo_spec.rb
View
6 lib/mingo/connection.rb
@@ -3,7 +3,11 @@ module Connection
attr_writer :db, :collection
def db
- @db || superclass.db
+ (defined?(@db) && @db) or superclass.respond_to?(:db) ? superclass.db : nil
+ end
+
+ def connected?
+ !!db
end
def connect(dbname_or_uri)
View
17 spec/mingo_spec.rb
@@ -16,7 +16,22 @@ def age=(value)
before :all do
User.collection.remove
end
-
+
+ it "has connection" do
+ Mingo.should be_connected
+ User.should be_connected
+
+ old_db = Mingo.db
+ Mingo.db = nil
+ begin
+ Mingo.should_not be_connected
+ User.should_not be_connected
+ User.db.should be_nil
+ ensure
+ Mingo.db = old_db
+ end
+ end
+
it "obtains an ID by saving" do
user = build :name => 'Mislav'
user.should_not be_persisted

0 comments on commit 8be69b2

Please sign in to comment.