Module: Hanami::Assets::Helpers
- Includes:
- Helpers::HtmlHelper
- Defined in:
- lib/hanami/assets/helpers.rb
Overview
HTML assets helpers
Include this helper 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".freeze
- 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.
'.*'.freeze
- 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'.freeze
- 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'.freeze
- 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'.freeze
- 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'.freeze
- 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'.freeze
- 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'.freeze
- 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'.freeze
- 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'.freeze
- 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'.freeze
- 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.
/\?/
Class Method Summary collapse
-
.included(base) ⇒ Object
private
Inject helpers into the given class.
Instance Method Summary collapse
-
#asset_path(source) ⇒ String
It generates the relative URL for the given source.
-
#asset_url(source) ⇒ String
It generates the absolute URL for the given source.
-
#audio(source = nil, options = {}, &blk) ⇒ Hanami::Utils::Helpers::HtmlBuilder
Generate
audiotag for given source. -
#favicon(source = DEFAULT_FAVICON, options = {}) ⇒ Hanami::Utils::Helpers::HtmlBuilder
Generate
linktag application favicon. -
#image(source, options = {}) ⇒ Hanami::Utils::Helpers::HtmlBuilder
Generate
imgtag for given source. -
#javascript(*sources, **options) ⇒ Hanami::Utils::Escape::SafeString
Generate
scripttag for given source(s). -
#stylesheet(*sources, **options) ⇒ Hanami::Utils::Escape::SafeString
Generate
linktag for given source(s). -
#video(source = nil, options = {}, &blk) ⇒ Hanami::Utils::Helpers::HtmlBuilder
Generate
videotag for given source.
Class Method Details
.included(base) ⇒ Object
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.
Inject helpers into the given class
75 76 77 78 79 80 81 82 83 |
# File 'lib/hanami/assets/helpers.rb', line 75 def self.included(base) conf = ::Hanami::Assets::Configuration.for(base) base.class_eval do include Utils::ClassAttribute class_attribute :assets_configuration self.assets_configuration = conf end end |
Instance Method Details
#asset_path(source) ⇒ String
It generates the relative URL for the given source.
It can be the name of the asset, coming from the sources or third party gems.
Absolute URLs are returned as they are.
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
689 690 691 |
# File 'lib/hanami/assets/helpers.rb', line 689 def asset_path(source) _asset_url(source) { _relative_url(source) } end |
#asset_url(source) ⇒ String
It generates the absolute URL for the given source.
It can be the name of the asset, coming from the sources or third party gems.
Absolute URLs are returned as they are.
If Fingerprint mode is on, it returns the fingerprint URL 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
737 738 739 |
# File 'lib/hanami/assets/helpers.rb', line 737 def asset_url(source) _asset_url(source) { _absolute_url(source) } end |
#audio(source = nil, options = {}, &blk) ⇒ Hanami::Utils::Helpers::HtmlBuilder
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 string 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
640 641 642 643 |
# File 'lib/hanami/assets/helpers.rb', line 640 def audio(source = nil, = {}, &blk) = (source, , &blk) html.audio(blk, ) end |
#favicon(source = DEFAULT_FAVICON, options = {}) ⇒ Hanami::Utils::Helpers::HtmlBuilder
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 |
# File 'lib/hanami/assets/helpers.rb', line 399 def favicon(source = DEFAULT_FAVICON, = {}) [:href] = asset_path(source) [:rel] ||= FAVICON_REL [:type] ||= FAVICON_MIME_TYPE html.link() end |
#image(source, options = {}) ⇒ Hanami::Utils::Helpers::HtmlBuilder
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 string 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
337 338 339 340 341 342 |
# File 'lib/hanami/assets/helpers.rb', line 337 def image(source, = {}) [:src] = asset_path(source) [:alt] ||= Utils::String.titleize(::File.basename(source, WILDCARD_EXT)) html.img() end |
#javascript(*sources, **options) ⇒ Hanami::Utils::Escape::SafeString
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, integriy 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
169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/hanami/assets/helpers.rb', line 169 def javascript(*sources, **) (*sources) do |source| = .dup [:src] ||= _typed_asset_path(source, JAVASCRIPT_EXT) [:type] ||= JAVASCRIPT_MIME_TYPE if _subresource_integrity? || .include?(:integrity) [:integrity] ||= _subresource_integrity_value(source, JAVASCRIPT_EXT) [:crossorigin] ||= CROSSORIGIN_ANONYMOUS end html.script(**).to_s end end |
#stylesheet(*sources, **options) ⇒ Hanami::Utils::Escape::SafeString
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, integriy 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
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/hanami/assets/helpers.rb', line 256 def stylesheet(*sources, **) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength (*sources) do |source| = .dup [:href] ||= _typed_asset_path(source, STYLESHEET_EXT) [:type] ||= STYLESHEET_MIME_TYPE [:rel] ||= STYLESHEET_REL if _subresource_integrity? || .include?(:integrity) [:integrity] ||= _subresource_integrity_value(source, STYLESHEET_EXT) [:crossorigin] ||= CROSSORIGIN_ANONYMOUS end html.link(**).to_s end end |
#video(source = nil, options = {}, &blk) ⇒ Hanami::Utils::Helpers::HtmlBuilder
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 string 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
521 522 523 524 |
# File 'lib/hanami/assets/helpers.rb', line 521 def video(source = nil, = {}, &blk) = (source, , &blk) html.video(blk, ) end |