cms_blocks are simplified. way less crazy

Oleg committed Nov 09, 2010
commit bd04cee3963753dabea404c337736b39a9637c63
Showing 30 changed files with 51 additions and 130 deletions
app/models/cms_block.rb +13 -14
@@ @@ -9,20 +9,19 @@ class CmsBlock < ActiveRecord::Base
:uniqueness => { :scope => :cms_page_id }
# -- Class Methods --------------------------------------------------------
- class << self
- # making sure that the correct class is initialized based on :type passed
- # primarily important for form processing
- def new_with_cast(*args, &block)
- if (h = args.first).is_a?(Hash) && (type = h[:type] || h['type']) && (klass = type.constantize) != self
- return klass.new(*args, &block)
- end
- new_without_cast(*args, &block)
- end
- alias_method_chain :new, :cast
-
- def initialize_or_find(cms_page, label)
- cms_page.cms_blocks.detect{ |b| b.label == label.to_s } ||
- self.new(:label => label.to_s, :type => self.name, :cms_page => cms_page)
+ def self.initialize_or_find(cms_page, label)
+ if block = cms_page.cms_blocks.detect{ |b| b.label == label.to_s }
+ self.new(
+ :record_id => block.id,
+ :cms_page => cms_page,
+ :label => block.label,
+ :content => block.content
+ )
+ else
+ self.new(
+ :label => label.to_s,
+ :cms_page => cms_page
+ )
end
end
app/models/cms_page.rb +0 -1
@@ @@ -84,7 +84,6 @@ class CmsPage < ActiveRecord::Base
def cms_blocks_attributes
self.cms_blocks.inject([]) do |arr, block|
block_attr = {}
- block_attr[:type] = block.class.name
block_attr[:label] = block.label
block_attr[:content] = block.content
block_attr[:id] = block.id
migrate/01_create_cms.rb b/db/migrate/01_create_cms.rb +2 -6
@@ @@ -43,16 +43,12 @@ class CreateCms < ActiveRecord::Migration
# -- Page Blocks --------------------------------------------------------
create_table :cms_blocks do |t|
- t.string :type
t.integer :cms_page_id
t.string :label
- t.string :content_string
- t.text :content_text
- t.integer :content_integer
- t.datetime :content_datetime
+ t.text :content
t.timestamps
end
- add_index :cms_blocks, [:cms_page_id, :type, :label]
+ add_index :cms_blocks, [:cms_page_id, :label]
# -- Snippets -----------------------------------------------------------
create_table :cms_snippets do |t|
comfortable_mexican_sofa/cms_tag.rb b/lib/comfortable_mexican_sofa/cms_tag.rb +2 -1
@@ @@ -9,7 +9,8 @@ module CmsTag
TOKENIZER_REGEX = /(\{\{\s*cms:[^{}]*\}\})|((?:\{?[^{])+|\{+)/
attr_accessor :params,
- :parent
+ :parent,
+ :record_id
module ClassMethods
# Regex that is used to match tags in the content
comfortable_mexican_sofa/cms_tag/field_datetime.rb b/lib/comfortable_mexican_sofa/cms_tag/field_datetime.rb +2 -2
@@ @@ -12,11 +12,11 @@ class CmsTag::FieldDateTime < CmsBlock
end
def content=(value)
- write_attribute(:content_datetime, value)
+ write_attribute(:content, value)
end
def content
- read_attribute(:content_datetime)
+ read_attribute(:content)
end
def render
comfortable_mexican_sofa/cms_tag/field_integer.rb b/lib/comfortable_mexican_sofa/cms_tag/field_integer.rb +2 -2
@@ @@ -12,11 +12,11 @@ class CmsTag::FieldInteger < CmsBlock
end
def content=(value)
- write_attribute(:content_integer, value)
+ write_attribute(:content, value)
end
def content
- read_attribute(:content_integer)
+ read_attribute(:content)
end
def render
comfortable_mexican_sofa/cms_tag/field_string.rb b/lib/comfortable_mexican_sofa/cms_tag/field_string.rb +2 -2
@@ @@ -12,11 +12,11 @@ class CmsTag::FieldString < CmsBlock
end
def content=(value)
- write_attribute(:content_string, value)
+ write_attribute(:content, value)
end
def content
- read_attribute(:content_string)
+ read_attribute(:content)
end
def render
comfortable_mexican_sofa/cms_tag/field_text.rb b/lib/comfortable_mexican_sofa/cms_tag/field_text.rb +2 -2
@@ @@ -12,11 +12,11 @@ class CmsTag::FieldText < CmsBlock
end
def content=(value)
- write_attribute(:content_text, value)
+ write_attribute(:content, value)
end
def content
- read_attribute(:content_text)
+ read_attribute(:content)
end
def render
comfortable_mexican_sofa/cms_tag/page_datetime.rb b/lib/comfortable_mexican_sofa/cms_tag/page_datetime.rb +2 -2
@@ @@ -12,11 +12,11 @@ class CmsTag::PageDateTime < CmsBlock
end
def content=(value)
- write_attribute(:content_datetime, value)
+ write_attribute(:content, value)
end
def content
- read_attribute(:content_datetime)
+ read_attribute(:content)
end
end
\ No newline at end of file
comfortable_mexican_sofa/cms_tag/page_integer.rb b/lib/comfortable_mexican_sofa/cms_tag/page_integer.rb +2 -2
@@ @@ -12,11 +12,11 @@ class CmsTag::PageInteger < CmsBlock
end
def content=(value)
- write_attribute(:content_integer, value)
+ write_attribute(:content, value)
end
def content
- read_attribute(:content_integer)
+ read_attribute(:content)
end
end
\ No newline at end of file
comfortable_mexican_sofa/cms_tag/page_string.rb b/lib/comfortable_mexican_sofa/cms_tag/page_string.rb +2 -2
@@ @@ -12,11 +12,11 @@ class CmsTag::PageString < CmsBlock
end
def content=(value)
- write_attribute(:content_string, value)
+ write_attribute(:content, value)
end
def content
- read_attribute(:content_string)
+ read_attribute(:content)
end
end
\ No newline at end of file
comfortable_mexican_sofa/cms_tag/page_text.rb b/lib/comfortable_mexican_sofa/cms_tag/page_text.rb +2 -2
@@ @@ -12,11 +12,11 @@ class CmsTag::PageText < CmsBlock
end
def content=(value)
- write_attribute(:content_text, value)
+ write_attribute(:content, value)
end
def content
- read_attribute(:content_text)
+ read_attribute(:content)
end
end
\ No newline at end of file
comfortable_mexican_sofa/cms_tag/snippet.rb b/lib/comfortable_mexican_sofa/cms_tag/snippet.rb +0 -2
@@ @@ -1,7 +1,5 @@
class CmsTag::Snippet < CmsSnippet
- attr_accessor :label
-
include CmsTag
def identifier
comfortable_mexican_sofa/form_builder.rb b/lib/comfortable_mexican_sofa/form_builder.rb +2 -3
@@ @@ -54,9 +54,8 @@ class ComfortableMexicanSofa::FormBuilder < ActionView::Helpers::FormBuilder
<div class='label'>#{label}</div>
<div class='value'>
#{field}
- #{@template.hidden_field_tag('cms_page[cms_blocks_attributes][][label]', tag.label)}
- #{@template.hidden_field_tag('cms_page[cms_blocks_attributes][][type]', tag.type)}
- #{@template.hidden_field_tag('cms_page[cms_blocks_attributes][][id]', tag.id) unless tag.new_record?}
+ #{@template.hidden_field_tag('cms_page[cms_blocks_attributes][][label]', tag.label, :id => nil)}
+ #{@template.hidden_field_tag('cms_page[cms_blocks_attributes][][id]', tag.record_id, :id => nil) if tag.record_id}
</div>
</div>
).html_safe
test/cms_seeds/test.host/pages/child.yml +0 -1
@@ @@ -6,6 +6,5 @@ slug: child
cms_blocks_attributes:
-
label: content
- type: CmsTag::PageText
content: |-
Child Page Content
\ No newline at end of file
test/cms_seeds/test.host/pages/child/subchild.yml +0 -1
@@ @@ -6,6 +6,5 @@ slug: subchild
cms_blocks_attributes:
-
label: content
- type: CmsTag::PageText
content: |-
Sub Child Page Content {{cms:snippet:default}}
\ No newline at end of file
test/cms_seeds/test.host/pages/index.yml +0 -1
@@ @@ -6,6 +6,5 @@ slug:
cms_blocks_attributes:
-
label: content
- type: CmsTag::PageText
content: |-
Default Page Content
\ No newline at end of file
test/fixtures/cms_blocks.yml +2 -4
@@ @@ -1,14 +1,12 @@
default_field_text:
cms_page: default
- type: CmsTag::FieldText
label: default_field_text
- content_text: default_field_text_content
+ content: default_field_text_content
default_page_text:
cms_page: default
- type: CmsTag::PageText
label: default_page_text
- content_text: |-
+ content: |-
default_page_text_content_a
{{cms:snippet:default}}
default_page_text_content_b
test/functional/cms_admin/pages_controller_test.rb +4 -18
@@ @@ -31,7 +31,6 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase
get :new
assert_select "input[type='datetime'][name='cms_page[cms_blocks_attributes][][content]']"
assert_select "input[type='hidden'][name='cms_page[cms_blocks_attributes][][label]'][value='test_label']"
- assert_select "input[type='hidden'][name='cms_page[cms_blocks_attributes][][type]'][value='CmsTag::FieldDateTime']"
end
def test_get_new_with_field_integer
@@ @@ -39,7 +38,6 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase
get :new
assert_select "input[type='number'][name='cms_page[cms_blocks_attributes][][content]']"
assert_select "input[type='hidden'][name='cms_page[cms_blocks_attributes][][label]'][value='test_label']"
- assert_select "input[type='hidden'][name='cms_page[cms_blocks_attributes][][type]'][value='CmsTag::FieldInteger']"
end
def test_get_new_with_field_string
@@ @@ -47,7 +45,6 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase
get :new
assert_select "input[type='text'][name='cms_page[cms_blocks_attributes][][content]']"
assert_select "input[type='hidden'][name='cms_page[cms_blocks_attributes][][label]'][value='test_label']"
- assert_select "input[type='hidden'][name='cms_page[cms_blocks_attributes][][type]'][value='CmsTag::FieldString']"
end
def test_get_new_with_field_text
@@ @@ -55,7 +52,6 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase
get :new
assert_select "textarea[name='cms_page[cms_blocks_attributes][][content]']"
assert_select "input[type='hidden'][name='cms_page[cms_blocks_attributes][][label]'][value='test_label']"
- assert_select "input[type='hidden'][name='cms_page[cms_blocks_attributes][][type]'][value='CmsTag::FieldText']"
end
def test_get_new_with_page_datetime
@@ @@ -63,7 +59,6 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase
get :new
assert_select "input[type='datetime'][name='cms_page[cms_blocks_attributes][][content]']"
assert_select "input[type='hidden'][name='cms_page[cms_blocks_attributes][][label]'][value='test_label']"
- assert_select "input[type='hidden'][name='cms_page[cms_blocks_attributes][][type]'][value='CmsTag::PageDateTime']"
end
def test_get_new_with_page_integer
@@ @@ -71,7 +66,6 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase
get :new
assert_select "input[type='number'][name='cms_page[cms_blocks_attributes][][content]']"
assert_select "input[type='hidden'][name='cms_page[cms_blocks_attributes][][label]'][value='test_label']"
- assert_select "input[type='hidden'][name='cms_page[cms_blocks_attributes][][type]'][value='CmsTag::PageInteger']"
end
def test_get_new_with_page_string
@@ @@ -79,7 +73,6 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase
get :new
assert_select "input[type='text'][name='cms_page[cms_blocks_attributes][][content]']"
assert_select "input[type='hidden'][name='cms_page[cms_blocks_attributes][][label]'][value='test_label']"
- assert_select "input[type='hidden'][name='cms_page[cms_blocks_attributes][][type]'][value='CmsTag::PageString']"
end
def test_get_new_with_page_text
@@ @@ -87,7 +80,6 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase
get :new
assert_select "textarea[name='cms_page[cms_blocks_attributes][][content]']"
assert_select "input[type='hidden'][name='cms_page[cms_blocks_attributes][][label]'][value='test_label']"
- assert_select "input[type='hidden'][name='cms_page[cms_blocks_attributes][][type]'][value='CmsTag::PageText']"
end
def test_get_new_as_child_page
@@ @@ -105,6 +97,8 @@ 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[cms_blocks_attributes][][id]'][value='#{cms_blocks(:default_field_text).id}']"
+ assert_select "input[name='cms_page[cms_blocks_attributes][][id]'][value='#{cms_blocks(:default_field_text).id}']"
end
def test_get_edit_failure
@@ @@ -133,13 +127,10 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase
: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' }
]
}
@@ @@ -158,20 +149,17 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase
: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
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_equal ['content content', 'title content', '999'], page.cms_blocks.collect{|b| b.content}
assert_template :new
assert_equal 'Failed to create page', flash[:error]
end
@@ @@ -199,11 +187,9 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase
:cms_layout_id => cms_layouts(:nested).id,
:cms_blocks_attributes => [
{ :label => 'content',
- :type => 'CmsTag::PageText',
:content => 'new_page_text_content',
:id => cms_blocks(:default_page_text).id },
{ :label => 'header',
- :type => 'CmsTag::PageString',
:content => 'new_page_string_content' }
]
}
@@ @@ -212,7 +198,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 ['default_field_text_content', 'new_page_string_content', 'new_page_text_content'], page.cms_blocks.collect{|b| b.content}
+ assert_equal ['new_page_text_content', 'default_field_text_content', 'new_page_string_content'], page.cms_blocks.collect{|b| b.content}
end
end
test/unit/cms_block_test.rb +8 -26
@@ @@ -8,19 +8,7 @@ class CmsBlockTest < ActiveSupport::TestCase
end
end
- def test_new_with_cast
- block = CmsBlock.new(:label => 'test_block', :content => 'test_content', :type => 'CmsTag::PageText')
- assert_equal 'CmsTag::PageText', block.class.name
- assert_equal 'test_block', block.label
- assert_equal 'test_content', block.content
-
- assert_difference 'CmsBlock.count' do
- block.cms_page = cms_pages(:default)
- block.save!
- end
- end
-
- def test_new_with_cast_via_page_nested_attributes
+ def test_new_via_page_nested_attributes
assert_difference ['CmsPage.count', 'CmsBlock.count'] do
page = CmsPage.create!(
:cms_site => cms_sites(:default),
@@ @@ -31,31 +19,25 @@ class CmsBlockTest < ActiveSupport::TestCase
:cms_blocks_attributes => [
{
:label => 'test_block',
- :content => 'test_content',
- :type => 'CmsTag::PageText'
+ :content => 'test_content'
}
]
)
assert_equal 1, page.cms_blocks.count
block = page.cms_blocks.first
- assert_equal 'CmsTag::PageText', block.class.name
assert_equal 'test_block', block.label
assert_equal 'test_content', block.content
end
end
def test_initialize_or_find
- block = CmsBlock.initialize_or_find(cms_pages(:default), :default_field_text)
- assert !block.new_record?
- assert_equal 'default_field_text', block.label
- assert_equal 'CmsTag::FieldText', block.class.name
- assert_equal 'default_field_text_content', block.content
+ tag = CmsTag::PageText.initialize_or_find(cms_pages(:default), :default_field_text)
+ assert_equal 'default_field_text', tag.label
+ assert_equal 'default_field_text_content', tag.content
- block = CmsTag::PageText.initialize_or_find(cms_pages(:default), :new_block)
- assert block.new_record?
- assert_equal 'new_block', block.label
- assert_equal 'CmsTag::PageText', block.class.name
- assert block.content.blank?
+ tag = CmsTag::PageText.initialize_or_find(cms_pages(:default), :new_block)
+ assert_equal 'new_block', tag.label
+ assert tag.content.blank?
end
end
test/unit/cms_page_test.rb +0 -1
@@ @@ -184,7 +184,6 @@ class CmsPageTest < ActiveSupport::TestCase
def test_cms_blocks_attributes_accessor
page = cms_pages(:default)
assert_equal page.cms_blocks.count, page.cms_blocks_attributes.size
- assert_equal 'CmsTag::FieldText', page.cms_blocks_attributes.first[:type]
assert_equal 'default_field_text', page.cms_blocks_attributes.first[:label]
assert_equal 'default_field_text_content', page.cms_blocks_attributes.first[:content]
assert page.cms_blocks_attributes.first[:id]
test/unit/cms_tag_test.rb +0 -25
@@ @@ -203,29 +203,4 @@ class CmsTagTest < ActiveSupport::TestCase
assert_equal 6, page.cms_tags.size
end
- def test_tag_initialization_for_existing_blocks_with_different_type
- layout = cms_layouts(:default)
- page = cms_pages(:default)
-
- assert page.content
- assert_equal 4, page.cms_tags.size
- assert tag = page.cms_tags.first
- assert !tag.new_record?
- assert_equal 'CmsTag::FieldText', tag.class.name
- assert_equal 'CmsTag::FieldText', tag.type
- assert_equal 'default_field_text', tag.label
- assert_equal 'default_field_text_content', tag.content
-
- layout.update_attribute(:content, '{{cms:page:default_field_text:string}}')
- page.reload
- assert page.content
- assert_equal 1, page.cms_tags.size
- assert tag = page.cms_tags.first
- assert !tag.new_record?
- assert_equal 'CmsTag::FieldText', tag.class.name
- assert_equal 'CmsTag::PageString', tag.type
- assert_equal 'default_field_text', tag.label
- assert_equal 'default_field_text_content', tag.content
- end
-
end
test/unit/cms_tags/field_datetime_test.rb +0 -1
@@ @@ -29,7 +29,6 @@ class FieldDateTimeTest < ActiveSupport::TestCase
time = 2.days.ago
tag.content = time
assert_equal time, tag.content
- assert_equal time, tag.read_attribute(:content_datetime)
assert_equal '', tag.render
end
test/unit/cms_tags/field_integer_test.rb +1 -2
@@ @@ -27,8 +27,7 @@ class FieldIntegerTest < ActiveSupport::TestCase
tag = CmsTag::FieldInteger.initialize_tag(cms_pages(:default), '{{cms:field:content:integer}}')
assert tag.content.blank?
tag.content = '5'
- assert_equal 5, tag.content
- assert_equal 5, tag.read_attribute(:content_integer)
+ assert_equal '5', tag.content
assert_equal '', tag.render
end
test/unit/cms_tags/field_string_test.rb +0 -1
@@ @@ -28,7 +28,6 @@ class FieldStringTest < ActiveSupport::TestCase
assert tag.content.blank?
tag.content = 'test_content'
assert_equal 'test_content', tag.content
- assert_equal 'test_content', tag.read_attribute(:content_string)
assert_equal '', tag.render
end
test/unit/cms_tags/field_text_test.rb +0 -1
@@ @@ -27,7 +27,6 @@ class FieldTextTest < ActiveSupport::TestCase
assert tag.content.blank?
tag.content = 'test_content'
assert_equal 'test_content', tag.content
- assert_equal 'test_content', tag.read_attribute(:content_text)
assert_equal '', tag.render
end
test/unit/cms_tags/page_datetime_test.rb +0 -1
@@ @@ -29,7 +29,6 @@ class PageDateTimeTest < ActiveSupport::TestCase
time = 2.days.ago
tag.content = time
assert_equal time, tag.content
- assert_equal time, tag.read_attribute(:content_datetime)
assert_equal time.to_s, tag.render
end
test/unit/cms_tags/page_integer_test.rb +1 -2
@@ @@ -27,8 +27,7 @@ class PageIntegerTest < ActiveSupport::TestCase
tag = CmsTag::PageInteger.initialize_tag(cms_pages(:default), '{{cms:page:content:integer}}')
assert tag.content.blank?
tag.content = '5'
- assert_equal 5, tag.content
- assert_equal 5, tag.read_attribute(:content_integer)
+ assert_equal '5', tag.content
assert_equal '5', tag.render
end
test/unit/cms_tags/page_string_test.rb +0 -1
@@ @@ -28,7 +28,6 @@ class PageStringTest < ActiveSupport::TestCase
assert tag.content.blank?
tag.content = 'test_content'
assert_equal 'test_content', tag.content
- assert_equal 'test_content', tag.read_attribute(:content_string)
assert_equal 'test_content', tag.render
end
test/unit/cms_tags/page_text_test.rb +0 -1
@@ @@ -28,7 +28,6 @@ class PageTextTest < ActiveSupport::TestCase
assert tag.content.blank?
tag.content = 'test_content'
assert_equal 'test_content', tag.content
- assert_equal 'test_content', tag.read_attribute(:content_text)
assert_equal 'test_content', tag.render
end