brianjlandau-vestal_versions 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/.gitignore +27 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +196 -0
  5. data/Rakefile +50 -0
  6. data/VERSION +1 -0
  7. data/generators/vestal_versions/templates/initializer.rb +9 -0
  8. data/generators/vestal_versions/templates/migration.rb +28 -0
  9. data/generators/vestal_versions/vestal_versions_generator.rb +10 -0
  10. data/init.rb +1 -0
  11. data/lib/vestal_versions.rb +104 -0
  12. data/lib/vestal_versions/associations.rb +67 -0
  13. data/lib/vestal_versions/changes.rb +125 -0
  14. data/lib/vestal_versions/conditions.rb +69 -0
  15. data/lib/vestal_versions/configuration.rb +40 -0
  16. data/lib/vestal_versions/control.rb +175 -0
  17. data/lib/vestal_versions/creation.rb +85 -0
  18. data/lib/vestal_versions/deletion.rb +46 -0
  19. data/lib/vestal_versions/options.rb +45 -0
  20. data/lib/vestal_versions/reload.rb +22 -0
  21. data/lib/vestal_versions/reset.rb +28 -0
  22. data/lib/vestal_versions/reversion.rb +92 -0
  23. data/lib/vestal_versions/tagging.rb +54 -0
  24. data/lib/vestal_versions/users.rb +56 -0
  25. data/lib/vestal_versions/version.rb +76 -0
  26. data/lib/vestal_versions/versioned.rb +30 -0
  27. data/lib/vestal_versions/versions.rb +74 -0
  28. data/test/associations_test.rb +49 -0
  29. data/test/changes_test.rb +169 -0
  30. data/test/conditions_test.rb +137 -0
  31. data/test/configuration_test.rb +39 -0
  32. data/test/control_test.rb +152 -0
  33. data/test/creation_test.rb +110 -0
  34. data/test/deletion_test.rb +121 -0
  35. data/test/options_test.rb +52 -0
  36. data/test/reload_test.rb +19 -0
  37. data/test/reset_test.rb +112 -0
  38. data/test/reversion_test.rb +99 -0
  39. data/test/schema.rb +62 -0
  40. data/test/tagging_test.rb +39 -0
  41. data/test/test_helper.rb +12 -0
  42. data/test/users_test.rb +25 -0
  43. data/test/version_test.rb +61 -0
  44. data/test/versioned_test.rb +18 -0
  45. data/test/versions_test.rb +172 -0
  46. data/vestal_versions.gemspec +124 -0
  47. metadata +245 -0
