Include all helpers from non-namespaced engines

This commit is contained in:
Piotr Sarnacki 2010-08-23 19:25:04 +02:00
parent 98ab4ded37
commit e35c2043b1
4 changed files with 38 additions and 12 deletions

View File

@ -1,22 +1,16 @@
module ActionController module ActionController
module Railties module Railties
module Paths module Paths
def self.with(_app) def self.with(app)
Module.new do Module.new do
define_method(:inherited) do |klass| define_method(:inherited) do |klass|
super(klass) super(klass)
if namespace = klass.parents.detect {|m| m.respond_to?(:_railtie) } if namespace = klass.parents.detect {|m| m.respond_to?(:_railtie) }
app = namespace._railtie klass.helpers_path = namespace._railtie.config.paths.app.helpers.to_a
else else
app = _app klass.helpers_path = app.config.helpers_paths
end end
paths = app.config.paths
options = app.config.action_controller
options.helpers_path ||= paths.app.helpers.to_a
options.each { |k,v| klass.send("#{k}=", v) }
klass.helper :all klass.helper :all
end end
end end

View File

@ -242,7 +242,7 @@ module Rails
autoload :Configuration, "rails/engine/configuration" autoload :Configuration, "rails/engine/configuration"
class << self class << self
attr_accessor :called_from attr_accessor :called_from, :namespaced
alias :engine_name :railtie_name alias :engine_name :railtie_name
def inherited(base) def inherited(base)
@ -292,11 +292,17 @@ module Rails
"#{name}_" "#{name}_"
end end
end end
self.namespaced = true
end
def namespaced?
!!namespaced
end end
end end
delegate :middleware, :root, :paths, :to => :config delegate :middleware, :root, :paths, :to => :config
delegate :engine_name, :to => "self.class" delegate :engine_name, :namespaced?, :to => "self.class"
def load_tasks def load_tasks
super super
@ -435,6 +441,14 @@ module Rails
config.static_asset_paths[config.compiled_asset_path] = public_path config.static_asset_paths[config.compiled_asset_path] = public_path
end end
end end
initializer :prepend_helpers_path do
unless namespaced?
config.helpers_paths = [] unless config.respond_to?(:helpers_paths)
config.helpers_paths = config.paths.app.helpers.to_a + config.helpers_paths
end
end
protected protected
def find_root_with_flag(flag, default=nil) def find_root_with_flag(flag, default=nil)
root_path = self.class.called_from root_path = self.class.called_from

View File

@ -11,6 +11,7 @@ module Rails
super() super()
@root = root @root = root
@middleware = Rails::Configuration::MiddlewareStackProxy.new @middleware = Rails::Configuration::MiddlewareStackProxy.new
@helpers_paths = []
end end
def paths def paths

View File

@ -304,11 +304,12 @@ module RailtiesTest
assert_equal response[2].path, File.join(app_path, "public/bukkits/file_from_app.html") assert_equal response[2].path, File.join(app_path, "public/bukkits/file_from_app.html")
end end
test "shared engine should include application's helpers" do test "shared engine should include application's helpers and own helpers" do
app_file "config/routes.rb", <<-RUBY app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do AppTemplate::Application.routes.draw do
match "/foo" => "bukkits/foo#index", :as => "foo" match "/foo" => "bukkits/foo#index", :as => "foo"
match "/foo/show" => "bukkits/foo#show" match "/foo/show" => "bukkits/foo#show"
match "/foo/bar" => "bukkits/foo#bar"
end end
RUBY RUBY
@ -320,6 +321,14 @@ module RailtiesTest
end end
RUBY RUBY
@plugin.write "app/helpers/bar_helper.rb", <<-RUBY
module BarHelper
def bar
"It's a bar."
end
end
RUBY
@plugin.write "app/controllers/bukkits/foo_controller.rb", <<-RUBY @plugin.write "app/controllers/bukkits/foo_controller.rb", <<-RUBY
class Bukkits::FooController < ActionController::Base class Bukkits::FooController < ActionController::Base
def index def index
@ -329,6 +338,10 @@ module RailtiesTest
def show def show
render :text => foo_path render :text => foo_path
end end
def bar
render :inline => "<%= bar %>"
end
end end
RUBY RUBY
@ -341,6 +354,10 @@ module RailtiesTest
env = Rack::MockRequest.env_for("/foo/show") env = Rack::MockRequest.env_for("/foo/show")
response = Rails.application.call(env) response = Rails.application.call(env)
assert_equal "/foo", response[2].body assert_equal "/foo", response[2].body
env = Rack::MockRequest.env_for("/foo/bar")
response = Rails.application.call(env)
assert_equal "It's a bar.", response[2].body
end end
test "namespaced engine should include only its own routes and helpers" do test "namespaced engine should include only its own routes and helpers" do