mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add view paths to Engine setup.
This commit is contained in:
parent
7fcf8590e7
commit
02c5137ead
7 changed files with 62 additions and 51 deletions
|
@ -21,8 +21,7 @@ module ActionMailer
|
|||
|
||||
initializer "action_mailer.view_paths" do |app|
|
||||
# TODO: this should be combined with the logic for default config.action_mailer.view_paths
|
||||
view_path = ActionView::PathSet.type_cast(app.config.view_path, app.config.cache_classes)
|
||||
ActionMailer::Base.template_root = view_path if ActionMailer::Base.view_paths.blank?
|
||||
ActionMailer::Base.template_root = [] if ActionMailer::Base.view_paths.blank?
|
||||
end
|
||||
end
|
||||
end
|
|
@ -29,8 +29,7 @@ module ActionController
|
|||
# set to use Configuration#view_path.
|
||||
initializer "action_controller.initialize_framework_views" do |app|
|
||||
# TODO: this should be combined with the logic for default config.action_controller.view_paths
|
||||
view_path = ActionView::PathSet.type_cast(app.config.view_path, app.config.cache_classes)
|
||||
ActionController::Base.view_paths = view_path if ActionController::Base.view_paths.blank?
|
||||
ActionController::Base.view_paths = [] if ActionController::Base.view_paths.blank?
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
require "fileutils"
|
||||
require 'fileutils'
|
||||
|
||||
module Rails
|
||||
class Application < Engine
|
||||
|
@ -6,7 +6,7 @@ module Rails
|
|||
|
||||
class << self
|
||||
alias :configure :class_eval
|
||||
delegate :initialize!, :load_tasks, :load_generators, :to => :instance
|
||||
delegate :initialize!, :load_tasks, :load_generators, :root, :to => :instance
|
||||
|
||||
private :new
|
||||
def instance
|
||||
|
@ -14,11 +14,11 @@ module Rails
|
|||
end
|
||||
|
||||
def config
|
||||
@config ||= Configuration.new(root)
|
||||
@config ||= Configuration.new(original_root)
|
||||
end
|
||||
|
||||
def root
|
||||
@root ||= find_root_with_file_flag("config.ru", Dir.pwd)
|
||||
def original_root
|
||||
@original_root ||= find_root_with_file_flag("config.ru", Dir.pwd)
|
||||
end
|
||||
|
||||
def inherited(base)
|
||||
|
@ -122,16 +122,16 @@ module Rails
|
|||
app.call(env)
|
||||
end
|
||||
|
||||
initializer :build_middleware_stack, :after => :load_application_initializers do
|
||||
app
|
||||
end
|
||||
|
||||
initializer :add_builtin_route do |app|
|
||||
if Rails.env.development?
|
||||
app.route_configuration_files << File.join(RAILTIES_PATH, 'builtin', 'routes.rb')
|
||||
end
|
||||
end
|
||||
|
||||
initializer :build_middleware_stack, :after => :load_application_initializers do
|
||||
app
|
||||
end
|
||||
|
||||
# Fires the user-supplied after_initialize block (Configuration#after_initialize)
|
||||
initializer :after_initialize, :after => :build_middleware_stack do
|
||||
config.after_initialize_blocks.each do |block|
|
||||
|
|
|
@ -70,7 +70,8 @@ module Rails
|
|||
end
|
||||
|
||||
class Engine::Configuration < Railtie::Configuration
|
||||
attr_reader :root
|
||||
attr_reader :root
|
||||
attr_accessor :eager_load_paths, :load_once_paths, :load_paths
|
||||
|
||||
def initialize(root)
|
||||
@root = root
|
||||
|
@ -79,14 +80,15 @@ module Rails
|
|||
|
||||
def paths
|
||||
@paths ||= begin
|
||||
paths = Rails::Application::Root.new(root)
|
||||
paths = Rails::Application::Root.new(@root)
|
||||
paths.app "app", :load_path => true
|
||||
paths.app_glob "app/*", :load_path => true, :eager_load => true
|
||||
paths.app.controllers "app/controllers"
|
||||
paths.app.controllers "app/controllers", :eager_load => true
|
||||
paths.app.metals "app/metal"
|
||||
paths.app.views "app/views"
|
||||
paths.lib "lib", :load_path => true
|
||||
paths.config "config"
|
||||
paths.config.environment "config/environments/#{Rails.env}.rb"
|
||||
paths.config.environments "config/environments", :glob => "#{Rails.env}.rb"
|
||||
paths.config.initializers "config/initializers"
|
||||
paths.config.locales "config/locales"
|
||||
|
@ -95,6 +97,10 @@ module Rails
|
|||
end
|
||||
end
|
||||
|
||||
def root=(value)
|
||||
@root = paths.path = Pathname.new(value).expand_path
|
||||
end
|
||||
|
||||
def eager_load_paths
|
||||
@eager_load_paths ||= paths.eager_load
|
||||
end
|
||||
|
@ -106,6 +112,10 @@ module Rails
|
|||
def load_paths
|
||||
@load_paths ||= paths.load_paths
|
||||
end
|
||||
|
||||
def controller_paths
|
||||
paths.app.controllers.to_a.uniq
|
||||
end
|
||||
end
|
||||
|
||||
class Configuration < Engine::Configuration
|
||||
|
@ -115,9 +125,7 @@ module Rails
|
|||
:preload_frameworks, :reload_plugins, :serve_static_assets,
|
||||
:time_zone, :whiny_nils
|
||||
|
||||
attr_writer :cache_store, :controller_paths,
|
||||
:database_configuration_file,
|
||||
:i18n, :log_level, :log_path
|
||||
attr_writer :cache_store, :controller_paths, :i18n, :log_level
|
||||
|
||||
def initialize(*)
|
||||
super
|
||||
|
@ -134,7 +142,7 @@ module Rails
|
|||
def paths
|
||||
@paths ||= begin
|
||||
paths = super
|
||||
paths.builtin_controller builtin_directories, :eager_load => true
|
||||
paths.app.controllers << builtin_directories
|
||||
paths.config.database "config/database.yml"
|
||||
paths.log "log/#{Rails.env}.log"
|
||||
paths.tmp "tmp"
|
||||
|
@ -144,7 +152,7 @@ module Rails
|
|||
|
||||
if File.exists?("#{root}/test/mocks/#{Rails.env}")
|
||||
ActiveSupport::Deprecation.warn "\"RAILS_ROOT/test/mocks/#{Rails.env}\" won't be added " <<
|
||||
"automatically to load paths anymore in next releases."
|
||||
"automatically to load paths anymore in future releases"
|
||||
paths.mocks_path "test/mocks/#{Rails.env}", :load_path => true
|
||||
end
|
||||
|
||||
|
@ -230,18 +238,6 @@ module Rails
|
|||
paths.config.log.to_a.first
|
||||
end
|
||||
|
||||
|
||||
|
||||
# TODO Router needs this, but this wouldn't work with engines.
|
||||
# There is a high chance of plugins routes to be broken.
|
||||
def controller_paths
|
||||
@controller_paths ||= begin
|
||||
paths = [File.join(root, 'app', 'controllers')]
|
||||
paths.concat builtin_directories
|
||||
paths
|
||||
end
|
||||
end
|
||||
|
||||
def cache_store
|
||||
@cache_store ||= begin
|
||||
if File.exist?("#{root}/tmp/cache/")
|
||||
|
|
|
@ -1,18 +1,19 @@
|
|||
require 'active_support/core_ext/module/delegation'
|
||||
|
||||
module Rails
|
||||
# TODO Move I18n and views path setup
|
||||
# TODO Move I18n here
|
||||
# TODO Set routes namespaces
|
||||
class Engine < Railtie
|
||||
|
||||
class << self
|
||||
attr_accessor :called_from
|
||||
|
||||
def root
|
||||
@root ||= find_root_with_file_flag("lib")
|
||||
def original_root
|
||||
@original_root ||= find_root_with_file_flag("lib")
|
||||
end
|
||||
|
||||
def config
|
||||
@config ||= Configuration.new(root)
|
||||
@config ||= Configuration.new(original_root)
|
||||
end
|
||||
|
||||
def inherited(base)
|
||||
|
@ -20,6 +21,7 @@ module Rails
|
|||
call_stack = caller.map { |p| p.split(':').first }
|
||||
File.dirname(call_stack.detect { |p| p !~ %r[railties/lib/rails|rack/lib/rack] })
|
||||
end
|
||||
|
||||
super
|
||||
end
|
||||
|
||||
|
@ -33,7 +35,7 @@ module Rails
|
|||
root_path = parent != root_path && parent
|
||||
end
|
||||
|
||||
root = File.exist?("#{root_path}/flag") ? root_path : default
|
||||
root = File.exist?("#{root_path}/#{flag}") ? root_path : default
|
||||
|
||||
raise "Could not find root path for #{self}" unless root
|
||||
|
||||
|
@ -43,8 +45,8 @@ module Rails
|
|||
end
|
||||
end
|
||||
|
||||
delegate :root, :config, :to => :'self.class'
|
||||
delegate :middleware, :to => :config
|
||||
delegate :config, :to => :'self.class'
|
||||
delegate :middleware, :root, :to => :config
|
||||
|
||||
# Add configured load paths to ruby load paths and remove duplicates.
|
||||
initializer :set_load_path, :before => :container do
|
||||
|
@ -57,10 +59,11 @@ module Rails
|
|||
initializer :set_autoload_paths, :before => :container do
|
||||
require 'active_support/dependencies'
|
||||
|
||||
ActiveSupport::Dependencies.load_paths = expand_load_path(config.load_paths)
|
||||
ActiveSupport::Dependencies.load_paths = expand_load_path(config.load_paths)
|
||||
ActiveSupport::Dependencies.load_once_paths = expand_load_path(config.load_once_paths)
|
||||
|
||||
extra = ActiveSupport::Dependencies.load_once_paths - ActiveSupport::Dependencies.load_paths
|
||||
extra = ActiveSupport::Dependencies.load_once_paths -
|
||||
ActiveSupport::Dependencies.load_paths
|
||||
|
||||
unless extra.empty?
|
||||
abort <<-end_error
|
||||
|
@ -73,15 +76,24 @@ module Rails
|
|||
config.load_once_paths.freeze
|
||||
end
|
||||
|
||||
initializer :load_application_initializers do
|
||||
Dir["#{root}/config/initializers/**/*.rb"].sort.each do |initializer|
|
||||
load(initializer)
|
||||
end
|
||||
# Routing must be initialized after plugins to allow the former to extend the routes
|
||||
initializer :add_routing_files do |app|
|
||||
routes = select_existing(config.paths.config.routes)
|
||||
app.route_configuration_files.concat(routes)
|
||||
end
|
||||
|
||||
# Routing must be initialized after plugins to allow the former to extend the routes
|
||||
initializer :initialize_routing do |app|
|
||||
app.route_configuration_files.concat(config.paths.config.routes.to_a)
|
||||
initializer :add_view_paths do
|
||||
views = select_existing(config.paths.app.views)
|
||||
ActionController::Base.view_paths.concat(views) if defined? ActionController
|
||||
ActionMailer::Base.view_paths.concat(views) if defined? ActionMailer
|
||||
end
|
||||
|
||||
initializer :load_application_initializers do
|
||||
select_existing(config.paths.config.initializers).each do |initializers|
|
||||
Dir["#{initializers}/**/*.rb"].sort.each do |initializer|
|
||||
load(initializer)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Eager load application classes
|
||||
|
@ -100,6 +112,10 @@ module Rails
|
|||
|
||||
private
|
||||
|
||||
def select_existing(paths)
|
||||
paths.to_a.select { |path| File.exists?(path) }.uniq
|
||||
end
|
||||
|
||||
def expand_load_path(load_paths)
|
||||
load_paths.map { |path| Dir.glob(path.to_s) }.flatten.uniq
|
||||
end
|
||||
|
|
|
@ -14,6 +14,7 @@ module Rails
|
|||
@@plugins << base unless abstract_railtie?(base)
|
||||
end
|
||||
|
||||
# This should be called railtie_name and engine_name
|
||||
def plugin_name(plugin_name = nil)
|
||||
@plugin_name ||= name.demodulize.underscore
|
||||
@plugin_name = plugin_name if plugin_name
|
||||
|
|
|
@ -34,9 +34,9 @@ module ApplicationTests
|
|||
add_to_config <<-RUBY
|
||||
config.root = '#{new_app}'
|
||||
RUBY
|
||||
|
||||
|
||||
use_frameworks []
|
||||
|
||||
|
||||
require "#{app_path}/config/environment"
|
||||
assert_equal Pathname.new(new_app), Rails.application.root
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue