Skip to content

Instantly share code, notes, and snippets.

@ahamid ahamid/gist:3626285
Created Sep 4, 2012

Embed
What would you like to do?
Comfortable Mexican Sofa render CMS content as partial
# config/initializers/comfortable_mexican_sofa.rb
require 'cms_renderer'
# lib/cms_renderer.rb
require 'action_view/renderer/renderer'
require 'action_view/renderer/template_renderer'
class CMSRenderer < ActionView::TemplateRenderer
# hook determine_template to handle CMS options
def determine_template(options) #:nodoc:
if options.is_a?(Hash) && identifier = options.delete(:cms_site)
unless cms_site = Cms::Site.find_by_identifier(identifier)
raise ComfortableMexicanSofa::MissingSite.new(identifier)
end
end
if options.is_a?(Hash) && path = options.delete(:cms_page)
cms_site ||= Cms::Site.find_site(@view.request.host.downcase, @view.request.fullpath)
if cms_page = cms_site && cms_site.pages.find_by_full_path(path)
#cms_layout = cms_page.layout
#cms_app_layout = cms_layout.try(:app_layout)
#options[:layout] ||= cms_app_layout.blank?? nil : cms_app_layout
options[:inline] = cms_page.content
else
raise ComfortableMexicanSofa::MissingPage.new(path)
end
elsif options.is_a?(Hash) && identifier = options.delete(:cms_layout)
cms_site ||= Cms::Site.find_site(@view.request.host.downcase, @view.request.fullpath)
if cms_layout = cms_site && cms_site.layouts.find_by_identifier(identifier)
cms_app_layout = cms_layout.try(:app_layout)
cms_page = cms_site.pages.build(:layout => cms_layout)
cms_blocks = options.delete(:cms_blocks) || { :content => render_to_string(:layout => false)}
cms_blocks.each do |identifier, value|
content = if value.is_a?(Hash)
render_to_string(value.keys.first.to_sym => value[value.keys.first], :layout => false)
else
value.to_s
end
cms_page.blocks.build(:identifier => identifier.to_s, :content => content)
end
options[:layout] ||= cms_app_layout.blank?? nil : cms_app_layout
options[:inline] = cms_page.content(true)
else
raise ComfortableMexicanSofa::MissingLayout.new(identifier)
end
end
super(options)
end
end
# use our uber CMS renderer in place of TemplateRenderer
ActionView::Renderer.class_eval do
def _template_renderer #:nodoc:
@_template_renderer ||= CMSRenderer.new(@lookup_context)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.