mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Added ability to set asset_path for engines
This commit is contained in:
parent
abeb0ff2cc
commit
a132229d7b
9 changed files with 141 additions and 13 deletions
|
@ -10,7 +10,7 @@ module ActionDispatch
|
|||
end
|
||||
|
||||
def initialize(env = {})
|
||||
env = Rails.application.env_defaults.merge(env) if defined?(Rails.application)
|
||||
env = Rails.application.env_config.merge(env) if defined?(Rails.application)
|
||||
super(DEFAULT_ENV.merge(env))
|
||||
|
||||
self.host = 'test.host'
|
||||
|
|
|
@ -727,6 +727,9 @@ module ActionView
|
|||
|
||||
source += ".#{ext}" if rewrite_extension?(source, dir, ext)
|
||||
source = "/#{dir}/#{source}" unless source[0] == ?/
|
||||
if controller.respond_to?(:env) && controller.env["action_dispatch.asset_path"]
|
||||
source = rewrite_asset_path(source, controller.env["action_dispatch.asset_path"])
|
||||
end
|
||||
source = rewrite_asset_path(source, config.asset_path)
|
||||
|
||||
has_request = controller.respond_to?(:request)
|
||||
|
|
|
@ -387,6 +387,15 @@ class AssetTagHelperTest < ActionView::TestCase
|
|||
assert_equal %(<img alt="Rails" src="#{expected_path}" />), image_tag("rails.png")
|
||||
end
|
||||
|
||||
def test_env_asset_path
|
||||
@controller.config.asset_path = "/assets%s"
|
||||
def @controller.env; @_env ||= {} end
|
||||
@controller.env["action_dispatch.asset_path"] = "/omg%s"
|
||||
|
||||
expected_path = "/assets/omg/images/rails.png"
|
||||
assert_equal %(<img alt="Rails" src="#{expected_path}" />), image_tag("rails.png")
|
||||
end
|
||||
|
||||
def test_proc_asset_id
|
||||
@controller.config.asset_path = Proc.new do |asset_path|
|
||||
"/assets.v12345#{asset_path}"
|
||||
|
@ -396,6 +405,20 @@ class AssetTagHelperTest < ActionView::TestCase
|
|||
assert_equal %(<img alt="Rails" src="#{expected_path}" />), image_tag("rails.png")
|
||||
end
|
||||
|
||||
def test_env_proc_asset_path
|
||||
@controller.config.asset_path = Proc.new do |asset_path|
|
||||
"/assets.v12345#{asset_path}"
|
||||
end
|
||||
|
||||
def @controller.env; @_env ||= {} end
|
||||
@controller.env["action_dispatch.asset_path"] = Proc.new do |asset_path|
|
||||
"/omg#{asset_path}"
|
||||
end
|
||||
|
||||
expected_path = "/assets.v12345/omg/images/rails.png"
|
||||
assert_equal %(<img alt="Rails" src="#{expected_path}" />), image_tag("rails.png")
|
||||
end
|
||||
|
||||
def test_image_tag_interpreting_email_cid_correctly
|
||||
# An inline image has no need for an alt tag to be automatically generated from the cid:
|
||||
assert_equal '<img src="cid:thi%25%25sis@acontentid" />', image_tag("cid:thi%25%25sis@acontentid")
|
||||
|
|
|
@ -124,16 +124,12 @@ module Rails
|
|||
|
||||
alias :build_middleware_stack :app
|
||||
|
||||
def call(env)
|
||||
env["action_dispatch.routes"] = routes
|
||||
app.call(env.reverse_merge!(env_defaults))
|
||||
end
|
||||
|
||||
def env_defaults
|
||||
@env_defaults ||= {
|
||||
def env_config
|
||||
@env_config ||= super.merge({
|
||||
"action_dispatch.parameter_filter" => config.filter_parameters,
|
||||
"action_dispatch.secret_token" => config.secret_token
|
||||
}
|
||||
"action_dispatch.secret_token" => config.secret_token,
|
||||
"action_dispatch.asset_path" => nil
|
||||
})
|
||||
end
|
||||
|
||||
def initializers
|
||||
|
|
|
@ -25,6 +25,18 @@ module Rails
|
|||
@middleware = app_middleware
|
||||
end
|
||||
|
||||
def asset_path=(value)
|
||||
action_mailer.asset_path = value if respond_to?(:action_mailer) && action_mailer
|
||||
action_controller.asset_path = value if respond_to?(:action_controller) && action_controller
|
||||
super(value)
|
||||
end
|
||||
|
||||
def asset_host=(value)
|
||||
action_mailer.asset_host = value if action_mailer
|
||||
action_controller.asset_host = value if action_controller
|
||||
super(value)
|
||||
end
|
||||
|
||||
def encoding=(value)
|
||||
@encoding = value
|
||||
if "ruby".encoding_aware?
|
||||
|
|
|
@ -147,6 +147,19 @@ module Rails
|
|||
#
|
||||
# Now, Engine will get only requests that were not handled by application.
|
||||
#
|
||||
# == Asset path
|
||||
#
|
||||
# When you use engine with its own public directory, you will probably want to copy or symlink it
|
||||
# to application's public directory. To simplify generating paths for assets, you can set asset_path
|
||||
# for an Engine:
|
||||
#
|
||||
# class MyEngine::Engine < Rails::Engine
|
||||
# config.asset_path = "/my_engine/%s"
|
||||
# end
|
||||
#
|
||||
# With such config, asset paths will be automatically modified inside Engine:
|
||||
# image_path("foo.jpg") #=> "/my_engine/images/foo.jpg"
|
||||
#
|
||||
class Engine < Railtie
|
||||
autoload :Configurable, "rails/engine/configurable"
|
||||
autoload :Configuration, "rails/engine/configuration"
|
||||
|
@ -219,8 +232,14 @@ module Rails
|
|||
end
|
||||
|
||||
def call(env)
|
||||
env["action_dispatch.routes"] = routes
|
||||
app.call(env)
|
||||
app.call(env.merge!(env_config))
|
||||
end
|
||||
|
||||
def env_config
|
||||
@env_config ||= {
|
||||
'action_dispatch.routes' => routes,
|
||||
'action_dispatch.asset_path' => config.asset_path
|
||||
}
|
||||
end
|
||||
|
||||
def routes
|
||||
|
|
|
@ -5,7 +5,7 @@ module Rails
|
|||
class Configuration < ::Rails::Railtie::Configuration
|
||||
attr_reader :root
|
||||
attr_writer :eager_load_paths, :autoload_once_paths, :autoload_paths
|
||||
attr_accessor :middleware, :plugins
|
||||
attr_accessor :middleware, :plugins, :asset_path
|
||||
|
||||
def initialize(root=nil)
|
||||
super()
|
||||
|
|
|
@ -260,5 +260,20 @@ module ApplicationTests
|
|||
get "/"
|
||||
assert_not_equal res, last_response.body
|
||||
end
|
||||
|
||||
test "config.asset_path is not passed through env" do
|
||||
make_basic_app do |app|
|
||||
app.config.asset_path = "/omg%s"
|
||||
end
|
||||
|
||||
class ::OmgController < ActionController::Base
|
||||
def index
|
||||
render :inline => "<%= image_path('foo.jpg') %>"
|
||||
end
|
||||
end
|
||||
|
||||
get "/"
|
||||
assert_equal "/omg/images/foo.jpg", last_response.body
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -199,5 +199,65 @@ module RailtiesTest
|
|||
|
||||
assert_equal Rails.application.routes, env['action_dispatch.routes']
|
||||
end
|
||||
|
||||
test "it allows to set asset_path" do
|
||||
@plugin.write "lib/bukkits.rb", <<-RUBY
|
||||
class Bukkits
|
||||
class Engine < ::Rails::Engine
|
||||
config.asset_path = "/bukkits%s"
|
||||
end
|
||||
end
|
||||
RUBY
|
||||
|
||||
|
||||
@plugin.write "config/routes.rb", <<-RUBY
|
||||
Bukkits::Engine.routes.draw do
|
||||
match "/foo" => "foo#index"
|
||||
end
|
||||
RUBY
|
||||
|
||||
@plugin.write "app/controllers/foo_controller.rb", <<-RUBY
|
||||
class FooController < ActionController::Base
|
||||
def index
|
||||
render :index
|
||||
end
|
||||
end
|
||||
RUBY
|
||||
|
||||
@plugin.write "app/views/foo/index.html.erb", <<-RUBY
|
||||
<%= compute_public_path("/foo", "") %>
|
||||
<%= image_path("foo.png") %>
|
||||
<%= javascript_include_tag("foo") %>
|
||||
<%= stylesheet_link_tag("foo") %>
|
||||
RUBY
|
||||
|
||||
|
||||
app_file "app/controllers/bar_controller.rb", <<-RUBY
|
||||
class BarController < ActionController::Base
|
||||
def index
|
||||
render :index
|
||||
end
|
||||
end
|
||||
RUBY
|
||||
|
||||
app_file "app/views/bar/index.html.erb", <<-RUBY
|
||||
<%= compute_public_path("/foo", "") %>
|
||||
RUBY
|
||||
|
||||
add_to_config 'config.asset_path = "/omg%s"'
|
||||
|
||||
boot_rails
|
||||
|
||||
env = Rack::MockRequest.env_for("/foo")
|
||||
response = Bukkits::Engine.call(env)
|
||||
stripped_body = response[2].body.split("\n").map(&:strip).join("\n")
|
||||
|
||||
expected = "/omg/bukkits/foo\n" +
|
||||
"/omg/bukkits/images/foo.png\n" +
|
||||
"<script src=\"/omg/bukkits/javascripts/foo.js\" type=\"text/javascript\"></script>\n" +
|
||||
"<link href=\"/omg/bukkits/stylesheets/foo.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />"
|
||||
assert_equal expected, stripped_body
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue