Skip to content

Commit

Permalink
only configure database in rails if couchdb.yml exists
Browse files Browse the repository at this point in the history
  • Loading branch information
langalex committed Apr 18, 2012
1 parent b44df57 commit aa743e7
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -11,6 +11,8 @@
* improve compatibility with state_machine (Alexander Lang) * improve compatibility with state_machine (Alexander Lang)
* allow false as default value for properties (Matthias Jakel) * allow false as default value for properties (Matthias Jakel)
* support for Erlang views (Alexander Lang) * support for Erlang views (Alexander Lang)
* don't crash, only warn if couchdb.yml is missing (Alexander Lang)
* use the therubyracer gem to run view specs instead of relying on a `js` executable (Alexander Lang)


### 0.6.0 ### 0.6.0


Expand Down
14 changes: 7 additions & 7 deletions Gemfile.lock
@@ -1,19 +1,19 @@
PATH PATH
remote: . remote: .
specs: specs:
couch_potato (0.6.0) couch_potato (0.7.0.pre.1)
activemodel activemodel
couchrest (>= 1.0.1) couchrest (>= 1.0.1)
json (~> 1.6.0) json (~> 1.6.0)


GEM GEM
remote: http://rubygems.org/ remote: http://rubygems.org/
specs: specs:
activemodel (3.1.1) activemodel (3.2.1)
activesupport (= 3.1.1) activesupport (= 3.2.1)
builder (~> 3.0.0) builder (~> 3.0.0)
activesupport (3.2.1)
i18n (~> 0.6) i18n (~> 0.6)
activesupport (3.1.1)
multi_json (~> 1.0) multi_json (~> 1.0)
builder (3.0.0) builder (3.0.0)
couchrest (1.1.2) couchrest (1.1.2)
Expand All @@ -22,10 +22,10 @@ GEM
rest-client (~> 1.6.1) rest-client (~> 1.6.1)
diff-lcs (1.1.3) diff-lcs (1.1.3)
i18n (0.6.0) i18n (0.6.0)
json (1.6.1) json (1.6.5)
libv8 (3.3.10.4) libv8 (3.3.10.4)
mime-types (1.16) mime-types (1.17.2)
multi_json (1.0.3) multi_json (1.0.4)
rake (0.9.2) rake (0.9.2)
rest-client (1.6.7) rest-client (1.6.7)
mime-types (>= 1.16) mime-types (>= 1.16)
Expand Down
8 changes: 4 additions & 4 deletions active_support_3_0.lock
@@ -1,7 +1,7 @@
PATH PATH
remote: . remote: .
specs: specs:
couch_potato (0.6.0) couch_potato (0.7.0.pre.1)
activemodel activemodel
couchrest (>= 1.0.1) couchrest (>= 1.0.1)
json (~> 1.6.0) json (~> 1.6.0)
Expand All @@ -21,10 +21,10 @@ GEM
rest-client (~> 1.6.1) rest-client (~> 1.6.1)
diff-lcs (1.1.2) diff-lcs (1.1.2)
i18n (0.5.0) i18n (0.5.0)
json (1.6.1) json (1.6.5)
libv8 (3.3.10.4) libv8 (3.3.10.4)
mime-types (1.16) mime-types (1.17.2)
multi_json (1.0.3) multi_json (1.0.4)
rake (0.8.7) rake (0.8.7)
rest-client (1.6.7) rest-client (1.6.7)
mime-types (>= 1.16) mime-types (>= 1.16)
Expand Down
6 changes: 3 additions & 3 deletions active_support_3_1.lock
@@ -1,7 +1,7 @@
PATH PATH
remote: . remote: .
specs: specs:
couch_potato (0.6.0) couch_potato (0.7.0.pre.1)
activemodel activemodel
couchrest (>= 1.0.1) couchrest (>= 1.0.1)
json (~> 1.6.0) json (~> 1.6.0)
Expand All @@ -24,9 +24,9 @@ GEM
rest-client (~> 1.6.1) rest-client (~> 1.6.1)
diff-lcs (1.1.2) diff-lcs (1.1.2)
i18n (0.6.0) i18n (0.6.0)
json (1.6.1) json (1.6.5)
libv8 (3.3.10.4) libv8 (3.3.10.4)
mime-types (1.16) mime-types (1.17.2)
multi_json (1.0.3) multi_json (1.0.3)
rake (0.9.2) rake (0.9.2)
rest-client (1.6.7) rest-client (1.6.7)
Expand Down
2 changes: 1 addition & 1 deletion active_support_3_2.lock
@@ -1,7 +1,7 @@
PATH PATH
remote: . remote: .
specs: specs:
couch_potato (0.6.0) couch_potato (0.7.0.pre.1)
activemodel activemodel
couchrest (>= 1.0.1) couchrest (>= 1.0.1)
json (~> 1.6.0) json (~> 1.6.0)
Expand Down
17 changes: 11 additions & 6 deletions lib/couch_potato/railtie.rb
Expand Up @@ -3,13 +3,18 @@


module CouchPotato module CouchPotato
def self.rails_init def self.rails_init
config = YAML::load(ERB.new(File.read(Rails.root.join('config/couchdb.yml'))).result)[Rails.env] path = Rails.root.join('config/couchdb.yml')
if config.is_a?(String) if File.exist?(path)
CouchPotato::Config.database_name = config config = YAML::load(ERB.new(File.read(path)).result)[Rails.env]
if config.is_a?(String)
CouchPotato::Config.database_name = config
else
CouchPotato::Config.database_name = config['database']
CouchPotato::Config.split_design_documents_per_view = config['split_design_documents_per_view'] if config['split_design_documents_per_view']
CouchPotato::Config.default_language = config['default_language'] if config['default_language']
end
else else
CouchPotato::Config.database_name = config['database'] Rails.logger.warn "Rails.root/config/couchdb.yml does not exist. Not configuring a database."
CouchPotato::Config.split_design_documents_per_view = config['split_design_documents_per_view'] if config['split_design_documents_per_view']
CouchPotato::Config.default_language = config['default_language'] if config['default_language']
end end
end end


Expand Down
21 changes: 21 additions & 0 deletions spec/railtie_spec.rb
Expand Up @@ -6,6 +6,7 @@ module Rails
def self.env def self.env
'test' 'test'
end end

class Railtie class Railtie
def self.initializer(*args) def self.initializer(*args)
end end
Expand All @@ -14,6 +15,10 @@ def self.initializer(*args)
def self.root def self.root
RSpec::Mocks::Mock.new :join => '' RSpec::Mocks::Mock.new :join => ''
end end

def self.logger
RSpec::Mocks::Mock.new :warn => nil
end
end end


require 'couch_potato/railtie' require 'couch_potato/railtie'
Expand All @@ -29,6 +34,22 @@ def self.root
CouchPotato::Config.default_language = @default_language CouchPotato::Config.default_language = @default_language
end end


before(:each) do
File.stub(exist?: true)
end

context 'when the yml file does not exist' do
before(:each) do
File.stub(exist?: false)
end

it 'does not configure the database' do
CouchPotato::Config.should_not_receive(:database_name=)

CouchPotato.rails_init
end
end

context 'yaml file contains only database names' do context 'yaml file contains only database names' do
it "should set the database name from the yaml file" do it "should set the database name from the yaml file" do
File.stub(:read => "test: test_db") File.stub(:read => "test: test_db")
Expand Down

0 comments on commit aa743e7

Please sign in to comment.