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


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

def cms_fragment_content(identifier, page = @cms_page)
  frag = page && 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.



27
28
29
30
31
# File 'app/helpers/comfy/cms_helper.rb', line 27

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

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

Raw content of a snippet. Example:

cms_snippet_content(:my_snippet)


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

def cms_snippet_content(identifier, cms_site = @cms_site)
  unless cms_site
    host, path = request.host_with_port.downcase, request.fullpath if respond_to?(:request) && request
    cms_site = Comfy::Cms::Site.find_site(host, path)
  end
  snippet = cms_site && 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
# File 'app/helpers/comfy/cms_helper.rb', line 48

def cms_snippet_render(identifier, cms_site = @cms_site)
  content = cms_snippet_content(identifier, cms_site)
  r = ComfortableMexicanSofa::Content::Renderer.new(Comfy::Cms::Page.new)
  render inline: r.render(r.nodes(r.tokenize(content)))
end

#comfy_paginate(collection) ⇒ Object

Wrapper to deal with Kaminari vs WillPaginate



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

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