starting on revisions

Oleg committed May 06, 2011
commit 58ada040675170195c56ceeee1d6aba1f1d43da9
Showing 9 changed files with 97 additions and 0 deletions
app/models/cms/layout.rb +2 -0
@@ @@ -4,6 +4,8 @@ class Cms::Layout < ActiveRecord::Base
acts_as_tree
+ has_revisions_for :content, :css, :js
+
# -- Relationships --------------------------------------------------------
belongs_to :site
has_many :pages, :dependent => :nullify
app/models/cms/page.rb +5 -0
@@ @@ -5,6 +5,11 @@ class Cms::Page < ActiveRecord::Base
# -- AR Extensions --------------------------------------------------------
acts_as_tree :counter_cache => :children_count
+ has_revisions_for :blocks_attributes
+
+ # ------ blocks_attributes_changed?
+ # ------ blocks_attributes_was
+
attr_accessor :tags
# -- Relationships --------------------------------------------------------
app/models/cms/revision.rb +10 -0
@@ @@ -0,0 +1,10 @@
+ class Cms::Revision < ActiveRecord::Base
+
+ set_table_name :cms_revisions
+
+ serialize :data
+
+ # -- Relationships --------------------------------------------------------
+ belongs_to :record, :polymorphic => true
+
+ end
\ No newline at end of file
app/models/cms/snippet.rb +2 -0
@@ @@ -2,6 +2,8 @@ class Cms::Snippet < ActiveRecord::Base
set_table_name :cms_snippets
+ has_revisions_for :content
+
# -- Relationships --------------------------------------------------------
belongs_to :site
migrate/01_create_cms.rb b/db/migrate/01_create_cms.rb +11 -0
@@ @@ -70,6 +70,16 @@ class CreateCms < ActiveRecord::Migration
t.timestamps
end
add_index :cms_uploads, [:site_id, :file_file_name]
+
+ # -- Revisions -----------------------------------------------------------
+ create_table :cms_revisions, :force => true do |t|
+ t.string :record_type
+ t.integer :record_id
+ t.text :data
+ t.datetime :created_at
+ end
+ add_index :cms_revisions, [:record_type, :record_id, :created_at]
+
end
def self.down
@@ @@ -79,5 +89,6 @@ class CreateCms < ActiveRecord::Migration
drop_table :cms_snippets
drop_table :cms_blocks
drop_table :cms_uploads
+ drop_table :cms_revisions
end
end
migrate/upgrades/03_upgrade_to_1_2_0.rb b/db/migrate/upgrades/03_upgrade_to_1_2_0.rb +15 -0
@@ @@ -0,0 +1,15 @@
+ class UpgradeTo120 < ActiveRecord::Migration
+ def self.up
+ create_table :cms_revisions, :force => true do |t|
+ t.string :record_type
+ t.integer :record_id
+ t.text :data
+ t.datetime :created_at
+ end
+ add_index :cms_revisions, [:record_type, :record_id, :created_at]
+ end
+
+ def self.down
+ drop_table :cms_revisions
+ end
+ end
\ No newline at end of file
comfortable_mexican_sofa.rb b/lib/comfortable_mexican_sofa.rb +1 -0
@@ @@ -12,6 +12,7 @@ end
'comfortable_mexican_sofa/view_methods',
'comfortable_mexican_sofa/form_builder',
'comfortable_mexican_sofa/acts_as_tree',
+ 'comfortable_mexican_sofa/has_revisions',
'comfortable_mexican_sofa/tag',
'comfortable_mexican_sofa/fixtures'
].each do |path|
comfortable_mexican_sofa/has_revisions.rb b/lib/comfortable_mexican_sofa/has_revisions.rb +34 -0
@@ @@ -0,0 +1,34 @@
+ module ComfortableMexicanSofa::HasRevisions
+
+ def self.included(base)
+ base.send :extend, ClassMethods
+ base.send :include, InstanceMethods
+ end
+
+ module ClassMethods
+
+ def has_revisions_for(*fields)
+
+ has_many :revisions,
+ :as => :record,
+ :dependent => :destroy
+
+ before_save :create_revision
+
+ define_method(:revision_fields) do
+ fields
+ end
+ end
+ end
+
+ module InstanceMethods
+
+ def create_revision
+ # ...
+ end
+
+ end
+
+ end
+
+ ActiveRecord::Base.send :include, ComfortableMexicanSofa::HasRevisions
\ No newline at end of file
test/unit/revisions_test.rb +17 -0
@@ @@ -0,0 +1,17 @@
+ require File.expand_path('../test_helper', File.dirname(__FILE__))
+
+ class RevisionsTest < ActiveSupport::TestCase
+
+ def test_init_for_layouts
+ assert_equal [:content, :css, :js], cms_layouts(:default).revision_fields
+ end
+
+ def test_init_for_pages
+ assert_equal [:blocks_attributes], cms_pages(:default).revision_fields
+ end
+
+ def test_init_for_snippets
+ assert_equal [:content], cms_snippets(:default).revision_fields
+ end
+
+ end
\ No newline at end of file