revisions can be created now
Oleg
committed May 06, 2011
commit 2ee26356abdda12a970002c8faa355901294cd35
Showing 3
changed files with
21 additions
and 14 deletions
app/models/cms/page.rb
+12
-9
| @@ | @@ -2,15 +2,12 @@ class Cms::Page < ActiveRecord::Base |
| set_table_name :cms_pages | |
| - | # -- AR Extensions -------------------------------------------------------- |
| acts_as_tree :counter_cache => :children_count | |
| has_revisions_for :blocks_attributes | |
| - | # ------ blocks_attributes_changed? |
| - | # ------ blocks_attributes_was |
| - | |
| - | attr_accessor :tags |
| + | attr_accessor :tags, |
| + | :blocks_attributes_changed |
| # -- Relationships -------------------------------------------------------- | |
| belongs_to :site | |
| @@ | @@ -69,12 +66,12 @@ class Cms::Page < ActiveRecord::Base |
| # Transforms existing cms_block information into a hash that can be used | |
| # during form processing. That's the only way to modify cms_blocks. | |
| - | def blocks_attributes |
| - | self.blocks.inject([]) do |arr, block| |
| + | def blocks_attributes(was = false) |
| + | self.blocks.collect do |block| |
| block_attr = {} | |
| block_attr[:label] = block.label | |
| - | block_attr[:content] = block.content |
| - | arr << block_attr |
| + | block_attr[:content] = was ? block.content_was : block.content |
| + | block_attr |
| end | |
| end | |
| @@ | @@ -88,6 +85,7 @@ class Cms::Page < ActiveRecord::Base |
| 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] | |
| + | self.blocks_attributes_changed = self.blocks_attributes_changed || block.content_changed? |
| end | |
| end | |
| @@ | @@ -114,6 +112,11 @@ class Cms::Page < ActiveRecord::Base |
| "http://#{self.site.hostname}#{self.full_path}" | |
| end | |
| + | # Method to collect prevous state of blocks for revisions |
| + | def blocks_attributes_was |
| + | blocks_attributes(true) |
| + | end |
| + | |
| protected | |
| def assign_parent | |
comfortable_mexican_sofa/has_revisions.rb b/lib/comfortable_mexican_sofa/has_revisions.rb
+2
-3
| @@ | @@ -27,7 +27,8 @@ module ComfortableMexicanSofa::HasRevisions |
| module InstanceMethods | |
| def prepare_revision | |
| - | if !(self.changed & revision_fields).empty? |
| + | if (self.respond_to?(:blocks_attributes_changed) && self.blocks_attributes_changed) || |
| + | !(self.changed & revision_fields).empty? |
| self.revision_data = revision_fields.inject({}) do |c, field| | |
| c[field] = self.send("#{field}_was") | |
| c | |
| @@ | @@ -39,9 +40,7 @@ module ComfortableMexicanSofa::HasRevisions |
| return unless self.revision_data | |
| self.revisions.create!(:data => self.revision_data) | |
| end | |
| - | |
| end | |
| - | |
| end | |
| ActiveRecord::Base.send :include, ComfortableMexicanSofa::HasRevisions | |
| \ No newline at end of file | |
test/unit/revisions_test.rb
+7
-2
| @@ | @@ -55,7 +55,6 @@ class RevisionsTest < ActiveSupport::TestCase |
| def test_creation_for_page | |
| page = cms_pages(:default) | |
| - | old_attributes = page.attributes.slice('blocks_attributes') |
| assert_difference 'page.revisions.count' do | |
| page.update_attributes!( | |
| @@ | @@ -67,7 +66,13 @@ class RevisionsTest < ActiveSupport::TestCase |
| page.reload | |
| assert_equal 2, page.revisions.count | |
| revision = page.revisions.last | |
| - | assert_equal old_attributes, revision.data |
| + | assert_equal ({ |
| + | 'blocks_attributes' => [ |
| + | { :label => 'default_field_text', |
| + | :content => 'default_field_text_content' }, |
| + | { :label => 'default_page_text', |
| + | :content => "default_page_text_content_a\n{{cms:snippet:default}}\ndefault_page_text_content_b" }] |
| + | }), revision.data |
| end | |
| end | |