host application can use cms pages to render views

Oleg committed Oct 18, 2010
commit 04a5e8bac1322da33c3c931c1ec6610c2459cda0
Showing 4 changed files with 120 additions and 1 deletions
comfortable_mexican_sofa.rb b/lib/comfortable_mexican_sofa.rb +1 -0
@@ @@ -6,6 +6,7 @@ end
[ 'comfortable_mexican_sofa/configuration',
'comfortable_mexican_sofa/http_auth',
'comfortable_mexican_sofa/rails_extensions',
+ 'comfortable_mexican_sofa/controller_methods',
'comfortable_mexican_sofa/view_methods',
'comfortable_mexican_sofa/form_builder',
'comfortable_mexican_sofa/acts_as_tree',
comfortable_mexican_sofa/controller_methods.rb b/lib/comfortable_mexican_sofa/controller_methods.rb +39 -1
@@ @@ -1,3 +1,41 @@
module ComfortableMexicanSofa::ControllerMethods
- end
\ No newline at end of file
+ def self.included(base)
+ base.alias_method_chain :render, :cms
+
+ # 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 ActionView::MissingTemplate
+ raise e
+ end
+ end
+ end
+
+ # 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 varaibles that can be used in partials (that are included by
+ # by the cms page and/or layout)
+ def render_with_cms(options = {}, locals = {}, &block)
+ if path = options.delete(:cms_page)
+ site = CmsSite.find_by_hostname(request.host.downcase)
+ page = site && site.cms_pages.find_by_full_path(path)
+ if page
+ cms_app_layout = page.cms_layout.try(:app_layout)
+ options[:layout] ||= cms_app_layout.blank?? nil : cms_app_layout
+ options[:inline] = page.content
+ render_without_cms(options, locals, &block)
+ else
+ raise ActionView::MissingTemplate.new([path], path, "CMS page not found", nil)
+ end
+ else
+ render_without_cms(options, locals, &block)
+ end
+ end
+ end
+
+ ActionController::Base.send :include, ComfortableMexicanSofa::ControllerMethods
\ No newline at end of file
test/integration/render_cms_test.rb +57 -0
@@ @@ -0,0 +1,57 @@
+ require File.dirname(__FILE__) + '/../test_helper'
+
+ class RenderCmsTest < ActionDispatch::IntegrationTest
+
+ def setup
+ Rails.application.routes.draw do
+ get '/render-implicit' => 'render_test#implicit'
+ get '/render-explicit' => 'render_test#explicit'
+ end
+ super
+ end
+
+ def teardown
+ load(File.expand_path('config/routes.rb', Rails.root))
+ end
+
+ class ::RenderTestController < ApplicationController
+ def implicit
+ render
+ end
+ def explicit
+ render :cms_page => '/render-explicit-page'
+ end
+ end
+
+ def test_get_with_no_template
+ assert_exception_raised ActionView::MissingTemplate do
+ get '/render-implicit'
+ end
+ end
+
+ def test_get_with_implicit_cms_template
+ page = cms_pages(:child)
+ page.slug = 'render-implicit'
+ page.save!
+ get '/render-implicit'
+ assert_response :success
+ end
+
+ def test_get_with_explicit_cms_template
+ page = cms_pages(:child)
+ page.slug = 'render-explicit-page'
+ page.save!
+ get '/render-explicit'
+ assert_response :success
+ end
+
+ def test_get_with_explicit_cms_template_failure
+ page = cms_pages(:child)
+ page.slug = 'render-explicit-404'
+ page.save!
+ assert_exception_raised ActionView::MissingTemplate do
+ get '/render-explicit'
+ end
+ end
+
+ end
\ No newline at end of file
test/test_helper.rb +23 -0
@@ @@ -31,6 +31,27 @@ class ActiveSupport::TestCase
end
end
+ # Example usage:
+ # assert_exception_raised do ... end
+ # assert_exception_raised ActiveRecord::RecordInvalid do ... end
+ # assert_exception_raised Plugin::Error, 'error_message' do ... end
+ def assert_exception_raised(exception_class = nil, error_message = nil, &block)
+ exception_raised = nil
+ yield
+ rescue => exception_raised
+ ensure
+ if exception_raised
+ if exception_class
+ assert_equal exception_class, exception_raised.class, exception_raised.to_s
+ else
+ assert true
+ end
+ assert_equal error_message, exception_raised.to_s if error_message
+ else
+ flunk 'Exception was not raised'
+ end
+ end
+
# Small method that allows for better formatting in tests
def rendered_content_formatter(string)
string.gsub(/^[ ]+/, '')
@@ @@ -51,6 +72,8 @@ class ActionDispatch::IntegrationTest
ComfortableMexicanSofa::HttpAuth.password = 'password'
end
+ # Attaching http_auth stuff with request. Example use:
+ # http_auth :get, '/cms-admin/pages'
def http_auth(method, path, options = {}, username = 'username', password = 'password')
send(method, path, options, {'HTTP_AUTHORIZATION' => "Basic #{Base64.encode64(username + ':' + password)}"})
end