Permalink
Browse files

Engine's assets are now served with ActionDispatch::Static

  • Loading branch information...
drogus committed Jul 29, 2010
1 parent 401cd97 commit 937f41919c6033272b40b147f7ac5e888207ac89
Showing with 46 additions and 6 deletions.
  1. +17 −1 railties/lib/rails/application.rb
  2. +4 −5 railties/lib/rails/engine.rb
  3. +25 −0 railties/test/railties/engine_test.rb
@@ -145,9 +145,25 @@ def config
protected
+ def static_paths
+ @static_paths ||= begin
+ static_paths = ActiveSupport::OrderedHash.new
+ static_paths["/"] = paths.public.to_a.first
+
+ railties.all do |railtie|
+ if railtie.config.respond_to?(:asset_path) && railtie.config.asset_path
+ public_path = railtie.config.paths.public.to_a.first
+ static_paths[railtie.config.asset_path % ""] = public_path if File.exists?(public_path)
+ end
+ end
+
+ static_paths
+ end
+ end
+
def default_middleware_stack
ActionDispatch::MiddlewareStack.new.tap do |middleware|
- middleware.use ::ActionDispatch::Static, paths.public.to_a.first if config.serve_static_assets
+ middleware.use ::ActionDispatch::Static, static_paths if config.serve_static_assets
middleware.use ::Rack::Lock if !config.allow_concurrency
middleware.use ::Rack::Runtime
middleware.use ::Rails::Rack::Logger
@@ -256,11 +256,7 @@ def initializers
end
def config
- @config ||= begin
- config = Engine::Configuration.new(find_root_with_flag("lib"))
- config.asset_path = "/#{engine_name}%s" if File.exists?(config.paths.public.to_a.first)
- config
- end
+ @config ||= Engine::Configuration.new(find_root_with_flag("lib"))
end
# Add configured load paths to ruby load paths and remove duplicates.
@@ -335,6 +331,9 @@ def config
require environment if environment
end
+ initializer :default_asset_path do
+ config.asset_path = "/#{engine_name}%s" unless config.asset_path
+ end
protected
def find_root_with_flag(flag, default=nil)
root_path = self.class.called_from
@@ -273,5 +273,30 @@ def index
"<link href=\"/omg/bukkits/stylesheets/foo.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />"
assert_equal expected, stripped_body
end
+
+ test "engine's files are served via ActionDispatch::Static" do
+ add_to_config "config.serve_static_assets = true"
+
+ @plugin.write "lib/bukkits.rb", <<-RUBY
+ class Bukkits
+ class Engine < ::Rails::Engine
+ engine_name :bukkits
+ end
+ end
+ RUBY
+
+ @plugin.write "public/bukkits.html", "/bukkits/bukkits.html"
+ app_file "public/app.html", "/app.html"
+
+ boot_rails
+
+ env = Rack::MockRequest.env_for("/app.html")
+ response = Rails.application.call(env)
+ assert_equal response[2].path, File.join(app_path, "public/app.html")
+
+ env = Rack::MockRequest.env_for("/bukkits/bukkits.html")
+ response = Rails.application.call(env)
+ assert_equal response[2].path, File.join(@plugin.path, "public/bukkits.html")
+ end
end
end

0 comments on commit 937f419

Please sign in to comment.