tag initialization kicks in on layout assignment
Oleg
committed Sep 03, 2010
commit 03da810f9e2aaed9a2b53483f61abf155c7358cd
Showing 4
changed files with
75 additions
and 18 deletions
app/controllers/cms_admin/pages_controller.rb
+1
-5
| @@ | @@ -1,5 +1,5 @@ |
| class CmsAdmin::PagesController < CmsAdmin::BaseController | |
| - | before_filter :build_cms_page, :only => [:new] |
| + | before_filter :build_cms_page, :only => [:new, :create] |
| before_filter :load_cms_page, :only => [:edit] | |
| def index | |
| @@ | @@ -15,7 +15,6 @@ class CmsAdmin::PagesController < CmsAdmin::BaseController |
| end | |
| def create | |
| - | @cms_page = CmsPage.new(params[:cms_page]) |
| @cms_page.save! | |
| flash[:notice] = 'Page saved' | |
| redirect_to :action => :edit, :id => @cms_page | |
| @@ | @@ -30,7 +29,6 @@ class CmsAdmin::PagesController < CmsAdmin::BaseController |
| def form_blocks | |
| @cms_page = CmsPage.find_by_id(params[:id]) || CmsPage.new | |
| @cms_page.cms_layout = CmsLayout.find_by_id(params[:layout_id]) | |
| - | @cms_page.initialize_tags |
| end | |
| protected | |
| @@ | @@ -38,12 +36,10 @@ protected |
| def build_cms_page | |
| @cms_page = CmsPage.new(params[:cms_page]) | |
| @cms_page.cms_layout ||= CmsLayout.first | |
| - | @cms_page.initialize_tags |
| end | |
| def load_cms_page | |
| @cms_page = CmsPage.find(params[:id]) | |
| - | @cms_page.initialize_tags |
| rescue ActiveRecord::RecordNotFound | |
| flash[:error] = 'Page not found' | |
| redirect_to :action => :index | |
app/models/cms_page.rb
+18
-4
| @@ | @@ -52,11 +52,19 @@ class CmsPage < ActiveRecord::Base |
| return content | |
| end | |
| - | # Returns an array of tag objects, at the same time populates cms_blocks |
| - | # of the current page |
| - | def initialize_tags |
| - | CmsTag.initialize_tags(self) |
| + | # Initilize tags the moment layout gets assigned. This way there's no need to |
| + | # call initialize tags manually. Need to do this on both association and |
| + | # foreign id assignments. |
| + | def cms_layout_id=(value) |
| + | write_attribute(:cms_layout_id, value) |
| + | self.cms_layout_with_tag_initialization = CmsLayout.find_by_id(value) |
| + | end |
| + | |
| + | def cms_layout_with_tag_initialization=(value) |
| + | self.cms_layout_without_tag_initialization = value |
| + | self.initialize_tags |
| end | |
| + | alias_method_chain :cms_layout=, :tag_initialization |
| protected | |
| @@ | @@ -69,4 +77,10 @@ protected |
| children.each{ |p| p.save! } if full_path_changed? | |
| end | |
| + | # Returns an array of tag objects, at the same time populates cms_blocks |
| + | # of the current page |
| + | def initialize_tags |
| + | CmsTag.initialize_tags(self) |
| + | end |
| + | |
| end | |
test/functional/cms_admin/pages_controller_test.rb
+45
-8
| @@ | @@ -66,19 +66,56 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase |
| def test_creation_failure | |
| assert_no_difference ['CmsPage.count', 'CmsBlock.count'] do | |
| - | post :create, :cms_page => {} |
| + | post :create, :cms_page => { |
| + | :cms_layout_id => cms_layouts(:default).id, |
| + | :cms_blocks_attributes => [ |
| + | { :label => 'content', |
| + | :type => 'CmsTag::PageText', |
| + | :content => 'content content' }, |
| + | { :label => 'title', |
| + | :type => 'CmsTag::PageString', |
| + | :content => 'title content' }, |
| + | { :label => 'number', |
| + | :type => 'CmsTag::PageInteger', |
| + | :content => '999' } |
| + | ] |
| + | } |
| assert_response :success | |
| - | assert assigns(:cms_page) |
| + | page = assigns(:cms_page) |
| + | assert_equal 3, page.cms_blocks.size |
| + | assert_equal ['content content', 'title content', 999], page.cms_blocks.collect{|b| b.content} |
| assert_template :new | |
| end | |
| end | |
| def test_creation_with_faked_blocks | |
| - | flunk |
| - | end |
| - | |
| - | def test_creation_persisting_blocks |
| - | flunk |
| + | assert_difference 'CmsPage.count' do |
| + | assert_difference 'CmsBlock.count', 3 do |
| + | post :create, :cms_page => { |
| + | :label => 'Test Page', |
| + | :slug => 'test-page', |
| + | :parent_id => cms_pages(:default).id, |
| + | :cms_layout_id => cms_layouts(:default).id, |
| + | :cms_blocks_attributes => [ |
| + | { :label => 'content', |
| + | :type => 'CmsTag::PageText', |
| + | :content => 'content content' }, |
| + | { :label => 'title', |
| + | :type => 'CmsTag::PageString', |
| + | :content => 'title content' }, |
| + | { :label => 'number', |
| + | :type => 'CmsTag::PageInteger', |
| + | :content => '999' }, |
| + | { :label => 'bogus', |
| + | :type => 'CmsTag::PageText', |
| + | :content => 'not defined in the layout'} |
| + | ] |
| + | } |
| + | assert_response :redirect |
| + | assert_redirected_to :action => :edit, :id => CmsPage.last |
| + | assert_equal 'Page saved', flash[:notice] |
| + | end |
| + | end |
| end | |
| def test_update | |
| @@ | @@ -104,7 +141,7 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase |
| assert_response :success | |
| assert assigns(:cms_page) | |
| assert_equal 3, assigns(:cms_page).cms_blocks.size | |
| - | assert_template form_blocks |
| + | assert_template :form_blocks |
| end | |
| def test_get_form_blocks_for_new_page | |
test/unit/cms_page_test.rb
+11
-1
| @@ | @@ -73,7 +73,17 @@ class CmsPageTest < ActiveSupport::TestCase |
| end | |
| def test_initialize_tags | |
| - | assert_equal 3, cms_pages(:default).initialize_tags.size |
| + | page = CmsPage.new |
| + | assert_equal 0, page.cms_blocks.size |
| + | |
| + | page.cms_layout = cms_layouts(:default) |
| + | assert_equal 3, page.cms_blocks.size |
| + | |
| + | page.cms_layout_id = '999999' |
| + | assert_equal 0, page.cms_blocks.size |
| + | |
| + | page.cms_layout_id = cms_layouts(:default).id |
| + | assert_equal 3, page.cms_blocks.size |
| end | |
| def test_render_content_for_saved_page | |