removing accepts_attributes_for, i can do it myself

Oleg committed May 06, 2011
commit cf68ee4ca069e99d9eaf452554c0ca0a8b273699
Showing 4 changed files with 18 additions and 11 deletions
app/models/cms/page.rb +15 -3
@@ @@ -13,8 +13,8 @@ class Cms::Page < ActiveRecord::Base
belongs_to :target_page,
:class_name => 'Cms::Page'
has_many :blocks,
- :dependent => :destroy
- accepts_nested_attributes_for :blocks
+ :dependent => :destroy,
+ :autosave => true
# -- Callbacks ------------------------------------------------------------
before_validation :assign_parent,
@@ @@ -69,11 +69,23 @@ class Cms::Page < ActiveRecord::Base
block_attr = {}
block_attr[:label] = block.label
block_attr[:content] = block.content
- block_attr[:id] = block.id
arr << block_attr
end
end
+ # Array of block hashes in the following format:
+ # [
+ # { :label => 'block_1', :content => 'block content' },
+ # { :label => 'block_2', :content => 'block content' }
+ # ]
+ def blocks_attributes=(block_hashes = [])
+ 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])
+ block.content = block_hash[:content]
+ end
+ end
+
# Processing content will return rendered content and will populate
# self.cms_tags with instances of CmsTag
def content(force_reload = false)
comfortable_mexican_sofa/form_builder.rb b/lib/comfortable_mexican_sofa/form_builder.rb +0 -1
@@ @@ -76,7 +76,6 @@ class ComfortableMexicanSofa::FormBuilder < ActionView::Helpers::FormBuilder
<div class='value'>
#{field}
#{@template.hidden_field_tag('cms_page[blocks_attributes][][label]', tag.label, :id => nil)}
- #{@template.hidden_field_tag('cms_page[blocks_attributes][][id]', tag.record_id, :id => nil) if tag.record_id}
</div>
</div>
).html_safe
test/functional/cms_admin/pages_controller_test.rb +3 -6
@@ @@ -103,8 +103,6 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase
assert assigns(:cms_page)
assert_template :edit
assert_select "form[action=/cms-admin/pages/#{page.id}]"
- assert_select "input[name='cms_page[blocks_attributes][][id]'][value='#{cms_blocks(:default_field_text).id}']"
- assert_select "input[name='cms_page[blocks_attributes][][id]'][value='#{cms_blocks(:default_field_text).id}']"
end
def test_get_edit_failure
@@ @@ -221,14 +219,13 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase
def test_update_with_layout_change
page = cms_pages(:default)
- assert_difference 'Cms::Block.count', 1 do
+ assert_difference 'Cms::Block.count', 2 do
put :update, :id => page, :cms_page => {
:label => 'Updated Label',
:layout_id => cms_layouts(:nested).id,
:blocks_attributes => [
{ :label => 'content',
- :content => 'new_page_text_content',
- :id => cms_blocks(:default_page_text).id },
+ :content => 'new_page_text_content' },
{ :label => 'header',
:content => 'new_page_string_content' }
]
@@ @@ -238,7 +235,7 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase
assert_redirected_to :action => :edit, :id => page
assert_equal 'Page updated', flash[:notice]
assert_equal 'Updated Label', page.label
- assert_equal ['new_page_text_content', 'default_field_text_content', 'new_page_string_content'], page.blocks.collect{|b| b.content}
+ assert_equal ['content', 'default_field_text', 'default_page_text', 'header'], page.blocks.collect{|b| b.label}
end
end
test/unit/models/page_test.rb +0 -1
@@ @@ -157,7 +157,6 @@ class CmsPageTest < ActiveSupport::TestCase
assert_equal page.blocks.count, page.blocks_attributes.size
assert_equal 'default_field_text', page.blocks_attributes.first[:label]
assert_equal 'default_field_text_content', page.blocks_attributes.first[:content]
- assert page.blocks_attributes.first[:id]
end
def test_content_caching