introducing limits to revisions
Oleg
committed May 09, 2011
commit ff810528bcd810a4939d9066d3bdd4fd96cd0cfd
Showing 6
changed files with
49 additions
and 1 deletions
config/initializers/comfortable_mexican_sofa.rb
+5
-0
| @@ | @@ -47,6 +47,11 @@ ComfortableMexicanSofa.configure do |config| |
| # Path where fixtures can be located. | |
| # config.fixtures_path = File.expand_path('db/cms_fixtures', Rails.root) | |
| + | # Content for Layouts, Pages and Snippets has a revision history. You can revert |
| + | # a previous version using this system. You can control how many revisions per |
| + | # object you want to keep. Set it to 0 if you wish to turn this feature off. |
| + | # config.revisions_limit = 25 |
| + | |
| end | |
| # Default credentials for ComfortableMexicanSofa::HttpAuth | |
comfortable_mexican_sofa/configuration.rb b/lib/comfortable_mexican_sofa/configuration.rb
+4
-0
| @@ | @@ -37,6 +37,9 @@ class ComfortableMexicanSofa::Configuration |
| # Path where fixtures can be located. | |
| attr_accessor :fixtures_path | |
| + | # Number of revisions kept. Default is 25. If you wish to disable: set this to 0. |
| + | attr_accessor :revisions_limit |
| + | |
| # Configuration defaults | |
| def initialize | |
| @cms_title = 'ComfortableMexicanSofa MicroCMS' | |
| @@ | @@ -51,6 +54,7 @@ class ComfortableMexicanSofa::Configuration |
| @upload_file_options = {} | |
| @enable_fixtures = false | |
| @fixtures_path = File.expand_path('db/cms_fixtures', Rails.root) | |
| + | @revisions_limit = 25 |
| end | |
| end | |
comfortable_mexican_sofa/has_revisions.rb b/lib/comfortable_mexican_sofa/has_revisions.rb
+10
-1
| @@ | @@ -41,7 +41,16 @@ module ComfortableMexicanSofa::HasRevisions |
| # Revision is created only if relevant data changed | |
| def create_revision | |
| return unless self.revision_data | |
| - | self.revisions.create!(:data => self.revision_data) |
| + | |
| + | # creating revision |
| + | if ComfortableMexicanSofa.config.revisions_limit.to_i != 0 |
| + | self.revisions.create!(:data => self.revision_data) |
| + | end |
| + | |
| + | # blowing away old revisions |
| + | ids = [0] + self.revisions.order('created_at DESC'). |
| + | limit(ComfortableMexicanSofa.config.revisions_limit.to_i).collect(&:id) |
| + | self.revisions.where('id NOT IN (?)', ids).destroy_all |
| end | |
| # Assigning whatever is found in revision data and attemptint to save the object | |
test/test_helper.rb
+1
-0
| @@ | @@ -24,6 +24,7 @@ class ActiveSupport::TestCase |
| config.enable_caching = true | |
| config.enable_fixtures = false | |
| config.fixtures_path = File.expand_path('db/cms_fixtures', Rails.root) | |
| + | config.revisions_limit = 25 |
| end | |
| ComfortableMexicanSofa::HttpAuth.username = 'username' | |
| ComfortableMexicanSofa::HttpAuth.password = 'password' | |
test/unit/configuration_test.rb
+1
-0
| @@ | @@ -14,6 +14,7 @@ class ConfigurationTest < ActiveSupport::TestCase |
| assert_equal true, config.enable_caching | |
| assert_equal false, config.enable_fixtures | |
| assert_equal File.expand_path('db/cms_fixtures', Rails.root), config.fixtures_path | |
| + | assert_equal 25, config.revisions_limit |
| end | |
| def test_initialization_overrides | |
test/unit/revisions_test.rb
+28
-0
| @@ | @@ -141,4 +141,32 @@ class RevisionsTest < ActiveSupport::TestCase |
| end | |
| end | |
| + | def test_restore_from_revision_with_wrong_revision_type |
| + | snippet = cms_snippets(:default) |
| + | revision = cms_revisions(:layout) |
| + | |
| + | assert_no_difference 'snippet.revisions.count' do |
| + | snippet.restore_from_revision(revision) |
| + | snippet.reload |
| + | assert_equal 'default_snippet_content', snippet.content |
| + | end |
| + | end |
| + | |
| + | def test_creation_with_limit |
| + | ComfortableMexicanSofa.config.revisions_limit = 1 |
| + | snippet = cms_snippets(:default) |
| + | revision = cms_revisions(:snippet) |
| + | |
| + | assert_equal 1, snippet.revisions.count |
| + | |
| + | assert_no_difference 'snippet.revisions.count' do |
| + | snippet.update_attribute(:content, 'new content') |
| + | assert_nil Cms::Revision.find_by_id(revision.id) |
| + | |
| + | snippet.reload |
| + | revision = snippet.revisions.first |
| + | assert_equal ({ 'content' => 'default_snippet_content' }), revision.data |
| + | end |
| + | end |
| + | |
| end | |
| \ No newline at end of file | |