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
@@ -0,0 +1,30 @@
|
|
1
|
+
module VestalVersions
|
2
|
+
# Simply adds a flag to determine whether a model class if versioned.
|
3
|
+
module Versioned
|
4
|
+
def self.extended(base) # :nodoc:
|
5
|
+
base.class_eval do
|
6
|
+
class << self
|
7
|
+
alias_method_chain :versioned, :flag
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
# Overrides the +versioned+ method to first define the +versioned?+ class method before
|
13
|
+
# deferring to the original +versioned+.
|
14
|
+
def versioned_with_flag(*args)
|
15
|
+
versioned_without_flag(*args)
|
16
|
+
|
17
|
+
class << self
|
18
|
+
def versioned?
|
19
|
+
true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# For all ActiveRecord::Base models that do not call the +versioned+ method, the +versioned?+
|
25
|
+
# method will return false.
|
26
|
+
def versioned?
|
27
|
+
false
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module VestalVersions
|
2
|
+
# An extension module for the +has_many+ association with versions.
|
3
|
+
module Versions
|
4
|
+
# Returns all versions between (and including) the two given arguments. See documentation for
|
5
|
+
# the +at+ extension method for what arguments are valid. If either of the given arguments is
|
6
|
+
# invalid, an empty array is returned.
|
7
|
+
#
|
8
|
+
# The +between+ method preserves returns an array of version records, preserving the order
|
9
|
+
# given by the arguments. If the +from+ value represents a version before that of the +to+
|
10
|
+
# value, the array will be ordered from earliest to latest. The reverse is also true.
|
11
|
+
def between(from, to)
|
12
|
+
from_number, to_number = number_at(from), number_at(to)
|
13
|
+
return [] if from_number.nil? || to_number.nil?
|
14
|
+
|
15
|
+
condition = (from_number == to_number) ? to_number : Range.new(*[from_number, to_number].sort)
|
16
|
+
all(
|
17
|
+
:conditions => {:number => condition},
|
18
|
+
:order => "#{aliased_table_name}.number #{(from_number > to_number) ? 'DESC' : 'ASC'}"
|
19
|
+
)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns all version records created before the version associated with the given value.
|
23
|
+
def before(value)
|
24
|
+
return [] if (number = number_at(value)).nil?
|
25
|
+
all(:conditions => "#{aliased_table_name}.number < #{number}")
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns all version records created after the version associated with the given value.
|
29
|
+
#
|
30
|
+
# This is useful for dissociating records during use of the +reset_to!+ method.
|
31
|
+
def after(value)
|
32
|
+
return [] if (number = number_at(value)).nil?
|
33
|
+
all(:conditions => "#{aliased_table_name}.number > #{number}")
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns a single version associated with the given value. The following formats are valid:
|
37
|
+
# * A Date or Time object: When given, +to_time+ is called on the value and the last version
|
38
|
+
# record in the history created before (or at) that time is returned.
|
39
|
+
# * A Numeric object: Typically a positive integer, these values correspond to version numbers
|
40
|
+
# and the associated version record is found by a version number equal to the given value
|
41
|
+
# rounded down to the nearest integer.
|
42
|
+
# * A String: A string value represents a version tag and the associated version is searched
|
43
|
+
# for by a matching tag value. *Note:* Be careful with string representations of numbers.
|
44
|
+
# * A Symbol: Symbols represent association class methods on the +has_many+ versions
|
45
|
+
# association. While all of the built-in association methods require arguments, additional
|
46
|
+
# extension modules can be defined using the <tt>:extend</tt> option on the +versioned+
|
47
|
+
# method. See the +versioned+ documentation for more information.
|
48
|
+
# * A Version object: If a version object is passed to the +at+ method, it is simply returned
|
49
|
+
# untouched.
|
50
|
+
def at(value)
|
51
|
+
case value
|
52
|
+
when Date, Time then last(:conditions => ["#{aliased_table_name}.created_at <= ?", value.to_time])
|
53
|
+
when Numeric then find_by_number(value.floor)
|
54
|
+
when String then find_by_tag(value)
|
55
|
+
when Symbol then respond_to?(value) ? send(value) : nil
|
56
|
+
when Version then value
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Returns the version number associated with the given value. In many cases, this involves
|
61
|
+
# simply passing the value to the +at+ method and then returning the subsequent version number.
|
62
|
+
# Hoever, for Numeric values, the version number can be returned directly and for Date/Time
|
63
|
+
# values, a default value of 1 is given to ensure that times prior to the first version
|
64
|
+
# still return a valid version number (useful for reversion).
|
65
|
+
def number_at(value)
|
66
|
+
case value
|
67
|
+
when Date, Time then (v = at(value)) ? v.number : 1
|
68
|
+
when Numeric then value.floor
|
69
|
+
when String, Symbol then (v = at(value)) ? v.number : nil
|
70
|
+
when Version then value.number
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
|
2
|
+
|
3
|
+
class AssociationsTest < Test::Unit::TestCase
|
4
|
+
context "A version's associations" do
|
5
|
+
should 'should propogate through belongs_to' do
|
6
|
+
group = Group.create :name => "The Group"
|
7
|
+
user = User.create(:name => 'Steve Richert', :group_id => group.id)
|
8
|
+
assert_equal 1, user.version
|
9
|
+
assert_equal 1, user.group.version
|
10
|
+
group.update_attributes(:name => "New Group")
|
11
|
+
user.update_attributes(:name => "New Name")
|
12
|
+
assert_equal 2, user.version
|
13
|
+
assert_equal 2, user.group.version
|
14
|
+
user.revert_to(1)
|
15
|
+
assert_equal 1, user.group.version
|
16
|
+
end
|
17
|
+
|
18
|
+
should 'return a versioned object through AssociationProxy#first' do
|
19
|
+
group = Group.create :name => "The Group"
|
20
|
+
user = User.create(:name => 'Steve Richert', :group_id => group.id)
|
21
|
+
group.update_attributes(:name => "New Group")
|
22
|
+
user.update_attributes(:name => "New Name")
|
23
|
+
group.revert_to(1)
|
24
|
+
assert_equal 1, group.users.first.version
|
25
|
+
end
|
26
|
+
|
27
|
+
should 'return a versioned object through AssociationProxy#last' do
|
28
|
+
group = Group.create :name => "The Group"
|
29
|
+
user = User.create(:name => 'Steve Richert', :group_id => group.id)
|
30
|
+
group.update_attributes(:name => "New Group")
|
31
|
+
user.update_attributes(:name => "New Name")
|
32
|
+
group.revert_to(1)
|
33
|
+
assert_equal 1, group.users.last.version
|
34
|
+
end
|
35
|
+
|
36
|
+
should 'should propogate through has_many' do
|
37
|
+
group = Group.create :name => "The Group"
|
38
|
+
user = User.create(:name => 'Steve Richert', :group_id => group.id)
|
39
|
+
assert_equal 1, group.version
|
40
|
+
assert_equal 1, group.users.first.version
|
41
|
+
group.update_attributes(:name => "New Group")
|
42
|
+
user.update_attributes(:name => "New Name")
|
43
|
+
assert_equal 2, group.version
|
44
|
+
assert_equal 2, group.users.first.version
|
45
|
+
group.revert_to(1)
|
46
|
+
assert_equal 1, group.users.last.version
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,169 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
|
2
|
+
|
3
|
+
class ChangesTest < Test::Unit::TestCase
|
4
|
+
context "A version's changes" do
|
5
|
+
setup do
|
6
|
+
@user = User.create(:name => 'Steve Richert')
|
7
|
+
@user.update_attributes(:last_name =>'Jobs')
|
8
|
+
@changes = @user.versions.last.changes
|
9
|
+
end
|
10
|
+
|
11
|
+
should 'be a hash' do
|
12
|
+
assert_kind_of Hash, @changes
|
13
|
+
end
|
14
|
+
|
15
|
+
should 'not be empty' do
|
16
|
+
assert !@changes.empty?
|
17
|
+
end
|
18
|
+
|
19
|
+
should 'have string keys' do
|
20
|
+
@changes.keys.each do |key|
|
21
|
+
assert_kind_of String, key
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
should 'have array values' do
|
26
|
+
@changes.values.each do |value|
|
27
|
+
assert_kind_of Array, value
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
should 'have two-element values' do
|
32
|
+
@changes.values.each do |value|
|
33
|
+
assert_equal 2, value.size
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
should 'have unique-element values' do
|
38
|
+
@changes.values.each do |value|
|
39
|
+
assert_equal value.uniq, value
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
should "equal the model's changes" do
|
44
|
+
@user.first_name = 'Stephen'
|
45
|
+
model_changes = @user.changes
|
46
|
+
@user.save
|
47
|
+
changes = @user.versions.last.changes
|
48
|
+
assert_equal model_changes, changes
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'A hash of changes' do
|
53
|
+
setup do
|
54
|
+
@changes = {'first_name' => ['Steve', 'Stephen']}
|
55
|
+
@other = {'first_name' => ['Catie', 'Catherine']}
|
56
|
+
end
|
57
|
+
|
58
|
+
should 'properly append other changes' do
|
59
|
+
expected = {'first_name' => ['Steve', 'Catherine']}
|
60
|
+
changes = @changes.append_changes(@other)
|
61
|
+
assert_equal expected, changes
|
62
|
+
@changes.append_changes!(@other)
|
63
|
+
assert_equal expected, @changes
|
64
|
+
end
|
65
|
+
|
66
|
+
should 'properly prepend other changes' do
|
67
|
+
expected = {'first_name' => ['Catie', 'Stephen']}
|
68
|
+
changes = @changes.prepend_changes(@other)
|
69
|
+
assert_equal expected, changes
|
70
|
+
@changes.prepend_changes!(@other)
|
71
|
+
assert_equal expected, @changes
|
72
|
+
end
|
73
|
+
|
74
|
+
should 'be reversible' do
|
75
|
+
expected = {'first_name' => ['Stephen', 'Steve']}
|
76
|
+
changes = @changes.reverse_changes
|
77
|
+
assert_equal expected, changes
|
78
|
+
@changes.reverse_changes!
|
79
|
+
assert_equal expected, @changes
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'The changes between two versions' do
|
84
|
+
setup do
|
85
|
+
name = 'Steve Richert'
|
86
|
+
@user = User.create(:name => name) # 1
|
87
|
+
@user.update_attributes(:last_name => 'Jobs') # 2
|
88
|
+
@user.update_attributes(:first_name => 'Stephen') # 3
|
89
|
+
@user.update_attributes(:last_name => 'Richert') # 4
|
90
|
+
@user.update_attributes(:name => name) # 5
|
91
|
+
@version = @user.version
|
92
|
+
end
|
93
|
+
|
94
|
+
should 'be a hash' do
|
95
|
+
1.upto(@version) do |i|
|
96
|
+
1.upto(@version) do |j|
|
97
|
+
changes = @user.changes_between(i, j)
|
98
|
+
assert_kind_of Hash, changes
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
should 'have string keys' do
|
104
|
+
1.upto(@version) do |i|
|
105
|
+
1.upto(@version) do |j|
|
106
|
+
changes = @user.changes_between(i, j)
|
107
|
+
changes.keys.each do |key|
|
108
|
+
assert_kind_of String, key
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
should 'have array values' do
|
115
|
+
1.upto(@version) do |i|
|
116
|
+
1.upto(@version) do |j|
|
117
|
+
changes = @user.changes_between(i, j)
|
118
|
+
changes.values.each do |value|
|
119
|
+
assert_kind_of Array, value
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
should 'have two-element values' do
|
126
|
+
1.upto(@version) do |i|
|
127
|
+
1.upto(@version) do |j|
|
128
|
+
changes = @user.changes_between(i, j)
|
129
|
+
changes.values.each do |value|
|
130
|
+
assert_equal 2, value.size
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
should 'have unique-element values' do
|
137
|
+
1.upto(@version) do |i|
|
138
|
+
1.upto(@version) do |j|
|
139
|
+
changes = @user.changes_between(i, j)
|
140
|
+
changes.values.each do |value|
|
141
|
+
assert_equal value.uniq, value
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
should 'be empty between identical versions' do
|
148
|
+
assert @user.changes_between(1, @version).empty?
|
149
|
+
assert @user.changes_between(@version, 1).empty?
|
150
|
+
end
|
151
|
+
|
152
|
+
should 'be should reverse with direction' do
|
153
|
+
1.upto(@version) do |i|
|
154
|
+
i.upto(@version) do |j|
|
155
|
+
up = @user.changes_between(i, j)
|
156
|
+
down = @user.changes_between(j, i)
|
157
|
+
assert_equal up, down.reverse_changes
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
should 'be empty with invalid arguments' do
|
163
|
+
1.upto(@version) do |i|
|
164
|
+
assert @user.changes_between(i, nil)
|
165
|
+
assert @user.changes_between(nil, i)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
@@ -0,0 +1,137 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
|
2
|
+
|
3
|
+
class ConditionsTest < Test::Unit::TestCase
|
4
|
+
context 'Converted :if conditions' do
|
5
|
+
setup do
|
6
|
+
User.class_eval do
|
7
|
+
def true; true; end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
should 'be an array' do
|
12
|
+
assert_kind_of Array, User.vestal_versions_options[:if]
|
13
|
+
User.prepare_versioned_options(:if => :true)
|
14
|
+
assert_kind_of Array, User.vestal_versions_options[:if]
|
15
|
+
end
|
16
|
+
|
17
|
+
should 'have proc values' do
|
18
|
+
User.prepare_versioned_options(:if => :true)
|
19
|
+
assert User.vestal_versions_options[:if].all?{|i| i.is_a?(Proc) }
|
20
|
+
end
|
21
|
+
|
22
|
+
teardown do
|
23
|
+
User.prepare_versioned_options(:if => [])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'Converted :unless conditions' do
|
28
|
+
setup do
|
29
|
+
User.class_eval do
|
30
|
+
def true; true; end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
should 'be an array' do
|
35
|
+
assert_kind_of Array, User.vestal_versions_options[:unless]
|
36
|
+
User.prepare_versioned_options(:unless => :true)
|
37
|
+
assert_kind_of Array, User.vestal_versions_options[:unless]
|
38
|
+
end
|
39
|
+
|
40
|
+
should 'have proc values' do
|
41
|
+
User.prepare_versioned_options(:unless => :true)
|
42
|
+
assert User.vestal_versions_options[:unless].all?{|i| i.is_a?(Proc) }
|
43
|
+
end
|
44
|
+
|
45
|
+
teardown do
|
46
|
+
User.prepare_versioned_options(:unless => [])
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'A new version' do
|
51
|
+
setup do
|
52
|
+
User.class_eval do
|
53
|
+
def true; true; end
|
54
|
+
def false; false; end
|
55
|
+
end
|
56
|
+
|
57
|
+
@user = User.create(:name => 'Steve Richert')
|
58
|
+
@count = @user.versions.count
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'with :if conditions' do
|
62
|
+
context 'that pass' do
|
63
|
+
setup do
|
64
|
+
User.prepare_versioned_options(:if => [:true])
|
65
|
+
@user.update_attributes(:last_name => 'Jobs')
|
66
|
+
end
|
67
|
+
|
68
|
+
should 'be created' do
|
69
|
+
assert_equal @count + 1, @user.versions.count
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'that fail' do
|
74
|
+
setup do
|
75
|
+
User.prepare_versioned_options(:if => [:false])
|
76
|
+
@user.update_attributes(:last_name => 'Jobs')
|
77
|
+
end
|
78
|
+
|
79
|
+
should 'not be created' do
|
80
|
+
assert_equal @count, @user.versions.count
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'with :unless conditions' do
|
86
|
+
context 'that pass' do
|
87
|
+
setup do
|
88
|
+
User.prepare_versioned_options(:unless => [:true])
|
89
|
+
@user.update_attributes(:last_name => 'Jobs')
|
90
|
+
end
|
91
|
+
|
92
|
+
should 'not be created' do
|
93
|
+
assert_equal @count, @user.versions.count
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context 'that fail' do
|
98
|
+
setup do
|
99
|
+
User.prepare_versioned_options(:unless => [:false])
|
100
|
+
@user.update_attributes(:last_name => 'Jobs')
|
101
|
+
end
|
102
|
+
|
103
|
+
should 'not be created' do
|
104
|
+
assert_equal @count + 1, @user.versions.count
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
context 'with :if and :unless conditions' do
|
110
|
+
context 'that pass' do
|
111
|
+
setup do
|
112
|
+
User.prepare_versioned_options(:if => [:true], :unless => [:true])
|
113
|
+
@user.update_attributes(:last_name => 'Jobs')
|
114
|
+
end
|
115
|
+
|
116
|
+
should 'not be created' do
|
117
|
+
assert_equal @count, @user.versions.count
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context 'that fail' do
|
122
|
+
setup do
|
123
|
+
User.prepare_versioned_options(:if => [:false], :unless => [:false])
|
124
|
+
@user.update_attributes(:last_name => 'Jobs')
|
125
|
+
end
|
126
|
+
|
127
|
+
should 'not be created' do
|
128
|
+
assert_equal @count, @user.versions.count
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
teardown do
|
134
|
+
User.prepare_versioned_options(:if => [], :unless => [])
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|