Module: Propshaft::Helper

Defined in:
lib/propshaft/helper.rb

Overview

Helper module that provides asset path resolution and integrity support for Rails applications.

This module extends Rails’ built-in asset helpers with additional functionality:

  • Subresource Integrity (SRI) support for enhanced security

  • Bulk stylesheet inclusion with :all and :app options

  • Asset path resolution with proper error handling

Subresource Integrity (SRI) Support

SRI helps protect against malicious modifications of assets by ensuring that resources fetched from CDNs or other sources haven’t been tampered with.

SRI is automatically enabled in secure contexts (HTTPS or local development) when the ‘integrity’ option is set to true:

<%= stylesheet_link_tag "application", integrity: true %>
<%= javascript_include_tag "application", integrity: true %>

This will generate integrity hashes and include them in the HTML:

<link rel="stylesheet" href="/assets/application-abc123.css"
      integrity="sha256-xyz789...">
<script src="/assets/application-def456.js"
        integrity="sha256-uvw012..."></script>

Bulk Stylesheet Inclusion

The stylesheet_link_tag helper supports special symbols for bulk inclusion:

  • :all - includes all CSS files found in the load path

  • :app - includes only CSS files from app/assets/*/.css

    <%= stylesheet_link_tag :all %> # All stylesheets <%= stylesheet_link_tag :app %> # Only app stylesheets

Instance Method Summary collapse

Instance Method Details

#all_stylesheets_pathsObject

Returns a sorted and unique array of logical paths for all stylesheets in the load path.



107
108
109
# File 'lib/propshaft/helper.rb', line 107

def all_stylesheets_paths
  Rails.application.assets.load_path.asset_paths_by_type("css")
end

#app_stylesheets_pathsObject

Returns a sorted and unique array of logical paths for all stylesheets in app/assets/*/.css.



112
113
114
# File 'lib/propshaft/helper.rb', line 112

def app_stylesheets_paths
  Rails.application.assets.load_path.asset_paths_by_glob("#{Rails.root.join("app/assets")}/**/*.css")
end

#asset_integrity(path, options = {}) ⇒ Object

Computes the Subresource Integrity (SRI) hash for the given asset path.

This method generates a cryptographic hash of the asset content that can be used to verify the integrity of the resource when it’s loaded by the browser.

asset_integrity("application.css")
# => "sha256-xyz789abcdef..."


43
44
45
46
# File 'lib/propshaft/helper.rb', line 43

def asset_integrity(path, options = {})
  path = _path_with_extname(path, options)
  Rails.application.assets.resolver.integrity(path)
end

#compute_asset_path(path, options = {}) ⇒ Object

Resolves the full path for an asset, raising an error if not found.



49
50
51
# File 'lib/propshaft/helper.rb', line 49

def compute_asset_path(path, options = {})
  Rails.application.assets.resolver.resolve(path) || raise(MissingAssetError.new(path))
end

#javascript_include_tag(*sources) ⇒ Object

Enhanced javascript_include_tag with automatic SRI (Subresource Integrity) support.

This method extends Rails’ built-in javascript_include_tag to automatically generate and include integrity hashes when running in secure contexts.

Options

  • :integrity - Enable SRI hash generation

Examples

javascript_include_tag "application", integrity: true
# => <script src="/assets/application-abc123.js"
#           integrity="sha256-xyz789..."></script>


100
101
102
103
104
# File 'lib/propshaft/helper.rb', line 100

def javascript_include_tag(*sources)
  options = sources.extract_options!

  _build_asset_tags(sources, options, :javascript) { |source, opts| super(source, opts) }
end

Enhanced stylesheet_link_tag with integrity support and bulk inclusion options.

In addition to the standard Rails functionality, this method supports:

  • Automatic SRI (Subresource Integrity) hash generation in secure contexts

  • Add an option to call stylesheet_link_tag with :all to include every css file found on the load path or :app to include css files found in Rails.root("app/assets/*/.css"), which will exclude lib/ and plugins.

Options

  • :integrity - Enable SRI hash generation

Examples

stylesheet_link_tag "application", integrity: true
# => <link rel="stylesheet" href="/assets/application-abc123.css"
#          integrity="sha256-xyz789...">

stylesheet_link_tag :all    # All stylesheets in load path
stylesheet_link_tag :app    # Only app/assets stylesheets


73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/propshaft/helper.rb', line 73

def stylesheet_link_tag(*sources)
  options = sources.extract_options!

  case sources.first
  when :all
    sources = all_stylesheets_paths
  when :app
    sources = app_stylesheets_paths
  end

  _build_asset_tags(sources, options, :stylesheet) { |source, opts| super(source, opts) }
end