mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add Engine#helpers method which loads all the engine's helpers
This commit is contained in:
parent
723a0f82c4
commit
e38b4436a5
2 changed files with 84 additions and 0 deletions
|
@ -286,6 +286,27 @@ module Rails
|
||||||
#
|
#
|
||||||
# This code will use <tt>my_engine.user_path(@user)</tt> to generate the proper route.
|
# This code will use <tt>my_engine.user_path(@user)</tt> to generate the proper route.
|
||||||
#
|
#
|
||||||
|
# == Isolated engine's helpers
|
||||||
|
#
|
||||||
|
# Sometimes you may want to isolate engine, but use helpers that are defined for it.
|
||||||
|
# If you want to share just a few specific helpers you can add them to application's
|
||||||
|
# helpers in ApplicationController:
|
||||||
|
#
|
||||||
|
# class ApplicationController < ActionController::Base
|
||||||
|
# helper MyEngine::SharedEngineHelper
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# If you want to include all of the engine's helpers, you can use #helpers method on egine's
|
||||||
|
# instance:
|
||||||
|
#
|
||||||
|
# class ApplicationController < ActionController::Base
|
||||||
|
# helper MyEngine::Engine.helpers
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# It will include all of the helpers from engine's directory. Take into account that this does
|
||||||
|
# not include helpers defined in controllers with helper_method or other similar solutions,
|
||||||
|
# only helpers defined in helpers directory will be included.
|
||||||
|
#
|
||||||
# == Migrations & seed data
|
# == Migrations & seed data
|
||||||
#
|
#
|
||||||
# Engines can have their own migrations. The default path for migrations is exactly the same
|
# Engines can have their own migrations. The default path for migrations is exactly the same
|
||||||
|
@ -384,6 +405,24 @@ module Rails
|
||||||
@railties ||= self.class::Railties.new(config)
|
@railties ||= self.class::Railties.new(config)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def helpers
|
||||||
|
@helpers ||= begin
|
||||||
|
helpers = Module.new
|
||||||
|
|
||||||
|
helpers_paths = if config.respond_to?(:helpers_paths)
|
||||||
|
config.helpers_paths
|
||||||
|
else
|
||||||
|
paths["app/helpers"].existent
|
||||||
|
end
|
||||||
|
|
||||||
|
all = ActionController::Base.send(:all_helpers_from_path, helpers_paths)
|
||||||
|
ActionController::Base.send(:modules_for_helpers, all).each do |mod|
|
||||||
|
helpers.send(:include, mod)
|
||||||
|
end
|
||||||
|
helpers
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def app
|
def app
|
||||||
@app ||= begin
|
@app ||= begin
|
||||||
config.middleware = config.middleware.merge_into(default_middleware_stack)
|
config.middleware = config.middleware.merge_into(default_middleware_stack)
|
||||||
|
|
|
@ -584,6 +584,51 @@ module RailtiesTest
|
||||||
assert_equal Bukkits::Engine.instance, Rails::Engine.find(engine_path)
|
assert_equal Bukkits::Engine.instance, Rails::Engine.find(engine_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "gather isolated engine's helpers in Engine#helpers" do
|
||||||
|
@plugin.write "lib/bukkits.rb", <<-RUBY
|
||||||
|
module Bukkits
|
||||||
|
class Engine < ::Rails::Engine
|
||||||
|
isolate_namespace Bukkits
|
||||||
|
end
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
app_file "app/helpers/some_helper.rb", <<-RUBY
|
||||||
|
module SomeHelper
|
||||||
|
def foo
|
||||||
|
'foo'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
@plugin.write "app/helpers/bukkits/engine_helper.rb", <<-RUBY
|
||||||
|
module Bukkits
|
||||||
|
module EngineHelper
|
||||||
|
def bar
|
||||||
|
'bar'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
@plugin.write "app/helpers/engine_helper.rb", <<-RUBY
|
||||||
|
module EngineHelper
|
||||||
|
def baz
|
||||||
|
'baz'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
add_to_config("config.action_dispatch.show_exceptions = false")
|
||||||
|
|
||||||
|
boot_rails
|
||||||
|
require "#{rails_root}/config/environment"
|
||||||
|
|
||||||
|
methods = Bukkits::Engine.helpers.public_instance_methods.sort
|
||||||
|
expected = ["bar", "baz"]
|
||||||
|
assert_equal expected, methods
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def app
|
def app
|
||||||
Rails.application
|
Rails.application
|
||||||
|
|
Loading…
Reference in a new issue