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
|
end
|
||||||
|
|
||||||
def initialize(env = {})
|
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))
|
super(DEFAULT_ENV.merge(env))
|
||||||
|
|
||||||
self.host = 'test.host'
|
self.host = 'test.host'
|
||||||
|
|
|
@ -727,6 +727,9 @@ module ActionView
|
||||||
|
|
||||||
source += ".#{ext}" if rewrite_extension?(source, dir, ext)
|
source += ".#{ext}" if rewrite_extension?(source, dir, ext)
|
||||||
source = "/#{dir}/#{source}" unless source[0] == ?/
|
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)
|
source = rewrite_asset_path(source, config.asset_path)
|
||||||
|
|
||||||
has_request = controller.respond_to?(:request)
|
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")
|
assert_equal %(<img alt="Rails" src="#{expected_path}" />), image_tag("rails.png")
|
||||||
end
|
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
|
def test_proc_asset_id
|
||||||
@controller.config.asset_path = Proc.new do |asset_path|
|
@controller.config.asset_path = Proc.new do |asset_path|
|
||||||
"/assets.v12345#{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")
|
assert_equal %(<img alt="Rails" src="#{expected_path}" />), image_tag("rails.png")
|
||||||
end
|
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
|
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:
|
# 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")
|
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
|
alias :build_middleware_stack :app
|
||||||
|
|
||||||
def call(env)
|
def env_config
|
||||||
env["action_dispatch.routes"] = routes
|
@env_config ||= super.merge({
|
||||||
app.call(env.reverse_merge!(env_defaults))
|
|
||||||
end
|
|
||||||
|
|
||||||
def env_defaults
|
|
||||||
@env_defaults ||= {
|
|
||||||
"action_dispatch.parameter_filter" => config.filter_parameters,
|
"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
|
end
|
||||||
|
|
||||||
def initializers
|
def initializers
|
||||||
|
|
|
@ -25,6 +25,18 @@ module Rails
|
||||||
@middleware = app_middleware
|
@middleware = app_middleware
|
||||||
end
|
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)
|
def encoding=(value)
|
||||||
@encoding = value
|
@encoding = value
|
||||||
if "ruby".encoding_aware?
|
if "ruby".encoding_aware?
|
||||||
|
|
|
@ -147,6 +147,19 @@ module Rails
|
||||||
#
|
#
|
||||||
# Now, Engine will get only requests that were not handled by application.
|
# 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
|
class Engine < Railtie
|
||||||
autoload :Configurable, "rails/engine/configurable"
|
autoload :Configurable, "rails/engine/configurable"
|
||||||
autoload :Configuration, "rails/engine/configuration"
|
autoload :Configuration, "rails/engine/configuration"
|
||||||
|
@ -219,8 +232,14 @@ module Rails
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
env["action_dispatch.routes"] = routes
|
app.call(env.merge!(env_config))
|
||||||
app.call(env)
|
end
|
||||||
|
|
||||||
|
def env_config
|
||||||
|
@env_config ||= {
|
||||||
|
'action_dispatch.routes' => routes,
|
||||||
|
'action_dispatch.asset_path' => config.asset_path
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def routes
|
def routes
|
||||||
|
|
|
@ -5,7 +5,7 @@ module Rails
|
||||||
class Configuration < ::Rails::Railtie::Configuration
|
class Configuration < ::Rails::Railtie::Configuration
|
||||||
attr_reader :root
|
attr_reader :root
|
||||||
attr_writer :eager_load_paths, :autoload_once_paths, :autoload_paths
|
attr_writer :eager_load_paths, :autoload_once_paths, :autoload_paths
|
||||||
attr_accessor :middleware, :plugins
|
attr_accessor :middleware, :plugins, :asset_path
|
||||||
|
|
||||||
def initialize(root=nil)
|
def initialize(root=nil)
|
||||||
super()
|
super()
|
||||||
|
|
|
@ -260,5 +260,20 @@ module ApplicationTests
|
||||||
get "/"
|
get "/"
|
||||||
assert_not_equal res, last_response.body
|
assert_not_equal res, last_response.body
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -199,5 +199,65 @@ module RailtiesTest
|
||||||
|
|
||||||
assert_equal Rails.application.routes, env['action_dispatch.routes']
|
assert_equal Rails.application.routes, env['action_dispatch.routes']
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue