bam! application layouts can be rendered
Oleg
committed Oct 13, 2010
commit c7a0b67a1d38ac6f3ed129f67f8c41e0200821a4
Showing 14
changed files with
48 additions
and 11 deletions
app/controllers/cms_admin/base_controller.rb
+1
-0
| @@ | @@ -1,4 +1,5 @@ |
| class CmsAdmin::BaseController < ApplicationController | |
| + | |
| layout 'cms_admin' | |
| end | |
app/controllers/cms_admin/pages_controller.rb
+1
-1
| @@ | @@ -4,7 +4,7 @@ class CmsAdmin::PagesController < CmsAdmin::BaseController |
| before_filter :load_cms_page, :only => [:edit, :update, :destroy] | |
| def index | |
| - | @cms_page = CmsPage.root |
| + | @cms_pages = [CmsPage.root].compact |
| end | |
| def new | |
app/controllers/cms_content_controller.rb
+2
-2
| @@ | @@ -4,8 +4,8 @@ class CmsContentController < ApplicationController |
| before_filter :load_cms_layout, :only => [:render_css, :render_js] | |
| def render_html | |
| - | # TODO |
| - | render :inline => @cms_page.content |
| + | layout = @cms_page.cms_layout.app_layout.blank?? false : @cms_page.cms_layout.app_layout |
| + | render :inline => @cms_page.content, :layout => layout |
| end | |
| def render_css | |
app/models/cms_layout.rb
+8
-1
| @@ | @@ -25,6 +25,14 @@ class CmsLayout < ActiveRecord::Base |
| return out.compact | |
| end | |
| + | # List of available application layouts |
| + | def self.app_layouts_for_select |
| + | Dir.glob(File.expand_path('app/views/layouts/*.html.*', Rails.root)).collect do |filename| |
| + | match = filename.match(/\w*.html.\w*$/) |
| + | match && match[0] |
| + | end.compact |
| + | end |
| + | |
| # -- Instance Methods ----------------------------------------------------- | |
| # magical merging tag is <cms:page:content> If parent layout has this tag | |
| # defined its content will be merged. If no such tag found, parent content | |
| @@ | @@ -45,5 +53,4 @@ class CmsLayout < ActiveRecord::Base |
| def merged_js | |
| self.parent ? self.parent.merged_js + self.js : self.js.to_s | |
| end | |
| - | |
| end | |
app/models/cms_page.rb
+1
-1
| @@ | @@ -31,7 +31,7 @@ class CmsPage < ActiveRecord::Base |
| # -- Class Methods -------------------------------------------------------- | |
| # Tree-like structure for pages | |
| def self.options_for_select(cms_page = nil, current_page = nil, depth = 0, spacer = '. . ') | |
| - | return [] if (current_page ||= CmsPage.root) == cms_page |
| + | return [] if (current_page ||= CmsPage.root) == cms_page || !current_page |
| out = [[ "#{spacer*depth}#{current_page.label}", current_page.id ]] | |
| current_page.children.each do |child| | |
| out += options_for_select(cms_page, child, depth + 1, spacer) | |
app/views/cms_admin/layouts/_form.html.erb
+2
-1
| @@ | @@ -1,5 +1,6 @@ |
| <%= form.text_field :label, :label => 'Layout Name' %> | |
| - | <%= form.select :parent_id, CmsLayout.options_for_select(@cms_layout), :include_blank => true, :label => 'Parent Layout' %> |
| + | <%= form.select :parent_id, CmsLayout.options_for_select(@cms_layout), :include_blank => true %> |
| + | <%= form.select :app_layout, CmsLayout.app_layouts_for_select, :include_blank => true %> |
| <%= form.text_area :content %> | |
| <%= form.text_area :css %> | |
| <%= form.text_area :js %> | |
app/views/cms_admin/pages/index.html.erb
+1
-1
| @@ | @@ -2,5 +2,5 @@ |
| <h1>Pages</h1> | |
| <ul class='tree'> | |
| - | <%= render :partial => 'index_branch', :collection => [@cms_page] %> |
| + | <%= render :partial => 'index_branch', :collection => @cms_pages %> |
| </ul> | |
| \ No newline at end of file | |
app/views/layouts/cms_admin.html.erb
+1
-1
| @@ | @@ -8,7 +8,7 @@ |
| <%= javascript_include_tag :cms %> | |
| <%= yield :head %> | |
| </head> | |
| - | <body> |
| + | <body id='cms_admin'> |
| <div class='body_wrapper'> | |
| <div class='left_column'> | |
| <div class='left_column_content'> | |
migrate/01_create_cms.rb b/db/migrate/01_create_cms.rb
+1
-0
| @@ | @@ -5,6 +5,7 @@ class CreateCms < ActiveRecord::Migration |
| # -- Layouts ------------------------------------------------------------ | |
| create_table :cms_layouts do |t| | |
| t.integer :parent_id | |
| + | t.string :app_layout |
| t.string :label | |
| t.text :content | |
| t.text :css | |
comfortable_mexican_sofa/cms_form_builder.rb b/lib/comfortable_mexican_sofa/cms_form_builder.rb
+1
-1
| @@ | @@ -25,7 +25,7 @@ class CmsFormBuilder < ActionView::Helpers::FormBuilder |
| end | |
| def label_for(field, options) | |
| - | label = options.delete(:label) || field.to_s.titleize.capitalize |
| + | label = options.delete(:label) || field.to_s.titleize.capitalize_all |
| "<label for=\"#{object_name}_#{field}\">#{label}</label>".html_safe | |
| end | |
comfortable_mexican_sofa/cms_rails_extensions.rb b/lib/comfortable_mexican_sofa/cms_rails_extensions.rb
+5
-0
| @@ | @@ -3,6 +3,11 @@ class String |
| def idify | |
| self.strip.gsub(/\W/, '_').gsub(/\s|^_*|_*$/, '').squeeze('_') | |
| end | |
| + | |
| + | # Capitalize all words in the string |
| + | def capitalize_all(delimiter = ' ') |
| + | self.split(delimiter).collect{|w| w.capitalize }.join(' ') |
| + | end |
| end | |
| module CmsViewHelpers | |
test/functional/cms_admin/pages_controller_test.rb
+1
-1
| @@ | @@ -5,7 +5,7 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase |
| def test_get_index | |
| get :index | |
| assert_response :success | |
| - | assert assigns(:cms_page) |
| + | assert assigns(:cms_pages) |
| assert_template :index | |
| end | |
test/functional/cms_content_controller_test.rb
+19
-1
| @@ | @@ -6,10 +6,28 @@ class CmsContentControllerTest < ActionController::TestCase |
| get :render_html, :cms_path => '' | |
| assert_equal assigns(:cms_page), cms_pages(:default) | |
| assert_response :success | |
| + | assert_equal rendered_content_formatter( |
| + | ' |
| + | layout_content_a |
| + | default_page_text_content_a |
| + | default_snippet_content |
| + | default_page_text_content_b |
| + | layout_content_b |
| + | default_snippet_content |
| + | layout_content_c' |
| + | ), @response.body |
| + | end |
| + | |
| + | def test_render_page_with_app_layout |
| + | cms_layouts(:default).update_attribute(:app_layout, 'cms_admin.html.erb') |
| + | get :render_html, :cms_path => '' |
| + | assert_response :success |
| + | assert_select 'body[id=cms_admin]' |
| end | |
| def test_render_page_not_found | |
| - | flunk 'TODO' |
| + | get :render_html, :cms_path => 'doesnotexist' |
| + | assert_response 404 |
| end | |
| def test_render_css | |
test/unit/cms_layout_test.rb
+4
-0
| @@ | @@ -20,6 +20,10 @@ class CmsLayoutTest < ActiveSupport::TestCase |
| assert_equal ['Default Layout'], CmsLayout.options_for_select(cms_layouts(:nested)).collect{|t| t.first} | |
| end | |
| + | def test_app_layouts_for_select |
| + | assert_equal ['cms_admin.html.erb'], CmsLayout.app_layouts_for_select |
| + | end |
| + | |
| def test_merged_content | |
| parent_layout = cms_layouts(:nested) | |
| layout = cms_layouts(:child) | |