Permalink
Browse files

Add lib to load paths when application is inherited to be able to loa…

…d lib code during configuration.
  • Loading branch information...
1 parent 53efdc2 commit 83a58e066090c5238822b712e0cfa8b72ddb02e5 @josevalim josevalim committed with mikel Jun 1, 2010
@@ -67,6 +67,7 @@ def inherited(base)
raise "You cannot have more than one Rails::Application" if Rails.application
super
Rails.application = base.instance
+ Rails.application.add_lib_to_load_paths!
ActiveSupport.run_load_hooks(:before_configuration, base.instance)
end
@@ -83,11 +84,21 @@ def method_missing(*args, &block)
delegate :middleware, :to => :config
+ def add_lib_to_load_paths!
+ path = config.root.join('lib').to_s
+ $LOAD_PATH.unshift(path) if File.exists?(path)
+ end
+
def require_environment!
environment = paths.config.environment.to_a.first
require environment if environment
end
+ def eager_load!
+ railties.all(&:eager_load!)
+ super
+ end
+
def routes
@routes ||= ActionDispatch::Routing::RouteSet.new
end
@@ -38,7 +38,7 @@ module Finisher
initializer :eager_load! do
if config.cache_classes && !$rails_rake_task
ActiveSupport.run_load_hooks(:before_eager_load, self)
- railties.all(&:eager_load!)
+ eager_load!
end
end
@@ -19,6 +19,23 @@ def setup
assert $:.include?("#{app_path}/app/models")
end
+ test "initializing an application adds lib path on inheritance hook" do
+ app_file "lib/foo.rb", <<-RUBY
+ module Foo; end
+ RUBY
+
+ add_to_config <<-RUBY
+ require "foo"
+ raise "Expected Foo to be defined" unless defined?(Foo)
+ RUBY
+
+ assert_nothing_raised do
+ require "#{app_path}/config/environment"
+ end
+
+ assert $:.include?("#{app_path}/lib")
+ end
+
test "initializing an application eager load any path under app" do
app_file "app/anything/foo.rb", <<-RUBY
module Foo; end

0 comments on commit 83a58e0

Please sign in to comment.