From 612454e00ee4ff48c4e03f8d8daf92c2a2d3ae3f Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 29 Mar 2011 21:16:44 -0500 Subject: [PATCH] Move sprockets initializers back to application --- railties/lib/rails/application.rb | 27 +++++++++++ .../lib/rails/application/configuration.rb | 5 +- railties/lib/rails/application/finisher.rb | 8 ++++ railties/lib/rails/engine.rb | 47 +------------------ railties/lib/rails/engine/configuration.rb | 5 +- 5 files changed, 41 insertions(+), 51 deletions(-) diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 94819820bc..032a505f26 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -137,6 +137,33 @@ module Rails @config ||= Application::Configuration.new(find_root_with_flag("config.ru", Dir.pwd)) end + def assets + @assets ||= build_asset_environment + end + + def build_asset_environment + return nil if !config.use_sprockets + require 'sprockets' + env = Sprockets::Environment.new(root.to_s) + env.static_root = root.join("public/assets") + env + end + + initializer :add_sprockets_paths do |app| + [ + "app/javascripts", + "app/stylesheets", + "vendor/plugins/*/app/javascripts", + "vendor/plugins/*/app/stylesheets", + "vendor/plugins/*/javascripts", + "vendor/plugins/*/stylesheets" + ].each do |pattern| + Dir[app.root.join(pattern)].each do |dir| + app.assets.paths << dir + end + end + end + protected def default_asset_path diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index c74bcbedf2..8dbc596357 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -9,7 +9,8 @@ module Rails :filter_parameters, :helpers_paths, :logger, :preload_frameworks, :reload_plugins, :secret_token, :serve_static_assets, :session_options, - :time_zone, :whiny_nils + :time_zone, :whiny_nils, + :use_sprockets, :compile_assets attr_writer :log_level @@ -28,6 +29,8 @@ module Rails @log_level = nil @middleware = app_middleware @generators = app_generators + @use_sprockets = false + @compile_assets = [] end def compiled_asset_path diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb index a45b61c99c..b57af48615 100644 --- a/railties/lib/rails/application/finisher.rb +++ b/railties/lib/rails/application/finisher.rb @@ -33,6 +33,14 @@ module Rails end end + initializer :add_sprockets_route do |app| + if config.use_sprockets + app.routes.append do + mount app.assets => "/assets" + end + end + end + initializer :build_middleware_stack do build_middleware_stack end diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 79d48889c9..8adeafaff5 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -433,56 +433,11 @@ module Rails end def routes - @routes ||= build_route_set + @routes ||= ActionDispatch::Routing::RouteSet.new @routes.append(&Proc.new) if block_given? @routes end - def build_route_set - routes = ActionDispatch::Routing::RouteSet.new - - engine = self - routes.append do - if engine.config.use_sprockets - routes.add_route(engine.assets, {:path_info => "/assets"}, {}, {}, nil, false) - end - end - - routes - end - - def self.default_sprockets_paths - [ - "app/javascripts", - "app/stylesheets", - "vendor/plugins/*/app/javascripts", - "vendor/plugins/*/app/stylesheets", - "vendor/plugins/*/javascripts", - "vendor/plugins/*/stylesheets" - ] - end - - def assets - @assets ||= build_asset_environment - end - - def build_asset_environment - return nil if !config.use_sprockets - - require 'sprockets' - - env = Sprockets::Environment.new(root.to_s) - env.static_root = root.join("public/assets") - - self.class.default_sprockets_paths.each do |pattern| - Dir[root.join(pattern)].each do |dir| - env.paths << dir - end - end - - env - end - def initializers initializers = [] railties.all { |r| initializers += r.initializers } diff --git a/railties/lib/rails/engine/configuration.rb b/railties/lib/rails/engine/configuration.rb index 1b4a712c75..4f458b0aee 100644 --- a/railties/lib/rails/engine/configuration.rb +++ b/railties/lib/rails/engine/configuration.rb @@ -5,15 +5,12 @@ module Rails class Configuration < ::Rails::Railtie::Configuration attr_reader :root attr_writer :middleware, :eager_load_paths, :autoload_once_paths, :autoload_paths - attr_accessor :plugins, :asset_path, :use_sprockets, :compile_assets + attr_accessor :plugins, :asset_path def initialize(root=nil) super() @root = root @generators = app_generators.dup - - @use_sprockets = false - @compile_assets = [] end # Returns the middleware stack for the engine.