Skip to content

Commit

Permalink
The without_versioning method was replaced by with_versioning( bool ).
Browse files Browse the repository at this point in the history
git-svn-id: http://rubymatt.rubyforge.org/svn/simply_versioned@10 9890f43a-5319-0410-996d-de745e0c2bc8
  • Loading branch information
mmower committed Jan 4, 2008
1 parent a8d4922 commit f6dd530
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
25 changes: 19 additions & 6 deletions README
@@ -1,8 +1,8 @@
SimplyVersioned
===============

Release: 0.6
Date: 01-01-2008
Release: 0.7
Date: 04-01-2008
Author: Matt Mower <self@mattmower.com>

SimplyVersioned is a simple, non-invasive, approach to versioning ActiveRecord models.
Expand All @@ -26,6 +26,10 @@ Usage

./script/generate simply_versioned_migration

Note that the migration defaults to storing the version info in a TEXT field. On MySQL this will default to a
limit of 64K. If you are versioning particularly large models you will want to modify the migration to include
a :limit => n condition to promote the yaml column to a MEDIUMTEXT or (god forbid) a LONGTEXT.

3. Create the versions table

rake db:migrate
Expand All @@ -48,12 +52,16 @@ Usage
thing = Thing.create!( :foo => bar ) # creates v1
thing.foo = baz
thing.save! # creates v2

To save without creating a version use:

thing.without_versioning do
thing.save
If you need to control whether a version is created or not, use #with_versioning. For example:

thing.with_versioning( false ) do |t|
t.save!
end

or, using the "magic pen" (http://dablog.rubypal.com/2007/2/18/the-magic-pens-of-ruby thanks hmj):

thing.with_versioning( false, &:save! )

6. Find versions

Expand All @@ -77,4 +85,9 @@ Usage

thing.versions.first.model # => Instantiated Thing with versioned values

Thanks to:

Josh Susser (http://blog.hasmanythrough.com/) for useful suggestions and feedback
Rick Olson (http://techno-weenie.net/) for all the many plugins whose code i've read

Copyright (c) 2007 Matt Mower <self@mattmower.com> and released under the MIT license
14 changes: 7 additions & 7 deletions lib/simply_versioned.rb
@@ -1,4 +1,4 @@
# SimplyVersioned 0.6
# SimplyVersioned 0.7
#
# Simple ActiveRecord versioning
# Copyright (c) 2007,2008 Matt Mower <self@mattmower.com>
Expand Down Expand Up @@ -79,14 +79,14 @@ def revert_to_version( version, options = {} )
end
end

# Invoke the block for this record with versioning disabled. Calls to save
# during the block will not result in the creation of a new version.
#
def without_versioning( &block )
# Invoke the supplied block passing the receiver as the sole block argument with
# versioning enabled or disabled depending upon the value of the +enabled+ parameter
# for the duration of the block.
def with_versioning( enabled, &block )
versioning_was_enabled = self.versioning_enabled?
self.versioning_enabled = false
self.versioning_enabled = enabled
begin
yield self
block.call( self )
ensure
self.versioning_enabled = versioning_was_enabled
end
Expand Down
2 changes: 1 addition & 1 deletion lib/version.rb
@@ -1,4 +1,4 @@
# SimplyVersioned 0.6
# SimplyVersioned 0.7
#
# Simple ActiveRecord versioning
# Copyright (c) 2007,2008 Matt Mower <self@mattmower.com>
Expand Down
16 changes: 15 additions & 1 deletion test/simply_versioned_test.rb
Expand Up @@ -139,12 +139,26 @@ def test_should_isolate_versioned_models
assert_equal 1, gary.versions.first.number
end

def test_should_be_able_to_control_versioning
anthony = Aardvark.create!( :name => 'Anthony', :age => 35 ) # v1

assert_no_difference( 'anthony.versions.count' ) do
anthony.age += 1
anthony.with_versioning( false, &:save! )
end

assert_difference( 'anthony.versions.count' ) do
anthony.age += 1
anthony.with_versioning( true, &:save! )
end
end

def test_should_not_version_in_block
anthony = Aardvark.create!( :name => 'Anthony', :age => 35 ) # v1

assert_no_difference( 'anthony.versions.count' ) do
anthony.age += 1
anthony.without_versioning do
anthony.with_versioning( false ) do
anthony.save!
end
end
Expand Down

0 comments on commit f6dd530

Please sign in to comment.