Module: Comfy::CmsHelper

Defined in:
app/helpers/comfy/cms_helper.rb

Instance Method Summary collapse

Instance Method Details

#cms_fragment_content(identifier, page = @cms_page) ⇒ Object

Raw content of a page fragment. This is how you get content from unrenderable tags like meta, render: false} Example:

cms_fragment_content(:left_column, CmsPage.first)
cms_fragment_content(:left_column) # if @cms_page is present


11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/helpers/comfy/cms_helper.rb', line 11

def cms_fragment_content(identifier, page = @cms_page)
  frag = page&.fragments&.detect { |f| f.identifier == identifier.to_s }
  return "" unless frag
  case frag.tag
  when "date", "datetime"
    frag.datetime
  when "checkbox"
    frag.boolean
  when "file", "files"
    frag.attachments
  else
    frag.content
  end
end

#cms_fragment_render(identifier, page = @cms_page) ⇒ Object

Same as cms_fragment_content but with cms tags expanded and rendered. Use it only if you know you got more stuff in the fragment content other than text because this is a potentially expensive call.



29
30
31
32
33
34
# File 'app/helpers/comfy/cms_helper.rb', line 29

def cms_fragment_render(identifier, page = @cms_page)
  node = page.fragment_nodes.detect { |n| n.identifier == identifier.to_s }
  return "" unless node
  node.renderable = true
  render inline: page.render([node])
end

#cms_site_detectObject



56
57
58
59
60
# File 'app/helpers/comfy/cms_helper.rb', line 56

def cms_site_detect
  host = request.host_with_port.downcase
  path = request.fullpath
  Comfy::Cms::Site.find_site(host, path)
end

#cms_snippet_content(identifier, cms_site = @cms_site) ⇒ Object

Raw content of a snippet. Example:

cms_snippet_content(:my_snippet)


39
40
41
42
43
44
# File 'app/helpers/comfy/cms_helper.rb', line 39

def cms_snippet_content(identifier, cms_site = @cms_site)
  cms_site ||= cms_site_detect
  snippet = cms_site&.snippets&.find_by_identifier(identifier)
  return "" unless snippet
  snippet.content
end

#cms_snippet_render(identifier, cms_site = @cms_site) ⇒ Object

Same as cms_snippet_content but cms tags will be expanded. Note that there is no page context, so snippet cannot contain fragment tags.



48
49
50
51
52
53
54
# File 'app/helpers/comfy/cms_helper.rb', line 48

def cms_snippet_render(identifier, cms_site = @cms_site)
  cms_site ||= cms_site_detect
  snippet = cms_site&.snippets&.find_by_identifier(identifier)
  return "" unless snippet
  r = ComfortableMexicanSofa::Content::Renderer.new(snippet)
  render inline: r.render(r.nodes(r.tokenize(snippet.content)))
end

#comfy_paginate(collection) ⇒ Object

Wrapper to deal with Kaminari vs WillPaginate



63
64
65
66
67
68
69
70
# File 'app/helpers/comfy/cms_helper.rb', line 63

def comfy_paginate(collection)
  return unless collection
  if defined?(WillPaginate)
    will_paginate collection
  elsif defined?(Kaminari)
    paginate collection, theme: "comfy"
  end
end