new rendering method, downgrading paperclip because they released broken versions
Oleg
committed Oct 03, 2011
commit 945433d18708ebd6b6533caf998fa47f7f9916d2
Showing 15
changed files with
202 additions
and 132 deletions
.travis.yml
+2
-2
| @@ | @@ -2,13 +2,13 @@ rvm: |
| - 1.8.7 | |
| - 1.9.2 | |
| # - 1.9.3 | |
| - | # - rbx-2.0 |
| + | - rbx-2.0 |
| - ree | |
| # - jruby | |
| gemfile: | |
| - test/gemfiles/Gemfile.rails-3.0.x | |
| - test/gemfiles/Gemfile.rails-3.1.x | |
| - | - test/gemfiles/Gemfile.rails-3.1.x.rc |
| + | - test/gemfiles/Gemfile.rails-3.1.stable |
| notifications: | |
| recipients: | |
| - oleg@twg.ca | |
Gemfile
+1
-1
| @@ | @@ -2,7 +2,7 @@ source 'http://rubygems.org' |
| gem 'rails', '>=3.0.0' | |
| gem 'active_link_to', '~>1.0.0' | |
| - | gem 'paperclip', '~>2.4.1' |
| + | gem 'paperclip', '~>2.3.0' |
| group :test do | |
| gem 'sqlite3' | |
app/models/cms/page.rb
+4
-6
| @@ | @@ -23,10 +23,10 @@ class Cms::Page < ActiveRecord::Base |
| # -- Callbacks ------------------------------------------------------------ | |
| before_validation :assigns_label, | |
| - | :assign_parent, |
| - | :assign_full_path |
| + | :assign_parent |
| before_create :assign_position | |
| - | before_save :set_cached_content |
| + | before_save :assign_full_path, |
| + | :set_cached_content |
| after_save :sync_child_pages | |
| # -- Validations ---------------------------------------------------------- | |
| @@ | @@ -37,12 +37,10 @@ class Cms::Page < ActiveRecord::Base |
| validates :slug, | |
| :presence => true, | |
| :format => /^\w[a-z0-9_-]*$/i, | |
| + | :uniqueness => { :scope => :parent_id }, |
| :unless => lambda{ |p| p.site && (p.site.pages.count == 0 || p.site.pages.root == self) } | |
| validates :layout, | |
| :presence => true | |
| - | validates :full_path, |
| - | :presence => true, |
| - | :uniqueness => { :scope => :site_id } |
| validate :validate_target_page | |
| # -- Scopes --------------------------------------------------------------- | |
comfortable_mexican_sofa.rb b/lib/comfortable_mexican_sofa.rb
+1
-1
| @@ | @@ -8,7 +8,7 @@ end |
| 'comfortable_mexican_sofa/configuration', | |
| 'comfortable_mexican_sofa/authentication/http_auth', | |
| 'comfortable_mexican_sofa/authentication/dummy_auth', | |
| - | 'comfortable_mexican_sofa/controller_methods', |
| + | 'comfortable_mexican_sofa/render_methods', |
| 'comfortable_mexican_sofa/view_hooks', | |
| 'comfortable_mexican_sofa/view_methods', | |
| 'comfortable_mexican_sofa/form_builder', | |
comfortable_mexican_sofa/controller_methods.rb b/lib/comfortable_mexican_sofa/controller_methods.rb
+0
-67
| @@ | @@ -1,67 +0,0 @@ |
| - | module ComfortableMexicanSofa::ControllerMethods |
| - | |
| - | 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 |
| - | raise e |
| - | 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 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) |
| - | 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) |
| - | 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 |
| - | end |
| - | end |
| - | end |
| - | |
| - | ActionController::Base.send :include, ComfortableMexicanSofa::ControllerMethods |
| \ No newline at end of file | |
comfortable_mexican_sofa/render_methods.rb b/lib/comfortable_mexican_sofa/render_methods.rb
+73
-0
| @@ | @@ -0,0 +1,73 @@ |
| + | module ComfortableMexicanSofa::RenderMethods |
| + | |
| + | 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 |
| + | raise e |
| + | 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 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_blocks => { |
| + | # :block_label_a => 'content text', |
| + | # :block_label_b => { :template => 'path/to/template' }, |
| + | # :block_label_c => { :partial => 'path/to/partial' } |
| + | # } |
| + | # This way you are populating page block content 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) |
| + | 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) |
| + | 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) |
| + | |
| + | cms_blocks = options.delete(:cms_blocks) || { :content => render_to_string } |
| + | cms_blocks.each do |block_label, value| |
| + | content = if value.is_a?(Hash) |
| + | render_to_string(value.keys.first.to_sym => value[value.keys.first]) |
| + | else |
| + | value.to_s |
| + | end |
| + | cms_page.blocks.build(:label => block_label.to_s, :content => content) |
| + | end |
| + | options[:layout] ||= cms_app_layout.blank?? nil : cms_app_layout |
| + | options[:inline] = cms_page.content(true) |
| + | super(options, locals, &block) |
| + | else |
| + | raise ComfortableMexicanSofa::MissingLayout.new(slug) |
| + | end |
| + | |
| + | else |
| + | super(options, locals, &block) |
| + | end |
| + | end |
| + | end |
| + | end |
| + | |
| + | ActionController::Base.send :include, ComfortableMexicanSofa::RenderMethods |
| \ No newline at end of file | |
test/fixtures/views/render_test/_test.html.erb
+1
-0
| @@ | @@ -0,0 +1 @@ |
| + | TestPartial <%= @test_value %> |
| \ No newline at end of file | |
test/fixtures/views/render_test/default.html.erb
+0
-1
| @@ | @@ -1 +0,0 @@ |
| - | Default <%= 'Template' %> |
| \ No newline at end of file | |
test/fixtures/views/render_test/render_layout.html.erb
+1
-0
| @@ | @@ -0,0 +1 @@ |
| + | TestTemplate <%= @test_value %> |
| \ No newline at end of file | |
test/gemfiles/Gemfile.rails-3.0.x
+1
-1
| @@ | @@ -2,7 +2,7 @@ source 'http://rubygems.org' |
| gem 'rails', '~>3.0.10' | |
| gem 'active_link_to', '~>1.0.0' | |
| - | gem 'paperclip', '~>2.4.2' |
| + | gem 'paperclip', '~>2.3.0' |
| group :test do | |
| gem 'sqlite3' | |
test/gemfiles/Gemfile.rails-3.1.stable
+10
-0
| @@ | @@ -0,0 +1,10 @@ |
| + | source 'http://rubygems.org' |
| + | |
| + | gem 'rails', :git => 'git://github.com/rails/rails.git', :branch => '3-1-stable' |
| + | gem 'active_link_to', '~>1.0.0' |
| + | gem 'paperclip', '~>2.3.0' |
| + | |
| + | group :test do |
| + | gem 'sqlite3' |
| + | gem 'jeweler' |
| + | end |
test/gemfiles/Gemfile.rails-3.1.x
+1
-1
| @@ | @@ -2,7 +2,7 @@ source 'http://rubygems.org' |
| gem 'rails', '~>3.1.0' | |
| gem 'active_link_to', '~>1.0.0' | |
| - | gem 'paperclip', '~>2.4.2' |
| + | gem 'paperclip', '~>2.3.0' |
| group :test do | |
| gem 'sqlite3' | |
test/gemfiles/Gemfile.rails-3.1.x.rc
+0
-10
| @@ | @@ -1,10 +0,0 @@ |
| - | source 'http://rubygems.org' |
| - | |
| - | gem 'rails', '~>3.1.1.rc2' |
| - | gem 'active_link_to', '~>1.0.0' |
| - | gem 'paperclip', '~>2.4.2' |
| - | |
| - | group :test do |
| - | gem 'sqlite3' |
| - | gem 'jeweler' |
| - | end |
test/integration/render_cms_test.rb
+106
-40
| @@ | @@ -4,12 +4,14 @@ class RenderCmsTest < ActionDispatch::IntegrationTest |
| def setup | |
| Rails.application.routes.draw do | |
| - | get '/render-implicit' => 'render_test#implicit' |
| - | 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' |
| + | get '/render-basic' => 'render_test#render_basic' |
| + | get '/render-page' => 'render_test#render_page' |
| + | get '/render-layout' => 'render_test#render_layout' |
| end | |
| + | cms_layouts(:default).update_attribute(:content, '{{cms:page:content}}') |
| + | cms_pages(:child).update_attribute(:blocks_attributes, [ |
| + | { :label => 'content', :content => 'TestBlockContent' } |
| + | ]) |
| super | |
| end | |
| @@ | @@ -20,77 +22,141 @@ class RenderCmsTest < ActionDispatch::IntegrationTest |
| class ::RenderTestController < ApplicationController | |
| append_view_path(File.expand_path('../fixtures/views', File.dirname(__FILE__))) | |
| - | def implicit |
| - | render |
| - | end |
| - | def explicit |
| - | render :cms_page => '/render-explicit-page' |
| - | end |
| - | def render_text |
| - | render :text => 'rendered text' |
| + | def render_basic |
| + | case params[:type] |
| + | when 'text' |
| + | render :text => 'TestText' |
| + | when 'update' |
| + | render :update do |page| |
| + | page.alert('rendered text') |
| + | end |
| + | else |
| + | render |
| + | end |
| end | |
| - | def render_update |
| - | render :update do |page| |
| - | page.alert('rendered text') |
| + | |
| + | def render_page |
| + | case params[:type] |
| + | when 'page_implicit' |
| + | render |
| + | when 'page_explicit' |
| + | render :cms_page => '/test-page' |
| + | when 'page_explicit_with_status' |
| + | render :cms_page => '/test-page', :status => 404 |
| + | else |
| + | raise 'Invalid or no param[:type] provided' |
| end | |
| end | |
| + | |
| def render_layout | |
| - | render :cms_layout => 'default', :default_page_text => 'default' |
| + | @test_value = 'TestValue' |
| + | case params[:type] |
| + | when 'layout_defaults' |
| + | render :cms_layout => 'default' |
| + | when 'layout' |
| + | render :cms_layout => 'default', :cms_blocks => { |
| + | :content => 'TestText', |
| + | :content_b => { :partial => 'render_test/test' }, |
| + | :content_c => { :template => 'render_test/render_layout' } |
| + | } |
| + | when 'layout_with_status' |
| + | render :cms_layout => 'default', :status => 404 |
| + | when 'layout_invalid' |
| + | render :cms_layout => 'invalid' |
| + | else |
| + | raise 'Invalid or no param[:type] provided' |
| + | end |
| end | |
| end | |
| - | def test_get_with_no_template |
| + | # -- Basic Render Tests --------------------------------------------------- |
| + | def test_text |
| + | get '/render-basic?type=text' |
| + | assert_response :success |
| + | assert_equal 'TestText', response.body |
| + | end |
| + | |
| + | def test_update |
| + | return 'Not supported in >= 3.1' if Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR >= 1 |
| + | get '/render-basic?type=update' |
| + | assert_response :success |
| + | end |
| + | |
| + | def test_implicit_cms_page_failure |
| assert_exception_raised ActionView::MissingTemplate do | |
| - | get '/render-implicit' |
| + | get '/render-basic' |
| end | |
| end | |
| - | def test_get_with_implicit_cms_template |
| + | # -- Page Render Test ----------------------------------------------------- |
| + | def test_implicit_cms_page |
| page = cms_pages(:child) | |
| - | page.slug = 'render-implicit' |
| - | page.save! |
| - | get '/render-implicit' |
| + | page.update_attribute(:slug, 'render-basic') |
| + | get '/render-basic?type=page_implicit' |
| assert_response :success | |
| assert assigns(:cms_site) | |
| assert assigns(:cms_layout) | |
| assert assigns(:cms_page) | |
| + | assert_equal page, assigns(:cms_page) |
| + | assert_equal 'TestBlockContent', response.body |
| end | |
| - | def test_get_with_explicit_cms_template |
| + | def test_explicit_cms_page |
| page = cms_pages(:child) | |
| - | page.slug = 'render-explicit-page' |
| - | page.save! |
| - | get '/render-explicit' |
| + | page.update_attribute(:slug, 'test-page') |
| + | get '/render-page?type=page_explicit' |
| assert_response :success | |
| assert assigns(:cms_site) | |
| assert assigns(:cms_layout) | |
| assert assigns(:cms_page) | |
| + | assert_equal page, assigns(:cms_page) |
| + | assert_equal 'TestBlockContent', response.body |
| end | |
| - | def test_get_with_explicit_cms_template_failure |
| + | def test_explicit_cms_page_with_status |
| page = cms_pages(:child) | |
| - | page.slug = 'render-explicit-404' |
| - | page.save! |
| + | page.update_attribute(:slug, 'test-page') |
| + | get '/render-page?type=page_explicit_with_status' |
| + | assert_response 404 |
| + | assert assigns(:cms_site) |
| + | assert assigns(:cms_layout) |
| + | assert assigns(:cms_page) |
| + | assert_equal page, assigns(:cms_page) |
| + | assert_equal 'TestBlockContent', response.body |
| + | end |
| + | |
| + | def test_explicit_cms_page_failure |
| + | page = cms_pages(:child) |
| + | page.update_attribute(:slug, 'invalid') |
| assert_exception_raised ComfortableMexicanSofa::MissingPage do | |
| - | get '/render-explicit' |
| + | get '/render-page?type=page_explicit' |
| end | |
| end | |
| - | def test_get_render_text |
| - | get '/render-text' |
| + | # -- Layout Render Tests -------------------------------------------------- |
| + | def test_cms_layout_defaults |
| + | get '/render-layout?type=layout_defaults' |
| assert_response :success | |
| + | assert_equal 'TestTemplate TestValue', response.body |
| end | |
| - | def test_get_render_update |
| - | return 'Not supported >= 3.1' if Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR >= 1 |
| - | get '/render-update' |
| + | def test_cms_layout |
| + | cms_layouts(:default).update_attribute(:content, '{{cms:page:content}} {{cms:page:content_b}} {{cms:page:content_c}}') |
| + | get '/render-layout?type=layout' |
| assert_response :success | |
| + | assert_equal 'TestText TestPartial TestValue TestTemplate TestValue', response.body |
| 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 |
| + | def test_cms_layout_with_status |
| + | get '/render-layout?type=layout_with_status' |
| + | assert_response 404 |
| + | assert_equal 'TestTemplate TestValue', response.body |
| + | end |
| + | |
| + | def test_cms_layout_failure |
| + | assert_exception_raised ComfortableMexicanSofa::MissingLayout do |
| + | get '/render-layout?type=layout_invalid' |
| + | end |
| end | |
| end | |
| \ No newline at end of file | |
test/unit/view_methods_test.rb
+1
-2
| @@ | @@ -1,9 +1,8 @@ |
| require File.expand_path('../test_helper', File.dirname(__FILE__)) | |
| class ViewMethodsTest < ActionView::TestCase | |
| - | include ComfortableMexicanSofa::ViewMethods |
| - | class HelpersTestController < ActionController::Base |
| + | class ::HelpersTestController < ActionController::Base |
| helper { def hello; 'hello' end } | |
| def test_cms_snippet_content | |
| render :inline => '<%= cms_snippet_content(:default) %>' | |