Fixing issues when loading with rails

This commit is contained in:
Sam Lown 2011-04-30 00:34:12 +02:00
parent 85109b4b22
commit 814e38f75f
9 changed files with 50 additions and 64 deletions

View file

@ -1 +1 @@
1.1.0.beta4
1.1.0.beta5

View file

@ -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 = [
@ -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")

View file

@ -1,3 +1,8 @@
== 1.1.0.beta5
* Major changes:
* Database auto configuration, with connection options!
== 1.1.0.beta4
* Major changes:

View file

@ -4,8 +4,8 @@ module CouchRest
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

View file

@ -20,20 +20,13 @@ module CouchRest
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,

View file

@ -47,20 +47,22 @@ module CouchRest
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

View file

@ -1,11 +1,20 @@
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

View file

@ -56,48 +56,6 @@ describe CouchRest::Model::Base do
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

View file

@ -33,6 +33,20 @@ describe CouchRest::Model::Base do
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
@ -91,6 +105,11 @@ describe CouchRest::Model::Base do
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