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
Class Method Summary collapse
Instance Method Summary collapse
-
#cms ⇒ Railspress::CMS::Query
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
32 33 34 |
# File 'app/helpers/railspress/cms_helper.rb', line 32 def self.cache Railspress::CMS.cache end |
.clear_cache ⇒ Object
36 37 38 |
# File 'app/helpers/railspress/cms_helper.rb', line 36 def self.clear_cache Railspress::CMS.clear_cache end |
Instance Method Details
#cms ⇒ Railspress::CMS::Query
Return a new CMSQuery instance for chainable API in views.
109 110 111 |
# File 'app/helpers/railspress/cms_helper.rb', line 109 def cms Railspress::CMS::Query.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.
57 58 59 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 |
# File 'app/helpers/railspress/cms_helper.rb', line 57 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.
103 104 105 |
# File 'app/helpers/railspress/cms_helper.rb', line 103 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.
44 45 46 |
# File 'app/helpers/railspress/cms_helper.rb', line 44 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.
89 90 91 92 93 94 95 96 |
# File 'app/helpers/railspress/cms_helper.rb', line 89 def inline_editor_enabled? check = Railspress.inline_editing_check return false unless check check.call(self) rescue false end |