Customize a Helper
Rails helpers are modules that define methods for use in views and controllers (helper methods). Occasionally, you may encounter the following use cases involving helper methods:
- From a plugin, you need to define a new helper method
- From a plugin or application, you need to re-define a helper method originally defined by Workarea
The Rails engine architecture does not provide an extension mechanism to enable these use cases. However, Workarea developers can use the following two-step procedure to satisfy these requirements:
- Define or re-define the helper method (depending on use case)
- Add the helper module to the ancestors of either the Admin or Storefront application controller
1. (Re)Define the Helper Method
For a new helper method (from a plugin), define a new helper and method. The following example defines a new helper method within the Blogs plugin:
# workarea-blog/app/helpers/workarea/storefront/blogs_helper.rb
module Workarea
module Storefront
module BlogsHelper
def blog_posting_schema(entry)
# implementation of new helper method
end
end
end
end
To redefine an existing method (from an app or a plugin), define a method with the same name within a new module. The following example re-defines a method from the Blogs plugin from the Boardgamez app:
# boardgamez/app/helpers/workarea/storefront/blogs_helper.rb
module Workarea
module Storefront
module BoardgamezBlogsHelper
def blog_posting_schema(entry)
# re-implementation of helper method
end
end
end
end
2. Add the Helper to an Application Controller
In both cases above, a new module is defined.
Add that module to the ancestors of either the Storefront or Admin application controller, depending on where you intend to use the helper method.
Do this using the .helper
class method on the controller.
The following examples accomplish this in different ways, and both techniques are applicable to applications and plugins.
The first decorates the chosen controller:
# workarea-blog/app/controllers/workarea/storefront/application_controller.decorator
module Workarea
decorate Storefront::ApplicationController, with: :blog do
decorated { helper Storefront::BlogsHelper }
end
end
While the following example does the work in a configuration file:
# boardgamez/config/application.rb
module Boardgamez
class Application < Rails::Application
config.to_prepare do
Workarea::Storefront::ApplicationController.helper(
Workarea::Storefront::BoardgamezBlogsHelper
)
end
end
end
Help Us Improve this Doc
Was this helpful? Open a GitHub issue to report a problem with this doc, suggest an improvement, or otherwise provide feedback. Thanks!