This is a Padrino project. For some reason I am getting my second database loaded in the development environment, but not the test environment. This is probably very specific to my project, and it's unlikely anyone can help... but I'm really stuck! :)
mongoid.yml
defaults: &defaults
host: localhost
port: 27017
max_retries_on_connection_failure: 2
databases:
seeds:
database: seeds_db
host: localhost
port: 27018
development:
<<: *defaults
database: db_development
test:
<<: *defaults
database: db_test
loaded with:
Mongoid.load!(File.dirname(__FILE__) + '/mongoid.yml')
My rspec_helper:
PADRINO_ENV = 'test' unless defined?(PADRINO_ENV)
require File.dirname(__FILE__) +开发者_StackOverflow "/../config/boot.rb"
RSpec.configure do |conf|
conf.include Rack::Test::Methods
end
My boot.rb:
PADRINO_ENV = ENV["PADRINO_ENV"] ||= ENV["RACK_ENV"] ||= "development" unless defined?(PADRINO_ENV)
PADRINO_ROOT = File.expand_path('../..', __FILE__) unless defined?(PADRINO_ROOT)
require 'rubygems' unless defined?(Gem)
require 'bundler'
Bundler.setup
Bundler.require(PADRINO_ENV, :default)
require 'redis' unless defined?(Redis)
require 'padrino' unless defined?(Padrino)
require 'mongoid' unless defined?(Mongoid)
Dir.glob(File.join(PADRINO_ROOT, 'config/initializers', "*.rb")){ |file| require file}
Dir.glob(File.join(PADRINO_ROOT, 'config/constants', "*.rb")){ |file| require file}
Padrino.before_load do
end
Padrino.after_load do
Card.load!
Position.load!
end
Padrino.load!
But this is not working after I ported my project to Padrino. I can't find anything in the documentation that explains why.
Trouble that Rspec use ENV["RACK_ENV"], instead PADRINO_ENV. So in the boot.rb add line to define RACK_ENV to PADRINO_ENV:
PADRINO_ENV = ENV["PADRINO_ENV"] ||= ENV["RACK_ENV"] ||= "development" unless defined?(PADRINO_ENV)
ENV["RACK_ENV"] = PADRINO_ENV
I found someone loading their mongoid.yml this way on github, and this seems to work:
config_file = Padrino.root("config", "mongoid.yml")
if File.exists?(config_file)
settings = YAML.load(ERB.new(File.read(config_file)).result)[Padrino.env.to_s]
::Mongoid.from_hash(settings) if settings.present?
end
精彩评论