a bit of a rework for tag inititialization, some tests are failing
Oleg
committed Sep 03, 2010
commit e45188f8ebb53e711b157f7edfa9940155f89fb5
Showing 7
changed files with
39 additions
and 22 deletions
app/helpers/cms_helper.rb
+10
-9
| @@ | @@ -9,11 +9,11 @@ module CmsHelper |
| # on tag type. Example: | |
| # tag.type = CmsTag::MyAwesomeTag | |
| # def my_awesome_tag ... end | |
| - | def cms_block_field(block, *args) |
| - | block_method = block.type.split('::').last.underscore.downcase |
| - | return send(block_method, block) if self.respond_to? block_method |
| + | def cms_tag_field(tag, *args) |
| + | tag_method = tag.type.split('::').last.underscore.downcase |
| + | return send(tag_method, block) if self.respond_to? tag_method |
| - | form_method = case block |
| + | form_method = case tag |
| when CmsTag::PageText | |
| :text_area_tag | |
| when CmsTag::PageString | |
| @@ | @@ -23,12 +23,13 @@ module CmsHelper |
| end | |
| %( | |
| - | <div class='form_element #{block_method}_element'> |
| - | <div class='label'>#{block.label.to_s.titleize}</div> |
| + | <div class='form_element #{tag_method}_element'> |
| + | <div class='label'>#{tag.label.to_s.titleize}</div> |
| <div class='value'> | |
| - | #{send(form_method, 'cms_page[cms_blocks_attributes][][content]', block.content)} |
| - | #{hidden_field_tag('cms_page[cms_blocks_attributes][][label]', block.label)} |
| - | #{hidden_field_tag('cms_page[cms_blocks_attributes][][type]', block.type)} |
| + | #{send(form_method, 'cms_page[cms_blocks_attributes][][content]', tag.content)} |
| + | #{hidden_field_tag('cms_page[cms_blocks_attributes][][label]', tag.label)} |
| + | #{hidden_field_tag('cms_page[cms_blocks_attributes][][type]', tag.type)} |
| + | #{hidden_field_tag('cms_page[cms_blocks_attributes][][id]', tag.id) unless tag.new_record?} |
| </div> | |
| </div> | |
| ).html_safe | |
app/models/cms_page.rb
+15
-8
| @@ | @@ -2,7 +2,9 @@ class CmsPage < ActiveRecord::Base |
| # -- AR Extensions -------------------------------------------------------- | |
| acts_as_tree :counter_cache => :children_count | |
| - | |
| + | |
| + | attr_writer :cms_tags |
| + | |
| # -- Relationships -------------------------------------------------------- | |
| belongs_to :cms_layout | |
| has_many :cms_blocks, | |
| @@ | @@ -59,15 +61,26 @@ class CmsPage < ActiveRecord::Base |
| 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 | |
| + | # Accessor to get tags |
| + | def cms_tags |
| + | @cms_tags ||= self.initialize_tags |
| + | end |
| + | |
| protected | |
| + | # 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 |
| + | |
| def assign_full_path | |
| self.full_path = self.parent ? "#{self.parent.full_path}/#{self.slug}".squeeze('/') : '/' | |
| end | |
| @@ | @@ -77,10 +90,4 @@ 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 | |
app/views/cms_admin/pages/_form_blocks.html.erb
+2
-2
| @@ | @@ -1,5 +1,5 @@ |
| <div id='form_blocks'> | |
| - | <% @cms_page.cms_blocks.each do |block| %> |
| - | <%= cms_block_field(block) %> |
| + | <% @cms_page.cms_tags.each do |tag| %> |
| + | <%= cms_tag_field(tag) %> |
| <% end %> | |
| </div> | |
| \ No newline at end of file | |
app/views/cms_admin/pages/edit.html.erb
+3
-1
| @@ | @@ -3,4 +3,6 @@ |
| <%= cms_form_for @cms_page, :url => {:action => :update} do |form| %> | |
| <%= render :partial => 'form', :object => form %> | |
| <%= form.submit 'Update Page' %> | |
| - | <% end %> |
| \ No newline at end of file | |
| + | <% end %> |
| + | |
| + | <%= debug @cms_page.cms_blocks.collect{|b| [b.id, b.type, b.label, b.content]} %> |
| \ No newline at end of file | |
comfortable_mexican_sofa/cms_tag.rb b/lib/comfortable_mexican_sofa/cms_tag.rb
+3
-1
| @@ | @@ -25,6 +25,7 @@ module CmsTag |
| def initialize_tag_objects(cms_page = nil, content = '') | |
| content.to_s.scan(regex_tag_signature).flatten.collect do |label| | |
| if self.superclass == CmsBlock && cms_page | |
| + | cms_page.reload unless cms_page.new_record? |
| cms_page.cms_blocks.detect{|b| b.label == label} || self.new(:label => label) | |
| else | |
| self.new(:label => label) | |
| @@ | @@ -82,7 +83,8 @@ private |
| end.flatten.compact | |
| # Initializing cms_blocks for the passed cms_page | |
| - | cms_page.cms_blocks = cms_tags.select{|t| t.class.superclass == CmsBlock} if cms_page |
| + | cms_page.cms_tags = cms_tags.select{|t| t.class.superclass == CmsBlock} if cms_page |
| + | |
| return cms_tags | |
| end | |
test/functional/cms_admin/pages_controller_test.rb
+2
-1
| @@ | @@ -146,7 +146,8 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase |
| :cms_blocks_attributes => [ | |
| { :label => 'content', | |
| :type => 'CmsTag::PageText', | |
| - | :content => 'content content' }, |
| + | :content => 'content content', |
| + | :id => cms_blocks(:default_page_text).id }, |
| { :label => 'header', | |
| :type => 'CmsTag::PageText', | |
| :content => 'header content' } | |
test/unit/cms_page_test.rb
+4
-0
| @@ | @@ -74,15 +74,19 @@ class CmsPageTest < ActiveSupport::TestCase |
| def test_initialize_tags | |
| page = CmsPage.new | |
| + | page.initialize_tags |
| assert_equal 0, page.cms_blocks.size | |
| page.cms_layout = cms_layouts(:default) | |
| + | page.initialize_tags |
| assert_equal 3, page.cms_blocks.size | |
| page.cms_layout_id = '999999' | |
| + | page.initialize_tags |
| assert_equal 0, page.cms_blocks.size | |
| page.cms_layout_id = cms_layouts(:default).id | |
| + | page.initialize_tags |
| assert_equal 3, page.cms_blocks.size | |
| end | |