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

Instance Method Summary collapse

Class Method Details

.cacheObject

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_cacheObject



37
38
39
# File 'app/helpers/railspress/cms_helper.rb', line 37

def self.clear_cache
  @cache = {}
end

Instance Method Details

#cmsCMSQuery

Return a new CMSQuery instance for chainable API in views.

Returns:



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.

Parameters:

  • group (String)

    the content group name

  • name (String)

    the content element name

  • html_options (Hash) (defaults to: {})

    additional HTML options

Yields:

  • (value, element)

    optional block for custom rendering

Returns:

  • (String)

    rendered content



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?
    img_options = html_options.dup
    if content_element.has_focal_point?(:image)
      focal_css = content_element.focal_point_css(:image)
      existing_style = img_options[:style].to_s
      img_options[:style] = [ existing_style, focal_css ].reject(&:blank?).join("; ")
    end
    img_options[:alt] ||= content_element.name
    return image_tag(main_app.url_for(content_element.image), img_options)
  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.

Parameters:

  • content_element (ContentElement)

    the element

  • display_frame_id (String)

    the Turbo Frame ID

Returns:

  • (String)

    HTML safe turbo-frame wrapped content



143
144
145
# File 'app/helpers/railspress/cms_helper.rb', line 143

def cms_element_display_frame(content_element, display_frame_id)
  ("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.

Parameters:

  • group_name (String)

    the content group name

  • element_name (String)

    the content element name

Returns:

  • (String, nil)

    the element value or nil



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.

Returns:

  • (Boolean)


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