actionview 4.1.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of actionview might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/CHANGELOG.md +274 -0
- data/MIT-LICENSE +21 -0
- data/README.rdoc +34 -0
- data/lib/action_view.rb +97 -0
- data/lib/action_view/base.rb +205 -0
- data/lib/action_view/buffers.rb +49 -0
- data/lib/action_view/context.rb +36 -0
- data/lib/action_view/dependency_tracker.rb +93 -0
- data/lib/action_view/digestor.rb +116 -0
- data/lib/action_view/flows.rb +76 -0
- data/lib/action_view/helpers.rb +64 -0
- data/lib/action_view/helpers/active_model_helper.rb +49 -0
- data/lib/action_view/helpers/asset_tag_helper.rb +322 -0
- data/lib/action_view/helpers/asset_url_helper.rb +355 -0
- data/lib/action_view/helpers/atom_feed_helper.rb +203 -0
- data/lib/action_view/helpers/cache_helper.rb +200 -0
- data/lib/action_view/helpers/capture_helper.rb +216 -0
- data/lib/action_view/helpers/controller_helper.rb +25 -0
- data/lib/action_view/helpers/csrf_helper.rb +30 -0
- data/lib/action_view/helpers/date_helper.rb +1075 -0
- data/lib/action_view/helpers/debug_helper.rb +39 -0
- data/lib/action_view/helpers/form_helper.rb +1876 -0
- data/lib/action_view/helpers/form_options_helper.rb +843 -0
- data/lib/action_view/helpers/form_tag_helper.rb +746 -0
- data/lib/action_view/helpers/javascript_helper.rb +75 -0
- data/lib/action_view/helpers/number_helper.rb +425 -0
- data/lib/action_view/helpers/output_safety_helper.rb +38 -0
- data/lib/action_view/helpers/record_tag_helper.rb +108 -0
- data/lib/action_view/helpers/rendering_helper.rb +90 -0
- data/lib/action_view/helpers/sanitize_helper.rb +256 -0
- data/lib/action_view/helpers/tag_helper.rb +176 -0
- data/lib/action_view/helpers/tags.rb +41 -0
- data/lib/action_view/helpers/tags/base.rb +148 -0
- data/lib/action_view/helpers/tags/check_box.rb +64 -0
- data/lib/action_view/helpers/tags/checkable.rb +16 -0
- data/lib/action_view/helpers/tags/collection_check_boxes.rb +44 -0
- data/lib/action_view/helpers/tags/collection_helpers.rb +85 -0
- data/lib/action_view/helpers/tags/collection_radio_buttons.rb +36 -0
- data/lib/action_view/helpers/tags/collection_select.rb +28 -0
- data/lib/action_view/helpers/tags/color_field.rb +25 -0
- data/lib/action_view/helpers/tags/date_field.rb +13 -0
- data/lib/action_view/helpers/tags/date_select.rb +72 -0
- data/lib/action_view/helpers/tags/datetime_field.rb +22 -0
- data/lib/action_view/helpers/tags/datetime_local_field.rb +19 -0
- data/lib/action_view/helpers/tags/datetime_select.rb +8 -0
- data/lib/action_view/helpers/tags/email_field.rb +8 -0
- data/lib/action_view/helpers/tags/file_field.rb +8 -0
- data/lib/action_view/helpers/tags/grouped_collection_select.rb +29 -0
- data/lib/action_view/helpers/tags/hidden_field.rb +8 -0
- data/lib/action_view/helpers/tags/label.rb +65 -0
- data/lib/action_view/helpers/tags/month_field.rb +13 -0
- data/lib/action_view/helpers/tags/number_field.rb +18 -0
- data/lib/action_view/helpers/tags/password_field.rb +12 -0
- data/lib/action_view/helpers/tags/radio_button.rb +31 -0
- data/lib/action_view/helpers/tags/range_field.rb +8 -0
- data/lib/action_view/helpers/tags/search_field.rb +24 -0
- data/lib/action_view/helpers/tags/select.rb +41 -0
- data/lib/action_view/helpers/tags/tel_field.rb +8 -0
- data/lib/action_view/helpers/tags/text_area.rb +18 -0
- data/lib/action_view/helpers/tags/text_field.rb +29 -0
- data/lib/action_view/helpers/tags/time_field.rb +13 -0
- data/lib/action_view/helpers/tags/time_select.rb +8 -0
- data/lib/action_view/helpers/tags/time_zone_select.rb +20 -0
- data/lib/action_view/helpers/tags/url_field.rb +8 -0
- data/lib/action_view/helpers/tags/week_field.rb +13 -0
- data/lib/action_view/helpers/text_helper.rb +447 -0
- data/lib/action_view/helpers/translation_helper.rb +111 -0
- data/lib/action_view/helpers/url_helper.rb +625 -0
- data/lib/action_view/layouts.rb +426 -0
- data/lib/action_view/locale/en.yml +56 -0
- data/lib/action_view/log_subscriber.rb +44 -0
- data/lib/action_view/lookup_context.rb +249 -0
- data/lib/action_view/model_naming.rb +12 -0
- data/lib/action_view/path_set.rb +77 -0
- data/lib/action_view/railtie.rb +49 -0
- data/lib/action_view/record_identifier.rb +84 -0
- data/lib/action_view/renderer/abstract_renderer.rb +47 -0
- data/lib/action_view/renderer/partial_renderer.rb +492 -0
- data/lib/action_view/renderer/renderer.rb +50 -0
- data/lib/action_view/renderer/streaming_template_renderer.rb +103 -0
- data/lib/action_view/renderer/template_renderer.rb +96 -0
- data/lib/action_view/rendering.rb +145 -0
- data/lib/action_view/routing_url_for.rb +109 -0
- data/lib/action_view/tasks/dependencies.rake +17 -0
- data/lib/action_view/template.rb +340 -0
- data/lib/action_view/template/error.rb +141 -0
- data/lib/action_view/template/handlers.rb +53 -0
- data/lib/action_view/template/handlers/builder.rb +26 -0
- data/lib/action_view/template/handlers/erb.rb +145 -0
- data/lib/action_view/template/handlers/raw.rb +11 -0
- data/lib/action_view/template/resolver.rb +329 -0
- data/lib/action_view/template/text.rb +34 -0
- data/lib/action_view/template/types.rb +57 -0
- data/lib/action_view/test_case.rb +272 -0
- data/lib/action_view/testing/resolvers.rb +50 -0
- data/lib/action_view/vendor/html-scanner.rb +20 -0
- data/lib/action_view/vendor/html-scanner/html/document.rb +68 -0
- data/lib/action_view/vendor/html-scanner/html/node.rb +532 -0
- data/lib/action_view/vendor/html-scanner/html/sanitizer.rb +188 -0
- data/lib/action_view/vendor/html-scanner/html/selector.rb +830 -0
- data/lib/action_view/vendor/html-scanner/html/tokenizer.rb +107 -0
- data/lib/action_view/vendor/html-scanner/html/version.rb +11 -0
- data/lib/action_view/version.rb +11 -0
- data/lib/action_view/view_paths.rb +96 -0
- metadata +218 -0
@@ -0,0 +1,322 @@
|
|
1
|
+
require 'active_support/core_ext/array/extract_options'
|
2
|
+
require 'active_support/core_ext/hash/keys'
|
3
|
+
require 'action_view/helpers/asset_url_helper'
|
4
|
+
require 'action_view/helpers/tag_helper'
|
5
|
+
|
6
|
+
module ActionView
|
7
|
+
# = Action View Asset Tag Helpers
|
8
|
+
module Helpers #:nodoc:
|
9
|
+
# This module provides methods for generating HTML that links views to assets such
|
10
|
+
# as images, javascripts, stylesheets, and feeds. These methods do not verify
|
11
|
+
# the assets exist before linking to them:
|
12
|
+
#
|
13
|
+
# image_tag("rails.png")
|
14
|
+
# # => <img alt="Rails" src="/assets/rails.png" />
|
15
|
+
# stylesheet_link_tag("application")
|
16
|
+
# # => <link href="/assets/application.css?body=1" media="screen" rel="stylesheet" />
|
17
|
+
module AssetTagHelper
|
18
|
+
extend ActiveSupport::Concern
|
19
|
+
|
20
|
+
include AssetUrlHelper
|
21
|
+
include TagHelper
|
22
|
+
|
23
|
+
# Returns an HTML script tag for each of the +sources+ provided.
|
24
|
+
#
|
25
|
+
# Sources may be paths to JavaScript files. Relative paths are assumed to be relative
|
26
|
+
# to <tt>assets/javascripts</tt>, full paths are assumed to be relative to the document
|
27
|
+
# root. Relative paths are idiomatic, use absolute paths only when needed.
|
28
|
+
#
|
29
|
+
# When passing paths, the ".js" extension is optional. If you do not want ".js"
|
30
|
+
# appended to the path <tt>extname: false</tt> can be set on the options.
|
31
|
+
#
|
32
|
+
# You can modify the HTML attributes of the script tag by passing a hash as the
|
33
|
+
# last argument.
|
34
|
+
#
|
35
|
+
# When the Asset Pipeline is enabled, you can pass the name of your manifest as
|
36
|
+
# source, and include other JavaScript or CoffeeScript files inside the manifest.
|
37
|
+
#
|
38
|
+
# javascript_include_tag "xmlhr"
|
39
|
+
# # => <script src="/assets/xmlhr.js?1284139606"></script>
|
40
|
+
#
|
41
|
+
# javascript_include_tag "template.jst", extname: false
|
42
|
+
# # => <script src="/assets/template.jst?1284139606"></script>
|
43
|
+
#
|
44
|
+
# javascript_include_tag "xmlhr.js"
|
45
|
+
# # => <script src="/assets/xmlhr.js?1284139606"></script>
|
46
|
+
#
|
47
|
+
# javascript_include_tag "common.javascript", "/elsewhere/cools"
|
48
|
+
# # => <script src="/assets/common.javascript?1284139606"></script>
|
49
|
+
# # <script src="/elsewhere/cools.js?1423139606"></script>
|
50
|
+
#
|
51
|
+
# javascript_include_tag "http://www.example.com/xmlhr"
|
52
|
+
# # => <script src="http://www.example.com/xmlhr"></script>
|
53
|
+
#
|
54
|
+
# javascript_include_tag "http://www.example.com/xmlhr.js"
|
55
|
+
# # => <script src="http://www.example.com/xmlhr.js"></script>
|
56
|
+
def javascript_include_tag(*sources)
|
57
|
+
options = sources.extract_options!.stringify_keys
|
58
|
+
path_options = options.extract!('protocol', 'extname').symbolize_keys
|
59
|
+
sources.uniq.map { |source|
|
60
|
+
tag_options = {
|
61
|
+
"src" => path_to_javascript(source, path_options)
|
62
|
+
}.merge!(options)
|
63
|
+
content_tag(:script, "", tag_options)
|
64
|
+
}.join("\n").html_safe
|
65
|
+
end
|
66
|
+
|
67
|
+
# Returns a stylesheet link tag for the sources specified as arguments. If
|
68
|
+
# you don't specify an extension, <tt>.css</tt> will be appended automatically.
|
69
|
+
# You can modify the link attributes by passing a hash as the last argument.
|
70
|
+
# For historical reasons, the 'media' attribute will always be present and defaults
|
71
|
+
# to "screen", so you must explicitly set it to "all" for the stylesheet(s) to
|
72
|
+
# apply to all media types.
|
73
|
+
#
|
74
|
+
# stylesheet_link_tag "style"
|
75
|
+
# # => <link href="/assets/style.css" media="screen" rel="stylesheet" />
|
76
|
+
#
|
77
|
+
# stylesheet_link_tag "style.css"
|
78
|
+
# # => <link href="/assets/style.css" media="screen" rel="stylesheet" />
|
79
|
+
#
|
80
|
+
# stylesheet_link_tag "http://www.example.com/style.css"
|
81
|
+
# # => <link href="http://www.example.com/style.css" media="screen" rel="stylesheet" />
|
82
|
+
#
|
83
|
+
# stylesheet_link_tag "style", media: "all"
|
84
|
+
# # => <link href="/assets/style.css" media="all" rel="stylesheet" />
|
85
|
+
#
|
86
|
+
# stylesheet_link_tag "style", media: "print"
|
87
|
+
# # => <link href="/assets/style.css" media="print" rel="stylesheet" />
|
88
|
+
#
|
89
|
+
# stylesheet_link_tag "random.styles", "/css/stylish"
|
90
|
+
# # => <link href="/assets/random.styles" media="screen" rel="stylesheet" />
|
91
|
+
# # <link href="/css/stylish.css" media="screen" rel="stylesheet" />
|
92
|
+
def stylesheet_link_tag(*sources)
|
93
|
+
options = sources.extract_options!.stringify_keys
|
94
|
+
path_options = options.extract!('protocol').symbolize_keys
|
95
|
+
|
96
|
+
sources.uniq.map { |source|
|
97
|
+
tag_options = {
|
98
|
+
"rel" => "stylesheet",
|
99
|
+
"media" => "screen",
|
100
|
+
"href" => path_to_stylesheet(source, path_options)
|
101
|
+
}.merge!(options)
|
102
|
+
tag(:link, tag_options)
|
103
|
+
}.join("\n").html_safe
|
104
|
+
end
|
105
|
+
|
106
|
+
# Returns a link tag that browsers and feed readers can use to auto-detect
|
107
|
+
# an RSS or Atom feed. The +type+ can either be <tt>:rss</tt> (default) or
|
108
|
+
# <tt>:atom</tt>. Control the link options in url_for format using the
|
109
|
+
# +url_options+. You can modify the LINK tag itself in +tag_options+.
|
110
|
+
#
|
111
|
+
# ==== Options
|
112
|
+
#
|
113
|
+
# * <tt>:rel</tt> - Specify the relation of this link, defaults to "alternate"
|
114
|
+
# * <tt>:type</tt> - Override the auto-generated mime type
|
115
|
+
# * <tt>:title</tt> - Specify the title of the link, defaults to the +type+
|
116
|
+
#
|
117
|
+
# ==== Examples
|
118
|
+
#
|
119
|
+
# auto_discovery_link_tag
|
120
|
+
# # => <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.currenthost.com/controller/action" />
|
121
|
+
# auto_discovery_link_tag(:atom)
|
122
|
+
# # => <link rel="alternate" type="application/atom+xml" title="ATOM" href="http://www.currenthost.com/controller/action" />
|
123
|
+
# auto_discovery_link_tag(:rss, {action: "feed"})
|
124
|
+
# # => <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.currenthost.com/controller/feed" />
|
125
|
+
# auto_discovery_link_tag(:rss, {action: "feed"}, {title: "My RSS"})
|
126
|
+
# # => <link rel="alternate" type="application/rss+xml" title="My RSS" href="http://www.currenthost.com/controller/feed" />
|
127
|
+
# auto_discovery_link_tag(:rss, {controller: "news", action: "feed"})
|
128
|
+
# # => <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.currenthost.com/news/feed" />
|
129
|
+
# auto_discovery_link_tag(:rss, "http://www.example.com/feed.rss", {title: "Example RSS"})
|
130
|
+
# # => <link rel="alternate" type="application/rss+xml" title="Example RSS" href="http://www.example.com/feed" />
|
131
|
+
def auto_discovery_link_tag(type = :rss, url_options = {}, tag_options = {})
|
132
|
+
if !(type == :rss || type == :atom) && tag_options[:type].blank?
|
133
|
+
raise ArgumentError.new("You should pass :type tag_option key explicitly, because you have passed #{type} type other than :rss or :atom.")
|
134
|
+
end
|
135
|
+
|
136
|
+
tag(
|
137
|
+
"link",
|
138
|
+
"rel" => tag_options[:rel] || "alternate",
|
139
|
+
"type" => tag_options[:type] || Mime::Type.lookup_by_extension(type.to_s).to_s,
|
140
|
+
"title" => tag_options[:title] || type.to_s.upcase,
|
141
|
+
"href" => url_options.is_a?(Hash) ? url_for(url_options.merge(:only_path => false)) : url_options
|
142
|
+
)
|
143
|
+
end
|
144
|
+
|
145
|
+
# Returns a link loading a favicon file. You may specify a different file
|
146
|
+
# in the first argument. The helper accepts an additional options hash where
|
147
|
+
# you can override "rel" and "type".
|
148
|
+
#
|
149
|
+
# ==== Options
|
150
|
+
#
|
151
|
+
# * <tt>:rel</tt> - Specify the relation of this link, defaults to 'shortcut icon'
|
152
|
+
# * <tt>:type</tt> - Override the auto-generated mime type, defaults to 'image/vnd.microsoft.icon'
|
153
|
+
#
|
154
|
+
# ==== Examples
|
155
|
+
#
|
156
|
+
# favicon_link_tag 'myicon.ico'
|
157
|
+
# # => <link href="/assets/myicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon" />
|
158
|
+
#
|
159
|
+
# Mobile Safari looks for a different <link> tag, pointing to an image that
|
160
|
+
# will be used if you add the page to the home screen of an iPod Touch, iPhone, or iPad.
|
161
|
+
# The following call would generate such a tag:
|
162
|
+
#
|
163
|
+
# favicon_link_tag 'mb-icon.png', rel: 'apple-touch-icon', type: 'image/png'
|
164
|
+
# # => <link href="/assets/mb-icon.png" rel="apple-touch-icon" type="image/png" />
|
165
|
+
def favicon_link_tag(source='favicon.ico', options={})
|
166
|
+
tag('link', {
|
167
|
+
:rel => 'shortcut icon',
|
168
|
+
:type => 'image/vnd.microsoft.icon',
|
169
|
+
:href => path_to_image(source)
|
170
|
+
}.merge!(options.symbolize_keys))
|
171
|
+
end
|
172
|
+
|
173
|
+
# Returns an HTML image tag for the +source+. The +source+ can be a full
|
174
|
+
# path or a file.
|
175
|
+
#
|
176
|
+
# ==== Options
|
177
|
+
#
|
178
|
+
# You can add HTML attributes using the +options+. The +options+ supports
|
179
|
+
# three additional keys for convenience and conformance:
|
180
|
+
#
|
181
|
+
# * <tt>:alt</tt> - If no alt text is given, the file name part of the
|
182
|
+
# +source+ is used (capitalized and without the extension)
|
183
|
+
# * <tt>:size</tt> - Supplied as "{Width}x{Height}" or "{Number}", so "30x45" becomes
|
184
|
+
# width="30" and height="45", and "50" becomes width="50" and height="50".
|
185
|
+
# <tt>:size</tt> will be ignored if the value is not in the correct format.
|
186
|
+
#
|
187
|
+
# ==== Examples
|
188
|
+
#
|
189
|
+
# image_tag("icon")
|
190
|
+
# # => <img alt="Icon" src="/assets/icon" />
|
191
|
+
# image_tag("icon.png")
|
192
|
+
# # => <img alt="Icon" src="/assets/icon.png" />
|
193
|
+
# image_tag("icon.png", size: "16x10", alt: "Edit Entry")
|
194
|
+
# # => <img src="/assets/icon.png" width="16" height="10" alt="Edit Entry" />
|
195
|
+
# image_tag("/icons/icon.gif", size: "16")
|
196
|
+
# # => <img src="/icons/icon.gif" width="16" height="16" alt="Icon" />
|
197
|
+
# image_tag("/icons/icon.gif", height: '32', width: '32')
|
198
|
+
# # => <img alt="Icon" height="32" src="/icons/icon.gif" width="32" />
|
199
|
+
# image_tag("/icons/icon.gif", class: "menu_icon")
|
200
|
+
# # => <img alt="Icon" class="menu_icon" src="/icons/icon.gif" />
|
201
|
+
def image_tag(source, options={})
|
202
|
+
options = options.symbolize_keys
|
203
|
+
|
204
|
+
src = options[:src] = path_to_image(source)
|
205
|
+
|
206
|
+
unless src =~ /^(?:cid|data):/ || src.blank?
|
207
|
+
options[:alt] = options.fetch(:alt){ image_alt(src) }
|
208
|
+
end
|
209
|
+
|
210
|
+
if size = options.delete(:size)
|
211
|
+
if size =~ %r{\A\d+x\d+\z}
|
212
|
+
options[:width], options[:height] = size.split('x')
|
213
|
+
elsif size =~ %r{\A\d+\z}
|
214
|
+
options[:width] = options[:height] = size
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
tag("img", options)
|
219
|
+
end
|
220
|
+
|
221
|
+
# Returns a string suitable for an html image tag alt attribute.
|
222
|
+
# The +src+ argument is meant to be an image file path.
|
223
|
+
# The method removes the basename of the file path and the digest,
|
224
|
+
# if any. It also removes hyphens and underscores from file names and
|
225
|
+
# replaces them with spaces, returning a space-separated, titleized
|
226
|
+
# string.
|
227
|
+
#
|
228
|
+
# ==== Examples
|
229
|
+
#
|
230
|
+
# image_alt('rails.png')
|
231
|
+
# # => Rails
|
232
|
+
#
|
233
|
+
# image_alt('hyphenated-file-name.png')
|
234
|
+
# # => Hyphenated file name
|
235
|
+
#
|
236
|
+
# image_alt('underscored_file_name.png')
|
237
|
+
# # => Underscored file name
|
238
|
+
def image_alt(src)
|
239
|
+
File.basename(src, '.*').sub(/-[[:xdigit:]]{32}\z/, '').tr('-_', ' ').capitalize
|
240
|
+
end
|
241
|
+
|
242
|
+
# Returns an html video tag for the +sources+. If +sources+ is a string,
|
243
|
+
# a single video tag will be returned. If +sources+ is an array, a video
|
244
|
+
# tag with nested source tags for each source will be returned. The
|
245
|
+
# +sources+ can be full paths or files that exists in your public videos
|
246
|
+
# directory.
|
247
|
+
#
|
248
|
+
# ==== Options
|
249
|
+
# You can add HTML attributes using the +options+. The +options+ supports
|
250
|
+
# two additional keys for convenience and conformance:
|
251
|
+
#
|
252
|
+
# * <tt>:poster</tt> - Set an image (like a screenshot) to be shown
|
253
|
+
# before the video loads. The path is calculated like the +src+ of +image_tag+.
|
254
|
+
# * <tt>:size</tt> - Supplied as "{Width}x{Height}", so "30x45" becomes
|
255
|
+
# width="30" and height="45". <tt>:size</tt> will be ignored if the
|
256
|
+
# value is not in the correct format.
|
257
|
+
#
|
258
|
+
# ==== Examples
|
259
|
+
#
|
260
|
+
# video_tag("trailer")
|
261
|
+
# # => <video src="/videos/trailer" />
|
262
|
+
# video_tag("trailer.ogg")
|
263
|
+
# # => <video src="/videos/trailer.ogg" />
|
264
|
+
# video_tag("trailer.ogg", controls: true, autobuffer: true)
|
265
|
+
# # => <video autobuffer="autobuffer" controls="controls" src="/videos/trailer.ogg" />
|
266
|
+
# video_tag("trailer.m4v", size: "16x10", poster: "screenshot.png")
|
267
|
+
# # => <video src="/videos/trailer.m4v" width="16" height="10" poster="/assets/screenshot.png" />
|
268
|
+
# video_tag("/trailers/hd.avi", size: "16x16")
|
269
|
+
# # => <video src="/trailers/hd.avi" width="16" height="16" />
|
270
|
+
# video_tag("/trailers/hd.avi", height: '32', width: '32')
|
271
|
+
# # => <video height="32" src="/trailers/hd.avi" width="32" />
|
272
|
+
# video_tag("trailer.ogg", "trailer.flv")
|
273
|
+
# # => <video><source src="/videos/trailer.ogg" /><source src="/videos/trailer.flv" /></video>
|
274
|
+
# video_tag(["trailer.ogg", "trailer.flv"])
|
275
|
+
# # => <video><source src="/videos/trailer.ogg" /><source src="/videos/trailer.flv" /></video>
|
276
|
+
# video_tag(["trailer.ogg", "trailer.flv"], size: "160x120")
|
277
|
+
# # => <video height="120" width="160"><source src="/videos/trailer.ogg" /><source src="/videos/trailer.flv" /></video>
|
278
|
+
def video_tag(*sources)
|
279
|
+
multiple_sources_tag('video', sources) do |options|
|
280
|
+
options[:poster] = path_to_image(options[:poster]) if options[:poster]
|
281
|
+
|
282
|
+
if size = options.delete(:size)
|
283
|
+
options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$}
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
# Returns an HTML audio tag for the +source+.
|
289
|
+
# The +source+ can be full path or file that exists in
|
290
|
+
# your public audios directory.
|
291
|
+
#
|
292
|
+
# audio_tag("sound")
|
293
|
+
# # => <audio src="/audios/sound" />
|
294
|
+
# audio_tag("sound.wav")
|
295
|
+
# # => <audio src="/audios/sound.wav" />
|
296
|
+
# audio_tag("sound.wav", autoplay: true, controls: true)
|
297
|
+
# # => <audio autoplay="autoplay" controls="controls" src="/audios/sound.wav" />
|
298
|
+
# audio_tag("sound.wav", "sound.mid")
|
299
|
+
# # => <audio><source src="/audios/sound.wav" /><source src="/audios/sound.mid" /></audio>
|
300
|
+
def audio_tag(*sources)
|
301
|
+
multiple_sources_tag('audio', sources)
|
302
|
+
end
|
303
|
+
|
304
|
+
private
|
305
|
+
def multiple_sources_tag(type, sources)
|
306
|
+
options = sources.extract_options!.symbolize_keys
|
307
|
+
sources.flatten!
|
308
|
+
|
309
|
+
yield options if block_given?
|
310
|
+
|
311
|
+
if sources.size > 1
|
312
|
+
content_tag(type, options) do
|
313
|
+
safe_join sources.map { |source| tag("source", :src => send("path_to_#{type}", source)) }
|
314
|
+
end
|
315
|
+
else
|
316
|
+
options[:src] = send("path_to_#{type}", sources.first)
|
317
|
+
content_tag(type, nil, options)
|
318
|
+
end
|
319
|
+
end
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
@@ -0,0 +1,355 @@
|
|
1
|
+
require 'zlib'
|
2
|
+
|
3
|
+
module ActionView
|
4
|
+
# = Action View Asset URL Helpers
|
5
|
+
module Helpers
|
6
|
+
# This module provides methods for generating asset paths and
|
7
|
+
# urls.
|
8
|
+
#
|
9
|
+
# image_path("rails.png")
|
10
|
+
# # => "/assets/rails.png"
|
11
|
+
#
|
12
|
+
# image_url("rails.png")
|
13
|
+
# # => "http://www.example.com/assets/rails.png"
|
14
|
+
#
|
15
|
+
# === Using asset hosts
|
16
|
+
#
|
17
|
+
# By default, Rails links to these assets on the current host in the public
|
18
|
+
# folder, but you can direct Rails to link to assets from a dedicated asset
|
19
|
+
# server by setting <tt>ActionController::Base.asset_host</tt> in the application
|
20
|
+
# configuration, typically in <tt>config/environments/production.rb</tt>.
|
21
|
+
# For example, you'd define <tt>assets.example.com</tt> to be your asset
|
22
|
+
# host this way, inside the <tt>configure</tt> block of your environment-specific
|
23
|
+
# configuration files or <tt>config/application.rb</tt>:
|
24
|
+
#
|
25
|
+
# config.action_controller.asset_host = "assets.example.com"
|
26
|
+
#
|
27
|
+
# Helpers take that into account:
|
28
|
+
#
|
29
|
+
# image_tag("rails.png")
|
30
|
+
# # => <img alt="Rails" src="http://assets.example.com/assets/rails.png" />
|
31
|
+
# stylesheet_link_tag("application")
|
32
|
+
# # => <link href="http://assets.example.com/assets/application.css" media="screen" rel="stylesheet" />
|
33
|
+
#
|
34
|
+
# Browsers typically open at most two simultaneous connections to a single
|
35
|
+
# host, which means your assets often have to wait for other assets to finish
|
36
|
+
# downloading. You can alleviate this by using a <tt>%d</tt> wildcard in the
|
37
|
+
# +asset_host+. For example, "assets%d.example.com". If that wildcard is
|
38
|
+
# present Rails distributes asset requests among the corresponding four hosts
|
39
|
+
# "assets0.example.com", ..., "assets3.example.com". With this trick browsers
|
40
|
+
# will open eight simultaneous connections rather than two.
|
41
|
+
#
|
42
|
+
# image_tag("rails.png")
|
43
|
+
# # => <img alt="Rails" src="http://assets0.example.com/assets/rails.png" />
|
44
|
+
# stylesheet_link_tag("application")
|
45
|
+
# # => <link href="http://assets2.example.com/assets/application.css" media="screen" rel="stylesheet" />
|
46
|
+
#
|
47
|
+
# To do this, you can either setup four actual hosts, or you can use wildcard
|
48
|
+
# DNS to CNAME the wildcard to a single asset host. You can read more about
|
49
|
+
# setting up your DNS CNAME records from your ISP.
|
50
|
+
#
|
51
|
+
# Note: This is purely a browser performance optimization and is not meant
|
52
|
+
# for server load balancing. See http://www.die.net/musings/page_load_time/
|
53
|
+
# for background.
|
54
|
+
#
|
55
|
+
# Alternatively, you can exert more control over the asset host by setting
|
56
|
+
# +asset_host+ to a proc like this:
|
57
|
+
#
|
58
|
+
# ActionController::Base.asset_host = Proc.new { |source|
|
59
|
+
# "http://assets#{Digest::MD5.hexdigest(source).to_i(16) % 2 + 1}.example.com"
|
60
|
+
# }
|
61
|
+
# image_tag("rails.png")
|
62
|
+
# # => <img alt="Rails" src="http://assets1.example.com/assets/rails.png" />
|
63
|
+
# stylesheet_link_tag("application")
|
64
|
+
# # => <link href="http://assets2.example.com/assets/application.css" media="screen" rel="stylesheet" />
|
65
|
+
#
|
66
|
+
# The example above generates "http://assets1.example.com" and
|
67
|
+
# "http://assets2.example.com". This option is useful for example if
|
68
|
+
# you need fewer/more than four hosts, custom host names, etc.
|
69
|
+
#
|
70
|
+
# As you see the proc takes a +source+ parameter. That's a string with the
|
71
|
+
# absolute path of the asset, for example "/assets/rails.png".
|
72
|
+
#
|
73
|
+
# ActionController::Base.asset_host = Proc.new { |source|
|
74
|
+
# if source.ends_with?('.css')
|
75
|
+
# "http://stylesheets.example.com"
|
76
|
+
# else
|
77
|
+
# "http://assets.example.com"
|
78
|
+
# end
|
79
|
+
# }
|
80
|
+
# image_tag("rails.png")
|
81
|
+
# # => <img alt="Rails" src="http://assets.example.com/assets/rails.png" />
|
82
|
+
# stylesheet_link_tag("application")
|
83
|
+
# # => <link href="http://stylesheets.example.com/assets/application.css" media="screen" rel="stylesheet" />
|
84
|
+
#
|
85
|
+
# Alternatively you may ask for a second parameter +request+. That one is
|
86
|
+
# particularly useful for serving assets from an SSL-protected page. The
|
87
|
+
# example proc below disables asset hosting for HTTPS connections, while
|
88
|
+
# still sending assets for plain HTTP requests from asset hosts. If you don't
|
89
|
+
# have SSL certificates for each of the asset hosts this technique allows you
|
90
|
+
# to avoid warnings in the client about mixed media.
|
91
|
+
#
|
92
|
+
# config.action_controller.asset_host = Proc.new { |source, request|
|
93
|
+
# if request.ssl?
|
94
|
+
# "#{request.protocol}#{request.host_with_port}"
|
95
|
+
# else
|
96
|
+
# "#{request.protocol}assets.example.com"
|
97
|
+
# end
|
98
|
+
# }
|
99
|
+
#
|
100
|
+
# You can also implement a custom asset host object that responds to +call+
|
101
|
+
# and takes either one or two parameters just like the proc.
|
102
|
+
#
|
103
|
+
# config.action_controller.asset_host = AssetHostingWithMinimumSsl.new(
|
104
|
+
# "http://asset%d.example.com", "https://asset1.example.com"
|
105
|
+
# )
|
106
|
+
#
|
107
|
+
module AssetUrlHelper
|
108
|
+
URI_REGEXP = %r{^[-a-z]+://|^(?:cid|data):|^//}i
|
109
|
+
|
110
|
+
# Computes the path to asset in public directory. If :type
|
111
|
+
# options is set, a file extension will be appended and scoped
|
112
|
+
# to the corresponding public directory.
|
113
|
+
#
|
114
|
+
# All other asset *_path helpers delegate through this method.
|
115
|
+
#
|
116
|
+
# asset_path "application.js" # => /application.js
|
117
|
+
# asset_path "application", type: :javascript # => /javascripts/application.js
|
118
|
+
# asset_path "application", type: :stylesheet # => /stylesheets/application.css
|
119
|
+
# asset_path "http://www.example.com/js/xmlhr.js" # => http://www.example.com/js/xmlhr.js
|
120
|
+
def asset_path(source, options = {})
|
121
|
+
source = source.to_s
|
122
|
+
return "" unless source.present?
|
123
|
+
return source if source =~ URI_REGEXP
|
124
|
+
|
125
|
+
tail, source = source[/([\?#].+)$/], source.sub(/([\?#].+)$/, '')
|
126
|
+
|
127
|
+
if extname = compute_asset_extname(source, options)
|
128
|
+
source = "#{source}#{extname}"
|
129
|
+
end
|
130
|
+
|
131
|
+
if source[0] != ?/
|
132
|
+
source = compute_asset_path(source, options)
|
133
|
+
end
|
134
|
+
|
135
|
+
relative_url_root = defined?(config.relative_url_root) && config.relative_url_root
|
136
|
+
if relative_url_root
|
137
|
+
source = "#{relative_url_root}#{source}" unless source.starts_with?("#{relative_url_root}/")
|
138
|
+
end
|
139
|
+
|
140
|
+
if host = compute_asset_host(source, options)
|
141
|
+
source = "#{host}#{source}"
|
142
|
+
end
|
143
|
+
|
144
|
+
"#{source}#{tail}"
|
145
|
+
end
|
146
|
+
alias_method :path_to_asset, :asset_path # aliased to avoid conflicts with an asset_path named route
|
147
|
+
|
148
|
+
# Computes the full URL to an asset in the public directory. This
|
149
|
+
# will use +asset_path+ internally, so most of their behaviors
|
150
|
+
# will be the same.
|
151
|
+
def asset_url(source, options = {})
|
152
|
+
path_to_asset(source, options.merge(:protocol => :request))
|
153
|
+
end
|
154
|
+
alias_method :url_to_asset, :asset_url # aliased to avoid conflicts with an asset_url named route
|
155
|
+
|
156
|
+
ASSET_EXTENSIONS = {
|
157
|
+
javascript: '.js',
|
158
|
+
stylesheet: '.css'
|
159
|
+
}
|
160
|
+
|
161
|
+
# Compute extname to append to asset path. Returns nil if
|
162
|
+
# nothing should be added.
|
163
|
+
def compute_asset_extname(source, options = {})
|
164
|
+
return if options[:extname] == false
|
165
|
+
extname = options[:extname] || ASSET_EXTENSIONS[options[:type]]
|
166
|
+
extname if extname && File.extname(source) != extname
|
167
|
+
end
|
168
|
+
|
169
|
+
# Maps asset types to public directory.
|
170
|
+
ASSET_PUBLIC_DIRECTORIES = {
|
171
|
+
audio: '/audios',
|
172
|
+
font: '/fonts',
|
173
|
+
image: '/images',
|
174
|
+
javascript: '/javascripts',
|
175
|
+
stylesheet: '/stylesheets',
|
176
|
+
video: '/videos'
|
177
|
+
}
|
178
|
+
|
179
|
+
# Computes asset path to public directory. Plugins and
|
180
|
+
# extensions can override this method to point to custom assets
|
181
|
+
# or generate digested paths or query strings.
|
182
|
+
def compute_asset_path(source, options = {})
|
183
|
+
dir = ASSET_PUBLIC_DIRECTORIES[options[:type]] || ""
|
184
|
+
File.join(dir, source)
|
185
|
+
end
|
186
|
+
|
187
|
+
# Pick an asset host for this source. Returns +nil+ if no host is set,
|
188
|
+
# the host if no wildcard is set, the host interpolated with the
|
189
|
+
# numbers 0-3 if it contains <tt>%d</tt> (the number is the source hash mod 4),
|
190
|
+
# or the value returned from invoking call on an object responding to call
|
191
|
+
# (proc or otherwise).
|
192
|
+
def compute_asset_host(source = "", options = {})
|
193
|
+
request = self.request if respond_to?(:request)
|
194
|
+
host = config.asset_host if defined? config.asset_host
|
195
|
+
host ||= request.base_url if request && options[:protocol] == :request
|
196
|
+
|
197
|
+
if host.respond_to?(:call)
|
198
|
+
arity = host.respond_to?(:arity) ? host.arity : host.method(:call).arity
|
199
|
+
args = [source]
|
200
|
+
args << request if request && (arity > 1 || arity < 0)
|
201
|
+
host = host.call(*args)
|
202
|
+
elsif host =~ /%d/
|
203
|
+
host = host % (Zlib.crc32(source) % 4)
|
204
|
+
end
|
205
|
+
|
206
|
+
return unless host
|
207
|
+
|
208
|
+
if host =~ URI_REGEXP
|
209
|
+
host
|
210
|
+
else
|
211
|
+
protocol = options[:protocol] || config.default_asset_host_protocol || (request ? :request : :relative)
|
212
|
+
case protocol
|
213
|
+
when :relative
|
214
|
+
"//#{host}"
|
215
|
+
when :request
|
216
|
+
"#{request.protocol}#{host}"
|
217
|
+
else
|
218
|
+
"#{protocol}://#{host}"
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
# Computes the path to a javascript asset in the public javascripts directory.
|
224
|
+
# If the +source+ filename has no extension, .js will be appended (except for explicit URIs)
|
225
|
+
# Full paths from the document root will be passed through.
|
226
|
+
# Used internally by javascript_include_tag to build the script path.
|
227
|
+
#
|
228
|
+
# javascript_path "xmlhr" # => /javascripts/xmlhr.js
|
229
|
+
# javascript_path "dir/xmlhr.js" # => /javascripts/dir/xmlhr.js
|
230
|
+
# javascript_path "/dir/xmlhr" # => /dir/xmlhr.js
|
231
|
+
# javascript_path "http://www.example.com/js/xmlhr" # => http://www.example.com/js/xmlhr
|
232
|
+
# javascript_path "http://www.example.com/js/xmlhr.js" # => http://www.example.com/js/xmlhr.js
|
233
|
+
def javascript_path(source, options = {})
|
234
|
+
path_to_asset(source, {type: :javascript}.merge!(options))
|
235
|
+
end
|
236
|
+
alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route
|
237
|
+
|
238
|
+
# Computes the full URL to a javascript asset in the public javascripts directory.
|
239
|
+
# This will use +javascript_path+ internally, so most of their behaviors will be the same.
|
240
|
+
def javascript_url(source, options = {})
|
241
|
+
url_to_asset(source, {type: :javascript}.merge!(options))
|
242
|
+
end
|
243
|
+
alias_method :url_to_javascript, :javascript_url # aliased to avoid conflicts with a javascript_url named route
|
244
|
+
|
245
|
+
# Computes the path to a stylesheet asset in the public stylesheets directory.
|
246
|
+
# If the +source+ filename has no extension, <tt>.css</tt> will be appended (except for explicit URIs).
|
247
|
+
# Full paths from the document root will be passed through.
|
248
|
+
# Used internally by +stylesheet_link_tag+ to build the stylesheet path.
|
249
|
+
#
|
250
|
+
# stylesheet_path "style" # => /stylesheets/style.css
|
251
|
+
# stylesheet_path "dir/style.css" # => /stylesheets/dir/style.css
|
252
|
+
# stylesheet_path "/dir/style.css" # => /dir/style.css
|
253
|
+
# stylesheet_path "http://www.example.com/css/style" # => http://www.example.com/css/style
|
254
|
+
# stylesheet_path "http://www.example.com/css/style.css" # => http://www.example.com/css/style.css
|
255
|
+
def stylesheet_path(source, options = {})
|
256
|
+
path_to_asset(source, {type: :stylesheet}.merge!(options))
|
257
|
+
end
|
258
|
+
alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with a stylesheet_path named route
|
259
|
+
|
260
|
+
# Computes the full URL to a stylesheet asset in the public stylesheets directory.
|
261
|
+
# This will use +stylesheet_path+ internally, so most of their behaviors will be the same.
|
262
|
+
def stylesheet_url(source, options = {})
|
263
|
+
url_to_asset(source, {type: :stylesheet}.merge!(options))
|
264
|
+
end
|
265
|
+
alias_method :url_to_stylesheet, :stylesheet_url # aliased to avoid conflicts with a stylesheet_url named route
|
266
|
+
|
267
|
+
# Computes the path to an image asset.
|
268
|
+
# Full paths from the document root will be passed through.
|
269
|
+
# Used internally by +image_tag+ to build the image path:
|
270
|
+
#
|
271
|
+
# image_path("edit") # => "/assets/edit"
|
272
|
+
# image_path("edit.png") # => "/assets/edit.png"
|
273
|
+
# image_path("icons/edit.png") # => "/assets/icons/edit.png"
|
274
|
+
# image_path("/icons/edit.png") # => "/icons/edit.png"
|
275
|
+
# image_path("http://www.example.com/img/edit.png") # => "http://www.example.com/img/edit.png"
|
276
|
+
#
|
277
|
+
# If you have images as application resources this method may conflict with their named routes.
|
278
|
+
# The alias +path_to_image+ is provided to avoid that. Rails uses the alias internally, and
|
279
|
+
# plugin authors are encouraged to do so.
|
280
|
+
def image_path(source, options = {})
|
281
|
+
path_to_asset(source, {type: :image}.merge!(options))
|
282
|
+
end
|
283
|
+
alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route
|
284
|
+
|
285
|
+
# Computes the full URL to an image asset.
|
286
|
+
# This will use +image_path+ internally, so most of their behaviors will be the same.
|
287
|
+
def image_url(source, options = {})
|
288
|
+
url_to_asset(source, {type: :image}.merge!(options))
|
289
|
+
end
|
290
|
+
alias_method :url_to_image, :image_url # aliased to avoid conflicts with an image_url named route
|
291
|
+
|
292
|
+
# Computes the path to a video asset in the public videos directory.
|
293
|
+
# Full paths from the document root will be passed through.
|
294
|
+
# Used internally by +video_tag+ to build the video path.
|
295
|
+
#
|
296
|
+
# video_path("hd") # => /videos/hd
|
297
|
+
# video_path("hd.avi") # => /videos/hd.avi
|
298
|
+
# video_path("trailers/hd.avi") # => /videos/trailers/hd.avi
|
299
|
+
# video_path("/trailers/hd.avi") # => /trailers/hd.avi
|
300
|
+
# video_path("http://www.example.com/vid/hd.avi") # => http://www.example.com/vid/hd.avi
|
301
|
+
def video_path(source, options = {})
|
302
|
+
path_to_asset(source, {type: :video}.merge!(options))
|
303
|
+
end
|
304
|
+
alias_method :path_to_video, :video_path # aliased to avoid conflicts with a video_path named route
|
305
|
+
|
306
|
+
# Computes the full URL to a video asset in the public videos directory.
|
307
|
+
# This will use +video_path+ internally, so most of their behaviors will be the same.
|
308
|
+
def video_url(source, options = {})
|
309
|
+
url_to_asset(source, {type: :video}.merge!(options))
|
310
|
+
end
|
311
|
+
alias_method :url_to_video, :video_url # aliased to avoid conflicts with an video_url named route
|
312
|
+
|
313
|
+
# Computes the path to an audio asset in the public audios directory.
|
314
|
+
# Full paths from the document root will be passed through.
|
315
|
+
# Used internally by +audio_tag+ to build the audio path.
|
316
|
+
#
|
317
|
+
# audio_path("horse") # => /audios/horse
|
318
|
+
# audio_path("horse.wav") # => /audios/horse.wav
|
319
|
+
# audio_path("sounds/horse.wav") # => /audios/sounds/horse.wav
|
320
|
+
# audio_path("/sounds/horse.wav") # => /sounds/horse.wav
|
321
|
+
# audio_path("http://www.example.com/sounds/horse.wav") # => http://www.example.com/sounds/horse.wav
|
322
|
+
def audio_path(source, options = {})
|
323
|
+
path_to_asset(source, {type: :audio}.merge!(options))
|
324
|
+
end
|
325
|
+
alias_method :path_to_audio, :audio_path # aliased to avoid conflicts with an audio_path named route
|
326
|
+
|
327
|
+
# Computes the full URL to an audio asset in the public audios directory.
|
328
|
+
# This will use +audio_path+ internally, so most of their behaviors will be the same.
|
329
|
+
def audio_url(source, options = {})
|
330
|
+
url_to_asset(source, {type: :audio}.merge!(options))
|
331
|
+
end
|
332
|
+
alias_method :url_to_audio, :audio_url # aliased to avoid conflicts with an audio_url named route
|
333
|
+
|
334
|
+
# Computes the path to a font asset.
|
335
|
+
# Full paths from the document root will be passed through.
|
336
|
+
#
|
337
|
+
# font_path("font") # => /assets/font
|
338
|
+
# font_path("font.ttf") # => /assets/font.ttf
|
339
|
+
# font_path("dir/font.ttf") # => /assets/dir/font.ttf
|
340
|
+
# font_path("/dir/font.ttf") # => /dir/font.ttf
|
341
|
+
# font_path("http://www.example.com/dir/font.ttf") # => http://www.example.com/dir/font.ttf
|
342
|
+
def font_path(source, options = {})
|
343
|
+
path_to_asset(source, {type: :font}.merge!(options))
|
344
|
+
end
|
345
|
+
alias_method :path_to_font, :font_path # aliased to avoid conflicts with an font_path named route
|
346
|
+
|
347
|
+
# Computes the full URL to a font asset.
|
348
|
+
# This will use +font_path+ internally, so most of their behaviors will be the same.
|
349
|
+
def font_url(source, options = {})
|
350
|
+
url_to_asset(source, {type: :font}.merge!(options))
|
351
|
+
end
|
352
|
+
alias_method :url_to_font, :font_url # aliased to avoid conflicts with an font_url named route
|
353
|
+
end
|
354
|
+
end
|
355
|
+
end
|