Permalink
Browse files

Set proper engine's asset directories when assets are served from eng…

…ine.

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
  • Loading branch information...
1 parent 8e955c5 commit a462a76bd323ef0087ead0bf0d1792a95655975c @drogus drogus committed with vijaydev Dec 15, 2010
Showing with 72 additions and 0 deletions.
  1. +8 −0 actionpack/lib/action_controller/railties/paths.rb
  2. +64 −0 railties/test/railties/engine_test.rb
View
8 actionpack/lib/action_controller/railties/paths.rb
@@ -16,6 +16,14 @@ def self.with(app)
if klass.superclass == ActionController::Base && ActionController::Base.include_all_helpers
klass.helper :all
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
View
64 railties/test/railties/engine_test.rb
@@ -721,5 +721,69 @@ class Engine < ::Rails::Engine
engine_path = File.join(@plugin.path, '..', engine_dir)
assert_equal Bukkits::Engine.instance, Rails::Engine.find(engine_path)
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

0 comments on commit a462a76

Please sign in to comment.