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.
-
#asset_type(path, options = {}) ⇒ Object
Get type 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
TODO: perhaps prepend this function and add integrity if set to true?.
-
#resolve_asset_path(path) ⇒ Object
Resolve the asset path against the Condenser manifest or environment.
-
#stylesheet_link_tag(*sources) ⇒ Object
TODO: perhaps prepend this function and add integrity if set to true?.
Class Method Details
.extended(obj) ⇒ Object
34 35 36 37 38 |
# File 'lib/condenser/rails/helper.rb', line 34 def self.extended(obj) obj.class_eval do attr_accessor(*VIEW_ACCESSORS) end end |
.included(klass) ⇒ Object
30 31 32 |
# File 'lib/condenser/rails/helper.rb', line 30 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.
63 64 65 |
# File 'lib/condenser/rails/helper.rb', line 63 def asset_integrity(path, = {}) asset_resolver.integrity(path) end |
#asset_type(path, options = {}) ⇒ Object
Get type for asset path. (module or nil for javascript)
path - String path options - Hash options
Returns String integrity attribute or nil if no asset was found.
73 74 75 |
# File 'lib/condenser/rails/helper.rb', line 73 def asset_type(path, = {}) asset_resolver.type(path) end |
#compute_asset_path(path, options = {}) ⇒ Object
Writes over the built in ActionView::Helpers::AssetUrlHelper#compute_asset_path to use the asset pipeline.
42 43 44 45 46 47 48 49 |
# File 'lib/condenser/rails/helper.rb', line 42 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
TODO: perhaps prepend this function and add integrity if set to true?
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/condenser/rails/helper.rb', line 78 def javascript_include_tag(*sources) = sources..stringify_keys = .extract!("protocol", "extname", "host", "skip_pipeline").symbolize_keys preload_links = [] use_preload_links_header = ["preload_links_header"].nil? ? preload_links_header : .delete("preload_links_header") nopush = ["nopush"].nil? ? true : .delete("nopush") crossorigin = .delete("crossorigin") crossorigin = "anonymous" if crossorigin == true integrity = ["integrity"] = sources.uniq.map { |source| href = path_to_javascript(source, ) integrity = if ["integrity"] == true asset_integrity(source.to_s.delete_suffix('.js')+'.js') elsif ["integrity"] != false ["integrity"] end type = if !.has_key?('type') asset_type(source.to_s.delete_suffix('.js')+'.js') else ["type"] end rel = ["type"] == "module" ? "modulepreload" : "preload" if use_preload_links_header && !["defer"] && href.present? && !href.start_with?("data:") preload_link = "<#{href}>; rel=#{rel}; as=script" preload_link += "; crossorigin=#{crossorigin}" unless crossorigin.nil? preload_link += "; integrity=#{integrity}" unless integrity.nil? preload_link += "; nonce=#{content_security_policy_nonce}" if ["nonce"] == true preload_link += "; nopush" if nopush preload_links << preload_link end = { "src" => href, "crossorigin" => crossorigin }.merge!(.except('integrity', 'type')) if ["nonce"] == true ["nonce"] = content_security_policy_nonce end ['type'] = type if type ['integrity'] = integrity if integrity content_tag("script", "", ) }.join("\n").html_safe if use_preload_links_header send_preload_links_header(preload_links) end 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.
53 54 55 |
# File 'lib/condenser/rails/helper.rb', line 53 def resolve_asset_path(path) #:nodoc: asset_resolver.asset_path(path) end |
#stylesheet_link_tag(*sources) ⇒ Object
TODO: perhaps prepend this function and add integrity if set to true?
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/condenser/rails/helper.rb', line 131 def stylesheet_link_tag(*sources) = sources..stringify_keys = .extract!("protocol", "extname", "host", "skip_pipeline").symbolize_keys use_preload_links_header = ["preload_links_header"].nil? ? preload_links_header : .delete("preload_links_header") preload_links = [] crossorigin = .delete("crossorigin") crossorigin = "anonymous" if crossorigin == true nopush = ["nopush"].nil? ? true : .delete("nopush") = sources.uniq.map { |source| href = path_to_stylesheet(source, ) integrity = if ["integrity"] == true asset_integrity(source.to_s.delete_suffix('.css')+'.css') elsif ["integrity"] != false ["integrity"] end if use_preload_links_header && href.present? && !href.start_with?("data:") preload_link = "<#{href}>; rel=preload; as=style" preload_link += "; crossorigin=#{crossorigin}" unless crossorigin.nil? preload_link += "; integrity=#{integrity}" unless integrity.nil? preload_link += "; nopush" if nopush preload_links << preload_link end = { "rel" => "stylesheet", "crossorigin" => crossorigin, "href" => href }.merge!(.except('integrity')) if ["nonce"] == true ["nonce"] = content_security_policy_nonce end ['integrity'] = integrity if integrity if apply_stylesheet_media_default && ["media"].blank? ["media"] = "screen" end tag(:link, ) }.join("\n").html_safe if use_preload_links_header send_preload_links_header(preload_links) end end |