Module: Condenser::Rails::Helper
- Includes:
- ActionView::Helpers::AssetTagHelper, ActionView::Helpers::AssetUrlHelper
- Defined in:
- lib/condenser/rails/helper.rb
Constant Summary collapse
- VIEW_ACCESSORS =
[ :assets, :assets_manifest, :assets_precompiled, :assets_precompiled_regexes, :assets_prefix, :resolve_assets_with ]
Class Method Summary collapse
Instance Method Summary collapse
-
#asset_integrity(path, options = {}) ⇒ Object
Get integrity for asset path.
-
#compute_asset_path(path, options = {}) ⇒ Object
Writes over the built in ActionView::Helpers::AssetUrlHelper#compute_asset_path to use the asset pipeline.
-
#javascript_include_tag(*sources) ⇒ Object
Override javascript tag helper to provide debugging support.
-
#resolve_asset_path(path) ⇒ Object
Resolve the asset path against the Condenser manifest or environment.
-
#stylesheet_link_tag(*sources) ⇒ Object
Override stylesheet tag helper to provide debugging support.
Class Method Details
.extended(obj) ⇒ Object
32 33 34 35 36 |
# File 'lib/condenser/rails/helper.rb', line 32 def self.extended(obj) obj.class_eval do attr_accessor(*VIEW_ACCESSORS) end end |
.included(klass) ⇒ Object
28 29 30 |
# File 'lib/condenser/rails/helper.rb', line 28 def self.included(klass) klass.class_attribute(*VIEW_ACCESSORS) end |
Instance Method Details
#asset_integrity(path, options = {}) ⇒ Object
Get integrity for asset path.
path - String path options - Hash options
Returns String integrity attribute or nil if no asset was found.
61 62 63 |
# File 'lib/condenser/rails/helper.rb', line 61 def asset_integrity(path, = {}) asset_resolver.integrity(path) end |
#compute_asset_path(path, options = {}) ⇒ Object
Writes over the built in ActionView::Helpers::AssetUrlHelper#compute_asset_path to use the asset pipeline.
40 41 42 43 44 45 46 47 |
# File 'lib/condenser/rails/helper.rb', line 40 def compute_asset_path(path, = {}) if asset_path = resolve_asset_path(path) File.join(assets_prefix || "/", asset_path) else raise Condenser::Rails::AssetNotPrecompiledError.new(path) # raise AssetNotFound, "The asset #{ path.inspect } is not present in the asset pipeline.\n" end end |
#javascript_include_tag(*sources) ⇒ Object
Override javascript tag helper to provide debugging support.
Eventually will be deprecated and replaced by source maps.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/condenser/rails/helper.rb', line 68 def javascript_include_tag(*sources) = sources..stringify_keys = .extract!("protocol", "extname", "host", "skip_pipeline").symbolize_keys early_hints_links = [] = sources.uniq.map { |source| href = path_to_javascript(source, ) early_hints_links << "<#{href}>; rel=preload; as=script" = { "src" => href }.merge!() if ["nonce"] == true ["nonce"] = content_security_policy_nonce end if secure_subresource_integrity_context? if ["integrity"] == true ["integrity"] = asset_integrity(source.to_s.delete_suffix('.js')+'.js') elsif ["integrity"] == false .delete('integrity') end else .delete('integrity') end content_tag("script", "", ) }.join("\n").html_safe request.send_early_hints("Link" => early_hints_links.join("\n")) if respond_to?(:request) && request end |
#resolve_asset_path(path) ⇒ Object
Resolve the asset path against the Condenser manifest or environment. Returns nil if it’s an asset we don’t know about.
51 52 53 |
# File 'lib/condenser/rails/helper.rb', line 51 def resolve_asset_path(path) #:nodoc: asset_resolver.asset_path(path) end |
#stylesheet_link_tag(*sources) ⇒ Object
Override stylesheet tag helper to provide debugging support.
Eventually will be deprecated and replaced by source maps.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/condenser/rails/helper.rb', line 105 def stylesheet_link_tag(*sources) = sources..stringify_keys = .extract!("protocol", "host", "skip_pipeline").symbolize_keys early_hints_links = [] = sources.uniq.map { |source| href = path_to_stylesheet(source, ) early_hints_links << "<#{href}>; rel=preload; as=style" = { "rel" => "stylesheet", "media" => "screen", "href" => href }.merge!() if secure_subresource_integrity_context? if ["integrity"] == true ["integrity"] = asset_integrity(source.to_s.delete_suffix('.css')+'.css') elsif ["integrity"] == false .delete('integrity') end else .delete('integrity') end tag(:link, ) }.join("\n").html_safe request.send_early_hints("Link" => early_hints_links.join("\n")) if respond_to?(:request) && request end |