Module: Comfy::CmsHelper

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

Instance Method Summary collapse

Instance Method Details

#cms_block_content(identifier, blockable = @cms_page, use_old_code = false) ⇒ Object

Content of a page block. This is how you get content from page:field Example:

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


149
150
151
152
153
154
155
156
157
# File 'app/helpers/comfy/cms_helper.rb', line 149

def cms_block_content(identifier, blockable = @cms_page, use_old_code = false)
  if use_old_code
    tag = blockable && (block = blockable.blocks.find_by_identifier(identifier)) && block.tag
    return '' unless tag
    tag.content
  else
    pluck_page_block_content(identifier, blockable)
  end
end

#cms_block_content_render(identifier, blockable = @cms_page) ⇒ Object

For those times when we need to render content that shouldn’t be renderable Example: {cms:field} tags



161
162
163
164
165
# File 'app/helpers/comfy/cms_helper.rb', line 161

def cms_block_content_render(identifier, blockable = @cms_page)
  tag = blockable && (block = blockable.blocks.find_by_identifier(identifier)) && block.tag
  return '' unless tag
  render :inline => ComfortableMexicanSofa::Tag.process_content(blockable, tag.content)
end

#cms_block_render(identifier, blockable = @cms_page) ⇒ Object

Same as cms_block_content but with cms tags expanded



168
169
170
171
172
# File 'app/helpers/comfy/cms_helper.rb', line 168

def cms_block_render(identifier, blockable = @cms_page)
  tag = blockable && (block = blockable.blocks.find_by_identifier(identifier)) && block.tag
  return '' unless tag
  render :inline => ComfortableMexicanSofa::Tag.process_content(blockable, tag.render)
end

#cms_hook(name, options = {}) ⇒ Object

Injects some content somewhere inside cms admin area



107
108
109
# File 'app/helpers/comfy/cms_helper.rb', line 107

def cms_hook(name, options = {})
  ComfortableMexicanSofa::ViewHooks.render(name, self, options)
end

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

Content of a snippet. Examples:

cms_snippet_content(:my_snippet)
<%= cms_snippet_content(:my_snippet) do %>
  Default content can go here.
<% end %>


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/helpers/comfy/cms_helper.rb', line 116

def cms_snippet_content(identifier, cms_site = @cms_site, &block)
  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
  return '' unless cms_site

  snippet = cms_site.snippets.find_by_identifier(identifier)

  if !snippet && block_given?
    snippet = cms_site.snippets.create(
      :identifier => identifier,
      :label      => identifier.to_s.titleize,
      :content    => capture(&block)
    )
  end

  snippet ? snippet.content : ''
end

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

Same as cms_snippet_content but cms tags will be expanded



137
138
139
140
141
142
143
# File 'app/helpers/comfy/cms_helper.rb', line 137

def cms_snippet_render(identifier, cms_site = @cms_site, &block)
  return '' unless cms_site
  content = cms_snippet_content(identifier, cms_site, &block)
  render :inline => ComfortableMexicanSofa::Tag.process_content(
    cms_site.pages.build, ComfortableMexicanSofa::Tag.sanitize_irb(content)
  )
end

#comfy_form_for(record, options = {}, &proc) ⇒ Object

Wrapper around ComfortableMexicanSofa::FormBuilder



100
101
102
103
104
# File 'app/helpers/comfy/cms_helper.rb', line 100

def comfy_form_for(record, options = {}, &proc)
  options[:builder] = ComfortableMexicanSofa::FormBuilder
  options[:layout] ||= :horizontal
  bootstrap_form_for(record, options, &proc)
end

#comfy_page_titleObject



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

def comfy_page_title
  pluck_page_block_content('seo.page_title')
end

#comfy_paginate(collection) ⇒ Object

Wrapper to deal with Kaminari vs WillPaginate



175
176
177
178
179
180
181
182
# File 'app/helpers/comfy/cms_helper.rb', line 175

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

#comfy_seo_tagsObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/helpers/comfy/cms_helper.rb', line 3

