Skip to content

Commit fbc9d0f

Browse files
committed
Simplify helpers handling. Ensure Metal can run AC hooks.
1 parent 5b94e73 commit fbc9d0f

File tree

6 files changed

+65
-37
lines changed

6 files changed

+65
-37
lines changed

actionpack/lib/action_controller/metal/helpers.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ module ActionController
5252
module Helpers
5353
extend ActiveSupport::Concern
5454

55+
class << self; attr_accessor :helpers_path; end
5556
include AbstractController::Helpers
5657

5758
included do

actionpack/lib/action_controller/railtie.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,41 @@
33
require "action_dispatch/railtie"
44
require "action_view/railtie"
55
require "abstract_controller/railties/routes_helpers"
6-
require "action_controller/railties/paths"
6+
require "action_controller/railties/helpers"
77

88
module ActionController
99
class Railtie < Rails::Railtie #:nodoc:
1010
config.action_controller = ActiveSupport::OrderedOptions.new
1111

12-
initializer "action_controller.logger" do
13-
ActiveSupport.on_load(:action_controller) { self.logger ||= Rails.logger }
14-
end
15-
16-
initializer "action_controller.initialize_framework_caches" do
17-
ActiveSupport.on_load(:action_controller) { self.cache_store ||= Rails.cache if respond_to?(:cache_store) }
18-
end
19-
2012
initializer "action_controller.assets_config", :group => :all do |app|
2113
app.config.action_controller.assets_dir ||= app.config.paths["public"].first
2214
end
2315

16+
initializer "action_controller.set_helpers_path" do |app|
17+
ActionController::Helpers.helpers_path = app.helpers_paths
18+
end
19+
2420
initializer "action_controller.set_configs" do |app|
2521
paths = app.config.paths
2622
options = app.config.action_controller
2723

24+
options.logger ||= Rails.logger
25+
options.cache_store ||= Rails.cache
26+
2827
options.javascripts_dir ||= paths["public/javascripts"].first
2928
options.stylesheets_dir ||= paths["public/stylesheets"].first
3029
options.page_cache_directory ||= paths["public"].first
3130

32-
# make sure readers methods get compiled
31+
# Ensure readers methods get compiled
3332
options.asset_path ||= app.config.asset_path
3433
options.asset_host ||= app.config.asset_host
3534
options.relative_url_root ||= app.config.relative_url_root
3635

3736
ActiveSupport.on_load(:action_controller) do
3837
include app.routes.mounted_helpers
3938
extend ::AbstractController::Railties::RoutesHelpers.with(app.routes)
40-
extend ::ActionController::Railties::Paths.with(app) if respond_to?(:helpers_path)
39+
extend ::ActionController::Railties::Helpers
40+
4141
options.each do |k,v|
4242
k = "#{k}="
4343
if respond_to?(k)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module ActionController
2+
module Railties
3+
module Helpers
4+
def inherited(klass)
5+
super
6+
return unless klass.respond_to?(:helpers_path=)
7+
8+
if namespace = klass.parents.detect { |m| m.respond_to?(:railtie_helpers_paths) }
9+
paths = namespace.railtie_helpers_paths
10+
else
11+
paths = ActionController::Helpers.helpers_path
12+
end
13+
14+
klass.helpers_path = paths
15+
16+
if klass.superclass == ActionController::Base && ActionController::Base.include_all_helpers
17+
klass.helper :all
18+
end
19+
end
20+
end
21+
end
22+
end

actionpack/lib/action_controller/railties/paths.rb

Lines changed: 0 additions & 24 deletions
This file was deleted.

railties/lib/rails/engine.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ def load_seed
561561
initializer :add_view_paths do
562562
views = paths["app/views"].existent
563563
unless views.empty?
564-
ActiveSupport.on_load(:action_controller){ prepend_view_path(views) }
564+
ActiveSupport.on_load(:action_controller){ prepend_view_path(views) if respond_to?(:prepend_view_path) }
565565
ActiveSupport.on_load(:action_mailer){ prepend_view_path(views) }
566566
end
567567
end

railties/test/application/loading_test.rb

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class Post < ActiveRecord::Base
9595
assert_equal [ActiveRecord::SchemaMigration], ActiveRecord::Base.descendants
9696
end
9797

98-
test "initialize_cant_be_called_twice" do
98+
test "initialize cant be called twice" do
9999
require "#{app_path}/config/environment"
100100
assert_raise(RuntimeError) { ::AppTemplate::Application.initialize! }
101101
end
@@ -256,6 +256,35 @@ def change
256256
assert_equal "BODY", last_response.body
257257
end
258258

259+
test "AC load hooks can be used with metal" do
260+
app_file "app/controllers/omg_controller.rb", <<-RUBY
261+
begin
262+
class OmgController < ActionController::Metal
263+
ActiveSupport.run_load_hooks(:action_controller, self)
264+
def show
265+
self.response_body = ["OK"]
266+
end
267+
end
268+
rescue => e
269+
puts "Error loading metal: \#{e.class} \#{e.message}"
270+
end
271+
RUBY
272+
273+
app_file "config/routes.rb", <<-RUBY
274+
AppTemplate::Application.routes.draw do
275+
match "/:controller(/:action)"
276+
end
277+
RUBY
278+
279+
require "#{rails_root}/config/environment"
280+
281+
require 'rack/test'
282+
extend Rack::Test::Methods
283+
284+
get '/omg/show'
285+
assert_equal 'OK', last_response.body
286+
end
287+
259288
protected
260289

261290
def setup_ar!

0 commit comments

Comments
 (0)