Module: Solidstats::AssetCompatibility

Extended by:
AssetCompatibility
Included in:
AssetCompatibility
Defined in:
lib/solidstats/asset_compatibility.rb

Overview

Asset compatibility layer for Rails engines Provides universal asset loading across different Rails asset pipelines

Instance Method Summary collapse

Instance Method Details

#add_asset_helpers(app) ⇒ Object

Add universal asset helper methods

Parameters:

  • app (Rails::Application)

    Rails application instance



60
61
62
63
64
65
66
67
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/solidstats/asset_compatibility.rb', line 60

def add_asset_helpers(app)
  return unless defined?(ActionView::Base)

  ActionView::Base.class_eval do
    # Universal stylesheet helper for Solidstats
    def solidstats_stylesheets(*args)
      options = args.extract_options!
      strategy = Solidstats.asset_strategy

      case strategy
      when :importmap, :sprockets, :propshaft
        stylesheet_link_tag "solidstats/application", **options
      when :webpacker
        if respond_to?(:stylesheet_pack_tag)
          stylesheet_pack_tag "solidstats/application", **options
        else
          stylesheet_link_tag "solidstats/application", **options
        end
      else
        stylesheet_link_tag "solidstats/application", **options
      end
    end

    # Universal javascript helper for Solidstats
    def solidstats_javascripts(*args)
      options = args.extract_options!
      strategy = Solidstats.asset_strategy

      case strategy
      when :importmap
        javascript_importmap_tags
        javascript_include_tag "solidstats", **options
      when :webpacker
        if respond_to?(:javascript_pack_tag)
          javascript_pack_tag "solidstats/application", **options
        else
          javascript_include_tag "solidstats/application", **options
        end
      when :sprockets, :propshaft
        javascript_include_tag "solidstats/application", **options
      else
        javascript_include_tag "solidstats/application", **options
      end
    end

    # Convenience method to include all Solidstats assets
    def solidstats_assets(*args)
      options = args.extract_options!

      content = []
      content << solidstats_stylesheets(**options.slice(:media, :crossorigin))
      content << solidstats_javascripts(**options.slice(:defer, :async, :crossorigin))

      safe_join(content, "\n")
    end
  end

  Solidstats.logger.info "[Solidstats] Added universal asset helpers"
rescue => e
  Solidstats.logger.warn "[Solidstats] Could not add asset helpers: #{e.message}"
end

#configure_asset_pipeline(app) ⇒ Object

Configure the detected asset pipeline

Parameters:

  • app (Rails::Application)

    Rails application instance



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/solidstats/asset_compatibility.rb', line 43

def configure_asset_pipeline(app)
  case detect_asset_pipeline(app)
  when :propshaft
    configure_propshaft(app)
  when :sprockets
    configure_sprockets(app)
  when :webpacker
    configure_webpacker(app)
  when :importmap
    configure_importmap(app)
  when :unknown
    Solidstats.logger.warn "[Solidstats] No supported asset pipeline detected"
  end
end

#detect_asset_pipeline(app, force_refresh: false) ⇒ Symbol

Detect which asset pipeline is being used

Parameters:

  • app (Rails::Application)

    Rails application instance

  • force_refresh (Boolean) (defaults to: false)

    Force refresh of cached detection

Returns:

  • (Symbol)

    Detected pipeline (:propshaft, :sprockets, :webpacker, :importmap, :unknown)



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/solidstats/asset_compatibility.rb', line 21

def detect_asset_pipeline(app, force_refresh: false)
  if force_refresh
    @pipeline = nil
  end

  @pipeline ||= begin
    if propshaft_available?(app)
      :propshaft
    elsif importmap_available?(app)
      :importmap
    elsif webpacker_available?(app)
      :webpacker
    elsif sprockets_available?(app)
      :sprockets
    else
      :unknown
    end
  end
end

#setup_assets(app) ⇒ Object

Include engine assets in host application

Parameters:

  • app (Rails::Application)

    Rails application instance



11
12
13
14
15
# File 'lib/solidstats/asset_compatibility.rb', line 11

def setup_assets(app)
  detect_asset_pipeline(app, force_refresh: true)
  configure_asset_pipeline(app)
  add_asset_helpers(app)
end