Skip to content

Commit

Permalink
Moved ActionMailer and ActionController railties options to inherited…
Browse files Browse the repository at this point in the history
… hook

This change is needed, because we must take namespace into account and if
controller's/mailer's class is namespaced, engine's paths should be set
instead of application's ones.

The nice side effect of this is removing unneeded logic in
ActionController::Base.inherited - now the helpers_path should be set
correctly even for engine's controllers, so helper(:all) will always
include correct helpers.
  • Loading branch information
drogus committed Sep 3, 2010
1 parent 8fb9df5 commit e5af8b7
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 22 deletions.
9 changes: 3 additions & 6 deletions actionmailer/lib/action_mailer/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "action_mailer"
require "rails"
require "abstract_controller/railties/routes_helpers"
require "action_mailer/railties/paths"

module ActionMailer
class Railtie < Rails::Railtie
Expand All @@ -11,17 +12,13 @@ class Railtie < Rails::Railtie
end

initializer "action_mailer.set_configs" do |app|
paths = app.config.paths
options = app.config.action_mailer

options.assets_dir ||= paths.public.to_a.first
options.javascripts_dir ||= paths.public.javascripts.to_a.first
options.stylesheets_dir ||= paths.public.stylesheets.to_a.first

ActiveSupport.on_load(:action_mailer) do
include AbstractController::UrlFor
extend ::AbstractController::Railties::RoutesHelpers.with(app.routes)
include app.routes.mounted_helpers(:app)
extend ::AbstractController::Railties::RoutesHelpers.with(app.routes)
extend ::ActionMailer::Railties::Paths.with(app)
options.each { |k,v| send("#{k}=", v) }
end
end
Expand Down
26 changes: 26 additions & 0 deletions actionmailer/lib/action_mailer/railties/paths.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module ActionMailer
module Railties
module Paths
def self.with(_app)
Module.new do
define_method(:inherited) do |klass|
super(klass)
if namespace = klass.parents.detect {|m| m.respond_to?(:_railtie) }
app = namespace._railtie
else
app = _app
end

paths = app.config.paths
options = app.config.action_mailer

options.assets_dir ||= paths.public.to_a.first
options.javascripts_dir ||= paths.public.javascripts.to_a.first
options.stylesheets_dir ||= paths.public.stylesheets.to_a.first
options.each { |k,v| klass.send("#{k}=", v) }
end
end
end
end
end
end
6 changes: 1 addition & 5 deletions actionpack/lib/action_controller/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,7 @@ def self.without_modules(*modules)

def self.inherited(klass)
super
if namespace = klass.parents.detect {|m| m.respond_to?(:_railtie) }
klass.helper(all_helpers_from_path(namespace._railtie.config.paths.app.helpers.to_a))
else
klass.helper :all if klass.superclass == ActionController::Base
end
klass.helper :all if klass.superclass == ActionController::Base
end

ActiveSupport.run_load_hooks(:action_controller, self)
Expand Down
14 changes: 3 additions & 11 deletions actionpack/lib/action_controller/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require "active_support/deprecation/proxy_wrappers"
require "active_support/deprecation"
require "abstract_controller/railties/routes_helpers"
require "action_controller/railties/paths"

module ActionController
class Railtie < Rails::Railtie
Expand Down Expand Up @@ -41,19 +42,10 @@ class Railtie < Rails::Railtie
end

initializer "action_controller.set_configs" do |app|
paths = app.config.paths
options = app.config.action_controller

options.assets_dir ||= paths.public.to_a.first
options.javascripts_dir ||= paths.public.javascripts.to_a.first
options.stylesheets_dir ||= paths.public.stylesheets.to_a.first
options.page_cache_directory ||= paths.public.to_a.first
options.helpers_path ||= paths.app.helpers.to_a

ActiveSupport.on_load(:action_controller) do
extend ::AbstractController::Railties::RoutesHelpers.with(app.routes)
include app.routes.mounted_helpers(:app)
options.each { |k,v| send("#{k}=", v) }
extend ::AbstractController::Railties::RoutesHelpers.with(app.routes)
extend ::ActionController::Railties::Paths.with(app)
end
end

Expand Down
28 changes: 28 additions & 0 deletions actionpack/lib/action_controller/railties/paths.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module ActionController
module Railties
module Paths
def self.with(_app)
Module.new do
define_method(:inherited) do |klass|
super(klass)
if namespace = klass.parents.detect {|m| m.respond_to?(:_railtie) }
app = namespace._railtie
else
app = _app
end

paths = app.config.paths
options = app.config.action_controller

options.assets_dir ||= paths.public.to_a.first
options.javascripts_dir ||= paths.public.javascripts.to_a.first
options.stylesheets_dir ||= paths.public.stylesheets.to_a.first
options.page_cache_directory ||= paths.public.to_a.first
options.helpers_path ||= paths.app.helpers.to_a
options.each { |k,v| klass.send("#{k}=", v) }
end
end
end
end
end
end

0 comments on commit e5af8b7

Please sign in to comment.