def comfy_seo_tags
  page_block_contents = Comfy::Cms::Block.where(blockable_type: 'Comfy::Cms::Page', blockable_id: @cms_page.id).pluck(:identifier, :content)
  meta_description = search_page_block_contents('seo.meta_description', page_block_contents)
  meta_index = search_page_block_contents('seo.meta_index', page_block_contents)
  page_title = search_page_block_contents('seo.page_title', page_block_contents)
  parent_page = @cms_page.parent_id.present? ? @cms_page.parent_id : false
  tags = []
  tags << tag('meta', name: 'description', content: meta_description) if meta_description.present?
  tags << tag('meta', name: 'robots', content: 'NOINDEX, FOLLOW') if meta_index.present? && meta_index

  # if no canonical is set, default to URL without any parameters
  href = search_page_block_contents('seo.canonical_href')
  href = href.present? ? href : request.url.split('?').first
  tags << tag('link', rel: 'canonical', href: href)

  ### Google plus: use meta_description and page title as defaults
  gplus_name = ('google_plus.name', page_title, page_block_contents)
  gplus_description = ('google_plus.description', meta_description, page_block_contents)
  gplus_image = self_or_parent_metafield('google_plus.image', @cms_page, page_block_contents)
  tags << tag('meta', itemprop: "name", content: gplus_name) if gplus_name.present?
  tags << tag('meta', itemprop: "description", content: gplus_description) if gplus_description.present?
  tags << tag('meta', itemprop: "image", content: gplus_image) if gplus_image.present?

  ### Twitter Card
  twitter_site = self_or_parent_metafield('twitter.site', @cms_page, page_block_contents)
  twitter_creator = self_or_parent_metafield('twitter.creator', @cms_page, page_block_contents)
  twitter_image_src = self_or_parent_metafield('twitter.image_src', @cms_page, page_block_contents)
  twitter_title = ('twitter.title', page_title, page_block_contents)
  twitter_description = ('twitter.description', meta_description, page_block_contents)
  tags << tag('meta', name: 'twitter:card', content: 'summary_large_image')
  tags << tag('meta', name: 'twitter:site', content: twitter_site) if twitter_site.present?
  tags << tag('meta', name: 'twitter:creator', content: twitter_creator) if twitter_creator.present?
  tags << tag('meta', name: 'twitter:image:src', content: twitter_image_src) if twitter_image_src.present?
  tags << tag('meta', name: 'twitter:title', content: twitter_title) if twitter_title.present?
  tags << tag('meta', name: 'twitter:description', content: twitter_description) if twitter_description.present?

  ### Facebook
  fb_description = ('facebook.description', meta_description, page_block_contents)
  fb_title = ('facebook.title', page_title, page_block_contents)
  fb_type = self_or_parent_metafield('facebook.type', @cms_page, page_block_contents)
  fb_image = self_or_parent_metafield('facebook.image', @cms_page, page_block_contents)
  fb_admins = self_or_parent_metafield('facebook.admins', @cms_page, page_block_contents)
  tags << tag('meta', property: 'og:description', content: fb_description) if fb_description.present?
  tags << tag('meta', property: 'og:title', content: fb_title) if fb_title.present?
  tags << tag('meta', property: 'og:type', content: fb_type) if fb_type.present?
  tags << tag('meta', property: 'og:image', content: fb_image) if fb_image.present?
  tags << tag('meta', property: 'fb:admins', content: fb_admins) if fb_admins.present?

  site_name = Comfy::Cms::Block.where(blockable_id: @cms_site.pages.first.id, blockable_type: 'Comfy::Cms::Page', identifier: 'seo.page_title').pluck(:content).first
  tags << tag('meta', property: 'og:site_name', content: site_name) if site_name.present?
  tags << tag('meta', property: 'og:url', content: request.url.split('?').first)

  return tags.join("\n").html_safe
end

#flash_css_class(type) ⇒ Object



92
93
94
95
96
97
# File 'app/helpers/comfy/cms_helper.rb', line 92

def flash_css_class(type)
  case type.to_sym
    when :notice, :success then 'alert-success'
    when :alert, :failure then 'alert-danger'
  end
end

#pluck_page_block_content(tag, page = @cms_page) ⇒ Object



87
88
89
90
# File 'app/helpers/comfy/cms_helper.rb', line 87

def pluck_page_block_content(tag, page = @cms_page)
  content = Comfy::Cms::Block.where(identifier: tag, blockable_type: 'Comfy::Cms::Page', blockable_id: page.id).pluck(:content).first
  return (content.present?) ? content : ''
end

#search_page_block_contents(identifier, page_block_contents = []) ⇒ Object



82
83
84
85
# File 'app/helpers/comfy/cms_helper.rb', line 82

def search_page_block_contents(identifier, page_block_contents = [])
  result = page_block_contents.select { |a| a.first == identifier }
  result.empty? ? '' : result.first.last
end

#self_or_inherit_metadata(block_identifier, metadata, page_block_contents = []) ⇒ Object



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

def (block_identifier, , page_block_contents = [])
  content = search_page_block_contents(block_identifier, page_block_contents)
  if content.present?
    return content
  else
    return 
  end
end

#self_or_parent_metafield(block_identifier, page = @cms_page, page_block_contents = []) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'app/helpers/comfy/cms_helper.rb', line 71

def self_or_parent_metafield(block_identifier, page = @cms_page, page_block_contents = [])
  content = search_page_block_contents(block_identifier, page_block_contents)
  if content.present?
    return content
  else
    if page.present?
      return Comfy::Cms::Block.where(identifier: block_identifier, blockable_type: 'Comfy::Cms::Page', blockable_id: page.parent_id).pluck(:content).first
    end
  end
end