mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Set proper engine's asset directories when assets are served from engine.
When using stylesheet_link_tag(:all) or javascript_include_tag(:all), assets directories are searched for css or js files. When config.serve_static_assets is set to true, those files can be served directly from engine's directories. That's why assets paths should be set individually for controllers inside engine if we want to serve static assets with ActionDispatch::Static
This commit is contained in:
parent
5e3b853aee
commit
0e5ee9af48
2 changed files with 72 additions and 0 deletions
|
@ -16,6 +16,14 @@ module ActionController
|
||||||
if klass.superclass == ActionController::Base && ActionController::Base.include_all_helpers
|
if klass.superclass == ActionController::Base && ActionController::Base.include_all_helpers
|
||||||
klass.helper :all
|
klass.helper :all
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if app.config.serve_static_assets && namespace
|
||||||
|
paths = namespace._railtie.config.paths
|
||||||
|
|
||||||
|
klass.config.assets_dir = paths["public"].first
|
||||||
|
klass.config.javascripts_dir = paths["public/javascripts"].first
|
||||||
|
klass.config.stylesheets_dir = paths["public/stylesheets"].first
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -721,5 +721,69 @@ module RailtiesTest
|
||||||
engine_path = File.join(@plugin.path, '..', engine_dir)
|
engine_path = File.join(@plugin.path, '..', engine_dir)
|
||||||
assert_equal Bukkits::Engine.instance, Rails::Engine.find(engine_path)
|
assert_equal Bukkits::Engine.instance, Rails::Engine.find(engine_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "ensure that engine properly sets assets directories" do
|
||||||
|
add_to_config("config.action_dispatch.show_exceptions = false")
|
||||||
|
add_to_config("config.serve_static_assets = true")
|
||||||
|
|
||||||
|
@plugin.write "lib/bukkits.rb", <<-RUBY
|
||||||
|
module Bukkits
|
||||||
|
class Engine < ::Rails::Engine
|
||||||
|
isolate_namespace Bukkits
|
||||||
|
end
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
@plugin.write "public/stylesheets/foo.css", ""
|
||||||
|
@plugin.write "public/javascripts/foo.js", ""
|
||||||
|
|
||||||
|
@plugin.write "app/views/layouts/bukkits/application.html.erb", <<-RUBY
|
||||||
|
<%= stylesheet_link_tag :all %>
|
||||||
|
<%= javascript_include_tag :all %>
|
||||||
|
<%= yield %>
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
@plugin.write "app/controllers/bukkits/home_controller.rb", <<-RUBY
|
||||||
|
module Bukkits
|
||||||
|
class HomeController < ActionController::Base
|
||||||
|
def index
|
||||||
|
render :text => "Good morning!", :layout => "bukkits/application"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
@plugin.write "config/routes.rb", <<-RUBY
|
||||||
|
Bukkits::Engine.routes.draw do
|
||||||
|
match "/home" => "home#index"
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
app_file "config/routes.rb", <<-RUBY
|
||||||
|
Rails.application.routes.draw do
|
||||||
|
mount Bukkits::Engine => "/bukkits"
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
require 'rack/test'
|
||||||
|
extend Rack::Test::Methods
|
||||||
|
|
||||||
|
boot_rails
|
||||||
|
|
||||||
|
require "#{rails_root}/config/environment"
|
||||||
|
|
||||||
|
assert_equal File.join(@plugin.path, "public"), Bukkits::HomeController.assets_dir
|
||||||
|
assert_equal File.join(@plugin.path, "public/stylesheets"), Bukkits::HomeController.stylesheets_dir
|
||||||
|
assert_equal File.join(@plugin.path, "public/javascripts"), Bukkits::HomeController.javascripts_dir
|
||||||
|
|
||||||
|
assert_equal File.join(app_path, "public"), ActionController::Base.assets_dir
|
||||||
|
assert_equal File.join(app_path, "public/stylesheets"), ActionController::Base.stylesheets_dir
|
||||||
|
assert_equal File.join(app_path, "public/javascripts"), ActionController::Base.javascripts_dir
|
||||||
|
|
||||||
|
get "/bukkits/home"
|
||||||
|
|
||||||
|
assert_match %r{bukkits/stylesheets/foo.css}, last_response.body
|
||||||
|
assert_match %r{bukkits/javascripts/foo.js}, last_response.body
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue