Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added ability to set asset_path for engines

  • Loading branch information...
commit a132229d7b4382d9ffe8847fa58f469cb8f2ecfc 1 parent abeb0ff
Piotr Sarnacki drogus authored
2  actionpack/lib/action_dispatch/testing/test_request.rb
View
@@ -10,7 +10,7 @@ def self.new(env = {})
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'
3  actionpack/lib/action_view/helpers/asset_tag_helper.rb
View
@@ -727,6 +727,9 @@ def compute_public_path(source, dir, ext = nil, include_host = true)
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)
23 actionpack/test/template/asset_tag_helper_test.rb
View
@@ -387,6 +387,15 @@ def test_string_asset_id
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 @@ def test_proc_asset_id
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")
14 railties/lib/rails/application.rb
View
@@ -124,16 +124,12 @@ def load_console(sandbox=false)
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
12 railties/lib/rails/application/configuration.rb
View
@@ -25,6 +25,18 @@ def initialize(*)
@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?
23 railties/lib/rails/engine.rb
View
@@ -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 @@ def endpoint
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
2  railties/lib/rails/engine/configuration.rb
View
@@ -5,7 +5,7 @@ class Engine
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()
15 railties/test/application/configuration_test.rb
View
@@ -260,5 +260,20 @@ def index
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
60 railties/test/railties/engine_test.rb
View
@@ -199,5 +199,65 @@ class Engine < ::Rails::Engine
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
Please sign in to comment.
Something went wrong with that request. Please try again.