Created
March 8, 2010 16:06
-
-
Save mwmitchell/325296 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# removed alias_method_chain | |
# calling super instead | |
# including into ActionView::Base | |
module Engines::RailsExtensions::AssetHelpers | |
# Adds plugin functionality to Rails' default stylesheet_link_tag method. | |
def stylesheet_link_tag(*sources) | |
super(*Engines::RailsExtensions::AssetHelpers.pluginify_sources("stylesheets", *sources)) | |
end | |
# Adds plugin functionality to Rails' default javascript_include_tag method. | |
def javascript_include_tag(*sources) | |
super(*Engines::RailsExtensions::AssetHelpers.pluginify_sources("javascripts", *sources)) | |
end | |
#-- | |
# Our modified image_path now takes a 'plugin' option, though it doesn't require it | |
#++ | |
# Adds plugin functionality to Rails' default image_path method. | |
def image_path(source, options={}) | |
options.stringify_keys! | |
source = Engines::RailsExtensions::AssetHelpers.plugin_asset_path(options["plugin"], "images", source) if options["plugin"] | |
super(source) | |
end | |
# Adds plugin functionality to Rails' default image_tag method. | |
def image_tag(source, options={}) | |
options.stringify_keys! | |
if options["plugin"] | |
source = Engines::RailsExtensions::AssetHelpers.plugin_asset_path(options["plugin"], "images", source) | |
options.delete("plugin") | |
end | |
super(source, options) | |
end | |
#-- | |
# The following are methods on this module directly because of the weird-freaky way | |
# Rails creates the helper instance that views actually get | |
#++ | |
# Convert sources to the paths for the given plugin, if any plugin option is given | |
def self.pluginify_sources(type, *sources) | |
options = sources.last.is_a?(Hash) ? sources.pop.stringify_keys : { } | |
sources.map! { |s| plugin_asset_path(options["plugin"], type, s) } if options["plugin"] | |
options.delete("plugin") # we don't want it appearing in the HTML | |
sources << options # re-add options | |
end | |
# Returns the publicly-addressable relative URI for the given asset, type and plugin | |
def self.plugin_asset_path(plugin_name, type, asset) | |
raise "No plugin called '#{plugin_name}' - please use the full name of a loaded plugin." if Engines.plugins[plugin_name].nil? | |
"/#{Engines.plugins[plugin_name].public_asset_directory}/#{type}/#{asset}" | |
end | |
end | |
ActionView::Base.class_eval do | |
include Engines::RailsExtensions::AssetHelpers | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment