public
Created

  • Download Gist
rails_engines_asset_mixin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
# 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.