| Module | Engines::RailsExtensions::AssetHelpers |
| In: |
lib/engines/rails_extensions/asset_helpers.rb
|
The engines plugin makes it trivial to share public assets using plugins. To do this, include an assets directory within your plugin, and put your javascripts, stylesheets and images in subdirectories of that folder:
my_plugin
|- init.rb
|- lib/
|- assets/
|- javascripts/
| |- my_functions.js
|
|- stylesheets/
| |- my_styles.css
|
|- images/
|- my_face.jpg
Files within the asset structure are automatically mirrored into a publicly-accessible folder each time your application starts (see Engines::Assets#mirror_assets).
It‘s also simple to use Rails’ helpers in your views to use plugin assets. The default helper methods have been enhanced by the engines plugin to accept a :plugin option, indicating the plugin containing the desired asset.
For example, it‘s easy to use plugin assets in your layouts:
<%= stylesheet_link_tag "my_styles", :plugin => "my_plugin", :media => "screen" %> <%= javascript_include_tag "my_functions", :plugin => "my_plugin" %>
… and similarly in views and partials, it‘s easy to use plugin images:
<%= image_tag "my_face", :plugin => "my_plugin" %> <!-- or --> <%= image_path "my_face", :plugin => "my_plugin" %>
Where the default helpers allow the specification of more than one file (i.e. the javascript and stylesheet helpers), you can do similarly for multiple assets from within a single plugin.
This module enhances four of the methods from ActionView::Helpers::AssetTagHelper:
* stylesheet_link_tag * javascript_include_tag * image_path * image_tag
Each one of these methods now accepts the key/value pair :plugin => "plugin_name", which can be used to specify the originating plugin for any assets.
Returns the publicly-addressable relative URI for the given asset, type and plugin
# File lib/engines/rails_extensions/asset_helpers.rb, line 110
110: def self.plugin_asset_path(plugin_name, type, asset)
111: raise "No plugin called '#{plugin_name}' - please use the full name of a loaded plugin." if Engines.plugins[plugin_name].nil?
112: "/#{Engines.plugins[plugin_name].public_asset_directory}/#{type}/#{asset}"
113: end
Convert sources to the paths for the given plugin, if any plugin option is given
# File lib/engines/rails_extensions/asset_helpers.rb, line 102
102: def self.pluginify_sources(type, *sources)
103: options = sources.last.is_a?(Hash) ? sources.pop.stringify_keys : { }
104: sources.map! { |s| plugin_asset_path(options["plugin"], type, s) } if options["plugin"]
105: options.delete("plugin") # we don't want it appearing in the HTML
106: sources << options # re-add options
107: end
Adds plugin functionality to Rails’ default image_path method.
# File lib/engines/rails_extensions/asset_helpers.rb, line 80
80: def image_path_with_engine_additions(source, options={})
81: options.stringify_keys!
82: source = Engines::RailsExtensions::AssetHelpers.plugin_asset_path(options["plugin"], "images", source) if options["plugin"]
83: image_path_without_engine_additions(source)
84: end
Adds plugin functionality to Rails’ default image_tag method.
# File lib/engines/rails_extensions/asset_helpers.rb, line 87
87: def image_tag_with_engine_additions(source, options={})
88: options.stringify_keys!
89: if options["plugin"]
90: source = Engines::RailsExtensions::AssetHelpers.plugin_asset_path(options["plugin"], "images", source)
91: options.delete("plugin")
92: end
93: image_tag_without_engine_additions(source, options)
94: end
Adds plugin functionality to Rails’ default javascript_include_tag method.
# File lib/engines/rails_extensions/asset_helpers.rb, line 71
71: def javascript_include_tag_with_engine_additions(*sources)
72: javascript_include_tag_without_engine_additions(*Engines::RailsExtensions::AssetHelpers.pluginify_sources("javascripts", *sources))
73: end
Adds plugin functionality to Rails’ default stylesheet_link_tag method.
# File lib/engines/rails_extensions/asset_helpers.rb, line 66
66: def stylesheet_link_tag_with_engine_additions(*sources)
67: stylesheet_link_tag_without_engine_additions(*Engines::RailsExtensions::AssetHelpers.pluginify_sources("stylesheets", *sources))
68: end