Module: Hanami::Helpers::AssetsHelper Private
- Includes:
- View::Helpers::TagHelper
- Included in:
- Extensions::View::StandardHelpers
- Defined in:
- lib/hanami/helpers/assets_helper.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
HTML assets helpers
Inject these helpers in a view
Constant Summary collapse
- NEW_LINE_SEPARATOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"\n"
- WILDCARD_EXT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
".*"
- JAVASCRIPT_EXT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
".js"
- STYLESHEET_EXT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
".css"
- JAVASCRIPT_MIME_TYPE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"text/javascript"
- STYLESHEET_MIME_TYPE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"text/css"
- FAVICON_MIME_TYPE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"image/x-icon"
- STYLESHEET_REL =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"stylesheet"
- FAVICON_REL =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"shortcut icon"
- DEFAULT_FAVICON =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"favicon.ico"
- CROSSORIGIN_ANONYMOUS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"anonymous"
- ABSOLUTE_URL_MATCHER =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
URI::DEFAULT_PARSER.make_regexp
- QUERY_STRING_MATCHER =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
/\?/
Instance Method Summary collapse
-
#asset_url(source_path) ⇒ String
private
It generates the relative or absolute URL for the given asset.
-
#audio_tag(source = nil, options = {}, &blk) ⇒ Hanami::View::HTML::SafeString
private
Generate ‘audio` tag for given source.
-
#favicon_tag(source = DEFAULT_FAVICON, options = {}) ⇒ Hanami::View::HTML::SafeString
private
Generate ‘link` tag application favicon.
-
#image_tag(source, options = {}) ⇒ Hanami::View::HTML::SafeString
private
Generate ‘img` tag for given source.
-
#javascript_tag(*source_paths, **options) ⇒ Hanami::View::HTML::SafeString
private
Generate ‘script` tag for given source(s).
-
#stylesheet_tag(*source_paths, **options) ⇒ Hanami::View::HTML::SafeString
private
Generate ‘link` tag for given source(s).
-
#video_tag(source = nil, options = {}, &blk) ⇒ Hanami::View::HTML::SafeString
private
Generate ‘video` tag for given source.
Instance Method Details
#asset_url(source_path) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
It generates the relative or absolute URL for the given asset. It automatically decides if it has to use the relative or absolute depending on the configuration and current environment.
Absolute URLs are returned as they are.
It can be the name of the asset, coming from the sources or third party gems.
If Fingerprint mode is on, it returns the fingerprinted path of the source
If CDN mode is on, it returns the absolute URL of the asset.
‘subresource_integrity` modes are on and the asset is missing from the manifest
668 669 670 671 672 673 |
# File 'lib/hanami/helpers/assets_helper.rb', line 668 def asset_url(source_path) return source_path.url if source_path.respond_to?(:url) return source_path if _absolute_url?(source_path) _context.assets[source_path].url end |
#audio_tag(source = nil, options = {}, &blk) ⇒ Hanami::View::HTML::SafeString
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generate ‘audio` tag for given source
It accepts one string representing the name of the asset, if it comes from the application or third party gems. It also accepts strings representing absolute URLs in case of public CDN (eg. Bootstrap CDN).
Alternatively, it accepts a block that allows to specify one or more sources via the ‘source` tag.
If the “fingerprint mode” is on, ‘src` is the fingerprinted version of the relative URL.
If the “CDN mode” is on, the ‘src` is an absolute URL of the application CDN.
‘subresource_integrity` modes are on and the audio file is missing from the manifest
611 612 613 614 |
# File 'lib/hanami/helpers/assets_helper.rb', line 611 def audio_tag(source = nil, = {}, &blk) = (source, , &blk) tag.audio(**, &blk) end |
#favicon_tag(source = DEFAULT_FAVICON, options = {}) ⇒ Hanami::View::HTML::SafeString
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generate ‘link` tag application favicon.
If no argument is given, it assumes ‘favico.ico` from the application.
It accepts one string representing the name of the asset.
If the “fingerprint mode” is on, ‘href` is the fingerprinted version of the relative URL.
If the “CDN mode” is on, the ‘href` is an absolute URL of the application CDN.
‘subresource_integrity` modes are on and the favicon is file missing from the manifest
399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/hanami/helpers/assets_helper.rb', line 399 def favicon_tag(source = DEFAULT_FAVICON, = {}) = .reject { |k, _| k.to_sym == :href } attributes = { href: asset_url(source), rel: FAVICON_REL, type: FAVICON_MIME_TYPE } attributes.merge!() tag.link(**attributes) end |
#image_tag(source, options = {}) ⇒ Hanami::View::HTML::SafeString
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generate ‘img` tag for given source
It accepts one string representing the name of the asset, if it comes from the application or third party gems. It also accepts strings representing absolute URLs in case of public CDN (eg. Bootstrap CDN).
‘alt` Attribute is auto generated from `src`. You can specify a different value, by passing the `:src` option.
If the “fingerprint mode” is on, ‘src` is the fingerprinted version of the relative URL.
If the “CDN mode” is on, the ‘src` is an absolute URL of the application CDN.
‘subresource_integrity` modes are on and the image file is missing from the manifest
333 334 335 336 337 338 339 340 341 342 |
# File 'lib/hanami/helpers/assets_helper.rb', line 333 def image_tag(source, = {}) = .reject { |k, _| k.to_sym == :src } attributes = { src: asset_url(source), alt: _context.inflector.humanize(::File.basename(source, WILDCARD_EXT)) } attributes.merge!() tag.img(**attributes) end |
#javascript_tag(*source_paths, **options) ⇒ Hanami::View::HTML::SafeString
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generate ‘script` tag for given source(s)
It accepts one or more strings representing the name of the asset, if it comes from the application or third party gems. It also accepts strings representing absolute URLs in case of public CDN (eg. jQuery CDN).
If the “fingerprint mode” is on, ‘src` is the fingerprinted version of the relative URL.
If the “CDN mode” is on, the ‘src` is an absolute URL of the application CDN.
If the “subresource integrity mode” is on, ‘integrity` is the name of the algorithm, then a hyphen, then the hash value of the file. If more than one algorithm is used, they“ll be separated by a space.
‘subresource_integrity` modes are on and the javascript file is missing from the manifest
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/hanami/helpers/assets_helper.rb', line 157 def javascript_tag(*source_paths, **) = .reject { |k, _| k.to_sym == :src } (*source_paths) do |source| attributes = { src: _typed_path(source, JAVASCRIPT_EXT), type: JAVASCRIPT_MIME_TYPE } attributes.merge!() if _context.assets.subresource_integrity? || attributes.include?(:integrity) attributes[:integrity] ||= _subresource_integrity_value(source, JAVASCRIPT_EXT) attributes[:crossorigin] ||= CROSSORIGIN_ANONYMOUS end tag.script(**attributes).to_s end end |
#stylesheet_tag(*source_paths, **options) ⇒ Hanami::View::HTML::SafeString
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generate ‘link` tag for given source(s)
It accepts one or more strings representing the name of the asset, if it comes from the application or third party gems. It also accepts strings representing absolute URLs in case of public CDN (eg. Bootstrap CDN).
If the “fingerprint mode” is on, ‘href` is the fingerprinted version of the relative URL.
If the “CDN mode” is on, the ‘href` is an absolute URL of the application CDN.
If the “subresource integrity mode” is on, ‘integrity` is the name of the algorithm, then a hyphen, then the hashed value of the file. If more than one algorithm is used, they“ll be separated by a space.
‘subresource_integrity` modes are on and the stylesheet file is missing from the manifest
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/hanami/helpers/assets_helper.rb', line 250 def stylesheet_tag(*source_paths, **) = .reject { |k, _| k.to_sym == :href } (*source_paths) do |source_path| attributes = { href: _typed_path(source_path, STYLESHEET_EXT), type: STYLESHEET_MIME_TYPE, rel: STYLESHEET_REL } attributes.merge!() if _context.assets.subresource_integrity? || attributes.include?(:integrity) attributes[:integrity] ||= _subresource_integrity_value(source_path, STYLESHEET_EXT) attributes[:crossorigin] ||= CROSSORIGIN_ANONYMOUS end tag.link(**attributes).to_s end end |
#video_tag(source = nil, options = {}, &blk) ⇒ Hanami::View::HTML::SafeString
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generate ‘video` tag for given source
It accepts one string representing the name of the asset, if it comes from the application or third party gems. It also accepts strings representing absolute URLs in case of public CDN (eg. Bootstrap CDN).
Alternatively, it accepts a block that allows to specify one or more sources via the ‘source` tag.
If the “fingerprint mode” is on, ‘src` is the fingerprinted version of the relative URL.
If the “CDN mode” is on, the ‘src` is an absolute URL of the application CDN.
‘subresource_integrity` modes are on and the video file is missing from the manifest
509 510 511 512 |
# File 'lib/hanami/helpers/assets_helper.rb', line 509 def video_tag(source = nil, = {}, &blk) = (source, , &blk) tag.video(**, &blk) end |