new experimental render option
Oleg
committed Sep 30, 2011
commit c215c275cd9a5496e8d32114b6f239740ee65ed8
Showing 4
changed files with
51 additions
and 6 deletions
comfortable_mexican_sofa/controller_methods.rb b/lib/comfortable_mexican_sofa/controller_methods.rb
+32
-6
| @@ | @@ -18,19 +18,45 @@ module ComfortableMexicanSofa::ControllerMethods |
| # 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', :block_label => 'template/view' |
| + | # This way you are populating page block content with `render :template` and |
| + | # rendering an instantialized CMS page. |
| def render(options = {}, locals = {}, &block) | |
| if options.is_a?(Hash) && path = options.delete(:cms_page) | |
| - | @cms_site = Cms::Site.find_site(request.host.downcase, request.fullpath) |
| - | @cms_page = @cms_site && @cms_site.pages.find_by_full_path(path) |
| - | if @cms_page |
| + | @cms_site = Cms::Site.find_site(request.host.downcase, 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 |
| - | super(options, locals, &block) |
| + | render_options = { } |
| + | render_options[:layout] ||= cms_app_layout.blank?? nil : cms_app_layout |
| + | render_options[:inline] = @cms_page.content |
| + | super(render_options, locals, &block) |
| else | |
| raise ComfortableMexicanSofa::MissingPage.new(path) | |
| end | |
| + | |
| + | elsif options.is_a?(Hash) && slug = options.delete(:cms_layout) |
| + | @cms_site = Cms::Site.find_site(request.host.downcase, request.fullpath) |
| + | if @cms_layout = @cms_site && @cms_site.layouts.find_by_slug(slug) |
| + | cms_app_layout = @cms_layout.try(:app_layout) |
| + | cms_page = @cms_site.pages.build(:layout => @cms_layout) |
| + | options.each do |block_label, template| |
| + | cms_page.blocks.build( |
| + | :label => block_label.to_s, |
| + | :content => render_to_string(template) |
| + | ) |
| + | end |
| + | render_options = { } |
| + | render_options[:layout] ||= cms_app_layout.blank?? nil : cms_app_layout |
| + | render_options[:inline] = cms_page.content(true) |
| + | super(render_options, locals, &block) |
| + | else |
| + | raise ComfortableMexicanSofa::MissingLayout.new(slug) |
| + | end |
| + | |
| else | |
| super(options, locals, &block) | |
| end | |
comfortable_mexican_sofa/error.rb b/lib/comfortable_mexican_sofa/error.rb
+6
-0
| @@ | @@ -8,4 +8,10 @@ module ComfortableMexicanSofa |
| super "Cannot find CMS page at #{path}" | |
| end | |
| end | |
| + | |
| + | class MissingLayout < ComfortableMexicanSofa::Error |
| + | def initialize(slug) |
| + | super "Cannot find CMS layout with slug: #{slug}" |
| + | end |
| + | end |
| end | |
| \ No newline at end of file | |
test/fixtures/views/render_test/default.html.erb
+1
-0
| @@ | @@ -0,0 +1 @@ |
| + | Default <%= 'Template' %> |
| \ No newline at end of file | |
test/integration/render_cms_test.rb
+12
-0
| @@ | @@ -8,6 +8,7 @@ class RenderCmsTest < ActionDispatch::IntegrationTest |
| get '/render-explicit' => 'render_test#explicit' | |
| get '/render-text' => 'render_test#render_text' | |
| get '/render-update' => 'render_test#render_update' | |
| + | get '/render-layout' => 'render_test#render_layout' |
| end | |
| super | |
| end | |
| @@ | @@ -17,6 +18,8 @@ class RenderCmsTest < ActionDispatch::IntegrationTest |
| end | |
| class ::RenderTestController < ApplicationController | |
| + | append_view_path(File.expand_path('../fixtures/views', File.dirname(__FILE__))) |
| + | |
| def implicit | |
| render | |
| end | |
| @@ | @@ -31,6 +34,9 @@ class RenderCmsTest < ActionDispatch::IntegrationTest |
| page.alert('rendered text') | |
| end | |
| end | |
| + | def render_layout |
| + | render :cms_layout => 'default', :default_page_text => 'default' |
| + | end |
| end | |
| def test_get_with_no_template | |
| @@ | @@ -81,4 +87,10 @@ class RenderCmsTest < ActionDispatch::IntegrationTest |
| assert_response :success | |
| end | |
| + | def test_get_render_with_cms_layout |
| + | get '/render-layout' |
| + | assert_response :success |
| + | assert_equal "\nlayout_content_a\nDefault Template\nlayout_content_b\ndefault_snippet_content\nlayout_content_c", response.body |
| + | end |
| + | |
| end | |
| \ No newline at end of file | |