drying up and adding tests
Oleg
committed Nov 16, 2011
commit 792f704220ea74ab50389f8fff3efab58be65212
Showing 3
changed files with
75 additions
and 24 deletions
comfortable_mexican_sofa/error.rb b/lib/comfortable_mexican_sofa/error.rb
+11
-4
| @@ | @@ -3,15 +3,22 @@ module ComfortableMexicanSofa |
| class Error < StandardError | |
| end | |
| - | class MissingPage < ComfortableMexicanSofa::Error |
| - | def initialize(path) |
| - | super "Cannot find CMS page at #{path}" |
| + | class MissingSite < ComfortableMexicanSofa::Error |
| + | def initialize(slug) |
| + | super "Cannot find CMS Site with slug: #{slug}" |
| end | |
| end | |
| class MissingLayout < ComfortableMexicanSofa::Error | |
| def initialize(slug) | |
| - | super "Cannot find CMS layout with slug: #{slug}" |
| + | super "Cannot find CMS Layout with slug: #{slug}" |
| end | |
| end | |
| + | |
| + | class MissingPage < ComfortableMexicanSofa::Error |
| + | def initialize(path) |
| + | super "Cannot find CMS Page at #{path}" |
| + | end |
| + | end |
| + | |
| end | |
| \ No newline at end of file | |
comfortable_mexican_sofa/render_methods.rb b/lib/comfortable_mexican_sofa/render_methods.rb
+14
-20
| @@ | @@ -30,27 +30,21 @@ module ComfortableMexicanSofa::RenderMethods |
| # This way you are populating page block content and rendering | |
| # an instantialized CMS page. | |
| # | |
| - | # Also you can render a cms_page directly in your Application, using: |
| - | # render cms_page: cms_path, cms_site: site_path |
| - | # For example: render 'about-us', cms_site: 'en' |
| - | # |
| + | # 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' |
| + | # |
| def render(options = {}, locals = {}, &block) | |
| - | if options.is_a?(Hash) && site_path = options.delete(:cms_site) |
| - | path = options.delete(:cms_page) |
| - | @cms_site = Cms::Site.find_by_path(site_path) |
| - | |
| - | 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) |
| - | else |
| - | raise ComfortableMexicanSofa::MissingPage.new(path) |
| + | |
| + | # TODO: add slug to Cms::Site as well |
| + | if options.is_a?(Hash) && site_slug = options.delete(:cms_site) |
| + | unless @cms_site = Cms::Site.find_by_label(site_slug) |
| + | raise ComfortableMexicanSofa::MissingSite.new(site_slug) |
| end | |
| - | |
| - | elsif options.is_a?(Hash) && path = options.delete(:cms_page) |
| - | @cms_site = Cms::Site.find_site(request.host.downcase, request.fullpath) |
| + | end |
| + | |
| + | if options.is_a?(Hash) && path = options.delete(: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) | |
| @@ | @@ -62,7 +56,7 @@ module ComfortableMexicanSofa::RenderMethods |
| end | |
| elsif options.is_a?(Hash) && slug = options.delete(:cms_layout) | |
| - | @cms_site = Cms::Site.find_site(request.host.downcase, request.fullpath) |
| + | @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) | |
test/integration/render_cms_test.rb
+50
-0
| @@ | @@ -19,6 +19,22 @@ class RenderCmsTest < ActionDispatch::IntegrationTest |
| load(File.expand_path('config/routes.rb', Rails.root)) | |
| end | |
| + | def create_site_b |
| + | site = cms_sites(:default).dup |
| + | site.update_attributes!( |
| + | :hostname => 'site-b.host', |
| + | :label => 'site-b') |
| + | layout = cms_layouts(:default).dup |
| + | layout.update_attributes!( |
| + | :site => site, |
| + | :content => 'site-b {{cms:page:content}}') |
| + | page = cms_pages(:default).dup |
| + | page.update_attributes!( |
| + | :site => site, |
| + | :layout => layout, |
| + | :blocks_attributes => [{ :label => 'content', :content => 'SiteBContent' }]) |
| + | end |
| + | |
| class ::RenderTestController < ApplicationController | |
| append_view_path(File.expand_path('../fixtures/views', File.dirname(__FILE__))) | |
| @@ | @@ -43,6 +59,8 @@ class RenderCmsTest < ActionDispatch::IntegrationTest |
| render :cms_page => '/test-page' | |
| when 'page_explicit_with_status' | |
| render :cms_page => '/test-page', :status => 404 | |
| + | when 'page_explicit_with_site' |
| + | render :cms_page => '/', :cms_site => 'site-b' |
| else | |
| raise 'Invalid or no param[:type] provided' | |
| end | |
| @@ | @@ -63,6 +81,8 @@ class RenderCmsTest < ActionDispatch::IntegrationTest |
| render :cms_layout => 'default', :status => 404 | |
| when 'layout_invalid' | |
| render :cms_layout => 'invalid' | |
| + | when 'layout_defaults_with_site' |
| + | render :cms_layout => 'default', :cms_site => 'site-b' |
| else | |
| raise 'Invalid or no param[:type] provided' | |
| end | |
| @@ | @@ -133,6 +153,21 @@ class RenderCmsTest < ActionDispatch::IntegrationTest |
| end | |
| end | |
| + | def test_explicit_with_site |
| + | create_site_b |
| + | get '/render-page?type=page_explicit_with_site' |
| + | assert_response :success |
| + | assert assigns(:cms_site) |
| + | assert_equal 'site-b', assigns(:cms_site).label |
| + | assert_equal 'site-b SiteBContent', response.body |
| + | end |
| + | |
| + | def test_explicit_with_site_failure |
| + | assert_exception_raised ComfortableMexicanSofa::MissingSite do |
| + | get '/render-page?type=page_explicit_with_site' |
| + | end |
| + | end |
| + | |
| # -- Layout Render Tests -------------------------------------------------- | |
| def test_cms_layout_defaults | |
| get '/render-layout?type=layout_defaults' | |
| @@ | @@ -168,4 +203,19 @@ class RenderCmsTest < ActionDispatch::IntegrationTest |
| end | |
| end | |
| + | def test_cms_layout_defaults_with_site |
| + | create_site_b |
| + | get '/render-layout?type=layout_defaults_with_site' |
| + | assert_response :success |
| + | assert assigns(:cms_site) |
| + | assert_equal 'site-b', assigns(:cms_site).label |
| + | assert_equal 'site-b TestTemplate TestValue', response.body |
| + | end |
| + | |
| + | def test_cms_layout_defaults_with_site_failure |
| + | assert_exception_raised ComfortableMexicanSofa::MissingSite do |
| + | get '/render-layout?type=layout_defaults_with_site' |
| + | end |
| + | end |
| + | |
| end | |
| \ No newline at end of file | |