Module: Comfy::CmsHelper
- Defined in:
- app/helpers/comfy/cms_helper.rb
Instance Method Summary collapse
-
#cms_block_content(identifier, blockable = @cms_page, use_old_code = false) ⇒ Object
Content of a page block.
-
#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.
-
#cms_block_render(identifier, blockable = @cms_page) ⇒ Object
Same as cms_block_content but with cms tags expanded.
-
#cms_hook(name, options = {}) ⇒ Object
Injects some content somewhere inside cms admin area.
-
#cms_snippet_content(identifier, cms_site = @cms_site, &block) ⇒ Object
Content of a snippet.
-
#cms_snippet_render(identifier, cms_site = @cms_site, &block) ⇒ Object
Same as cms_snippet_content but cms tags will be expanded.
-
#comfy_form_for(record, options = {}, &proc) ⇒ Object
Wrapper around ComfortableMexicanSofa::FormBuilder.
- #comfy_page_title ⇒ Object
-
#comfy_paginate(collection) ⇒ Object
Wrapper to deal with Kaminari vs WillPaginate.
- #comfy_seo_tags ⇒ Object
- #flash_css_class(type) ⇒ Object
- #pluck_page_block_content(tag, page = @cms_page) ⇒ Object
- #search_page_block_contents(identifier, page_block_contents = []) ⇒ Object
- #self_or_inherit_metadata(block_identifier, metadata, page_block_contents = []) ⇒ Object
- #self_or_parent_metafield(block_identifier, page = @cms_page, page_block_contents = []) ⇒ Object
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, = {}) ComfortableMexicanSofa::ViewHooks.render(name, self, ) 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, = {}, &proc) [:builder] = ComfortableMexicanSofa::FormBuilder [:layout] ||= :horizontal bootstrap_form_for(record, , &proc) end |
#comfy_page_title ⇒ Object
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_tags ⇒ Object
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 page_block_contents = Comfy::Cms::Block.where(blockable_type: 'Comfy::Cms::Page', blockable_id: @cms_page.id).pluck(:identifier, :content) = search_page_block_contents('seo.meta_description', page_block_contents) = 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 = [] << tag('meta', name: 'description', content: ) if .present? << tag('meta', name: 'robots', content: 'NOINDEX, FOLLOW') if .present? && # 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 << 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', , page_block_contents) gplus_image = ('google_plus.image', @cms_page, page_block_contents) << tag('meta', itemprop: "name", content: gplus_name) if gplus_name.present? << tag('meta', itemprop: "description", content: gplus_description) if gplus_description.present? << tag('meta', itemprop: "image", content: gplus_image) if gplus_image.present? ### Twitter Card twitter_site = ('twitter.site', @cms_page, page_block_contents) twitter_creator = ('twitter.creator', @cms_page, page_block_contents) twitter_image_src = ('twitter.image_src', @cms_page, page_block_contents) twitter_title = ('twitter.title', page_title, page_block_contents) twitter_description = ('twitter.description', , page_block_contents) << tag('meta', name: 'twitter:card', content: 'summary_large_image') << tag('meta', name: 'twitter:site', content: twitter_site) if twitter_site.present? << tag('meta', name: 'twitter:creator', content: twitter_creator) if twitter_creator.present? << tag('meta', name: 'twitter:image:src', content: twitter_image_src) if twitter_image_src.present? << tag('meta', name: 'twitter:title', content: twitter_title) if twitter_title.present? << tag('meta', name: 'twitter:description', content: twitter_description) if twitter_description.present? ### Facebook fb_description = ('facebook.description', , page_block_contents) fb_title = ('facebook.title', page_title, page_block_contents) fb_type = ('facebook.type', @cms_page, page_block_contents) fb_image = ('facebook.image', @cms_page, page_block_contents) fb_admins = ('facebook.admins', @cms_page, page_block_contents) << tag('meta', property: 'og:description', content: fb_description) if fb_description.present? << tag('meta', property: 'og:title', content: fb_title) if fb_title.present? << tag('meta', property: 'og:type', content: fb_type) if fb_type.present? << tag('meta', property: 'og:image', content: fb_image) if fb_image.present? << 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 << tag('meta', property: 'og:site_name', content: site_name) if site_name.present? << tag('meta', property: 'og:url', content: request.url.split('?').first) return .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 (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 |