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