adjusting generation of form elements for blocks
Oleg
committed Sep 28, 2011
commit 983d8832101db09ebc28fbc763f0fcbf07c61543
Showing 5
changed files with
99 additions
and 50 deletions
app/models/cms/page.rb
+1
-0
| @@ | @@ -84,6 +84,7 @@ class Cms::Page < ActiveRecord::Base |
| # { :label => 'block_2', :content => 'block content' } | |
| # ] | |
| def blocks_attributes=(block_hashes = []) | |
| + | block_hashes = block_hashes.values if block_hashes.is_a?(Hash) |
| block_hashes.each do |block_hash| | |
| block_hash.symbolize_keys! unless block_hash.is_a?(HashWithIndifferentAccess) | |
| block = self.blocks.detect{|b| b.label == block_hash[:label]} || self.blocks.build(:label => block_hash[:label]) | |
app/views/cms_admin/pages/_form_blocks.html.erb
+2
-2
| @@ | @@ -8,8 +8,8 @@ |
| </div> | |
| <% else %> | |
| <%= fields_for :blocks, nil, :builder => ComfortableMexicanSofa::FormBuilder do |cms_blocks| %> | |
| - | <% block_tags.each do |tag| %> |
| - | <%= cms_blocks.send(tag.class.to_s.demodulize.underscore, tag)%> |
| + | <% block_tags.each_with_index do |tag, index| %> |
| + | <%= cms_blocks.send(tag.class.to_s.demodulize.underscore, tag, index) rescue nil %> |
| <% end %> | |
| <% end %> | |
| <% end %> | |
comfortable_mexican_sofa/form_builder.rb b/lib/comfortable_mexican_sofa/form_builder.rb
+27
-27
| @@ | @@ -55,7 +55,7 @@ class ComfortableMexicanSofa::FormBuilder < ActionView::Helpers::FormBuilder |
| end | |
| # -- Tag Field Fields ----------------------------------------------------- | |
| - | def default_tag_field(tag, options = {}) |
| + | def default_tag_field(tag, i, options = {}) |
| label = options[:label] || tag.label.to_s.titleize | |
| css_class = options[:css_class] || tag.class.to_s.demodulize.underscore | |
| @@ | @@ -73,67 +73,67 @@ class ComfortableMexicanSofa::FormBuilder < ActionView::Helpers::FormBuilder |
| options[:field] || | |
| case method = options[:content_field_method] | |
| when :file_field_tag | |
| - | @template.send(method, 'page[blocks_attributes][][content]', :id => nil, :class => field_css_class) + |
| + | @template.send(method, "page[blocks_attributes][#{i}][content]", :id => nil, :class => field_css_class) + |
| @template.render(:partial => 'cms_admin/files/page_form', :object => tag.block) | |
| else | |
| - | @template.send(method, 'page[blocks_attributes][][content]', tag.content, :id => nil, :class => field_css_class) |
| + | @template.send(method, "page[blocks_attributes][#{i}][content]", tag.content, :id => nil, :class => field_css_class) |
| end | |
| - | content = "#{field} #{@template.hidden_field_tag('page[blocks_attributes][][label]', tag.label, :id => nil)}" |
| + | content = "#{field} #{@template.hidden_field_tag("page[blocks_attributes][#{i}][label]", tag.label, :id => nil)}" |
| simple_field(label, content, :class => css_class) | |
| end | |
| - | def field_date_time(tag) |
| - | default_tag_field(tag) |
| + | def field_date_time(tag, i) |
| + | default_tag_field(tag, i) |
| end | |
| - | def field_integer(tag) |
| - | default_tag_field(tag, :content_field_method => :number_field_tag) |
| + | def field_integer(tag, i) |
| + | default_tag_field(tag, i, :content_field_method => :number_field_tag) |
| end | |
| - | def field_string(tag) |
| - | default_tag_field(tag) |
| + | def field_string(tag, i) |
| + | default_tag_field(tag, i) |
| end | |
| - | def field_text(tag) |
| - | default_tag_field(tag, :content_field_method => :text_area_tag) |
| + | def field_text(tag, i) |
| + | default_tag_field(tag, i, :content_field_method => :text_area_tag) |
| end | |
| - | def page_date_time(tag) |
| - | default_tag_field(tag) |
| + | def page_date_time(tag, i) |
| + | default_tag_field(tag, i) |
| end | |
| - | def page_integer(tag) |
| - | default_tag_field(tag, :content_field_method => :number_field_tag) |
| + | def page_integer(tag, i) |
| + | default_tag_field(tag, i, :content_field_method => :number_field_tag) |
| end | |
| - | def page_string(tag) |
| - | default_tag_field(tag) |
| + | def page_string(tag, i) |
| + | default_tag_field(tag, i) |
| end | |
| - | def page_text(tag) |
| - | default_tag_field(tag, :content_field_method => :text_area_tag) |
| + | def page_text(tag, i) |
| + | default_tag_field(tag, i, :content_field_method => :text_area_tag) |
| end | |
| - | def page_rich_text(tag) |
| - | default_tag_field(tag, :content_field_method => :text_area_tag) |
| + | def page_rich_text(tag, i) |
| + | default_tag_field(tag, i, :content_field_method => :text_area_tag) |
| end | |
| - | def page_file(tag) |
| - | default_tag_field(tag, :content_field_method => :file_field_tag) |
| + | def page_file(tag, i) |
| + | default_tag_field(tag, i, :content_field_method => :file_field_tag) |
| end | |
| - | def collection(tag) |
| + | def collection(tag, i) |
| options = [["---- Select #{tag.collection_class.titleize} ----", nil]] + | |
| tag.collection_objects.collect do |m| | |
| [m.send(tag.collection_title), m.send(tag.collection_identifier)] | |
| end | |
| content = @template.select_tag( | |
| - | 'page[blocks_attributes][][content]', |
| + | "page[blocks_attributes][#{i}][content]", |
| @template.options_for_select(options, :selected => tag.content), | |
| :id => nil | |
| ) | |
| - | content << @template.hidden_field_tag('page[blocks_attributes][][label]', tag.label, :id => nil) |
| + | content << @template.hidden_field_tag("page[blocks_attributes][#{i}][label]", tag.label, :id => nil) |
| simple_field(tag.label, content, :class => tag.class.to_s.demodulize.underscore ) | |
| end | |
test/functional/cms_admin/pages_controller_test.rb
+47
-21
| @@ | @@ -49,77 +49,103 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase |
| def test_get_new_with_field_datetime | |
| cms_layouts(:default).update_attribute(:content, '{{cms:field:test_label:datetime}}') | |
| get :new, :site_id => cms_sites(:default) | |
| - | assert_select "input[type='text'][name='page[blocks_attributes][][content]'][class='datetime']" |
| - | assert_select "input[type='hidden'][name='page[blocks_attributes][][label]'][value='test_label']" |
| + | assert_response :success |
| + | assert_select "input[type='text'][name='page[blocks_attributes][0][content]'][class='datetime']" |
| + | assert_select "input[type='hidden'][name='page[blocks_attributes][0][label]'][value='test_label']" |
| end | |
| def test_get_new_with_field_integer | |
| cms_layouts(:default).update_attribute(:content, '{{cms:field:test_label:integer}}') | |
| get :new, :site_id => cms_sites(:default) | |
| - | assert_select "input[type='number'][name='page[blocks_attributes][][content]']" |
| - | assert_select "input[type='hidden'][name='page[blocks_attributes][][label]'][value='test_label']" |
| + | assert_response :success |
| + | assert_select "input[type='number'][name='page[blocks_attributes][0][content]']" |
| + | assert_select "input[type='hidden'][name='page[blocks_attributes][0][label]'][value='test_label']" |
| end | |
| def test_get_new_with_field_string | |
| cms_layouts(:default).update_attribute(:content, '{{cms:field:test_label:string}}') | |
| get :new, :site_id => cms_sites(:default) | |
| - | assert_select "input[type='text'][name='page[blocks_attributes][][content]']" |
| - | assert_select "input[type='hidden'][name='page[blocks_attributes][][label]'][value='test_label']" |
| + | assert_response :success |
| + | assert_select "input[type='text'][name='page[blocks_attributes][0][content]']" |
| + | assert_select "input[type='hidden'][name='page[blocks_attributes][0][label]'][value='test_label']" |
| end | |
| def test_get_new_with_field_text | |
| cms_layouts(:default).update_attribute(:content, '{{cms:field:test_label:text}}') | |
| get :new, :site_id => cms_sites(:default) | |
| - | assert_select "textarea[name='page[blocks_attributes][][content]'][class='code']" |
| - | assert_select "input[type='hidden'][name='page[blocks_attributes][][label]'][value='test_label']" |
| + | assert_response :success |
| + | assert_select "textarea[name='page[blocks_attributes][0][content]'][class='code']" |
| + | assert_select "input[type='hidden'][name='page[blocks_attributes][0][label]'][value='test_label']" |
| end | |
| def test_get_new_with_page_datetime | |
| cms_layouts(:default).update_attribute(:content, '{{cms:page:test_label:datetime}}') | |
| get :new, :site_id => cms_sites(:default) | |
| - | assert_select "input[type='text'][name='page[blocks_attributes][][content]'][class='datetime']" |
| - | assert_select "input[type='hidden'][name='page[blocks_attributes][][label]'][value='test_label']" |
| + | assert_response :success |
| + | assert_select "input[type='text'][name='page[blocks_attributes][0][content]'][class='datetime']" |
| + | assert_select "input[type='hidden'][name='page[blocks_attributes][0][label]'][value='test_label']" |
| end | |
| def test_get_new_with_page_integer | |
| cms_layouts(:default).update_attribute(:content, '{{cms:page:test_label:integer}}') | |
| get :new, :site_id => cms_sites(:default) | |
| - | assert_select "input[type='number'][name='page[blocks_attributes][][content]']" |
| - | assert_select "input[type='hidden'][name='page[blocks_attributes][][label]'][value='test_label']" |
| + | assert_response :success |
| + | assert_select "input[type='number'][name='page[blocks_attributes][0][content]']" |
| + | assert_select "input[type='hidden'][name='page[blocks_attributes][0][label]'][value='test_label']" |
| end | |
| def test_get_new_with_page_string | |
| cms_layouts(:default).update_attribute(:content, '{{cms:page:test_label:string}}') | |
| get :new, :site_id => cms_sites(:default) | |
| - | assert_select "input[type='text'][name='page[blocks_attributes][][content]']" |
| - | assert_select "input[type='hidden'][name='page[blocks_attributes][][label]'][value='test_label']" |
| + | assert_response :success |
| + | assert_select "input[type='text'][name='page[blocks_attributes][0][content]']" |
| + | assert_select "input[type='hidden'][name='page[blocks_attributes][0][label]'][value='test_label']" |
| end | |
| def test_get_new_with_page_text | |
| cms_layouts(:default).update_attribute(:content, '{{cms:page:test_label}}') | |
| get :new, :site_id => cms_sites(:default) | |
| - | assert_select "textarea[name='page[blocks_attributes][][content]'][class='code']" |
| - | assert_select "input[type='hidden'][name='page[blocks_attributes][][label]'][value='test_label']" |
| + | assert_response :success |
| + | assert_select "textarea[name='page[blocks_attributes][0][content]'][class='code']" |
| + | assert_select "input[type='hidden'][name='page[blocks_attributes][0][label]'][value='test_label']" |
| end | |
| def test_get_new_with_collection | |
| snippet = cms_snippets(:default) | |
| cms_layouts(:default).update_attribute(:content, '{{cms:collection:snippet:cms/snippet}}') | |
| get :new, :site_id => cms_sites(:default) | |
| - | assert_select "select[name='page[blocks_attributes][][content]']" do |
| + | assert_response :success |
| + | assert_select "select[name='page[blocks_attributes][0][content]']" do |
| assert_select "option[value='']", :html => '---- Select Cms/Snippet ----' | |
| assert_select "option[value='#{snippet.id}']", :html => snippet.label | |
| end | |
| - | assert_select "input[type='hidden'][name='page[blocks_attributes][][label]'][value='snippet']" |
| + | assert_select "input[type='hidden'][name='page[blocks_attributes][0][label]'][value='snippet']" |
| end | |
| def test_get_new_with_rich_page_text | |
| cms_layouts(:default).update_attribute(:content, '{{cms:page:test_label:rich_text}}') | |
| get :new, :site_id => cms_sites(:default) | |
| - | assert_select "textarea[name='page[blocks_attributes][][content]'][class='rich_text']" |
| - | assert_select "input[type='hidden'][name='page[blocks_attributes][][label]'][value='test_label']" |
| + | assert_response :success |
| + | assert_select "textarea[name='page[blocks_attributes][0][content]'][class='rich_text']" |
| + | assert_select "input[type='hidden'][name='page[blocks_attributes][0][label]'][value='test_label']" |
| + | end |
| + | |
| + | def test_get_new_with_several_tag_fields |
| + | cms_layouts(:default).update_attribute(:content, '{{cms:page:label_a}}{{cms:page:label_b}}') |
| + | get :new, :site_id => cms_sites(:default) |
| + | assert_response :success |
| + | assert_select "textarea[name='page[blocks_attributes][0][content]']" |
| + | assert_select "input[type='hidden'][name='page[blocks_attributes][0][label]'][value='label_a']" |
| + | assert_select "textarea[name='page[blocks_attributes][1][content]']" |
| + | assert_select "input[type='hidden'][name='page[blocks_attributes][1][label]'][value='label_b']" |
| end | |
| - | |
| + | |
| + | def test_get_new_with_crashy_tag |
| + | cms_layouts(:default).update_attribute(:content, '{{cms:collection:label:invalid}}') |
| + | get :new, :site_id => cms_sites(:default) |
| + | assert_response :success |
| + | end |
| + | |
| def test_get_new_as_child_page | |
| get :new, :site_id => cms_sites(:default), :parent_id => cms_pages(:default) | |
| assert_response :success | |
test/unit/models/block_test.rb
+22
-0
| @@ | @@ -36,6 +36,28 @@ class CmsBlockTest < ActiveSupport::TestCase |
| end | |
| end | |
| + | def test_new_via_page_nested_attributes_as_hash |
| + | assert_difference ['Cms::Page.count', 'Cms::Block.count'] do |
| + | page = Cms::Page.create!( |
| + | :site => cms_sites(:default), |
| + | :layout => cms_layouts(:default), |
| + | :label => 'test page', |
| + | :slug => 'test_page', |
| + | :parent_id => cms_pages(:default).id, |
| + | :blocks_attributes => { |
| + | '0' => { |
| + | :label => 'default_page_text', |
| + | :content => 'test_content' |
| + | } |
| + | } |
| + | ) |
| + | assert_equal 1, page.blocks.count |
| + | block = page.blocks.first |
| + | assert_equal 'default_page_text', block.label |
| + | assert_equal 'test_content', block.content |
| + | end |
| + | end |
| + | |
| def test_new_via_nested_attributes_with_files | |
| assert_difference ['Cms::Page.count', 'Cms::Block.count'] do | |
| assert_difference 'Cms::File.count', 2 do | |