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

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, options = {})
  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, options = {})
  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, options = {})
  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)
  options = sources.extract_options!.stringify_keys
  path_options = options.extract!("protocol", "extname", "host", "skip_pipeline").symbolize_keys
  preload_links = []
  use_preload_links_header = options["preload_links_header"].nil? ? preload_links_header : options.delete("preload_links_header")
  nopush = options["nopush"].nil? ? true : options.delete("nopush")
  crossorigin = options.delete("crossorigin")
  crossorigin = "anonymous" if crossorigin == true
  integrity = options["integrity"]

  sources_tags = sources.uniq.map { |source|
    href = path_to_javascript(source, path_options)
    integrity = if options["integrity"] == true
      asset_integrity(source.to_s.delete_suffix('.js')+'.js')
    elsif options["integrity"] != false
      options["integrity"]
    end
    type = if !options.has_key?('type')
      asset_type(source.to_s.delete_suffix('.js')+'.js') 
    else
      options["type"]
    end
    rel = options["type"] == "module" ? "modulepreload" : "preload"
    
    if use_preload_links_header && !options["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 options["nonce"] == true
      preload_link += "; nopush" if nopush
      preload_links << preload_link
    end
    tag_options = {
      "src" => href,
      "crossorigin" => crossorigin
    }.merge!(options.except('integrity', 'type'))
    if tag_options["nonce"] == true
      tag_options["nonce"] = content_security_policy_nonce
    end
    tag_options['type'] = type if type
    tag_options['integrity'] = integrity if integrity
    
    ("script", "", tag_options)
  }.join("\n").html_safe

  if use_preload_links_header
    send_preload_links_header(preload_links)
  end

  sources_tags
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

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)
  options = sources.extract_options!.stringify_keys
  path_options = options.extract!("protocol", "extname", "host", "skip_pipeline").symbolize_keys
  use_preload_links_header = options["preload_links_header"].nil? ? preload_links_header : options.delete("preload_links_header")
  preload_links = []
  crossorigin = options.delete("crossorigin")
  crossorigin = "anonymous" if crossorigin == true
  nopush = options["nopush"].nil? ? true : options.delete("nopush")
  
  sources_tags = sources.uniq.map { |source|
    href = path_to_stylesheet(source, path_options)
    integrity = if options["integrity"] == true
      asset_integrity(source.to_s.delete_suffix('.css')+'.css')
    elsif options["integrity"] != false
      options["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
    tag_options = {
      "rel" => "stylesheet",
      "crossorigin" => crossorigin,
      "href" => href
    }.merge!(options.except('integrity'))
    if tag_options["nonce"] == true
      tag_options["nonce"] = content_security_policy_nonce
    end
    tag_options['integrity'] = integrity if integrity

    if apply_stylesheet_media_default && tag_options["media"].blank?
      tag_options["media"] = "screen"
    end
    
    tag(:link, tag_options)
  }.join("\n").html_safe

  if use_preload_links_header
    send_preload_links_header(preload_links)
  end

  sources_tags
end