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