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

Class Method Details

.cacheObject



32
33
34
# File 'app/helpers/railspress/cms_helper.rb', line 32

def self.cache
  Railspress::CMS.cache
end

.clear_cacheObject



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

def self.clear_cache
  Railspress::CMS.clear_cache
end

Instance Method Details

#cmsRailspress::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.

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



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?
    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



103
104
105
# File 'app/helpers/railspress/cms_helper.rb', line 103

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



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.

Returns:

  • (Boolean)


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