bitfluent-vestal_versions 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. data/.gitignore +23 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +175 -0
  4. data/Rakefile +45 -0
  5. data/VERSION +1 -0
  6. data/generators/vestal_versions/templates/initializer.rb +9 -0
  7. data/generators/vestal_versions/templates/migration.rb +27 -0
  8. data/generators/vestal_versions/vestal_versions_generator.rb +24 -0
  9. data/init.rb +1 -0
  10. data/lib/vestal_versions.rb +103 -0
  11. data/lib/vestal_versions/changes.rb +125 -0
  12. data/lib/vestal_versions/conditions.rb +69 -0
  13. data/lib/vestal_versions/configuration.rb +40 -0
  14. data/lib/vestal_versions/control.rb +175 -0
  15. data/lib/vestal_versions/creation.rb +100 -0
  16. data/lib/vestal_versions/options.rb +45 -0
  17. data/lib/vestal_versions/reload.rb +23 -0
  18. data/lib/vestal_versions/reset.rb +28 -0
  19. data/lib/vestal_versions/reversion.rb +69 -0
  20. data/lib/vestal_versions/tagging.rb +50 -0
  21. data/lib/vestal_versions/users.rb +57 -0
  22. data/lib/vestal_versions/version.rb +32 -0
  23. data/lib/vestal_versions/versioned.rb +30 -0
  24. data/lib/vestal_versions/versions.rb +74 -0
  25. data/test/changes_test.rb +169 -0
  26. data/test/conditions_test.rb +137 -0
  27. data/test/configuration_test.rb +39 -0
  28. data/test/control_test.rb +152 -0
  29. data/test/creation_test.rb +148 -0
  30. data/test/options_test.rb +52 -0
  31. data/test/reload_test.rb +19 -0
  32. data/test/reset_test.rb +112 -0
  33. data/test/reversion_test.rb +68 -0
  34. data/test/schema.rb +43 -0
  35. data/test/tagging_test.rb +39 -0
  36. data/test/test_helper.rb +12 -0
  37. data/test/users_test.rb +25 -0
  38. data/test/version_test.rb +43 -0
  39. data/test/versioned_test.rb +18 -0
  40. data/test/versions_test.rb +172 -0
  41. data/vestal_versions.gemspec +105 -0
  42. metadata +167 -0
@@ -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_attribute(: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_attribute(: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_attribute(: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_attribute(: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_attribute(: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_attribute(: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
@@ -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_attribute(: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_attribute(:first_name, 'Stephen')
26
+ @user.update_attribute(:last_name, 'Jobs')
27
+ @user.update_attribute(: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_attribute(: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_attribute(:first_name, 'Stephen')
59
+ @user.update_attribute(:last_name, 'Jobs')
60
+ @user.update_attribute(: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_attribute(: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_attribute(:first_name, 'Stephen')
93
+ @user.update_attribute(:last_name, 'Jobs')
94
+ @user.update_attribute(: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_attribute(:last_name, 'Jobs')
108
+ @user.update_attribute(: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_attribute(: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_attribute(:first_name, 'Stephen')
135
+ @user.update_attribute(:last_name, 'Jobs')
136
+ @user.update_attribute(: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,148 @@
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
+ context '(with :initial_version option)' do
16
+ setup do
17
+ User.prepare_versioned_options(:initial_version => true)
18
+ @user = User.create(:name => @name)
19
+ @count = @user.versions.count
20
+ end
21
+ should 'initially equal one' do
22
+ assert_equal 1, @count
23
+ end
24
+ teardown do
25
+ User.prepare_versioned_options(:initial_version => nil)
26
+ end
27
+ end
28
+
29
+ should 'not increase when no changes are made in an update' do
30
+ @user.update_attribute(:name, @name)
31
+ assert_equal @count, @user.versions.count
32
+ end
33
+
34
+ should 'not increase when no changes are made before a save' do
35
+ @user.save
36
+ assert_equal @count, @user.versions.count
37
+ end
38
+
39
+ context 'after an update' do
40
+ setup do
41
+ @user.update_attribute(:last_name, 'Jobs')
42
+ end
43
+
44
+ should 'increase by one' do
45
+ assert_equal @count + 1, @user.versions.count
46
+ end
47
+ end
48
+
49
+ context 'after multiple updates' do
50
+ setup do
51
+ @user.update_attribute(:last_name, 'Jobs')
52
+ @user.update_attribute(:last_name, 'Richert')
53
+ end
54
+
55
+ should 'increase multiple times' do
56
+ assert_operator @count + 1, :<, @user.versions.count
57
+ end
58
+ end
59
+ end
60
+
61
+ context "A created version's changes" do
62
+ setup do
63
+ @user = User.create(:name => 'Steve Richert')
64
+ @user.update_attribute(:last_name, 'Jobs')
65
+ end
66
+
67
+ should 'not contain Rails timestamps' do
68
+ %w(created_at created_on updated_at updated_on).each do |timestamp|
69
+ assert_does_not_contain @user.versions.last.changes.keys, timestamp
70
+ end
71
+ end
72
+
73
+ context '(with :only options)' do
74
+ setup do
75
+ @only = %w(first_name)
76
+ User.prepare_versioned_options(:only => @only)
77
+ @user.update_attribute(:name, 'Steven Tyler')
78
+ end
79
+
80
+ should 'only contain the specified columns' do
81
+ assert_equal @only, @user.versions.last.changes.keys
82
+ end
83
+
84
+ teardown do
85
+ User.prepare_versioned_options(:only => nil)
86
+ end
87
+ end
88
+
89
+ context '(with :except options)' do
90
+ setup do
91
+ @except = %w(first_name)
92
+ User.prepare_versioned_options(:except => @except)
93
+ @user.update_attribute(:name, 'Steven Tyler')
94
+ end
95
+
96
+ should 'not contain the specified columns' do
97
+ @except.each do |column|
98
+ assert_does_not_contain @user.versions.last.changes.keys, column
99
+ end
100
+ end
101
+
102
+ teardown do
103
+ User.prepare_versioned_options(:except => nil)
104
+ end
105
+ end
106
+
107
+ context '(with both :only and :except options)' do
108
+ setup do
109
+ @only = %w(first_name)
110
+ @except = @only
111
+ User.prepare_versioned_options(:only => @only, :except => @except)
112
+ @user.update_attribute(:name, 'Steven Tyler')
113
+ end
114
+
115
+ should 'respect only the :only options' do
116
+ assert_equal @only, @user.versions.last.changes.keys
117
+ end
118
+
119
+ teardown do
120
+ User.prepare_versioned_options(:only => nil, :except => nil)
121
+ end
122
+ end
123
+ end
124
+
125
+ context 'First version' do
126
+ setup do
127
+ @name = 'Steve Richert'
128
+ @user = User.create(:name => @name)
129
+ end
130
+ should 'should be number 2 (after an update)' do
131
+ @user.update_attribute(:last_name, 'Jobs')
132
+ assert_equal 2, @user.versions.first.number
133
+ end
134
+
135
+ context "With :initial_version option" do
136
+ setup do
137
+ User.prepare_versioned_options(:initial_version => true)
138
+ @user = User.create(:name => @name)
139
+ end
140
+ should 'should be number 1' do
141
+ assert_equal 1, @user.versions.first.number
142
+ end
143
+ teardown do
144
+ User.prepare_versioned_options(:initial_version => nil)
145
+ end
146
+ end
147
+ end
148
+ end