rails/rails — Issue #3282: Provide a standard way to define custom asset helpers
Author: sidonath
Created: 2011-10-10 17:35:21+00:00
State: open

While it's easy to define a custom view helper in Rails, it's not trivial to define similar helpers for the Asset Pipeline assets.

Such helpers would be useful for precompiled JS templates, static HTML pages (eg. we have `app/assets/html/404.html.erb` that is symlinked to `public/404.html` in production) and probably other code reuse scenarios in not-so-static assets.

Currently, we solve this problem by including a custom `AssetHelper` module (defined in `app/helpers`) into `Sprockets::Helpers::RailsHelper`. We do this in `config/application.rb` (since initializers are not loaded for assets:precompile).

A better solution could be to define app/helpers/asset_helper.rb and include it by default into both views and assets contexts (since there is a high chance that a helpers would be reused between views and assets). Of course a configuration directive could be provided to allow including any helpers into the asset pipeline context, like:

config.assets.helpers << %w{other_asset_helpers}