Chris@909
|
1 # The engines plugin makes it trivial to share public assets using plugins.
|
Chris@909
|
2 # To do this, include an <tt>assets</tt> directory within your plugin, and put
|
Chris@909
|
3 # your javascripts, stylesheets and images in subdirectories of that folder:
|
Chris@909
|
4 #
|
Chris@909
|
5 # my_plugin
|
Chris@909
|
6 # |- init.rb
|
Chris@909
|
7 # |- lib/
|
Chris@909
|
8 # |- assets/
|
Chris@909
|
9 # |- javascripts/
|
Chris@909
|
10 # | |- my_functions.js
|
Chris@909
|
11 # |
|
Chris@909
|
12 # |- stylesheets/
|
Chris@909
|
13 # | |- my_styles.css
|
Chris@909
|
14 # |
|
Chris@909
|
15 # |- images/
|
Chris@909
|
16 # |- my_face.jpg
|
Chris@909
|
17 #
|
Chris@909
|
18 # Files within the <tt>asset</tt> structure are automatically mirrored into
|
Chris@909
|
19 # a publicly-accessible folder each time your application starts (see
|
Chris@909
|
20 # Engines::Assets#mirror_assets).
|
Chris@909
|
21 #
|
Chris@909
|
22 #
|
Chris@909
|
23 # == Using plugin assets in views
|
Chris@909
|
24 #
|
Chris@909
|
25 # It's also simple to use Rails' helpers in your views to use plugin assets.
|
Chris@909
|
26 # The default helper methods have been enhanced by the engines plugin to accept
|
Chris@909
|
27 # a <tt>:plugin</tt> option, indicating the plugin containing the desired asset.
|
Chris@909
|
28 #
|
Chris@909
|
29 # For example, it's easy to use plugin assets in your layouts:
|
Chris@909
|
30 #
|
Chris@909
|
31 # <%= stylesheet_link_tag "my_styles", :plugin => "my_plugin", :media => "screen" %>
|
Chris@909
|
32 # <%= javascript_include_tag "my_functions", :plugin => "my_plugin" %>
|
Chris@909
|
33 #
|
Chris@909
|
34 # ... and similarly in views and partials, it's easy to use plugin images:
|
Chris@909
|
35 #
|
Chris@909
|
36 # <%= image_tag "my_face", :plugin => "my_plugin" %>
|
Chris@909
|
37 # <!-- or -->
|
Chris@909
|
38 # <%= image_path "my_face", :plugin => "my_plugin" %>
|
Chris@909
|
39 #
|
Chris@909
|
40 # Where the default helpers allow the specification of more than one file (i.e. the
|
Chris@909
|
41 # javascript and stylesheet helpers), you can do similarly for multiple assets from
|
Chris@909
|
42 # within a single plugin.
|
Chris@909
|
43 #
|
Chris@909
|
44 # ---
|
Chris@909
|
45 #
|
Chris@909
|
46 # This module enhances four of the methods from ActionView::Helpers::AssetTagHelper:
|
Chris@909
|
47 #
|
Chris@909
|
48 # * stylesheet_link_tag
|
Chris@909
|
49 # * javascript_include_tag
|
Chris@909
|
50 # * image_path
|
Chris@909
|
51 # * image_tag
|
Chris@909
|
52 #
|
Chris@909
|
53 # Each one of these methods now accepts the key/value pair <tt>:plugin => "plugin_name"</tt>,
|
Chris@909
|
54 # which can be used to specify the originating plugin for any assets.
|
Chris@909
|
55 #
|
Chris@909
|
56 module Engines::RailsExtensions::AssetHelpers
|
Chris@909
|
57 def self.included(base) #:nodoc:
|
Chris@909
|
58 base.class_eval do
|
Chris@909
|
59 [:stylesheet_link_tag, :javascript_include_tag, :image_path, :image_tag].each do |m|
|
Chris@909
|
60 alias_method_chain m, :engine_additions
|
Chris@909
|
61 end
|
Chris@909
|
62 end
|
Chris@909
|
63 end
|
Chris@909
|
64
|
Chris@909
|
65 # Adds plugin functionality to Rails' default stylesheet_link_tag method.
|
Chris@909
|
66 def stylesheet_link_tag_with_engine_additions(*sources)
|
Chris@909
|
67 stylesheet_link_tag_without_engine_additions(*Engines::RailsExtensions::AssetHelpers.pluginify_sources("stylesheets", *sources))
|
Chris@909
|
68 end
|
Chris@909
|
69
|
Chris@909
|
70 # Adds plugin functionality to Rails' default javascript_include_tag method.
|
Chris@909
|
71 def javascript_include_tag_with_engine_additions(*sources)
|
Chris@909
|
72 javascript_include_tag_without_engine_additions(*Engines::RailsExtensions::AssetHelpers.pluginify_sources("javascripts", *sources))
|
Chris@909
|
73 end
|
Chris@909
|
74
|
Chris@909
|
75 #--
|
Chris@909
|
76 # Our modified image_path now takes a 'plugin' option, though it doesn't require it
|
Chris@909
|
77 #++
|
Chris@909
|
78
|
Chris@909
|
79 # Adds plugin functionality to Rails' default image_path method.
|
Chris@909
|
80 def image_path_with_engine_additions(source, options={})
|
Chris@909
|
81 options.stringify_keys!
|
Chris@909
|
82 source = Engines::RailsExtensions::AssetHelpers.plugin_asset_path(options["plugin"], "images", source) if options["plugin"]
|
Chris@909
|
83 image_path_without_engine_additions(source)
|
Chris@909
|
84 end
|
Chris@909
|
85
|
Chris@909
|
86 # Adds plugin functionality to Rails' default image_tag method.
|
Chris@909
|
87 def image_tag_with_engine_additions(source, options={})
|
Chris@909
|
88 options.stringify_keys!
|
Chris@909
|
89 if options["plugin"]
|
Chris@909
|
90 source = Engines::RailsExtensions::AssetHelpers.plugin_asset_path(options["plugin"], "images", source)
|
Chris@909
|
91 options.delete("plugin")
|
Chris@909
|
92 end
|
Chris@909
|
93 image_tag_without_engine_additions(source, options)
|
Chris@909
|
94 end
|
Chris@909
|
95
|
Chris@909
|
96 #--
|
Chris@909
|
97 # The following are methods on this module directly because of the weird-freaky way
|
Chris@909
|
98 # Rails creates the helper instance that views actually get
|
Chris@909
|
99 #++
|
Chris@909
|
100
|
Chris@909
|
101 # Convert sources to the paths for the given plugin, if any plugin option is given
|
Chris@909
|
102 def self.pluginify_sources(type, *sources)
|
Chris@909
|
103 options = sources.last.is_a?(Hash) ? sources.pop.stringify_keys : { }
|
Chris@909
|
104 sources.map! { |s| plugin_asset_path(options["plugin"], type, s) } if options["plugin"]
|
Chris@909
|
105 options.delete("plugin") # we don't want it appearing in the HTML
|
Chris@909
|
106 sources << options # re-add options
|
Chris@909
|
107 end
|
Chris@909
|
108
|
Chris@909
|
109 # Returns the publicly-addressable relative URI for the given asset, type and plugin
|
Chris@909
|
110 def self.plugin_asset_path(plugin_name, type, asset)
|
Chris@909
|
111 raise "No plugin called '#{plugin_name}' - please use the full name of a loaded plugin." if Engines.plugins[plugin_name].nil?
|
Chris@909
|
112 "#{ActionController::Base.relative_url_root}/#{Engines.plugins[plugin_name].public_asset_directory}/#{type}/#{asset}"
|
Chris@909
|
113 end
|
Chris@909
|
114
|
Chris@909
|
115 end
|
Chris@909
|
116
|
Chris@909
|
117 module ::ActionView::Helpers::AssetTagHelper #:nodoc:
|
Chris@909
|
118 include Engines::RailsExtensions::AssetHelpers
|
Chris@909
|
119 end
|