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



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, options = {})
  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, 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

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)
  options = sources.extract_options!.stringify_keys
  path_options = options.extract!("protocol", "extname", "host", "skip_pipeline").symbolize_keys
  early_hints_links = []

  sources_tags = sources.uniq.map { |source|
    href = path_to_javascript(source, path_options)
    early_hints_links << "<#{href}>; rel=preload; as=script"
    tag_options = {
      "src" => href
    }.merge!(options)
    
    if tag_options["nonce"] == true
      tag_options["nonce"] = content_security_policy_nonce
    end
    
    if secure_subresource_integrity_context?
      if tag_options["integrity"] == true
        tag_options["integrity"] = asset_integrity(source.to_s.delete_suffix('.js')+'.js')
      elsif tag_options["integrity"] == false
        tag_options.delete('integrity')
      end
    else
      tag_options.delete('integrity')
    end
    
    ("script", "", tag_options)
  }.join("\n").html_safe

  request.send_early_hints("Link" => early_hints_links.join("\n")) if respond_to?(:request) && request

  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.



51
52
53
# File 'lib/condenser/rails/helper.rb', line 51

def resolve_asset_path(path) #:nodoc:
  asset_resolver.asset_path(path)
end

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)
  options = sources.extract_options!.stringify_keys
  path_options = options.extract!("protocol", "host", "skip_pipeline").symbolize_keys
  early_hints_links = []

  sources_tags = sources.uniq.map { |source|
    href = path_to_stylesheet(source, path_options)
    early_hints_links << "<#{href}>; rel=preload; as=style"
    tag_options = {
      "rel" => "stylesheet",
      "media" => "screen",
      "href" => href
    }.merge!(options)
    
    if secure_subresource_integrity_context?
      if tag_options["integrity"] == true
        tag_options["integrity"] = asset_integrity(source.to_s.delete_suffix('.css')+'.css')
      elsif tag_options["integrity"] == false
        tag_options.delete('integrity')
      end
    else
      tag_options.delete('integrity')
    end

    tag(:link, tag_options)
  }.join("\n").html_safe

  request.send_early_hints("Link" => early_hints_links.join("\n")) if respond_to?(:request) && request

  sources_tags
end