@@ -0,0 +1,39 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
2
+
3
+ class ConfigurationTest < Test::Unit::TestCase
4
+ context 'Global configuration options' do
5
+ setup do
6
+ module Extension; end
7
+
8
+ @options = {
9
+ 'class_name' => 'CustomVersion',
10
+ :extend => Extension,
11
+ :as => :parent
12
+ }
13
+
14
+ VestalVersions.configure do |config|
15
+ @options.each do |key, value|
16
+ config.send("#{key}=", value)
17
+ end
18
+ end
19
+
20
+ @configuration = VestalVersions::Configuration.options
21
+ end
22
+
23
+ should 'should be a hash' do
24
+ assert_kind_of Hash, @configuration
25
+ end
26
+
27
+ should 'have symbol keys' do
28
+ assert @configuration.keys.all?{|k| k.is_a?(Symbol) }
29
+ end
30
+
31
+ should 'store values identical to those given' do
32
+ assert_equal @options.symbolize_keys, @configuration
33
+ end
34
+
35
+ teardown do
36
+ VestalVersions::Configuration.options.clear
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,152 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
2
+
3
+ class ControlTest < Test::Unit::TestCase
4
+ context 'Within a skip_version block,' do
5
+ setup do
6
+ @user = User.create(:name => 'Steve Richert')
7
+ @count = @user.versions.count
8
+ end
9
+
10
+ context 'a model update' do
11
+ setup do
12
+ @user.skip_version do
13
+ @user.update_attributes(:last_name => 'Jobs')
14
+ end
15
+ end
16
+
17
+ should 'not create a version' do
18
+ assert_equal @count, @user.versions.count
19
+ end
20
+ end
21
+
22
+ context 'multiple model updates' do
23
+ setup do
24
+ @user.skip_version do
25
+ @user.update_attributes(:first_name => 'Stephen')
26
+ @user.update_attributes(:last_name => 'Jobs')
27
+ @user.update_attributes(:first_name => 'Steve')
28
+ end
29
+ end
30
+
31
+ should 'not create a version' do
32
+ assert_equal @count, @user.versions.count
33
+ end
34
+ end
35
+ end
36
+
37
+ context 'Within a merge_version block,' do
38
+ setup do
39
+ @user = User.create(:name => 'Steve Richert')
40
+ @count = @user.versions.count
41
+ end
42
+
43
+ context 'a model update' do
44
+ setup do
45
+ @user.merge_version do
46
+ @user.update_attributes(:last_name => 'Jobs')
47
+ end
48
+ end
49
+
50
+ should 'create a version' do
51
+ assert_equal @count + 1, @user.versions.count
52
+ end
53
+ end
54
+
55
+ context 'multiple model updates' do
56
+ setup do
57
+ @user.merge_version do
58
+ @user.update_attributes(:first_name => 'Stephen')
59
+ @user.update_attributes(:last_name => 'Jobs')
60
+ @user.update_attributes(:first_name => 'Steve')
61
+ end
62
+ end
63
+
64
+ should 'create a version' do
65
+ assert_equal @count + 1, @user.versions.count
66
+ end
67
+ end
68
+ end
69
+
70
+ context 'Within a append_version block' do
71
+ context '(when no versions exist),' do
72
+ setup do
73
+ @user = User.create(:name => 'Steve Richert')
74
+ @count = @user.versions.count
75
+ end
76
+
77
+ context 'a model update' do
78
+ setup do
79
+ @user.append_version do
80
+ @user.update_attributes(:last_name => 'Jobs')
81
+ end
82
+ end
83
+
84
+ should 'create a version' do
85
+ assert_equal @count + 1, @user.versions.count
86
+ end
87
+ end
88
+
89
+ context 'multiple model updates' do
90
+ setup do
91
+ @user.append_version do
92
+ @user.update_attributes(:first_name => 'Stephen')
93
+ @user.update_attributes(:last_name => 'Jobs')
94
+ @user.update_attributes(:first_name => 'Steve')
95
+ end
96
+ end
97
+
98
+ should 'create a version' do
99
+ assert_equal @count + 1, @user.versions.count
100
+ end
101
+ end
102
+ end
103
+
104
+ context '(when versions exist),' do
105
+ setup do
106
+ @user = User.create(:name => 'Steve Richert')
107
+ @user.update_attributes(:last_name => 'Jobs')
108
+ @user.update_attributes(:last_name => 'Richert')
109
+ @last_version = @user.versions.last
110
+ @count = @user.versions.count
111
+ end
112
+
113
+ context 'a model update' do
114
+ setup do
115
+ @user.append_version do
116
+ @user.update_attributes(:last_name => 'Jobs')
117
+ end
118
+ end
119
+
120
+ should 'not create a version' do
121
+ assert_equal @count, @user.versions.count
122
+ end
123
+
124
+ should 'update the last version' do
125
+ last_version = @user.versions(true).last
126
+ assert_equal @last_version.id, last_version.id
127
+ assert_not_equal @last_version.attributes, last_version.attributes
128
+ end
129
+ end
130
+
131
+ context 'multiple model updates' do
132
+ setup do
133
+ @user.append_version do
134
+ @user.update_attributes(:first_name => 'Stephen')
135
+ @user.update_attributes(:last_name => 'Jobs')
136
+ @user.update_attributes(:first_name => 'Steve')
137
+ end
138
+ end
139
+
140
+ should 'not create a version' do
141
+ assert_equal @count, @user.versions.count
142
+ end
143
+
144
+ should 'update the last version' do
145
+ last_version = @user.versions(true).last
146
+ assert_equal @last_version.id, last_version.id
147
+ assert_not_equal @last_version.attributes, last_version.attributes
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,110 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
2
+
3
+ class CreationTest < Test::Unit::TestCase
4
+ context 'The number of versions' do
5
+ setup do
6
+ @name = 'Steve Richert'
7
+ @user = User.create(:name => @name)
8
+ @count = @user.versions.count
9
+ end
10
+
11
+ should 'initially equal zero' do
12
+ assert_equal 0, @count
13
+ end
14
+
15
+ should 'not increase when no changes are made in an update' do
16
+ @user.update_attributes(:name => @name)
17
+ assert_equal @count, @user.versions.count
18
+ end
19
+
20
+ should 'not increase when no changes are made before a save' do
21
+ @user.save
22
+ assert_equal @count, @user.versions.count
23
+ end
24
+
25
+ context 'after an update' do
26
+ setup do
27
+ @user.update_attributes(:last_name => 'Jobs')
28
+ end
29
+
30
+ should 'increase by one' do
31
+ assert_equal @count + 1, @user.versions.count
32
+ end
33
+ end
34
+
35
+ context 'after multiple updates' do
36
+ setup do
37
+ @user.update_attributes(:last_name => 'Jobs')
38
+ @user.update_attributes(:last_name => 'Richert')
39
+ end
40
+
41
+ should 'increase multiple times' do
42
+ assert_operator @count + 1, :<, @user.versions.count
43
+ end
44
+ end
45
+ end
46
+
47
+ context "A created version's changes" do
48
+ setup do
49
+ @user = User.create(:name => 'Steve Richert')
50
+ @user.update_attributes(:last_name => 'Jobs')
51
+ end
52
+
53
+ should 'not contain Rails timestamps' do
54
+ %w(created_at created_on updated_at updated_on).each do |timestamp|
55
+ assert_does_not_contain @user.versions.last.changes.keys, timestamp
56
+ end
57
+ end
58
+
59
+ context '(with :only options)' do
60
+ setup do
61
+ @only = %w(first_name)
62
+ User.prepare_versioned_options(:only => @only)
63
+ @user.update_attributes(:name => 'Steven Tyler')
64
+ end
65
+
66
+ should 'only contain the specified columns' do
67
+ assert_equal @only, @user.versions.last.changes.keys
68
+ end
69
+
70
+ teardown do
71
+ User.prepare_versioned_options(:only => nil)
72
+ end
73
+ end
74
+
75
+ context '(with :except options)' do
76
+ setup do
77
+ @except = %w(first_name)
78
+ User.prepare_versioned_options(:except => @except)
79
+ @user.update_attributes(:name => 'Steven Tyler')
80
+ end
81
+
82
+ should 'not contain the specified columns' do
83
+ @except.each do |column|
84
+ assert_does_not_contain @user.versions.last.changes.keys, column
85
+ end
86
+ end
87
+
88
+ teardown do
89
+ User.prepare_versioned_options(:except => nil)
90
+ end
91
+ end
92
+
93
+ context '(with both :only and :except options)' do
94
+ setup do
95
+ @only = %w(first_name)
96
+ @except = @only
97
+ User.prepare_versioned_options(:only => @only, :except => @except)
98
+ @user.update_attributes(:name => 'Steven Tyler')
99
+ end
100
+
101
+ should 'respect only the :only options' do
102
+ assert_equal @only, @user.versions.last.changes.keys
103
+ end
104
+
105
+ teardown do
106
+ User.prepare_versioned_options(:only => nil, :except => nil)
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,121 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
2
+
3
+ class DeletionTest < Test::Unit::TestCase
4
+ # context 'The number of versions' do
5
+ # setup do
6
+ # @name = 'Steve Richert'
7
+ # @user = User.create(:name => @name)
8
+ # @count = @user.versions.count
9
+ # end
10
+ #
11
+ # should 'initially equal zero' do
12
+ # assert_equal 0, @count
13
+ # end
14
+ #
15
+ # should 'not increase when no changes are made in an update' do
16
+ # @user.update_attributes(:name => @name)
17
+ # assert_equal @count, @user.versions.count
18
+ # end
19
+ #
20
+ # should 'not increase when no changes are made before a save' do
21
+ # @user.save
22
+ # assert_equal @count, @user.versions.count
23
+ # end
24
+ #
25
+ # context 'after an update' do
26
+ # setup do
27
+ # @user.update_attributes(:last_name => 'Jobs')
28
+ # end
29
+ #
30
+ # should 'increase by one' do
31
+ # assert_equal @count + 1, @user.versions.count
32
+ # end
33
+ # end
34
+ #
35
+ # context 'after multiple updates' do
36
+ # setup do
37
+ # @user.update_attributes(:last_name => 'Jobs')
38
+ # @user.update_attributes(:last_name => 'Richert')
39
+ # end
40
+ #
41
+ # should 'increase multiple times' do
42
+ # assert_operator @count + 1, :<, @user.versions.count
43
+ # end
44
+ # end
45
+ # end
46
+
47
+ context "A deleted version's changes" do
48
+ setup do
49
+ @user = DeletedUser.create(:first_name => 'Steve', :last_name => 'Richert')
50
+ @user.update_attributes(:last_name => 'Jobs')
51
+ @original_version_count = VestalVersions::Version.count
52
+ @user.destroy
53
+ @user_id = @user.id
54
+ end
55
+
56
+ should "remove the original record" do
57
+ assert_equal nil, DeletedUser.find_by_id(@user_id)
58
+ end
59
+
60
+ should "create another version record" do
61
+ assert_equal @original_version_count + 1, VestalVersions::Version.count
62
+ end
63
+
64
+ should "create a version with a tag 'deleted'" do
65
+ assert_equal 'deleted', VestalVersions::Version.last.tag
66
+ end
67
+
68
+ end
69
+
70
+ context "restoring a deleted version" do
71
+ setup do
72
+ @user = DeletedUser.create(:first_name => 'Steve', :last_name => 'Richert')
73
+ @user.update_attributes(:last_name => 'Jobs')
74
+ @user.destroy
75
+ @last_version = VestalVersions::Version.last
76
+ end
77
+ should "be able to restore the user record" do
78
+ @last_version.restore!
79
+ assert_equal @user, @last_version.versioned
80
+ end
81
+ should "remove the last versioned entry" do
82
+ old_version_count = VestalVersions::Version.count
83
+ @last_version.restore!
84
+ assert_equal old_version_count - 1, VestalVersions::Version.count
85
+ end
86
+ should "work properly even if it's not on the proper version" do
87
+ another_version = VestalVersions::Version.find(:first, :conditions => {:versioned_id => @last_version.versioned_id, :versioned_type => @last_version.versioned_type})
88
+ assert_not_equal another_version, @last_version
89
+
90
+ restored_user = another_version.restore!
91
+
92
+ assert_equal @user, restored_user
93
+ end
94
+ should "restore even if the schema has changed" do
95
+ @last_version.update_attributes(:modifications => @last_version.modifications.merge(:old_column => 'old'))
96
+ user = @last_version.restore
97
+ assert_equal user, @user
98
+ end
99
+ end
100
+ context "restoring a deleted version without save" do
101
+ setup do
102
+ @user = DeletedUser.create(:first_name => 'Steve', :last_name => 'Richert')
103
+ @user.update_attributes(:last_name => 'Jobs')
104
+ @user.destroy
105
+ @last_version = VestalVersions::Version.last
106
+ end
107
+ should "not save the DeletedUser when restoring" do
108
+ user = @last_version.restore
109
+ assert_equal user.new_record?, true
110
+ end
111
+ should "restore the user object properly" do
112
+ user = @last_version.restore
113
+ assert_equal user, @user
114
+ end
115
+ should "not decrement the versions table" do
116
+ old_version_count = VestalVersions::Version.count
117
+ @last_version.restore
118
+ assert_equal old_version_count, VestalVersions::Version.count
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,52 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
2
+
3
+ class OptionsTest < Test::Unit::TestCase
4
+ context 'Configuration options' do
5
+ setup do
6
+ @options = {:dependent => :destroy}
7
+ @configuration = {:class_name => 'MyCustomVersion'}
8
+
9
+ VestalVersions::Configuration.options.clear
10
+ @configuration.each{|k,v| VestalVersions::Configuration.send("#{k}=", v) }
11
+
12
+ @prepared_options = User.prepare_versioned_options(@options.dup)
13
+ end
14
+
15
+ should 'have symbolized keys' do
16
+ assert User.vestal_versions_options.keys.all?{|k| k.is_a?(Symbol) }
17
+ end
18
+
19
+ should 'combine class-level and global configuration options' do
20
+ combined_keys = (@options.keys + @configuration.keys).map(&:to_sym).uniq
21
+ combined_options = @configuration.symbolize_keys.merge(@options.symbolize_keys)
22
+ assert_equal @prepared_options.slice(*combined_keys), combined_options
23
+ end
24
+
25
+ teardown do
26
+ VestalVersions::Configuration.options.clear
27
+ User.prepare_versioned_options({})
28
+ end
29
+ end
30
+
31
+ context 'Given no options, configuration options' do
32
+ setup do
33
+ @prepared_options = User.prepare_versioned_options({})
34
+ end
35
+
36
+ should 'default to "VestalVersions::Version" for :class_name' do
37
+ assert_equal 'VestalVersions::Version', @prepared_options[:class_name]
38
+ end
39
+
40
+ should 'default to :delete_all for :dependent' do
41
+ assert_equal :delete_all, @prepared_options[:dependent]
42
+ end
43
+
44
+ should 'force the :as option value to :versioned' do
45
+ assert_equal :versioned, @prepared_options[:as]
46
+ end
47
+
48
+ should 'default to [VestalVersions::Versions] for :extend' do
49
+ assert_equal [VestalVersions::Versions], @prepared_options[:extend]
50
+ end
51
+ end
52
+ end