Module: ComfortableMexicanSofa::RenderMethods
- Defined in:
- lib/comfortable_mexican_sofa/render_methods.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#render(options = {}, locals = {}, &block) ⇒ Object
Now you can render cms_page simply by calling: render cms_page: ‘/path/to/page’ This way application controllers can use CMS content while populating instance variables that can be used in partials (that are included by by the cms page and/or layout).
- #render_cms_layout(identifier, options = {}, locals = {}, &block) ⇒ Object
- #render_cms_page(path, options = {}, locals = {}, &block) ⇒ Object
Class Method Details
.included(base) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/comfortable_mexican_sofa/render_methods.rb', line 5 def self.included(base) # If application controller doesn't have template associated with it # CMS will attempt to find one. This is so you don't have to explicitly # call render cms_page: '/something' base.rescue_from "ActionView::MissingTemplate" do |e| begin render cms_page: request.path rescue ComfortableMexicanSofa::MissingPage, ComfortableMexicanSofa::MissingSite raise e end end end |
Instance Method Details
#render(options = {}, locals = {}, &block) ⇒ Object
Now you can render cms_page simply by calling:
render cms_page: '/path/to/page'
This way application controllers can use CMS content while populating instance variables that can be used in partials (that are included by by the cms page and/or layout)
Or how about not worrying about setting up CMS pages and rendering application view using a CMS layout?
render cms_layout: 'layout_slug', cms_fragments: {
fragment_identifier_a: 'content text',
fragment_identifier_b: {template: 'path/to/template' },
fragment_identifier_c: {partial: 'path/to/partial' }
}
This way you are populating page block content and rendering an instantialized CMS page.
Site is loaded automatically based on the request. However you can force it by passing :cms_site parameter with site’s slug. For example:
render cms_page: '/path/to/page', cms_site: 'default'
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/comfortable_mexican_sofa/render_methods.rb', line 39 def render( = {}, locals = {}, &block) return super unless .is_a?(Hash) if (site_identifier = .delete(:cms_site)) unless (@cms_site = Comfy::Cms::Site.find_by_identifier(site_identifier)) raise ComfortableMexicanSofa::MissingSite, site_identifier end end if (page_path = .delete(:cms_page)) || (layout_identifier = .delete(:cms_layout)) unless @cms_site ||= Comfy::Cms::Site.find_site(request.host_with_port.downcase, request.fullpath) raise ComfortableMexicanSofa::MissingSite, "#{request.host.downcase}/#{request.fullpath}" end end if page_path render_cms_page(page_path, , locals, &block) elsif layout_identifier render_cms_layout(layout_identifier, , locals, &block) else super end end |
#render_cms_layout(identifier, options = {}, locals = {}, &block) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/comfortable_mexican_sofa/render_methods.rb', line 88 def render_cms_layout(identifier, = {}, locals = {}, &block) unless (@cms_layout = @cms_site.layouts.find_by_identifier(identifier)) raise ComfortableMexicanSofa::MissingLayout, identifier end cms_app_layout = @cms_layout.app_layout cms_page = @cms_site.pages.build(layout: @cms_layout) cms_fragments = .delete(:cms_fragments) || { content: render_to_string({ layout: false }.merge()) } cms_fragments.each do |frag_identifier, value| content = value.is_a?(Hash) ? render_to_string(value.merge(layout: false)) : value.to_s cms_page.fragments.build(identifier: frag_identifier.to_s, content: content) end [:layout] ||= cms_app_layout.blank? ? nil : cms_app_layout [:inline] = cms_page.render render(, locals, &block) end |
#render_cms_page(path, options = {}, locals = {}, &block) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/comfortable_mexican_sofa/render_methods.rb', line 63 def render_cms_page(path, = {}, locals = {}, &block) path.gsub!(%r{^/#{@cms_site.path}}, "") if @cms_site.path.present? unless (@cms_page = @cms_site.pages.find_by_full_path(path)) raise ComfortableMexicanSofa::MissingPage, path end @cms_page.translate! @cms_layout = @cms_page.layout if (cms_fragments = .delete(:cms_fragments)).present? cms_fragments.each do |identifier, value| content = value.is_a?(Hash) ? render_to_string(value.merge(layout: false)) : value.to_s page_fragment = @cms_page.fragments.detect { |f| f.identifier == identifier.to_s } || @cms_page.fragments.build(identifier: identifier.to_s) page_fragment.content = content end end cms_app_layout = @cms_layout.app_layout [:layout] ||= cms_app_layout.blank? ? nil : cms_app_layout [:inline] = @cms_page.render render(, locals, &block) end |