Module: Railspress::CmsHelper
- Defined in:
- app/helpers/railspress/cms_helper.rb
Overview
CMS Helper provides a clean API for loading content elements in views.
Usage in views:
<%= cms_value("Homepage", "Hero H1") %>
<%= cms_element(group: "Homepage", name: "Hero H1") do |value| %>
<h1><%= value %></h1>
<% end %>
Chainable API (works in controllers, services, etc.):
Railspress::CMS.find("Homepage").load("Hero H1").value
Railspress::CMS.find("Homepage").load("Hero H1").element
Defined Under Namespace
Modules: DisabledStub Classes: CMSQuery
Class Method Summary collapse
-
.cache ⇒ Object
Request-level cache to avoid repeated queries.
- .clear_cache ⇒ Object
Instance Method Summary collapse
-
#cms ⇒ CMSQuery
Return a new CMSQuery instance for chainable API in views.
-
#cms_element(group:, name:, html_options: {}) {|value, element| ... } ⇒ String
Render a content element, optionally with a block for custom rendering.
-
#cms_element_display_frame(content_element, display_frame_id) ⇒ String
Render the display content within a Turbo Frame for inline replacement.
-
#cms_value(group_name, element_name) ⇒ String?
Get a content element’s value by group and element name.
-
#inline_editor_enabled? ⇒ Boolean
Check if inline editing is enabled for the current request.
Class Method Details
.cache ⇒ Object
Request-level cache to avoid repeated queries
33 34 35 |
# File 'app/helpers/railspress/cms_helper.rb', line 33 def self.cache @cache ||= {} end |
.clear_cache ⇒ Object
37 38 39 |
# File 'app/helpers/railspress/cms_helper.rb', line 37 def self.clear_cache @cache = {} end |
Instance Method Details
#cms ⇒ CMSQuery
Return a new CMSQuery instance for chainable API in views.
149 150 151 |
# File 'app/helpers/railspress/cms_helper.rb', line 149 def cms CmsHelper::CMSQuery.new end |
#cms_element(group:, name:, html_options: {}) {|value, element| ... } ⇒ String
Render a content element, optionally with a block for custom rendering. When inline editing is enabled, wraps content in a Stimulus-controlled <span> with context menu and Turbo Frame markup for right-click editing.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'app/helpers/railspress/cms_helper.rb', line 97 def cms_element(group:, name:, html_options: {}, &block) content_element = Railspress::CMS.find(group).load(name).element element_value = content_element&.value if content_element&.image? && content_element&.image&.attached? = .dup if content_element.has_focal_point?(:image) focal_css = content_element.focal_point_css(:image) existing_style = [:style].to_s [:style] = [ existing_style, focal_css ].reject(&:blank?).join("; ") end [:alt] ||= content_element.name return image_tag(main_app.url_for(content_element.image), ) end rendered = if block_given? args = block.arity.zero? ? [] : [ element_value, content_element ] capture(*args, &block) else element_value end if content_element && !content_element.image? && inline_editor_enabled? inline_wrapper_for(content_element, rendered) else rendered end end |
#cms_element_display_frame(content_element, display_frame_id) ⇒ String
Render the display content within a Turbo Frame for inline replacement. Used by the controller to replace display content after inline save.
143 144 145 |
# File 'app/helpers/railspress/cms_helper.rb', line 143 def cms_element_display_frame(content_element, display_frame_id) content_tag("turbo-frame", content_element.value, id: display_frame_id) end |
#cms_value(group_name, element_name) ⇒ String?
Get a content element’s value by group and element name.
84 85 86 |
# File 'app/helpers/railspress/cms_helper.rb', line 84 def cms_value(group_name, element_name) Railspress::CMS.find(group_name).load(element_name).value end |
#inline_editor_enabled? ⇒ Boolean
Check if inline editing is enabled for the current request. Uses the configured inline_editing_check proc.
129 130 131 132 133 134 135 136 |
# File 'app/helpers/railspress/cms_helper.rb', line 129 def inline_editor_enabled? check = Railspress.inline_editing_check return false unless check check.call(self) rescue false end |