Module: Panda::CMS::AssetHelper

Includes:
Panda::Core::AssetHelper
Defined in:
app/helpers/panda/cms/asset_helper.rb

Instance Method Summary collapse

Instance Method Details

#ensure_panda_cms_assets!Object

Download and cache assets if needed Call this in an initializer or controller to pre-cache assets



67
68
69
# File 'app/helpers/panda/cms/asset_helper.rb', line 67

def ensure_panda_cms_assets!
  Panda::CMS::AssetLoader.ensure_assets_available!
end

#panda_cms_asset_debugObject

Debug information about asset loading



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/helpers/panda/cms/asset_helper.rb', line 72

def panda_cms_asset_debug
  return "" unless Rails.env.development? || Rails.env.test?

  version = Panda::CMS::VERSION
  js_url = Panda::CMS::AssetLoader.javascript_url
  css_url = Panda::CMS::AssetLoader.css_url
  Panda::CMS::AssetLoader.use_github_assets?

  debug_info = [
    "<!-- Panda CMS Asset Debug Info -->",
    "<!-- Version: #{version} -->",
    "<!-- Using importmaps: true (no compilation) -->",
    "<!-- JavaScript URL: #{js_url} -->",
    "<!-- CSS URL: #{css_url || "CSS from panda-core"} -->",
    "<!-- Rails environment: #{Rails.env} -->",
    "<!-- Rails root: #{Rails.root} -->"
  ]

  debug_info.join("\n").html_safe
end

#panda_cms_assetsObject

Include Panda CMS JavaScript and CSS assets Automatically chooses between GitHub-hosted assets (production) and local development assets



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/helpers/panda/cms/asset_helper.rb', line 12

def panda_cms_assets
  tags = []

  # Include Core assets first (if Core is available)
  if defined?(Panda::Core::AssetHelper)
    tags << panda_core_assets
  end

  # Then include CMS-specific assets
  tags << Panda::CMS::AssetLoader.asset_tags

  tags.join("\n").html_safe
end

#panda_cms_complete_assetsObject

Complete asset loading with initialization This is the recommended way to include all Panda CMS assets



122
123
124
125
126
127
128
129
130
# File 'app/helpers/panda/cms/asset_helper.rb', line 122

def panda_cms_complete_assets
  [
    panda_cms_asset_debug,
    panda_cms_assets,
    panda_cms_stimulus_init,
    # Add immediate JavaScript execution test for CI debugging
    (Rails.env.test? ? javascript_tag("window.pandaCmsInlineTest = true; console.log('[Panda CMS] Inline script executed');") : "")
  ].join("\n").html_safe
end

#panda_cms_injectable_assetsObject

Returns asset HTML for injection into iframe Used by editor_iframe_controller to inject assets dynamically Returns the raw HTML string (not JSON-encoded)



135
136
137
# File 'app/helpers/panda/cms/asset_helper.rb', line 135

def panda_cms_injectable_assets
  panda_cms_complete_assets.to_s
end

#panda_cms_stimulus_initObject

Initialize Panda CMS Stimulus application Call this after the asset tags to ensure proper initialization



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/helpers/panda/cms/asset_helper.rb', line 95

def panda_cms_stimulus_init
  javascript_tag("    // Initialize Panda CMS Stimulus application\n    document.addEventListener('DOMContentLoaded', function() {\n      if (window.pandaCmsStimulus) {\n        console.debug('[Panda CMS] Stimulus application initialized');\n\n        // Set debug mode based on Rails environment\n        const railsEnv = document.body?.dataset?.environment || 'production';\n        window.pandaCmsStimulus.debug = (railsEnv === 'development');\n\n        // Trigger a custom event to signal Panda CMS is ready\n        document.dispatchEvent(new CustomEvent('panda-cms:ready', {\n          detail: {\n            version: '\#{Panda::CMS::VERSION}',\n            usingGitHubAssets: \#{Panda::CMS::AssetLoader.use_github_assets?}\n          }\n        }));\n      } else {\n        console.warn('[Panda CMS] Stimulus application not found. Assets may not have loaded properly.');\n      }\n    });\n  JS\nend\n", type: "module")

#panda_cms_stylesheetObject

Include only Panda CMS CSS



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/helpers/panda/cms/asset_helper.rb', line 32

def panda_cms_stylesheet
  css_url = Panda::CMS::AssetLoader.css_url
  return "" unless css_url

  if Panda::CMS::AssetLoader.use_github_assets?
    # GitHub-hosted assets with integrity check
    version = Panda::CMS::VERSION
    integrity = asset_integrity(version, "panda-cms-#{version}.css")

    tag_options = {
      rel: "stylesheet",
      href: css_url
    }
    tag_options[:integrity] = integrity if integrity
    tag_options[:crossorigin] = "anonymous" if integrity

    tag(:link, tag_options)
  else
    # Development assets
    stylesheet_link_tag(css_url)
  end
end

#panda_cms_versionObject

Get the current Panda CMS version



56
57
58
# File 'app/helpers/panda/cms/asset_helper.rb', line 56

def panda_cms_version
  Panda::CMS::VERSION
end

#using_github_assets?Boolean

Check if using GitHub-hosted assets

Returns:

  • (Boolean)


61
62
63
# File 'app/helpers/panda/cms/asset_helper.rb', line 61

def using_github_assets?
  Panda::CMS::AssetLoader.use_github_assets?
end