File: asset_propshaft_provider.rb

package info (click to toggle)
ruby-roadie-rails 3.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,280 kB
  • sloc: ruby: 1,670; sh: 47; makefile: 4
file content (41 lines) | stat: -rw-r--r-- 1,184 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen_string_literal: true

module Roadie
  module Rails
    class AssetPropshaftProvider
      include Roadie::AssetProvider

      def initialize(assembly)
        @assembly = assembly
        @stylesheets = {}
      end

      def find_stylesheet(stylesheet_asset_path)
        stylesheet = @stylesheets[stylesheet_asset_path]
        return stylesheet if stylesheet.present?

        path, digest = extract_path_and_digest(stylesheet_asset_path)

        asset = @assembly.load_path.find(path)
        if asset.present? && asset.fresh?(digest)
          compiled_content = @assembly.compilers.compile(asset)

          stylesheet = Stylesheet.new(stylesheet_asset_path, compiled_content.force_encoding("UTF-8"))
          @stylesheets[stylesheet_asset_path] = stylesheet
        end

        stylesheet
      end

      private

      def extract_path_and_digest(stylesheet_asset_path)
        full_path = stylesheet_asset_path.sub(%r{#{::Rails.configuration.assets.prefix}/}, "")
        digest = full_path[/-([0-9a-zA-Z]{7,128})\.(?!digested)[^.]+\z/, 1]
        path = digest ? full_path.sub("-#{digest}", "") : full_path

        [path, digest]
      end
    end
  end
end