Skip to content

Commit

Permalink
Fixing issues when loading with rails
Browse files Browse the repository at this point in the history
  • Loading branch information
samlown committed Apr 29, 2011
1 parent 85109b4 commit 814e38f
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 64 deletions.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
1.1.0.beta4
1.1.0.beta5
4 changes: 2 additions & 2 deletions couchrest_model.gemspec
Expand Up @@ -6,7 +6,7 @@ Gem::Specification.new do |s|

s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
s.authors = ["J. Chris Anderson", "Matt Aimonetti", "Marcos Tapajos", "Will Leinweber", "Sam Lown"]
s.date = %q{2011-04-20}
s.date = %q{2011-04-29}
s.description = %q{CouchRest Model provides aditional features to the standard CouchRest Document class such as properties, view designs, associations, callbacks, typecasting and validations.}
s.email = %q{jchris@apache.org}
s.extra_rdoc_files = [
Expand All @@ -25,7 +25,7 @@ Gem::Specification.new do |s|

s.add_dependency(%q<couchrest>, "1.1.0.pre2")
s.add_dependency(%q<mime-types>, "~> 1.15")
s.add_dependency(%q<activemodel>, "~> 3.0.5")
s.add_dependency(%q<activemodel>, "~> 3.0.0")
s.add_dependency(%q<tzinfo>, "~> 0.3.22")
s.add_dependency(%q<railties>, "~> 3.0.0")
s.add_development_dependency(%q<rspec>, ">= 2.0.0")
Expand Down
5 changes: 5 additions & 0 deletions history.txt
@@ -1,3 +1,8 @@
== 1.1.0.beta5

* Major changes:
* Database auto configuration, with connection options!

== 1.1.0.beta4

* Major changes:
Expand Down
2 changes: 1 addition & 1 deletion lib/couchrest/model/base.rb
Expand Up @@ -4,8 +4,8 @@ class Base < Document

extend ActiveModel::Naming

include CouchRest::Model::Connection
include CouchRest::Model::Configuration
include CouchRest::Model::Connection
include CouchRest::Model::Persistence
include CouchRest::Model::Callbacks
include CouchRest::Model::DocumentQueries
Expand Down
13 changes: 3 additions & 10 deletions lib/couchrest/model/configuration.rb
Expand Up @@ -20,20 +20,13 @@ module Configuration
config.mass_assign_any_attribute = false
config.auto_update_design_doc = true

config.environment = defined?(Rails) ? Rails.env : :development

config.connection_config_file =
File.join(
defined?(Rails) ? Rails.root : Dir.pwd,
'config', 'couchdb.yml'
)

app_name = defined?(Rails) ? Rails.application.class.to_s.underscore.gsub(/\/.*/, '') : 'couchrest'
config.environment = :development
config.connection_config_file = File.join(Dir.pwd, 'config', 'couchdb.yml')
config.connection = {
:protocol => 'http',
:host => 'localhost',
:port => '5984',
:prefix => app_name,
:prefix => 'couchrest',
:suffix => nil,
:join => '_',
:username => nil,
Expand Down
10 changes: 6 additions & 4 deletions lib/couchrest/model/connection.rb
Expand Up @@ -47,20 +47,22 @@ def prepare_server_uri
def connection_configuration
@connection_configuration ||=
self.connection.update(
(load_connection_config_file[environment] || {}).symbolize_keys
(load_connection_config_file[environment.to_sym] || {}).symbolize_keys
)
end

def load_connection_config_file
connection_config_cache[connection_config_file] ||=
(File.exists?(connection_config_file) ?
YAML::load(ERB.new(IO.read(connection_config_file)).result) :
file = connection_config_file
connection_config_cache[file] ||=
(File.exists?(file) ?
YAML::load(ERB.new(IO.read(file)).result) :
{ }).symbolize_keys
end

def connection_config_cache
Thread.current[:connection_config_cache] ||= {}
end

end

end
Expand Down
17 changes: 13 additions & 4 deletions lib/couchrest/railtie.rb
@@ -1,12 +1,21 @@
require "rails"
require "active_model/railtie"

module CouchrestModel
module CouchRest
# = Active Record Railtie
class Railtie < Rails::Railtie
class ModelRailtie < Rails::Railtie
config.generators.orm :couchrest_model
config.generators.test_framework :test_unit, :fixture => false

initializer "couchrest_model.configure_default_connection" do
CouchRest::Model::Base.configure do |conf|
conf.environment = Rails.env
conf.connection_config_file = File.join(Rails.root, 'config', 'couchdb.yml')
conf.connection[:prefix] =
Rails.application.class.to_s.underscore.gsub(/\/.*/, '')
end
end
end

end

42 changes: 0 additions & 42 deletions spec/couchrest/configuration_spec.rb
Expand Up @@ -56,48 +56,6 @@
end
end

describe "default configuration" do

it "should provide environment" do
@class.environment.should eql(:development)
end
it "should provide connection config file" do
@class.connection_config_file.should eql(File.join(Dir.pwd, 'config', 'couchdb.yml'))
end
it "should provided simple connection details" do
@class.connection[:prefix].should eql('couchrest')
end

end

describe "default configuration with Rails" do
before do
Rails = mock('Rails') unless defined?(Rails)
Rails.stub!(:env).and_return(:dev)
Rails.stub!(:root).and_return("/rails/root")
app = mock('Application')
app.stub!(:class).and_return("SampleCouch::Application")
Rails.stub!(:application).and_return(app)

# New anon class!
@class = Class.new()
@class.class_eval do
include CouchRest::Model::Configuration
end
end

it "should provide environment" do
@class.environment.should eql(:dev)
end
it "should provide connection config file" do
@class.connection_config_file.should eql(File.join("/rails/root", 'config', 'couchdb.yml'))
end
it "should provided simple connection details" do
@class.connection[:prefix].should eql('sample_couch')
end

end

describe "General examples" do

before(:all) do
Expand Down
19 changes: 19 additions & 0 deletions spec/couchrest/connection_spec.rb
Expand Up @@ -33,6 +33,20 @@
end
end

describe "default configuration" do

it "should provide environment" do
@class.environment.should eql(:development)
end
it "should provide connection config file" do
@class.connection_config_file.should eql(File.join(Dir.pwd, 'config', 'couchdb.yml'))
end
it "should provided simple connection details" do
@class.connection[:prefix].should eql('couchrest')
end

end

describe "class methods" do

describe ".use_database" do
Expand Down Expand Up @@ -91,6 +105,11 @@
db.name.should eql('couchrest_test_db')
end

it "should ignore nil values in database name" do
@class.connection[:suffix] = nil
db = @class.prepare_database('test')
db.name.should eql('couchrest_test')
end
end

describe "protected methods" do
Expand Down

0 comments on commit 814e38f

Please sign in to comment.