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
-
#add_asset_helpers(app) ⇒ Object
Add universal asset helper methods.
-
#configure_asset_pipeline(app) ⇒ Object
Configure the detected asset pipeline.
-
#detect_asset_pipeline(app, force_refresh: false) ⇒ Symbol
Detect which asset pipeline is being used.
-
#setup_assets(app) ⇒ Object
Include engine assets in host application.
Instance Method Details
#add_asset_helpers(app) ⇒ Object
Add universal asset helper methods
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) = args. strategy = Solidstats.asset_strategy case strategy when :importmap, :sprockets, :propshaft stylesheet_link_tag "solidstats/application", ** when :webpacker if respond_to?(:stylesheet_pack_tag) stylesheet_pack_tag "solidstats/application", ** else stylesheet_link_tag "solidstats/application", ** end else stylesheet_link_tag "solidstats/application", ** end end # Universal javascript helper for Solidstats def solidstats_javascripts(*args) = args. strategy = Solidstats.asset_strategy case strategy when :importmap javascript_include_tag "solidstats", ** when :webpacker if respond_to?(:javascript_pack_tag) javascript_pack_tag "solidstats/application", ** else javascript_include_tag "solidstats/application", ** end when :sprockets, :propshaft javascript_include_tag "solidstats/application", ** else javascript_include_tag "solidstats/application", ** end end # Convenience method to include all Solidstats assets def solidstats_assets(*args) = args. content = [] content << solidstats_stylesheets(**.slice(:media, :crossorigin)) content << solidstats_javascripts(**.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.}" end |
#configure_asset_pipeline(app) ⇒ Object
Configure the detected asset pipeline
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
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
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 |