Module: RubyCms::ContentBlocksHelper

Defined in:
app/helpers/ruby_cms/content_blocks_helper.rb

Instance Method Summary collapse

Instance Method Details

#content_block(key, default_or_nil = nil, locale: nil, fallback: nil, default: nil, translation_namespace: nil, **options) ⇒ Object Also known as: cms_content_block

Renders a content block by key. Rendering uses the block’s content_type from the DB (text, rich_text, image, link, list). Usage:

content_block("hero_title")
content_block("hero_title", "Welcome")          # key + default when block missing
content_block("hero_title", default: "Welcome") # same via keyword

Wraps in a span with data-content-key, data-block-id, and .content-block for editor hooks.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/ruby_cms/content_blocks_helper.rb', line 12

def content_block(key, default_or_nil=nil, locale: nil, fallback: nil, default: nil,
                  translation_namespace: nil, **options)
  # Support content_block("key", "Default") and content_block("key", default: "Default")
  default = default_or_nil.kind_of?(Hash) ? default : (default_or_nil || default)
  cache_opts = options.delete(:cache)
  wrap = options.delete(:wrap)
  wrap = true if wrap.nil?

  cache_key = cache_key_for_content_block(key, cache_opts)

  if cache_opts && cache_key
    Rails.cache.fetch(cache_key) do
      render_content_block(key, locale, default, fallback, translation_namespace, options,
                           wrap:)
    end
  else
    render_content_block(key, locale, default, fallback, translation_namespace, options, wrap:)
  end
end

#content_block_list_items(key, locale: nil, fallback: nil) ⇒ Object Also known as: cms_content_block_list_items

Returns array of strings from list-type content block



50
51
52
53
54
55
56
57
58
# File 'app/helpers/ruby_cms/content_blocks_helper.rb', line 50

def content_block_list_items(key, locale: nil, fallback: nil)
  locale = normalize_locale(locale)
  block = find_content_block(key, locale)
  return Array(fallback) if block.blank?

  raw = block.content.to_s
  items = parse_list_items(raw)
  items.presence || Array(fallback)
end

#content_block_text(key, locale: nil, default: nil, fallback: nil, translation_namespace: nil) ⇒ Object Also known as: cms_content_block_text

Returns plain text (no HTML wrapper)



35
36
37
38
39
40
41
42
43
44
45
# File 'app/helpers/ruby_cms/content_blocks_helper.rb', line 35

def content_block_text(key, locale: nil, default: nil, fallback: nil,
                       translation_namespace: nil)
  locale = normalize_locale(locale)
  block = find_content_block(key, locale)

  content = content_for_block_text(
    block, default, fallback, key, translation_namespace, locale
  )

  strip_html_tags(content)
end