brianjlandau-vestal_versions 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +27 -0
- data/Gemfile +3 -0
- data/LICENSE +20 -0
- data/README.rdoc +196 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/generators/vestal_versions/templates/initializer.rb +9 -0
- data/generators/vestal_versions/templates/migration.rb +28 -0
- data/generators/vestal_versions/vestal_versions_generator.rb +10 -0
- data/init.rb +1 -0
- data/lib/vestal_versions.rb +104 -0
- data/lib/vestal_versions/associations.rb +67 -0
- data/lib/vestal_versions/changes.rb +125 -0
- data/lib/vestal_versions/conditions.rb +69 -0
- data/lib/vestal_versions/configuration.rb +40 -0
- data/lib/vestal_versions/control.rb +175 -0
- data/lib/vestal_versions/creation.rb +85 -0
- data/lib/vestal_versions/deletion.rb +46 -0
- data/lib/vestal_versions/options.rb +45 -0
- data/lib/vestal_versions/reload.rb +22 -0
- data/lib/vestal_versions/reset.rb +28 -0
- data/lib/vestal_versions/reversion.rb +92 -0
- data/lib/vestal_versions/tagging.rb +54 -0
- data/lib/vestal_versions/users.rb +56 -0
- data/lib/vestal_versions/version.rb +76 -0
- data/lib/vestal_versions/versioned.rb +30 -0
- data/lib/vestal_versions/versions.rb +74 -0
- data/test/associations_test.rb +49 -0
- data/test/changes_test.rb +169 -0
- data/test/conditions_test.rb +137 -0
- data/test/configuration_test.rb +39 -0
- data/test/control_test.rb +152 -0
- data/test/creation_test.rb +110 -0
- data/test/deletion_test.rb +121 -0
- data/test/options_test.rb +52 -0
- data/test/reload_test.rb +19 -0
- data/test/reset_test.rb +112 -0
- data/test/reversion_test.rb +99 -0
- data/test/schema.rb +62 -0
- data/test/tagging_test.rb +39 -0
- data/test/test_helper.rb +12 -0
- data/test/users_test.rb +25 -0
- data/test/version_test.rb +61 -0
- data/test/versioned_test.rb +18 -0
- data/test/versions_test.rb +172 -0
- data/vestal_versions.gemspec +124 -0
- metadata +245 -0
data/.gitignore
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
## MAC OS
|
2
|
+
.DS_Store
|
3
|
+
|
4
|
+
## TEXTMATE
|
5
|
+
*.tmproj
|
6
|
+
tmtags
|
7
|
+
|
8
|
+
## EMACS
|
9
|
+
*~
|
10
|
+
\#*
|
11
|
+
.\#*
|
12
|
+
|
13
|
+
## VIM
|
14
|
+
*.swp
|
15
|
+
|
16
|
+
## PROJECT::GENERAL
|
17
|
+
coverage
|
18
|
+
rdoc
|
19
|
+
pkg
|
20
|
+
|
21
|
+
## PROJECT::SPECIFIC
|
22
|
+
*.db
|
23
|
+
.rvmrc
|
24
|
+
|
25
|
+
## BUNDLER
|
26
|
+
.bundle
|
27
|
+
Gemfile.lock
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Steve Richert
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,196 @@
|
|
1
|
+
= vestal_versions for Rails 3
|
2
|
+
|
3
|
+
Finally, DRY ActiveRecord versioning!
|
4
|
+
|
5
|
+
<tt>acts_as_versioned</tt>[http://github.com/technoweenie/acts_as_versioned] by technoweenie[http://github.com/technoweenie] was a great start, but it failed to keep up with ActiveRecord's introduction of dirty objects in version 2.1. Additionally, each versioned model needs its own versions table that duplicates most of the original table's columns. The versions table is then populated with records that often duplicate most of the original record's attributes. All in all, not very DRY.
|
6
|
+
|
7
|
+
<tt>vestal_versions</tt>[http://github.com/laserlemon/vestal_versions] requires only one versions table (polymorphically associated with its parent models) and no changes whatsoever to existing tables. But it goes one step DRYer by storing a serialized hash of _only_ the models' changes. Think modern version control systems. By traversing the record of changes, the models can be reverted to any point in time.
|
8
|
+
|
9
|
+
And that's just what <tt>vestal_versions</tt> does. Not only can a model be reverted to a previous version number but also to a date or time!
|
10
|
+
|
11
|
+
== Compatibility
|
12
|
+
|
13
|
+
Tested with Active Record 3.0.0 with Ruby 1.8.7 and 1.9.2-preview3.
|
14
|
+
|
15
|
+
== Installation
|
16
|
+
|
17
|
+
In the Gemfile:
|
18
|
+
|
19
|
+
gem 'vestal_versions', :git => 'git://github.com/lailsonbm/vestal_versions', :branch => 'rails3'
|
20
|
+
|
21
|
+
Next, generate and run the first and last versioning migration you'll ever need:
|
22
|
+
|
23
|
+
$ rails generate vestal_versions
|
24
|
+
$ rake db:migrate
|
25
|
+
|
26
|
+
== Example
|
27
|
+
|
28
|
+
To version an ActiveRecord model, simply add <tt>versioned</tt> to your class like so:
|
29
|
+
|
30
|
+
class User < ActiveRecord::Base
|
31
|
+
versioned
|
32
|
+
|
33
|
+
validates_presence_of :first_name, :last_name
|
34
|
+
|
35
|
+
def name
|
36
|
+
"#{first_name} #{last_name}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
It's that easy! Now watch it in action...
|
41
|
+
|
42
|
+
>> u = User.create(:first_name => "Steve", :last_name => "Richert")
|
43
|
+
=> #<User first_name: "Steve", last_name: "Richert">
|
44
|
+
>> u.version
|
45
|
+
=> 1
|
46
|
+
>> u.update_attribute(:first_name, "Stephen")
|
47
|
+
=> true
|
48
|
+
>> u.name
|
49
|
+
=> "Stephen Richert"
|
50
|
+
>> u.version
|
51
|
+
=> 2
|
52
|
+
>> u.revert_to(10.seconds.ago)
|
53
|
+
=> 1
|
54
|
+
>> u.name
|
55
|
+
=> "Steve Richert"
|
56
|
+
>> u.version
|
57
|
+
=> 1
|
58
|
+
>> u.save
|
59
|
+
=> true
|
60
|
+
>> u.version
|
61
|
+
=> 3
|
62
|
+
>> u.update_attribute(:last_name, "Jobs")
|
63
|
+
=> true
|
64
|
+
>> u.name
|
65
|
+
=> "Steve Jobs"
|
66
|
+
>> u.version
|
67
|
+
=> 4
|
68
|
+
>> u.revert_to!(2)
|
69
|
+
=> true
|
70
|
+
>> u.name
|
71
|
+
=> "Stephen Richert"
|
72
|
+
>> u.version
|
73
|
+
=> 5
|
74
|
+
|
75
|
+
== Upgrading to 1.0
|
76
|
+
|
77
|
+
For the most part, version 1.0 of <tt>vestal_versions</tt> is backwards compatible, with just a few notable changes:
|
78
|
+
|
79
|
+
* The versions table has been beefed up. You'll need to add the following columns (and indexes, if you feel so inclined):
|
80
|
+
|
81
|
+
change_table :versions do |t|
|
82
|
+
t.belongs_to :user, :polymorphic => true
|
83
|
+
t.string :user_name
|
84
|
+
t.string :tag
|
85
|
+
end
|
86
|
+
|
87
|
+
change_table :versions do |t|
|
88
|
+
t.index [:user_id, :user_type]
|
89
|
+
t.index :user_name
|
90
|
+
t.index :tag
|
91
|
+
end
|
92
|
+
|
93
|
+
* When a model is created (or updated the first time after being versioned), an initial version record with a number of 1 is no longer created. These aren't used during reversion and so they end up just being dead weight. Feel free to scrap all your versions where <tt>number == 1</tt> after the upgrade if you'd like to free up some room in your database (but you don't have to).
|
94
|
+
|
95
|
+
* Models that have no version records in the database will return a <tt>@user.version</tt> of 1. In the past, this would have returned <tt>nil</tt> instead.
|
96
|
+
|
97
|
+
* <tt>Version</tt> has moved to <tt>VestalVersions::Version</tt> to make way for custom version classes.
|
98
|
+
|
99
|
+
* <tt>Version#version</tt> did not survive the move to <tt>VestalVersions::Version#version</tt>. That alias was dropped (too confusing). Use <tt>VestalVersions::Version#number</tt>.
|
100
|
+
|
101
|
+
== New to 1.0
|
102
|
+
|
103
|
+
There are a handful of exciting new additions in version 1.0 of <tt>vestal_versions</tt>. A lot has changed in the code: much better documentation, more modular organization of features, and a more exhaustive test suite. But there are also a number of new features that are available in this release of <tt>vestal_versions</tt>:
|
104
|
+
|
105
|
+
* The ability to completely skip versioning within a new <tt>skip_version</tt> block:
|
106
|
+
|
107
|
+
@user.version # => 1
|
108
|
+
@user.skip_version do
|
109
|
+
@user.update_attribute(:first_name, "Stephen")
|
110
|
+
@user.first_name = "Steve"
|
111
|
+
@user.save
|
112
|
+
@user.update_attributes(:last_name => "Jobs")
|
113
|
+
end
|
114
|
+
@user.version # => 1
|
115
|
+
|
116
|
+
Also available, are <tt>merge_version</tt> and <tt>append_version</tt> blocks. The <tt>merge_version</tt> block will compile the possibly multiple versions that would result from the updates inside the block into one summary version. The single resulting version is then tacked onto the version history as usual. The <tt>append_version</tt> block works similarly except that the resulting single version is combined with the most recent version in the history and saved.
|
117
|
+
|
118
|
+
* Version tagging. Any version can have a tag attached to it (must be unique within the scope of the versioned parent) and that tag can be used for reversion.
|
119
|
+
|
120
|
+
@user.name # => "Steve Richert"
|
121
|
+
@user.update_attribute(:last_name, "Jobs")
|
122
|
+
@user.name # => "Steve Jobs"
|
123
|
+
@user.tag_version("apple")
|
124
|
+
@user.update_attribute(:last_name, "Richert")
|
125
|
+
@user.name # => "Steve Richert"
|
126
|
+
@user.revert_to("apple")
|
127
|
+
@user.name # => "Steve Jobs"
|
128
|
+
|
129
|
+
So if you're not big on version numbers, you could just tag your versions and avoid the numbers altogether.
|
130
|
+
|
131
|
+
* Resetting. This is basically a hard revert. The new <tt>reset_to!</tt> instance method behaves just like the <tt>revert_to!</tt> method except that after the reversion, it will also scrap all the versions that came after that target version.
|
132
|
+
|
133
|
+
@user.name # => "Steve Richert"
|
134
|
+
@user.version # => 1
|
135
|
+
@user.versions.count # => 0
|
136
|
+
@user.update_attribute(:last_name, "Jobs")
|
137
|
+
@user.name # => "Steve Jobs"
|
138
|
+
@user.version # => 2
|
139
|
+
@user.versions.count # => 1
|
140
|
+
@user.reset_to!(1)
|
141
|
+
@user.name # => "Steve Richert"
|
142
|
+
@user.version # => 1
|
143
|
+
@user.versions.count # => 0
|
144
|
+
|
145
|
+
* Storing which user is responsible for a revision. Rather than introduce a lot of controller magic to guess what to store, you can simply update an additional attribute on your versioned model: <tt>updated_by</tt>.
|
146
|
+
|
147
|
+
@user.update_attributes(:last_name => "Jobs", :updated_by => "Tyler")
|
148
|
+
@user.versions.last.user # => "Tyler"
|
149
|
+
|
150
|
+
Instead of passing a simple string to the <tt>updated_by</tt> setter, you can pass a model instance, such as an ActiveRecord user or administrator. The association will be saved polymorphically alongside the version.
|
151
|
+
|
152
|
+
@user.update_attributes(:last_name => "Jobs", :updated_by => current_user)
|
153
|
+
@user.versions.last.user # => #<User first_name: "Steven", last_name: "Tyler">
|
154
|
+
|
155
|
+
* Global configuration. The new <tt>vestal_versions</tt> Rails generator also writes an initializer with instructions on how to set application-wide options for the <tt>versioned</tt> method.
|
156
|
+
|
157
|
+
* Conditional version creation. The <tt>versioned</tt> method now accepts <tt>:if</tt> and <tt>:unless</tt> options. Each expects a symbol representing an instance method or a proc that will be evaluated to determine whether or not to create a new version after an update. An array containing any combination of symbols and procs can also be given.
|
158
|
+
|
159
|
+
class User < ActiveRecord::Base
|
160
|
+
versioned :if => :really_create_a_version?
|
161
|
+
end
|
162
|
+
|
163
|
+
* Custom version classes. By passing a <tt>:class_name</tt> option to the <tt>versioned</tt> method, you can specify your own ActiveRecord version model. <tt>VestalVersions::Version</tt> is the default, but feel free to stray from that. I recommend that your custom model inherit from <tt>VestalVersions::Version</tt>, but that's up to you!
|
164
|
+
|
165
|
+
* A <tt>versioned?</tt> convenience class method. If your user model is versioned, <tt>User.versioned?</tt> will let you know.
|
166
|
+
|
167
|
+
* Soft Deletes & Restoration. By setting <tt>:dependent</tt> to <tt>:tracking</tt> destroys will be tracked. On destroy a new version will be created storing the complete details of the object with a tag of 'deleted'. The object can later be restored using the <tt>restore!</tt> method on the VestalVersions::Version record. The attributes of the restored object will be set using the attribute writer methods. After a restore! is performed the version record with the 'deleted' tag is removed from the history.
|
168
|
+
|
169
|
+
class User < ActiveRecord::Base
|
170
|
+
versioned :dependent => :tracking
|
171
|
+
end
|
172
|
+
|
173
|
+
>> @user.version
|
174
|
+
=> 2
|
175
|
+
>> @user.destroy
|
176
|
+
=> <User id: 2, first_name: "Steve", last_name: "Jobs", ... >
|
177
|
+
>> User.find(2)
|
178
|
+
=> ActiveRecord::RecordNotFound: Couldn't find User with ID=2
|
179
|
+
>> VestalVersions::Version.last
|
180
|
+
=> <VestalVersions::Version id: 4, versioned_id: 2, versioned_type: "User", user_id: nil, user_type: nil, user_name: nil, modifications: {"created_at"=>Sun Aug 01 18:39:57 UTC 2010, "updated_at"=>Sun Aug 01 18:42:28 UTC 2010, "id"=>2, "last_name"=>"Jobs", "first_name"=>"Stephen"}, number: 3, tag: "deleted", created_at: "2010-08-01 18:42:43", updated_at: "2010-08-01 18:42:43">
|
181
|
+
>> VestalVersions::Version.last.restore!
|
182
|
+
=> <User id: 2, first_name => "Steven", last_name: "Jobs", ... >
|
183
|
+
>> @user = User.find(2)
|
184
|
+
=> <User id: 2, first_name => "Steven", last_name: "Jobs", ... >
|
185
|
+
>> @user.version
|
186
|
+
=> 2
|
187
|
+
|
188
|
+
== Thanks!
|
189
|
+
|
190
|
+
Thank you to all those who post {issues and suggestions}[http://github.com/laserlemon/vestal_versions/issues]. And special thanks to:
|
191
|
+
|
192
|
+
* splattael[http://github.com/splattael], who first bugged (and helped) me to write some tests for this thing
|
193
|
+
* snaury[http://github.com/snaury], who helped out early on with the <tt>between</tt> association method, the <tt>:dependent</tt> option and a conflict from using a method called <tt>changes</tt>
|
194
|
+
* sthapit[http://github.com/sthapit], who was responsible for the <tt>:only</tt> and <tt>:except</tt> options as well as showing me that I'm a dummy for storing a useless first version
|
195
|
+
|
196
|
+
To contribute to <tt>vestal_versions</tt>, please fork, hack away in the integration[http://github.com/laserlemon/vestal_versions/tree/integration] branch and send me a pull request. Remember your tests!
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rcov/rcovtask'
|
5
|
+
require 'rake/rdoctask'
|
6
|
+
|
7
|
+
begin
|
8
|
+
require 'jeweler'
|
9
|
+
Jeweler::Tasks.new do |g|
|
10
|
+
g.name = 'brianjlandau-vestal_versions'
|
11
|
+
g.summary = %(Keep a DRY history of your ActiveRecord models' changes)
|
12
|
+
g.description = %(Keep a DRY history of your ActiveRecord models' changes)
|
13
|
+
g.email = 'steve@laserlemon.com'
|
14
|
+
g.homepage = 'http://github.com/laserlemon/vestal_versions'
|
15
|
+
g.authors = %w(laserlemon)
|
16
|
+
g.add_dependency 'activerecord', '>= 2.3.8'
|
17
|
+
g.add_dependency 'rails', '>= 2.3.8'
|
18
|
+
g.add_development_dependency 'shoulda'
|
19
|
+
g.add_development_dependency 'mocha'
|
20
|
+
g.add_development_dependency 'rcov'
|
21
|
+
g.add_development_dependency 'rake'
|
22
|
+
g.add_development_dependency 'jeweler'
|
23
|
+
g.add_development_dependency 'sqlite3-ruby'
|
24
|
+
end
|
25
|
+
Jeweler::GemcutterTasks.new
|
26
|
+
rescue LoadError
|
27
|
+
puts 'Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler'
|
28
|
+
end
|
29
|
+
|
30
|
+
Rake::TestTask.new do |t|
|
31
|
+
t.libs = %w(test)
|
32
|
+
t.pattern = 'test/**/*_test.rb'
|
33
|
+
end
|
34
|
+
|
35
|
+
Rcov::RcovTask.new do |t|
|
36
|
+
t.libs = %w(test)
|
37
|
+
t.pattern = 'test/**/*_test.rb'
|
38
|
+
end
|
39
|
+
|
40
|
+
task :test => :check_dependencies
|
41
|
+
task :default => :test
|
42
|
+
|
43
|
+
Rake::RDocTask.new do |r|
|
44
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : nil
|
45
|
+
r.rdoc_dir = 'rdoc'
|
46
|
+
r.title = ['vestal_versions', version].compact.join(' ')
|
47
|
+
r.options << '--line-numbers' << '--inline-source'
|
48
|
+
r.rdoc_files.include('README*')
|
49
|
+
r.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.3.0
|
@@ -0,0 +1,9 @@
|
|
1
|
+
VestalVersions.configure do |config|
|
2
|
+
# Place any global options here. For example, in order to specify your own version model to use
|
3
|
+
# throughout the application, simply specify:
|
4
|
+
#
|
5
|
+
# config.class_name = "MyCustomVersion"
|
6
|
+
#
|
7
|
+
# Any options passed to the "versioned" method in the model itself will override this global
|
8
|
+
# configuration.
|
9
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class CreateVestalVersions < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :versions do |t|
|
4
|
+
t.belongs_to :versioned, :polymorphic => true
|
5
|
+
t.belongs_to :user, :polymorphic => true
|
6
|
+
t.string :user_name
|
7
|
+
t.text :modifications
|
8
|
+
t.integer :number
|
9
|
+
t.integer :reverted_from
|
10
|
+
t.string :tag
|
11
|
+
|
12
|
+
t.timestamps
|
13
|
+
end
|
14
|
+
|
15
|
+
change_table :versions do |t|
|
16
|
+
t.index [:versioned_id, :versioned_type]
|
17
|
+
t.index [:user_id, :user_type]
|
18
|
+
t.index :user_name
|
19
|
+
t.index :number
|
20
|
+
t.index :tag
|
21
|
+
t.index :created_at
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.down
|
26
|
+
drop_table :versions
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class VestalVersionsGenerator < Rails::Generator::Base
|
2
|
+
def manifest
|
3
|
+
record do |m|
|
4
|
+
m.migration_template 'migration.rb', File.join('db', 'migrate'), :migration_file_name => 'create_vestal_versions'
|
5
|
+
|
6
|
+
m.directory File.join('config', 'initializers')
|
7
|
+
m.template 'initializer.rb', File.join('config', 'initializers', 'vestal_versions.rb')
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'lib', 'vestal_versions')
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# +vestal_versions+ keeps track of updates to ActiveRecord models, leveraging the introduction of
|
2
|
+
# dirty attributes in Rails 2.1. By storing only the updated attributes in a serialized column of a
|
3
|
+
# single version model, the history is kept DRY and no additional schema changes are necessary.
|
4
|
+
#
|
5
|
+
# Author:: Steve Richert
|
6
|
+
# Copyright:: Copyright (c) 2009 Steve Richert
|
7
|
+
# License:: MIT License (http://www.opensource.org/licenses/mit-license.php)
|
8
|
+
#
|
9
|
+
# To enable versioning on a model, simply use the +versioned+ method:
|
10
|
+
#
|
11
|
+
# class User < ActiveRecord::Base
|
12
|
+
# versioned
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# user = User.create(:name => "Steve Richert")
|
16
|
+
# user.version # => 1
|
17
|
+
# user.update_attributes(:name => "Steve Jobs")
|
18
|
+
# user.version # => 2
|
19
|
+
# user.revert_to(1)
|
20
|
+
# user.name # => "Steve Richert"
|
21
|
+
#
|
22
|
+
# See the +versioned+ documentation for more details.
|
23
|
+
|
24
|
+
# require File.expand_path(File.join(File.dirname(__FILE__), '..', 'generators', 'vestal_versions', 'vestal_versions_generator'))
|
25
|
+
Dir[File.join(File.dirname(__FILE__), 'vestal_versions', '*.rb')].each{|f| require f }
|
26
|
+
|
27
|
+
# The base module that gets included in ActiveRecord::Base. See the documentation for
|
28
|
+
# VestalVersions::ClassMethods for more useful information.
|
29
|
+
module VestalVersions
|
30
|
+
def self.included(base) # :nodoc:
|
31
|
+
base.class_eval do
|
32
|
+
extend ClassMethods
|
33
|
+
extend Versioned
|
34
|
+
extend Associations
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module ClassMethods
|
39
|
+
# +versioned+ associates an ActiveRecord model with many versions. When the object is updated,
|
40
|
+
# a new version containing the changes is created. There are several options available to the
|
41
|
+
# +versioned+ method, most of which are passed to the +has_many+ association itself:
|
42
|
+
# * <tt>:class_name</tt>: The class name of the version model to use for the association. By
|
43
|
+
# default, this is set to "VestalVersions::Version", representing the built-in version class.
|
44
|
+
# By specifying this option, you can override the version class, to include custom version
|
45
|
+
# behavior. It's recommended that a custom version inherit from VestalVersions::Version.
|
46
|
+
# * <tt>:dependent</tt>: Also common to +has_many+ associations, this describes the behavior of
|
47
|
+
# version records when the parent object is destroyed. This defaults to :delete_all, which
|
48
|
+
# will permanently remove all associated versions *without* triggering any destroy callbacks.
|
49
|
+
# Other options are :destroy which removes the associated versions *with* callbacks, or
|
50
|
+
# :nullify which leaves the version records in the database, but dissociates them from the
|
51
|
+
# parent object by setting the foreign key columns to +nil+ values. Setting this option to
|
52
|
+
# :tracking will perform a soft delete on destroy and create a new version record preserving
|
53
|
+
# details of this record for later restoration.
|
54
|
+
# * <tt>:except</tt>: An update will trigger version creation as long as at least one column
|
55
|
+
# outside those specified here was updated. Also, upon version creation, the columns
|
56
|
+
# specified here will be excluded from the change history. This is useful when dealing with
|
57
|
+
# unimportant, constantly changing, or sensitive information. This option accepts a symbol,
|
58
|
+
# string or an array of either, representing column names to exclude. It is completely
|
59
|
+
# optional and defaults to +nil+, allowing all columns to be versioned. This option is also
|
60
|
+
# ignored if the +only+ option is used.
|
61
|
+
# * <tt>:extend</tt>: This option allows you to extend the +has_many+ association proxy with a
|
62
|
+
# module or an array of modules. Any methods defined in those modules become available on the
|
63
|
+
# +versions+ association. The VestalVersions::Versions module is essential to the
|
64
|
+
# functionality of +vestal_versions+ and so is prepended to any additional modules that you
|
65
|
+
# might specify here.
|
66
|
+
# * <tt>:if</tt>: Accepts a symbol, a proc or an array of either to be evaluated when the parent
|
67
|
+
# object is updated to determine whether a new version should be created. +to_proc+ is called
|
68
|
+
# on any symbols given and the resulting procs are called, passing in the object itself. If
|
69
|
+
# an array is given, all must be evaluate to +true+ in order for a version to be created.
|
70
|
+
# * <tt>:only</tt>: An update will trigger version creation as long as at least one updated
|
71
|
+
# column falls within those specified here. Also, upon version creation, only the columns
|
72
|
+
# specified here will be included in the change history. This option accepts a symbol, string
|
73
|
+
# or an array of either, representing column names to include. It is completely optional and
|
74
|
+
# defaults to +nil+, allowing all columns to be versioned. This option takes precedence over
|
75
|
+
# the +except+ option if both are specified.
|
76
|
+
# * <tt>:unless</tt>: Accepts a symbol, a proc or an array of either to be evaluated when the
|
77
|
+
# parent object is updated to determine whether version creation should be skipped. +to_proc+
|
78
|
+
# is called on any symbols given and the resulting procs are called, passing in the object
|
79
|
+
# itself. If an array is given and any element evaluates as +true+, the version creation will
|
80
|
+
# be skipped.
|
81
|
+
def versioned(options = {}, &block)
|
82
|
+
return if versioned?
|
83
|
+
|
84
|
+
include Options
|
85
|
+
include Changes
|
86
|
+
include Creation
|
87
|
+
include Users
|
88
|
+
include Reversion
|
89
|
+
include Reset
|
90
|
+
include Conditions
|
91
|
+
include Control
|
92
|
+
include Tagging
|
93
|
+
include Reload
|
94
|
+
include Deletion
|
95
|
+
|
96
|
+
prepare_versioned_options(options)
|
97
|
+
has_many :versions, options, &block
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
extend Configuration
|
102
|
+
end
|
103
|
+
|
104
|
+
ActiveRecord::Base.send(:include, VestalVersions)
|