Skip to content

Commit

Permalink
Engine's assets are now served with ActionDispatch::Static
Browse files Browse the repository at this point in the history
  • Loading branch information
drogus committed Sep 3, 2010
1 parent 401cd97 commit 937f419
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
18 changes: 17 additions & 1 deletion railties/lib/rails/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions railties/lib/rails/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions railties/test/railties/engine_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.