paper_trail 4.2.0 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/{CONTRIBUTING.md → .github/CONTRIBUTING.md} +28 -9
- data/.github/ISSUE_TEMPLATE.md +13 -0
- data/.gitignore +2 -1
- data/.rubocop.yml +100 -0
- data/.rubocop_todo.yml +14 -0
- data/.travis.yml +8 -9
- data/Appraisals +41 -0
- data/CHANGELOG.md +49 -9
- data/Gemfile +1 -1
- data/README.md +130 -109
- data/Rakefile +19 -19
- data/doc/bug_report_template.rb +20 -14
- data/gemfiles/ar3.gemfile +10 -53
- data/gemfiles/ar4.gemfile +7 -0
- data/gemfiles/ar5.gemfile +13 -0
- data/lib/generators/paper_trail/install_generator.rb +26 -18
- data/lib/generators/paper_trail/templates/add_object_changes_to_versions.rb +4 -2
- data/lib/generators/paper_trail/templates/add_transaction_id_column_to_versions.rb +2 -0
- data/lib/generators/paper_trail/templates/create_version_associations.rb +9 -4
- data/lib/generators/paper_trail/templates/create_versions.rb +39 -5
- data/lib/paper_trail.rb +169 -146
- data/lib/paper_trail/attributes_serialization.rb +89 -17
- data/lib/paper_trail/cleaner.rb +15 -9
- data/lib/paper_trail/config.rb +28 -11
- data/lib/paper_trail/frameworks/active_record.rb +4 -0
- data/lib/paper_trail/frameworks/active_record/models/paper_trail/version.rb +5 -1
- data/lib/paper_trail/frameworks/active_record/models/paper_trail/version_association.rb +6 -2
- data/lib/paper_trail/frameworks/cucumber.rb +1 -0
- data/lib/paper_trail/frameworks/rails.rb +2 -7
- data/lib/paper_trail/frameworks/rails/controller.rb +29 -9
- data/lib/paper_trail/frameworks/rails/engine.rb +7 -1
- data/lib/paper_trail/frameworks/rspec.rb +5 -5
- data/lib/paper_trail/frameworks/rspec/helpers.rb +3 -1
- data/lib/paper_trail/frameworks/sinatra.rb +6 -4
- data/lib/paper_trail/has_paper_trail.rb +199 -106
- data/lib/paper_trail/record_history.rb +1 -3
- data/lib/paper_trail/reifier.rb +297 -118
- data/lib/paper_trail/serializers/json.rb +3 -3
- data/lib/paper_trail/serializers/yaml.rb +27 -8
- data/lib/paper_trail/version_association_concern.rb +3 -1
- data/lib/paper_trail/version_concern.rb +75 -35
- data/lib/paper_trail/version_number.rb +6 -9
- data/paper_trail.gemspec +44 -51
- data/spec/generators/install_generator_spec.rb +24 -25
- data/spec/generators/paper_trail/templates/create_versions_spec.rb +51 -0
- data/spec/models/animal_spec.rb +12 -12
- data/spec/models/boolit_spec.rb +8 -8
- data/spec/models/callback_modifier_spec.rb +47 -47
- data/spec/models/car_spec.rb +13 -0
- data/spec/models/fluxor_spec.rb +3 -3
- data/spec/models/gadget_spec.rb +19 -19
- data/spec/models/joined_version_spec.rb +3 -3
- data/spec/models/json_version_spec.rb +23 -24
- data/spec/models/kitchen/banana_spec.rb +3 -3
- data/spec/models/not_on_update_spec.rb +7 -4
- data/spec/models/post_with_status_spec.rb +13 -3
- data/spec/models/skipper_spec.rb +10 -10
- data/spec/models/thing_spec.rb +4 -4
- data/spec/models/truck_spec.rb +5 -0
- data/spec/models/vehicle_spec.rb +5 -0
- data/spec/models/version_spec.rb +103 -59
- data/spec/models/widget_spec.rb +82 -52
- data/spec/modules/paper_trail_spec.rb +2 -2
- data/spec/modules/version_concern_spec.rb +11 -12
- data/spec/modules/version_number_spec.rb +2 -4
- data/spec/paper_trail/config_spec.rb +10 -29
- data/spec/paper_trail_spec.rb +16 -14
- data/spec/rails_helper.rb +10 -9
- data/spec/requests/articles_spec.rb +11 -7
- data/spec/spec_helper.rb +41 -22
- data/spec/support/alt_db_init.rb +8 -13
- data/test/custom_json_serializer.rb +3 -3
- data/test/dummy/Rakefile +2 -2
- data/test/dummy/app/controllers/application_controller.rb +21 -8
- data/test/dummy/app/controllers/articles_controller.rb +11 -8
- data/test/dummy/app/controllers/widgets_controller.rb +13 -12
- data/test/dummy/app/models/animal.rb +1 -1
- data/test/dummy/app/models/article.rb +19 -11
- data/test/dummy/app/models/authorship.rb +1 -1
- data/test/dummy/app/models/bar_habtm.rb +4 -0
- data/test/dummy/app/models/book.rb +4 -4
- data/test/dummy/app/models/boolit.rb +1 -1
- data/test/dummy/app/models/callback_modifier.rb +6 -6
- data/test/dummy/app/models/car.rb +3 -0
- data/test/dummy/app/models/chapter.rb +4 -4
- data/test/dummy/app/models/customer.rb +1 -1
- data/test/dummy/app/models/document.rb +2 -2
- data/test/dummy/app/models/editor.rb +1 -1
- data/test/dummy/app/models/foo_habtm.rb +4 -0
- data/test/dummy/app/models/fruit.rb +2 -2
- data/test/dummy/app/models/gadget.rb +1 -1
- data/test/dummy/app/models/kitchen/banana.rb +1 -1
- data/test/dummy/app/models/legacy_widget.rb +2 -2
- data/test/dummy/app/models/line_item.rb +1 -1
- data/test/dummy/app/models/not_on_update.rb +1 -1
- data/test/dummy/app/models/person.rb +6 -6
- data/test/dummy/app/models/post.rb +1 -1
- data/test/dummy/app/models/post_with_status.rb +1 -1
- data/test/dummy/app/models/quotation.rb +1 -1
- data/test/dummy/app/models/section.rb +1 -1
- data/test/dummy/app/models/skipper.rb +2 -2
- data/test/dummy/app/models/song.rb +13 -4
- data/test/dummy/app/models/thing.rb +2 -2
- data/test/dummy/app/models/translation.rb +2 -2
- data/test/dummy/app/models/truck.rb +4 -0
- data/test/dummy/app/models/vehicle.rb +4 -0
- data/test/dummy/app/models/whatchamajigger.rb +1 -1
- data/test/dummy/app/models/widget.rb +7 -6
- data/test/dummy/app/versions/joined_version.rb +4 -3
- data/test/dummy/app/versions/json_version.rb +1 -1
- data/test/dummy/app/versions/kitchen/banana_version.rb +1 -1
- data/test/dummy/app/versions/post_version.rb +2 -2
- data/test/dummy/config.ru +1 -1
- data/test/dummy/config/application.rb +20 -9
- data/test/dummy/config/boot.rb +5 -5
- data/test/dummy/config/environment.rb +1 -1
- data/test/dummy/config/environments/development.rb +4 -3
- data/test/dummy/config/environments/production.rb +3 -2
- data/test/dummy/config/environments/test.rb +15 -5
- data/test/dummy/config/initializers/backtrace_silencers.rb +4 -2
- data/test/dummy/config/initializers/paper_trail.rb +1 -2
- data/test/dummy/config/initializers/secret_token.rb +3 -1
- data/test/dummy/config/initializers/session_store.rb +1 -1
- data/test/dummy/config/routes.rb +2 -2
- data/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb +120 -74
- data/test/dummy/db/schema.rb +29 -6
- data/test/dummy/script/rails +6 -4
- data/test/functional/controller_test.rb +34 -35
- data/test/functional/enabled_for_controller_test.rb +6 -7
- data/test/functional/modular_sinatra_test.rb +43 -38
- data/test/functional/sinatra_test.rb +49 -40
- data/test/functional/thread_safety_test.rb +4 -6
- data/test/paper_trail_test.rb +15 -14
- data/test/test_helper.rb +68 -44
- data/test/time_travel_helper.rb +1 -15
- data/test/unit/associations_test.rb +517 -251
- data/test/unit/cleaner_test.rb +66 -60
- data/test/unit/inheritance_column_test.rb +17 -17
- data/test/unit/model_test.rb +611 -504
- data/test/unit/protected_attrs_test.rb +16 -12
- data/test/unit/serializer_test.rb +44 -43
- data/test/unit/serializers/json_test.rb +17 -18
- data/test/unit/serializers/mixin_json_test.rb +15 -14
- data/test/unit/serializers/mixin_yaml_test.rb +20 -16
- data/test/unit/serializers/yaml_test.rb +12 -13
- data/test/unit/timestamp_test.rb +10 -12
- data/test/unit/version_test.rb +7 -7
- metadata +92 -40
data/test/unit/cleaner_test.rb
CHANGED
@@ -1,71 +1,71 @@
|
|
1
|
-
require
|
1
|
+
require "test_helper"
|
2
2
|
|
3
3
|
class PaperTrailCleanerTest < ActiveSupport::TestCase
|
4
|
-
|
5
4
|
def populate_db!
|
6
5
|
@animals = [@animal = Animal.new, @dog = Dog.new, @cat = Cat.new]
|
7
6
|
@animals.each do |animal|
|
8
|
-
3.times { animal.update_attribute(:name,
|
7
|
+
3.times { animal.update_attribute(:name, FFaker::Name.name) }
|
9
8
|
end
|
10
9
|
end
|
11
10
|
|
12
|
-
context
|
13
|
-
setup {
|
11
|
+
context "`clean_versions!` method" do
|
12
|
+
setup { populate_db! }
|
14
13
|
|
15
|
-
should
|
14
|
+
should "Baseline" do
|
16
15
|
assert_equal 9, PaperTrail::Version.count
|
17
16
|
@animals.each { |animal| assert_equal 3, animal.versions.size }
|
18
17
|
end
|
19
18
|
|
20
|
-
should
|
19
|
+
should "be extended by `PaperTrail` module" do
|
21
20
|
assert_respond_to PaperTrail, :clean_versions!
|
22
21
|
end
|
23
22
|
|
24
|
-
context
|
25
|
-
should
|
23
|
+
context "No options provided" do
|
24
|
+
should "removes extra versions for each item" do
|
26
25
|
PaperTrail.clean_versions!
|
27
26
|
assert_equal 3, PaperTrail::Version.count
|
28
27
|
@animals.each { |animal| assert_equal 1, animal.versions.size }
|
29
28
|
end
|
30
29
|
|
31
|
-
should
|
32
|
-
|
30
|
+
should "removes the earliest version(s)" do
|
31
|
+
before = @animals.map { |animal| animal.versions.last.reify.name }
|
33
32
|
PaperTrail.clean_versions!
|
34
|
-
|
33
|
+
after = @animals.map { |animal| animal.versions.last.reify.name }
|
34
|
+
assert_equal before, after
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
-
context
|
39
|
-
should
|
40
|
-
PaperTrail.clean_versions!(:
|
38
|
+
context "`:keeping` option" do
|
39
|
+
should "modifies the number of versions ommitted from destruction" do
|
40
|
+
PaperTrail.clean_versions!(keeping: 2)
|
41
41
|
assert_equal 6, PaperTrail::Version.all.count
|
42
42
|
@animals.each { |animal| assert_equal 2, animal.versions.size }
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
-
context
|
46
|
+
context "`:date` option" do
|
47
47
|
setup do
|
48
48
|
@animal.versions.each { |ver| ver.update_attribute(:created_at, ver.created_at - 1.day) }
|
49
49
|
@date = @animal.versions.first.created_at.to_date
|
50
|
-
@animal.update_attribute(:name,
|
50
|
+
@animal.update_attribute(:name, FFaker::Name.name)
|
51
51
|
end
|
52
52
|
|
53
|
-
should
|
53
|
+
should "restrict the versions destroyed to those that were created on the date provided" do
|
54
54
|
assert_equal 10, PaperTrail::Version.count
|
55
55
|
assert_equal 4, @animal.versions.size
|
56
56
|
assert_equal 3, @animal.versions_between(@date, @date + 1.day).size
|
57
|
-
PaperTrail.clean_versions!(:
|
57
|
+
PaperTrail.clean_versions!(date: @date)
|
58
58
|
assert_equal 8, PaperTrail::Version.count
|
59
|
-
assert_equal 2, @animal.versions
|
59
|
+
assert_equal 2, @animal.versions.reload.size
|
60
60
|
assert_equal @date, @animal.versions.first.created_at.to_date
|
61
61
|
assert_not_same @date, @animal.versions.last.created_at.to_date
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
|
-
context
|
66
|
-
context
|
67
|
-
should
|
68
|
-
PaperTrail.clean_versions!(:
|
65
|
+
context "`:item_id` option" do
|
66
|
+
context "single ID received" do
|
67
|
+
should "restrict the versions destroyed to the versions for the Item with that ID" do
|
68
|
+
PaperTrail.clean_versions!(item_id: @animal.id)
|
69
69
|
assert_equal 1, @animal.versions.size
|
70
70
|
assert_equal 7, PaperTrail::Version.count
|
71
71
|
end
|
@@ -73,7 +73,7 @@ class PaperTrailCleanerTest < ActiveSupport::TestCase
|
|
73
73
|
|
74
74
|
context "collection of ID's received" do
|
75
75
|
should "restrict the versions destroyed to the versions for the Item with those ID's" do
|
76
|
-
PaperTrail.clean_versions!(:
|
76
|
+
PaperTrail.clean_versions!(item_id: [@animal.id, @dog.id])
|
77
77
|
assert_equal 1, @animal.versions.size
|
78
78
|
assert_equal 1, @dog.versions.size
|
79
79
|
assert_equal 5, PaperTrail::Version.count
|
@@ -81,72 +81,78 @@ class PaperTrailCleanerTest < ActiveSupport::TestCase
|
|
81
81
|
end
|
82
82
|
end
|
83
83
|
|
84
|
-
context
|
85
|
-
context
|
84
|
+
context "options combinations" do # additional tests to cover combinations of options
|
85
|
+
context "`:date`" do
|
86
86
|
setup do
|
87
87
|
[@animal, @dog].each do |animal|
|
88
88
|
animal.versions.each { |ver| ver.update_attribute(:created_at, ver.created_at - 1.day) }
|
89
|
-
animal.update_attribute(:name,
|
89
|
+
animal.update_attribute(:name, FFaker::Name.name)
|
90
90
|
end
|
91
91
|
@date = @animal.versions.first.created_at.to_date
|
92
92
|
end
|
93
93
|
|
94
|
-
should
|
94
|
+
should "Baseline" do
|
95
95
|
assert_equal 11, PaperTrail::Version.count
|
96
96
|
[@animal, @dog].each do |animal|
|
97
97
|
assert_equal 4, animal.versions.size
|
98
|
-
assert_equal 3, animal.versions.between(@date, @date+1.day).size
|
98
|
+
assert_equal 3, animal.versions.between(@date, @date + 1.day).size
|
99
99
|
end
|
100
100
|
end
|
101
101
|
|
102
|
-
context
|
103
|
-
should
|
104
|
-
PaperTrail.clean_versions!(:
|
102
|
+
context "and `:keeping`" do
|
103
|
+
should "restrict cleaning properly" do
|
104
|
+
PaperTrail.clean_versions!(date: @date, keeping: 2)
|
105
105
|
[@animal, @dog].each do |animal|
|
106
|
-
|
106
|
+
# reload the association to pick up the destructions made by the `Cleaner`
|
107
|
+
animal.versions.reload
|
107
108
|
assert_equal 3, animal.versions.size
|
108
|
-
assert_equal 2, animal.versions.between(@date, @date+1.day).size
|
109
|
+
assert_equal 2, animal.versions.between(@date, @date + 1.day).size
|
109
110
|
end
|
110
|
-
|
111
|
+
# ensure that the versions for the `@cat` instance wasn't touched
|
112
|
+
assert_equal 9, PaperTrail::Version.count
|
111
113
|
end
|
112
114
|
end
|
113
115
|
|
114
|
-
context
|
115
|
-
should
|
116
|
-
PaperTrail.clean_versions!(:
|
117
|
-
|
116
|
+
context "and `:item_id`" do
|
117
|
+
should "restrict cleaning properly" do
|
118
|
+
PaperTrail.clean_versions!(date: @date, item_id: @dog.id)
|
119
|
+
# reload the association to pick up the destructions made by the `Cleaner`
|
120
|
+
@dog.versions.reload
|
118
121
|
assert_equal 2, @dog.versions.size
|
119
|
-
assert_equal 1, @dog.versions.between(@date, @date+1.day).size
|
120
|
-
|
122
|
+
assert_equal 1, @dog.versions.between(@date, @date + 1.day).size
|
123
|
+
# ensure the versions for other animals besides `@animal` weren't touched
|
124
|
+
assert_equal 9, PaperTrail::Version.count
|
121
125
|
end
|
122
126
|
end
|
123
127
|
|
124
|
-
context
|
125
|
-
should
|
126
|
-
PaperTrail.clean_versions!(:
|
127
|
-
|
128
|
+
context ", `:item_id`, and `:keeping`" do
|
129
|
+
should "restrict cleaning properly" do
|
130
|
+
PaperTrail.clean_versions!(date: @date, item_id: @dog.id, keeping: 2)
|
131
|
+
# reload the association to pick up the destructions made by the `Cleaner`
|
132
|
+
@dog.versions.reload
|
128
133
|
assert_equal 3, @dog.versions.size
|
129
|
-
assert_equal 2, @dog.versions.between(@date, @date+1.day).size
|
130
|
-
|
134
|
+
assert_equal 2, @dog.versions.between(@date, @date + 1.day).size
|
135
|
+
# ensure the versions for other animals besides `@animal` weren't touched
|
136
|
+
assert_equal 10, PaperTrail::Version.count
|
131
137
|
end
|
132
138
|
end
|
133
139
|
end
|
134
140
|
|
135
|
-
context
|
136
|
-
should
|
137
|
-
PaperTrail.clean_versions!(:
|
141
|
+
context "`:keeping` and `:item_id`" do
|
142
|
+
should "restrict cleaning properly" do
|
143
|
+
PaperTrail.clean_versions!(keeping: 2, item_id: @animal.id)
|
138
144
|
assert_equal 2, @animal.versions.size
|
139
|
-
|
145
|
+
# ensure the versions for other animals besides `@animal` weren't touched
|
146
|
+
assert_equal 8, PaperTrail::Version.count
|
140
147
|
end
|
141
148
|
end
|
142
149
|
end
|
143
|
-
|
144
150
|
end # clean_versions! method
|
145
151
|
|
146
152
|
context "Custom timestamp field" do
|
147
153
|
setup do
|
148
154
|
change_schema
|
149
|
-
|
155
|
+
populate_db!
|
150
156
|
# now mess with the timestamps
|
151
157
|
@animals.each do |animal|
|
152
158
|
animal.versions.reverse.each_with_index do |version, index|
|
@@ -154,7 +160,7 @@ class PaperTrailCleanerTest < ActiveSupport::TestCase
|
|
154
160
|
end
|
155
161
|
end
|
156
162
|
PaperTrail.timestamp_field = :custom_created_at
|
157
|
-
@animals.map { |a| a.versions
|
163
|
+
@animals.map { |a| a.versions.reload } # reload the `versions` association for each animal
|
158
164
|
end
|
159
165
|
|
160
166
|
teardown do
|
@@ -162,18 +168,18 @@ class PaperTrailCleanerTest < ActiveSupport::TestCase
|
|
162
168
|
restore_schema
|
163
169
|
end
|
164
170
|
|
165
|
-
should
|
171
|
+
should "Baseline" do
|
166
172
|
assert_equal 9, PaperTrail::Version.count
|
167
173
|
@animals.each do |animal|
|
168
174
|
assert_equal 3, animal.versions.size
|
169
|
-
animal.versions.each_cons(2) do |a,b|
|
170
|
-
a.created_at.to_date
|
171
|
-
a.custom_created_at.to_date
|
175
|
+
animal.versions.each_cons(2) do |a, b|
|
176
|
+
assert_equal a.created_at.to_date, b.created_at.to_date
|
177
|
+
assert_not_equal a.custom_created_at.to_date, b.custom_created_at.to_date
|
172
178
|
end
|
173
179
|
end
|
174
180
|
end
|
175
181
|
|
176
|
-
should
|
182
|
+
should "group by `PaperTrail.timestamp_field` when seperating the versions by date to clean" do
|
177
183
|
assert_equal 9, PaperTrail::Version.count
|
178
184
|
PaperTrail.clean_versions!
|
179
185
|
assert_equal 9, PaperTrail::Version.count
|
@@ -1,43 +1,43 @@
|
|
1
|
-
require
|
1
|
+
require "test_helper"
|
2
2
|
|
3
3
|
class InheritanceColumnTest < ActiveSupport::TestCase
|
4
|
-
context
|
4
|
+
context "STI models" do
|
5
5
|
setup do
|
6
|
-
@animal = Animal.create :
|
7
|
-
@animal.update_attributes :
|
8
|
-
@animal.update_attributes :
|
6
|
+
@animal = Animal.create name: "Animal"
|
7
|
+
@animal.update_attributes name: "Animal from the Muppets"
|
8
|
+
@animal.update_attributes name: "Animal Muppet"
|
9
9
|
@animal.destroy
|
10
10
|
|
11
|
-
@dog = Dog.create :
|
12
|
-
@dog.update_attributes :
|
13
|
-
@dog.update_attributes :
|
11
|
+
@dog = Dog.create name: "Snoopy"
|
12
|
+
@dog.update_attributes name: "Scooby"
|
13
|
+
@dog.update_attributes name: "Scooby Doo"
|
14
14
|
@dog.destroy
|
15
15
|
|
16
|
-
@cat = Cat.create :
|
17
|
-
@cat.update_attributes :
|
18
|
-
@cat.update_attributes :
|
16
|
+
@cat = Cat.create name: "Garfield"
|
17
|
+
@cat.update_attributes name: "Garfield (I hate Mondays)"
|
18
|
+
@cat.update_attributes name: "Garfield The Cat"
|
19
19
|
@cat.destroy
|
20
20
|
end
|
21
21
|
|
22
|
-
should
|
22
|
+
should "work with custom STI inheritance column" do
|
23
23
|
assert_equal 12, PaperTrail::Version.count
|
24
24
|
assert_equal 4, @animal.versions.count
|
25
25
|
assert_nil @animal.versions.first.reify
|
26
|
-
@animal.versions[1..-1].each { |v| assert_equal
|
26
|
+
@animal.versions[1..-1].each { |v| assert_equal "Animal", v.reify.class.name }
|
27
27
|
|
28
28
|
# For some reason `@dog.versions` doesn't include the final `destroy` version.
|
29
29
|
# Neither do `@dog.versions.scoped` nor `@dog.versions(true)` nor `@dog.versions.reload`.
|
30
|
-
dog_versions = PaperTrail::Version.where(:
|
30
|
+
dog_versions = PaperTrail::Version.where(item_id: @dog.id).
|
31
31
|
order(PaperTrail.timestamp_field)
|
32
32
|
assert_equal 4, dog_versions.count
|
33
33
|
assert_nil dog_versions.first.reify
|
34
|
-
assert_equal %w
|
34
|
+
assert_equal %w(NilClass Dog Dog Dog), dog_versions.map { |v| v.reify.class.name }
|
35
35
|
|
36
|
-
cat_versions = PaperTrail::Version.where(:
|
36
|
+
cat_versions = PaperTrail::Version.where(item_id: @cat.id).
|
37
37
|
order(PaperTrail.timestamp_field)
|
38
38
|
assert_equal 4, cat_versions.count
|
39
39
|
assert_nil cat_versions.first.reify
|
40
|
-
assert_equal %w
|
40
|
+
assert_equal %w(NilClass Cat Cat Cat), cat_versions.map { |v| v.reify.class.name }
|
41
41
|
end
|
42
42
|
end
|
43
43
|
end
|
data/test/unit/model_test.rb
CHANGED
@@ -1,109 +1,152 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "test_helper"
|
2
|
+
require "time_travel_helper"
|
3
3
|
|
4
4
|
class HasPaperTrailModelTest < ActiveSupport::TestCase
|
5
|
-
|
6
5
|
context "A record with defined 'only' and 'ignore' attributes" do
|
7
6
|
setup { @article = Article.create }
|
8
|
-
should 'creation should change the number of versions' do assert_equal(1, PaperTrail::Version.count) end
|
9
7
|
|
10
|
-
|
11
|
-
|
12
|
-
should 'not change the number of versions' do assert_equal(1, PaperTrail::Version.count) end
|
8
|
+
should "creation should change the number of versions" do
|
9
|
+
assert_equal(1, PaperTrail::Version.count)
|
13
10
|
end
|
14
11
|
|
15
|
-
context
|
16
|
-
|
17
|
-
|
12
|
+
context "which updates an ignored column" do
|
13
|
+
should "not change the number of versions" do
|
14
|
+
@article.update_attributes title: "My first title"
|
15
|
+
assert_equal(1, PaperTrail::Version.count)
|
16
|
+
end
|
18
17
|
end
|
19
18
|
|
20
|
-
context
|
21
|
-
|
22
|
-
|
19
|
+
context "which updates an ignored column with truly Proc" do
|
20
|
+
should "not change the number of versions" do
|
21
|
+
@article.update_attributes abstract: "ignore abstract"
|
22
|
+
assert_equal(1, PaperTrail::Version.count)
|
23
|
+
end
|
23
24
|
end
|
24
25
|
|
25
|
-
context
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
26
|
+
context "which updates an ignored column with falsy Proc" do
|
27
|
+
should "change the number of versions" do
|
28
|
+
@article.update_attributes abstract: "do not ignore abstract!"
|
29
|
+
assert_equal(2, PaperTrail::Version.count)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "which updates an ignored column, ignored with truly Proc and a selected column" do
|
34
|
+
setup do
|
35
|
+
@article.update_attributes(
|
36
|
+
title: "My first title",
|
37
|
+
content: "Some text here.",
|
38
|
+
abstract: "ignore abstract"
|
39
|
+
)
|
40
|
+
end
|
41
|
+
|
42
|
+
should "change the number of versions" do
|
43
|
+
assert_equal(2, PaperTrail::Version.count)
|
44
|
+
end
|
31
45
|
|
32
46
|
should "show the new version in the model's `versions` association" do
|
33
47
|
assert_equal(2, @article.versions.size)
|
34
48
|
end
|
35
49
|
|
36
|
-
should
|
37
|
-
|
50
|
+
should "have stored only non-ignored attributes" do
|
51
|
+
expected = { "content" => [nil, "Some text here."] }
|
52
|
+
assert_equal expected, @article.versions.last.changeset
|
38
53
|
end
|
39
54
|
end
|
40
55
|
|
41
|
-
context
|
42
|
-
setup
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
56
|
+
context "which updates an ignored column, ignored with falsy Proc and a selected column" do
|
57
|
+
setup do
|
58
|
+
@article.update_attributes(
|
59
|
+
title: "My first title",
|
60
|
+
content: "Some text here.",
|
61
|
+
abstract: "do not ignore abstract"
|
62
|
+
)
|
63
|
+
end
|
64
|
+
|
65
|
+
should "change the number of versions" do
|
66
|
+
assert_equal(2, PaperTrail::Version.count)
|
67
|
+
end
|
47
68
|
|
48
69
|
should "show the new version in the model's `versions` association" do
|
49
70
|
assert_equal(2, @article.versions.size)
|
50
71
|
end
|
51
72
|
|
52
|
-
should
|
53
|
-
|
73
|
+
should "have stored only non-ignored attributes" do
|
74
|
+
expected = {
|
75
|
+
"content" => [nil, "Some text here."],
|
76
|
+
"abstract" => [nil, "do not ignore abstract"]
|
77
|
+
}
|
78
|
+
assert_equal expected, @article.versions.last.changeset
|
54
79
|
end
|
55
80
|
end
|
56
81
|
|
57
|
-
context
|
58
|
-
setup { @article.update_attributes :
|
59
|
-
should
|
82
|
+
context "which updates a selected column" do
|
83
|
+
setup { @article.update_attributes content: "Some text here." }
|
84
|
+
should "change the number of versions" do
|
85
|
+
assert_equal(2, PaperTrail::Version.count)
|
86
|
+
end
|
60
87
|
|
61
88
|
should "show the new version in the model's `versions` association" do
|
62
89
|
assert_equal(2, @article.versions.size)
|
63
90
|
end
|
64
91
|
end
|
65
92
|
|
66
|
-
context
|
67
|
-
|
68
|
-
|
93
|
+
context "which updates a non-ignored and non-selected column" do
|
94
|
+
should "not change the number of versions" do
|
95
|
+
@article.update_attributes abstract: "Other abstract"
|
96
|
+
assert_equal(1, PaperTrail::Version.count)
|
97
|
+
end
|
69
98
|
end
|
70
99
|
|
71
|
-
context
|
72
|
-
|
73
|
-
|
100
|
+
context "which updates a skipped column" do
|
101
|
+
should "not change the number of versions" do
|
102
|
+
@article.update_attributes file_upload: "Your data goes here"
|
103
|
+
assert_equal(1, PaperTrail::Version.count)
|
104
|
+
end
|
74
105
|
end
|
75
106
|
|
76
|
-
context
|
77
|
-
setup
|
78
|
-
|
107
|
+
context "which updates a skipped column and a selected column" do
|
108
|
+
setup do
|
109
|
+
@article.update_attributes(
|
110
|
+
file_upload: "Your data goes here",
|
111
|
+
content: "Some text here."
|
112
|
+
)
|
113
|
+
end
|
114
|
+
|
115
|
+
should "change the number of versions" do
|
116
|
+
assert_equal(2, PaperTrail::Version.count)
|
117
|
+
end
|
79
118
|
|
80
119
|
should "show the new version in the model's `versions` association" do
|
81
120
|
assert_equal(2, @article.versions.size)
|
82
121
|
end
|
83
122
|
|
84
|
-
should
|
85
|
-
assert_equal ({
|
123
|
+
should "have stored only non-skipped attributes" do
|
124
|
+
assert_equal ({ "content" => [nil, "Some text here."] }),
|
125
|
+
@article.versions.last.changeset
|
86
126
|
end
|
87
127
|
|
88
|
-
context
|
128
|
+
context "and when updated again" do
|
89
129
|
setup do
|
90
|
-
@article.update_attributes
|
130
|
+
@article.update_attributes(
|
131
|
+
file_upload: "More data goes here",
|
132
|
+
content: "More text here."
|
133
|
+
)
|
91
134
|
@old_article = @article.versions.last
|
92
135
|
end
|
93
136
|
|
94
|
-
should
|
95
|
-
assert_equal nil, PaperTrail.serializer.load(@old_article.object)[
|
137
|
+
should "have removed the skipped attributes when saving the previous version" do
|
138
|
+
assert_equal nil, PaperTrail.serializer.load(@old_article.object)["file_upload"]
|
96
139
|
end
|
97
140
|
|
98
|
-
should
|
99
|
-
assert_equal
|
141
|
+
should "have kept the non-skipped attributes in the previous version" do
|
142
|
+
assert_equal "Some text here.", PaperTrail.serializer.load(@old_article.object)["content"]
|
100
143
|
end
|
101
144
|
end
|
102
145
|
end
|
103
146
|
|
104
|
-
context
|
147
|
+
context "which gets destroyed" do
|
105
148
|
setup { @article.destroy }
|
106
|
-
should
|
149
|
+
should "change the number of versions" do assert_equal(2, PaperTrail::Version.count) end
|
107
150
|
|
108
151
|
should "show the new version in the model's `versions` association" do
|
109
152
|
assert_equal(2, @article.versions.size)
|
@@ -114,64 +157,74 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
114
157
|
context "A record with defined 'ignore' attribute" do
|
115
158
|
setup { @legacy_widget = LegacyWidget.create }
|
116
159
|
|
117
|
-
context
|
118
|
-
setup { @legacy_widget.update_attributes :
|
119
|
-
should
|
160
|
+
context "which updates an ignored column" do
|
161
|
+
setup { @legacy_widget.update_attributes version: 1 }
|
162
|
+
should "not change the number of versions" do assert_equal(1, PaperTrail::Version.count) end
|
120
163
|
end
|
121
164
|
end
|
122
165
|
|
123
166
|
context 'A record with defined "if" and "unless" attributes' do
|
124
|
-
setup { @translation = Translation.new :
|
167
|
+
setup { @translation = Translation.new headline: "Headline" }
|
125
168
|
|
126
|
-
context
|
169
|
+
context "for non-US translations" do
|
127
170
|
setup { @translation.save }
|
128
|
-
should
|
171
|
+
should "not change the number of versions" do assert_equal(0, PaperTrail::Version.count) end
|
129
172
|
|
130
|
-
context
|
131
|
-
setup { @translation.update_attributes :
|
132
|
-
should
|
173
|
+
context "after update" do
|
174
|
+
setup { @translation.update_attributes content: "Content" }
|
175
|
+
should "not change the number of versions" do assert_equal(0, PaperTrail::Version.count) end
|
133
176
|
end
|
134
177
|
|
135
|
-
context
|
178
|
+
context "after destroy" do
|
136
179
|
setup { @translation.destroy }
|
137
|
-
should
|
180
|
+
should "not change the number of versions" do assert_equal(0, PaperTrail::Version.count) end
|
138
181
|
end
|
139
182
|
end
|
140
183
|
|
141
|
-
context
|
184
|
+
context "for US translations" do
|
142
185
|
setup { @translation.language_code = "US" }
|
143
186
|
|
144
|
-
context
|
187
|
+
context "that are drafts" do
|
145
188
|
setup do
|
146
|
-
@translation.type =
|
189
|
+
@translation.type = "DRAFT"
|
147
190
|
@translation.save
|
148
191
|
end
|
149
192
|
|
150
|
-
should
|
193
|
+
should "not change the number of versions" do
|
194
|
+
assert_equal(0, PaperTrail::Version.count)
|
195
|
+
end
|
151
196
|
|
152
|
-
context
|
153
|
-
setup { @translation.update_attributes :
|
154
|
-
should
|
197
|
+
context "after update" do
|
198
|
+
setup { @translation.update_attributes content: "Content" }
|
199
|
+
should "not change the number of versions" do
|
200
|
+
assert_equal(0, PaperTrail::Version.count)
|
201
|
+
end
|
155
202
|
end
|
156
203
|
end
|
157
204
|
|
158
|
-
context
|
205
|
+
context "that are not drafts" do
|
159
206
|
setup { @translation.save }
|
160
207
|
|
161
|
-
should
|
208
|
+
should "change the number of versions" do
|
209
|
+
assert_equal(1, PaperTrail::Version.count)
|
210
|
+
end
|
162
211
|
|
163
|
-
context
|
164
|
-
setup { @translation.update_attributes :
|
165
|
-
should
|
212
|
+
context "after update" do
|
213
|
+
setup { @translation.update_attributes content: "Content" }
|
214
|
+
should "change the number of versions" do
|
215
|
+
assert_equal(2, PaperTrail::Version.count)
|
216
|
+
end
|
166
217
|
|
167
218
|
should "show the new version in the model's `versions` association" do
|
168
219
|
assert_equal(2, @translation.versions.size)
|
169
220
|
end
|
170
221
|
end
|
171
222
|
|
172
|
-
context
|
223
|
+
context "after destroy" do
|
173
224
|
setup { @translation.destroy }
|
174
|
-
should
|
225
|
+
should "change the number of versions" do
|
226
|
+
assert_equal(2, PaperTrail::Version.count)
|
227
|
+
end
|
175
228
|
|
176
229
|
should "show the new version in the model's `versions` association" do
|
177
230
|
assert_equal(2, @translation.versions.size)
|
@@ -181,34 +234,34 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
181
234
|
end
|
182
235
|
end
|
183
236
|
|
184
|
-
context
|
237
|
+
context "A new record" do
|
185
238
|
setup { @widget = Widget.new }
|
186
239
|
|
187
|
-
should
|
240
|
+
should "not have any previous versions" do
|
188
241
|
assert_equal [], @widget.versions
|
189
242
|
end
|
190
243
|
|
191
|
-
should
|
244
|
+
should "be live" do
|
192
245
|
assert @widget.live?
|
193
246
|
end
|
194
247
|
|
195
|
-
context
|
196
|
-
setup { @widget.update_attributes :
|
248
|
+
context "which is then created" do
|
249
|
+
setup { @widget.update_attributes name: "Henry", created_at: Time.now - 1.day }
|
197
250
|
|
198
|
-
should
|
251
|
+
should "have one previous version" do
|
199
252
|
assert_equal 1, @widget.versions.length
|
200
253
|
end
|
201
254
|
|
202
|
-
should
|
255
|
+
should "be nil in its previous version" do
|
203
256
|
assert_nil @widget.versions.first.object
|
204
257
|
assert_nil @widget.versions.first.reify
|
205
258
|
end
|
206
259
|
|
207
|
-
should
|
208
|
-
assert_match
|
260
|
+
should "record the correct event" do
|
261
|
+
assert_match(/create/i, @widget.versions.first.event)
|
209
262
|
end
|
210
263
|
|
211
|
-
should
|
264
|
+
should "be live" do
|
212
265
|
assert @widget.live?
|
213
266
|
end
|
214
267
|
|
@@ -216,114 +269,107 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
216
269
|
assert_equal @widget.updated_at.to_i, @widget.versions.first.created_at.to_i
|
217
270
|
end
|
218
271
|
|
219
|
-
should
|
220
|
-
|
221
|
-
#
|
222
|
-
#
|
272
|
+
should "have changes" do
|
273
|
+
# TODO: Postgres does not appear to pass back
|
274
|
+
# ActiveSupport::TimeWithZone, so choosing the lowest common denominator
|
275
|
+
# to test.
|
223
276
|
|
224
277
|
changes = {
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
278
|
+
"name" => [nil, "Henry"],
|
279
|
+
"created_at" => [nil, @widget.created_at.to_time.utc],
|
280
|
+
"updated_at" => [nil, @widget.updated_at.to_time.utc],
|
281
|
+
"id" => [nil, @widget.id]
|
229
282
|
}
|
230
283
|
|
231
|
-
assert_kind_of Time, @widget.versions.last.changeset[
|
284
|
+
assert_kind_of Time, @widget.versions.last.changeset["updated_at"][1]
|
232
285
|
assert_changes_equal changes, @widget.versions.last.changeset
|
233
286
|
end
|
234
287
|
|
235
|
-
context
|
288
|
+
context "and then updated without any changes" do
|
236
289
|
setup { @widget.touch }
|
237
290
|
|
238
|
-
should
|
291
|
+
should "not have a new version" do
|
239
292
|
assert_equal 1, @widget.versions.length
|
240
293
|
end
|
241
294
|
end
|
242
295
|
|
296
|
+
context "and then updated with changes" do
|
297
|
+
setup { @widget.update_attributes name: "Harry" }
|
243
298
|
|
244
|
-
|
245
|
-
setup { @widget.update_attributes :name => 'Harry' }
|
246
|
-
|
247
|
-
should 'have two previous versions' do
|
299
|
+
should "have two previous versions" do
|
248
300
|
assert_equal 2, @widget.versions.length
|
249
301
|
end
|
250
302
|
|
251
|
-
should
|
252
|
-
assert_equal
|
303
|
+
should "be available in its previous version" do
|
304
|
+
assert_equal "Harry", @widget.name
|
253
305
|
assert_not_nil @widget.versions.last.object
|
254
306
|
widget = @widget.versions.last.reify
|
255
|
-
assert_equal
|
256
|
-
assert_equal
|
307
|
+
assert_equal "Henry", widget.name
|
308
|
+
assert_equal "Harry", @widget.name
|
257
309
|
end
|
258
310
|
|
259
|
-
should
|
311
|
+
should "have the same ID in its previous version" do
|
260
312
|
assert_equal @widget.id, @widget.versions.last.reify.id
|
261
313
|
end
|
262
314
|
|
263
|
-
should
|
264
|
-
assert_match
|
315
|
+
should "record the correct event" do
|
316
|
+
assert_match(/update/i, @widget.versions.last.event)
|
265
317
|
end
|
266
318
|
|
267
|
-
should
|
319
|
+
should "have versions that are not live" do
|
268
320
|
assert @widget.versions.map(&:reify).compact.all? { |w| !w.live? }
|
269
321
|
end
|
270
322
|
|
271
|
-
should
|
323
|
+
should "have stored changes" do
|
272
324
|
# Behavior for ActiveRecord 4 is different than ActiveRecord 3;
|
273
|
-
# AR4 includes the `updated_at` column in changes for updates, which
|
274
|
-
|
275
|
-
|
325
|
+
# AR4 includes the `updated_at` column in changes for updates, which
|
326
|
+
# is why we reject it from the right side of this assertion.
|
327
|
+
last_obj_changes = @widget.versions.last.object_changes
|
328
|
+
actual = PaperTrail.serializer.load(last_obj_changes).reject { |k, _v|
|
329
|
+
k.to_sym == :updated_at
|
330
|
+
}
|
331
|
+
assert_equal ({ "name" => %w(Henry Harry) }), actual
|
332
|
+
actual = @widget.versions.last.changeset.reject { |k, _v|
|
333
|
+
k.to_sym == :updated_at
|
334
|
+
}
|
335
|
+
assert_equal ({ "name" => %w(Henry Harry) }), actual
|
276
336
|
end
|
277
337
|
|
278
|
-
should
|
279
|
-
assert_equal
|
280
|
-
assert_equal
|
281
|
-
end
|
282
|
-
|
283
|
-
if defined?(ActiveRecord::IdentityMap) && ActiveRecord::IdentityMap.respond_to?(:without)
|
284
|
-
should 'not clobber the IdentityMap when reifying' do
|
285
|
-
module ActiveRecord::IdentityMap
|
286
|
-
class << self
|
287
|
-
alias :__without :without
|
288
|
-
def without(&block)
|
289
|
-
@unclobbered = true
|
290
|
-
__without(&block)
|
291
|
-
end
|
292
|
-
end
|
293
|
-
end
|
294
|
-
|
295
|
-
@widget.versions.last.reify
|
296
|
-
assert ActiveRecord::IdentityMap.instance_variable_get("@unclobbered")
|
297
|
-
end
|
338
|
+
should "return changes with indifferent access" do
|
339
|
+
assert_equal %w(Henry Harry), @widget.versions.last.changeset[:name]
|
340
|
+
assert_equal %w(Henry Harry), @widget.versions.last.changeset["name"]
|
298
341
|
end
|
299
342
|
|
300
|
-
context
|
343
|
+
context "and has one associated object" do
|
301
344
|
setup do
|
302
|
-
@wotsit = @widget.create_wotsit :
|
345
|
+
@wotsit = @widget.create_wotsit name: "John"
|
303
346
|
end
|
304
347
|
|
305
|
-
should
|
348
|
+
should "not copy the has_one association by default when reifying" do
|
306
349
|
reified_widget = @widget.versions.last.reify
|
307
|
-
|
308
|
-
assert_equal @wotsit,
|
350
|
+
# association hasn't been affected by reifying
|
351
|
+
assert_equal @wotsit, reified_widget.wotsit
|
352
|
+
# confirm that the association is correct
|
353
|
+
assert_equal @wotsit, @widget.reload.wotsit
|
309
354
|
end
|
310
355
|
|
311
|
-
should
|
312
|
-
reified_widget = @widget.versions.last.reify(:
|
313
|
-
|
314
|
-
|
356
|
+
should "copy the has_one association when reifying with :has_one => true" do
|
357
|
+
reified_widget = @widget.versions.last.reify(has_one: true)
|
358
|
+
# wotsit wasn't there at the last version
|
359
|
+
assert_nil reified_widget.wotsit
|
360
|
+
# wotsit should still exist on live object
|
361
|
+
assert_equal @wotsit, @widget.reload.wotsit
|
315
362
|
end
|
316
363
|
end
|
317
364
|
|
318
|
-
|
319
|
-
context 'and has many associated objects' do
|
365
|
+
context "and has many associated objects" do
|
320
366
|
setup do
|
321
|
-
@f0 = @widget.fluxors.create :
|
322
|
-
@f1 = @widget.fluxors.create :
|
367
|
+
@f0 = @widget.fluxors.create name: "f-zero"
|
368
|
+
@f1 = @widget.fluxors.create name: "f-one"
|
323
369
|
@reified_widget = @widget.versions.last.reify
|
324
370
|
end
|
325
371
|
|
326
|
-
should
|
372
|
+
should "copy the has_many associations when reifying" do
|
327
373
|
assert_equal @widget.fluxors.length, @reified_widget.fluxors.length
|
328
374
|
assert_same_elements @widget.fluxors, @reified_widget.fluxors
|
329
375
|
|
@@ -332,14 +378,14 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
332
378
|
end
|
333
379
|
end
|
334
380
|
|
335
|
-
context
|
381
|
+
context "and has many associated polymorphic objects" do
|
336
382
|
setup do
|
337
|
-
@f0 = @widget.whatchamajiggers.create :
|
338
|
-
@f1 = @widget.whatchamajiggers.create :
|
383
|
+
@f0 = @widget.whatchamajiggers.create name: "f-zero"
|
384
|
+
@f1 = @widget.whatchamajiggers.create name: "f-zero"
|
339
385
|
@reified_widget = @widget.versions.last.reify
|
340
386
|
end
|
341
387
|
|
342
|
-
should
|
388
|
+
should "copy the has_many associations when reifying" do
|
343
389
|
assert_equal @widget.whatchamajiggers.length, @reified_widget.whatchamajiggers.length
|
344
390
|
assert_same_elements @widget.whatchamajiggers, @reified_widget.whatchamajiggers
|
345
391
|
|
@@ -348,54 +394,57 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
348
394
|
end
|
349
395
|
end
|
350
396
|
|
351
|
-
context
|
397
|
+
context "polymorphic objects by themselves" do
|
352
398
|
setup do
|
353
|
-
@widget = Whatchamajigger.new :
|
399
|
+
@widget = Whatchamajigger.new name: "f-zero"
|
354
400
|
end
|
355
401
|
|
356
|
-
should
|
402
|
+
should "not fail with a nil pointer on the polymorphic association" do
|
357
403
|
@widget.save!
|
358
404
|
end
|
359
405
|
end
|
360
406
|
|
361
|
-
context
|
407
|
+
context "and then destroyed" do
|
362
408
|
setup do
|
363
|
-
@fluxor = @widget.fluxors.create :
|
409
|
+
@fluxor = @widget.fluxors.create name: "flux"
|
364
410
|
@widget.destroy
|
365
411
|
@reified_widget = PaperTrail::Version.last.reify
|
366
412
|
end
|
367
413
|
|
368
|
-
should
|
369
|
-
assert_match
|
414
|
+
should "record the correct event" do
|
415
|
+
assert_match(/destroy/i, PaperTrail::Version.last.event)
|
370
416
|
end
|
371
417
|
|
372
|
-
should
|
373
|
-
assert_equal 3, PaperTrail::Version.with_item_keys(
|
418
|
+
should "have three previous versions" do
|
419
|
+
assert_equal 3, PaperTrail::Version.with_item_keys("Widget", @widget.id).length
|
374
420
|
end
|
375
421
|
|
376
|
-
should
|
422
|
+
should "be available in its previous version" do
|
377
423
|
assert_equal @widget.id, @reified_widget.id
|
378
424
|
assert_attributes_equal @widget.attributes, @reified_widget.attributes
|
379
425
|
end
|
380
426
|
|
381
|
-
should
|
427
|
+
should "be re-creatable from its previous version" do
|
382
428
|
assert @reified_widget.save
|
383
429
|
end
|
384
430
|
|
385
|
-
should
|
431
|
+
should "restore its associations on its previous version" do
|
386
432
|
@reified_widget.save
|
387
433
|
assert_equal 1, @reified_widget.fluxors.length
|
388
434
|
end
|
389
435
|
|
390
|
-
should
|
391
|
-
|
436
|
+
should "have nil item for last version" do
|
437
|
+
assert_nil(@widget.versions.last.item)
|
438
|
+
end
|
439
|
+
|
440
|
+
should "not have changes" do
|
441
|
+
assert_equal({}, @widget.versions.last.changeset)
|
392
442
|
end
|
393
443
|
end
|
394
444
|
end
|
395
445
|
end
|
396
446
|
end
|
397
447
|
|
398
|
-
|
399
448
|
# Test the serialisation and deserialisation.
|
400
449
|
# TODO: binary
|
401
450
|
context "A record's papertrail" do
|
@@ -403,65 +452,67 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
403
452
|
@date_time = DateTime.now.utc
|
404
453
|
@time = Time.now
|
405
454
|
@date = Date.new 2009, 5, 29
|
406
|
-
@widget = Widget.create
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
455
|
+
@widget = Widget.create(
|
456
|
+
name: "Warble",
|
457
|
+
a_text: "The quick brown fox",
|
458
|
+
an_integer: 42,
|
459
|
+
a_float: 153.01,
|
460
|
+
a_decimal: 2.71828,
|
461
|
+
a_datetime: @date_time,
|
462
|
+
a_time: @time,
|
463
|
+
a_date: @date,
|
464
|
+
a_boolean: true
|
465
|
+
)
|
466
|
+
@widget.update_attributes(
|
467
|
+
name: nil,
|
468
|
+
a_text: nil,
|
469
|
+
an_integer: nil,
|
470
|
+
a_float: nil,
|
471
|
+
a_decimal: nil,
|
472
|
+
a_datetime: nil,
|
473
|
+
a_time: nil,
|
474
|
+
a_date: nil,
|
475
|
+
a_boolean: false
|
476
|
+
)
|
425
477
|
@previous = @widget.versions.last.reify
|
426
478
|
end
|
427
479
|
|
428
|
-
should
|
429
|
-
assert_equal
|
480
|
+
should "handle strings" do
|
481
|
+
assert_equal "Warble", @previous.name
|
430
482
|
end
|
431
483
|
|
432
|
-
should
|
433
|
-
assert_equal
|
484
|
+
should "handle text" do
|
485
|
+
assert_equal "The quick brown fox", @previous.a_text
|
434
486
|
end
|
435
487
|
|
436
|
-
should
|
488
|
+
should "handle integers" do
|
437
489
|
assert_equal 42, @previous.an_integer
|
438
490
|
end
|
439
491
|
|
440
|
-
should
|
492
|
+
should "handle floats" do
|
441
493
|
assert_in_delta 153.01, @previous.a_float, 0.001
|
442
494
|
end
|
443
495
|
|
444
|
-
should
|
496
|
+
should "handle decimals" do
|
445
497
|
assert_in_delta 2.7183, @previous.a_decimal, 0.0001
|
446
498
|
end
|
447
499
|
|
448
|
-
should
|
500
|
+
should "handle datetimes" do
|
449
501
|
assert_equal @date_time.to_time.utc.to_i, @previous.a_datetime.to_time.utc.to_i
|
450
502
|
end
|
451
503
|
|
452
|
-
should
|
504
|
+
should "handle times" do
|
453
505
|
assert_equal @time.utc.to_i, @previous.a_time.utc.to_i
|
454
506
|
end
|
455
507
|
|
456
|
-
should
|
508
|
+
should "handle dates" do
|
457
509
|
assert_equal @date, @previous.a_date
|
458
510
|
end
|
459
511
|
|
460
|
-
should
|
512
|
+
should "handle booleans" do
|
461
513
|
assert @previous.a_boolean
|
462
514
|
end
|
463
515
|
|
464
|
-
|
465
516
|
context "after a column is removed from the record's schema" do
|
466
517
|
setup do
|
467
518
|
change_schema
|
@@ -475,13 +526,13 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
475
526
|
restore_schema
|
476
527
|
end
|
477
528
|
|
478
|
-
should
|
529
|
+
should "reify previous version" do
|
479
530
|
assert_kind_of Widget, @last.reify
|
480
531
|
end
|
481
532
|
|
482
|
-
should
|
483
|
-
assert_equal
|
484
|
-
assert_equal
|
533
|
+
should "restore all forward-compatible attributes" do
|
534
|
+
assert_equal "Warble", @last.reify.name
|
535
|
+
assert_equal "The quick brown fox", @last.reify.a_text
|
485
536
|
assert_equal 42, @last.reify.an_integer
|
486
537
|
assert_in_delta 153.01, @last.reify.a_float, 0.001
|
487
538
|
assert_in_delta 2.7183, @last.reify.a_decimal, 0.0001
|
@@ -493,11 +544,10 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
493
544
|
end
|
494
545
|
end
|
495
546
|
|
547
|
+
context "A record" do
|
548
|
+
setup { @widget = Widget.create name: "Zaphod" }
|
496
549
|
|
497
|
-
|
498
|
-
setup { @widget = Widget.create :name => 'Zaphod' }
|
499
|
-
|
500
|
-
context 'with PaperTrail globally disabled' do
|
550
|
+
context "with PaperTrail globally disabled" do
|
501
551
|
setup do
|
502
552
|
PaperTrail.enabled = false
|
503
553
|
@count = @widget.versions.length
|
@@ -505,16 +555,16 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
505
555
|
|
506
556
|
teardown { PaperTrail.enabled = true }
|
507
557
|
|
508
|
-
context
|
509
|
-
setup { @widget.update_attributes :
|
558
|
+
context "when updated" do
|
559
|
+
setup { @widget.update_attributes name: "Beeblebrox" }
|
510
560
|
|
511
|
-
should
|
561
|
+
should "not add to its trail" do
|
512
562
|
assert_equal @count, @widget.versions.length
|
513
563
|
end
|
514
564
|
end
|
515
565
|
end
|
516
566
|
|
517
|
-
context
|
567
|
+
context "with its paper trail turned off" do
|
518
568
|
setup do
|
519
569
|
Widget.paper_trail_off!
|
520
570
|
@count = @widget.versions.length
|
@@ -522,28 +572,28 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
522
572
|
|
523
573
|
teardown { Widget.paper_trail_on! }
|
524
574
|
|
525
|
-
context
|
526
|
-
setup { @widget.update_attributes :
|
575
|
+
context "when updated" do
|
576
|
+
setup { @widget.update_attributes name: "Beeblebrox" }
|
527
577
|
|
528
|
-
should
|
578
|
+
should "not add to its trail" do
|
529
579
|
assert_equal @count, @widget.versions.length
|
530
580
|
end
|
531
581
|
end
|
532
582
|
|
533
583
|
context 'when destroyed "without versioning"' do
|
534
|
-
should
|
584
|
+
should "leave paper trail off after call" do
|
535
585
|
@widget.without_versioning :destroy
|
536
586
|
assert !Widget.paper_trail_enabled_for_model?
|
537
587
|
end
|
538
588
|
end
|
539
589
|
|
540
|
-
context
|
590
|
+
context "and then its paper trail turned on" do
|
541
591
|
setup { Widget.paper_trail_on! }
|
542
592
|
|
543
|
-
context
|
544
|
-
setup { @widget.update_attributes :
|
593
|
+
context "when updated" do
|
594
|
+
setup { @widget.update_attributes name: "Ford" }
|
545
595
|
|
546
|
-
should
|
596
|
+
should "add to its trail" do
|
547
597
|
assert_equal @count + 1, @widget.versions.length
|
548
598
|
end
|
549
599
|
end
|
@@ -551,29 +601,29 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
551
601
|
context 'when updated "without versioning"' do
|
552
602
|
setup do
|
553
603
|
@widget.without_versioning do
|
554
|
-
@widget.update_attributes :
|
604
|
+
@widget.update_attributes name: "Ford"
|
555
605
|
end
|
556
606
|
# The model instance should yield itself for convenience purposes
|
557
|
-
@widget.without_versioning { |w| w.update_attributes :
|
607
|
+
@widget.without_versioning { |w| w.update_attributes name: "Nixon" }
|
558
608
|
end
|
559
609
|
|
560
|
-
should
|
610
|
+
should "not create new version" do
|
561
611
|
assert_equal @count, @widget.versions.length
|
562
612
|
end
|
563
613
|
|
564
|
-
should
|
614
|
+
should "enable paper trail after call" do
|
565
615
|
assert Widget.paper_trail_enabled_for_model?
|
566
616
|
end
|
567
617
|
end
|
568
618
|
|
569
|
-
context
|
619
|
+
context "when receiving a method name as an argument" do
|
570
620
|
setup { @widget.without_versioning(:touch_with_version) }
|
571
621
|
|
572
|
-
should
|
622
|
+
should "not create new version" do
|
573
623
|
assert_equal @count, @widget.versions.length
|
574
624
|
end
|
575
625
|
|
576
|
-
should
|
626
|
+
should "enable paper trail after call" do
|
577
627
|
assert Widget.paper_trail_enabled_for_model?
|
578
628
|
end
|
579
629
|
end
|
@@ -581,107 +631,117 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
581
631
|
end
|
582
632
|
end
|
583
633
|
|
584
|
-
|
585
|
-
context 'A papertrail with somebody making changes' do
|
634
|
+
context "A papertrail with somebody making changes" do
|
586
635
|
setup do
|
587
|
-
@widget = Widget.new :
|
636
|
+
@widget = Widget.new name: "Fidget"
|
588
637
|
end
|
589
638
|
|
590
|
-
context
|
639
|
+
context "when a record is created" do
|
591
640
|
setup do
|
592
|
-
PaperTrail.whodunnit =
|
641
|
+
PaperTrail.whodunnit = "Alice"
|
593
642
|
@widget.save
|
594
|
-
@version = @widget.versions.last
|
643
|
+
@version = @widget.versions.last # only 1 version
|
595
644
|
end
|
596
645
|
|
597
|
-
should
|
598
|
-
assert_equal
|
646
|
+
should "track who made the change" do
|
647
|
+
assert_equal "Alice", @version.whodunnit
|
599
648
|
assert_nil @version.paper_trail_originator
|
600
|
-
assert_equal
|
601
|
-
assert_equal
|
649
|
+
assert_equal "Alice", @version.terminator
|
650
|
+
assert_equal "Alice", @widget.paper_trail_originator
|
602
651
|
end
|
603
652
|
|
604
|
-
context
|
653
|
+
context "when a record is updated" do
|
605
654
|
setup do
|
606
|
-
PaperTrail.whodunnit =
|
607
|
-
@widget.update_attributes :
|
655
|
+
PaperTrail.whodunnit = "Bob"
|
656
|
+
@widget.update_attributes name: "Rivet"
|
608
657
|
@version = @widget.versions.last
|
609
658
|
end
|
610
659
|
|
611
|
-
should
|
612
|
-
assert_equal
|
613
|
-
assert_equal
|
614
|
-
assert_equal
|
615
|
-
assert_equal
|
660
|
+
should "track who made the change" do
|
661
|
+
assert_equal "Bob", @version.whodunnit
|
662
|
+
assert_equal "Alice", @version.paper_trail_originator
|
663
|
+
assert_equal "Bob", @version.terminator
|
664
|
+
assert_equal "Bob", @widget.paper_trail_originator
|
616
665
|
end
|
617
666
|
|
618
|
-
context
|
667
|
+
context "when a record is destroyed" do
|
619
668
|
setup do
|
620
|
-
PaperTrail.whodunnit =
|
669
|
+
PaperTrail.whodunnit = "Charlie"
|
621
670
|
@widget.destroy
|
622
671
|
@version = PaperTrail::Version.last
|
623
672
|
end
|
624
673
|
|
625
|
-
should
|
626
|
-
assert_equal
|
627
|
-
assert_equal
|
628
|
-
assert_equal
|
629
|
-
assert_equal
|
674
|
+
should "track who made the change" do
|
675
|
+
assert_equal "Charlie", @version.whodunnit
|
676
|
+
assert_equal "Bob", @version.paper_trail_originator
|
677
|
+
assert_equal "Charlie", @version.terminator
|
678
|
+
assert_equal "Charlie", @widget.paper_trail_originator
|
630
679
|
end
|
631
680
|
end
|
632
681
|
end
|
633
682
|
end
|
634
683
|
end
|
635
684
|
|
636
|
-
|
637
|
-
context 'Timestamps' do
|
685
|
+
context "Timestamps" do
|
638
686
|
setup do
|
639
|
-
@wotsit = Wotsit.create! :
|
687
|
+
@wotsit = Wotsit.create! name: "wotsit"
|
640
688
|
end
|
641
689
|
|
642
|
-
should
|
643
|
-
@wotsit.update_attributes! :
|
690
|
+
should "record timestamps" do
|
691
|
+
@wotsit.update_attributes! name: "changed"
|
644
692
|
assert_not_nil @wotsit.versions.last.reify.created_at
|
645
693
|
assert_not_nil @wotsit.versions.last.reify.updated_at
|
646
694
|
end
|
647
695
|
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
696
|
+
# Tests that it doesn't try to write created_on as an attribute just because
|
697
|
+
# a created_on method exists.
|
698
|
+
#
|
699
|
+
# - Deprecation warning in Rails 3.2
|
700
|
+
# - ActiveModel::MissingAttributeError in Rails 4
|
701
|
+
#
|
702
|
+
# In rails 5, `capture` is deprecated in favor of `capture_io`.
|
703
|
+
#
|
704
|
+
should "not generate warning" do
|
705
|
+
assert_update_raises_nothing = lambda {
|
706
|
+
assert_nothing_raised {
|
707
|
+
@wotsit.update_attributes! name: "changed"
|
654
708
|
}
|
655
709
|
}
|
656
|
-
|
710
|
+
warnings =
|
711
|
+
if respond_to?(:capture_io)
|
712
|
+
capture_io { assert_update_raises_nothing.call }.last
|
713
|
+
else
|
714
|
+
capture(:stderr) { assert_update_raises_nothing.call }
|
715
|
+
end
|
716
|
+
assert_equal "", warnings
|
657
717
|
end
|
658
|
-
|
659
718
|
end
|
660
719
|
|
661
|
-
|
662
|
-
context 'A subclass' do
|
720
|
+
context "A subclass" do
|
663
721
|
setup do
|
664
722
|
@foo = FooWidget.create
|
665
|
-
@foo.update_attributes! :
|
723
|
+
@foo.update_attributes! name: "Foo"
|
666
724
|
end
|
667
725
|
|
668
|
-
should
|
669
|
-
# For some reason this test appears to be broken on AR4 in the test env.
|
726
|
+
should "reify with the correct type" do
|
727
|
+
# For some reason this test appears to be broken on AR4 in the test env.
|
728
|
+
# Executing it manually in the Rails console seems to work.. not sure what
|
729
|
+
# the issues is here.
|
670
730
|
assert_kind_of FooWidget, @foo.versions.last.reify if ActiveRecord::VERSION::MAJOR < 4
|
671
731
|
assert_equal @foo.versions.first, PaperTrail::Version.last.previous
|
672
732
|
assert_nil PaperTrail::Version.last.next
|
673
733
|
end
|
674
734
|
|
675
|
-
should
|
676
|
-
PaperTrail.whodunnit =
|
677
|
-
@foo.update_attribute(:name,
|
735
|
+
should "should return the correct originator" do
|
736
|
+
PaperTrail.whodunnit = "Ben"
|
737
|
+
@foo.update_attribute(:name, "Geoffrey")
|
678
738
|
assert_equal PaperTrail.whodunnit, @foo.paper_trail_originator
|
679
739
|
end
|
680
740
|
|
681
|
-
context
|
741
|
+
context "when destroyed" do
|
682
742
|
setup { @foo.destroy }
|
683
743
|
|
684
|
-
should
|
744
|
+
should "reify with the correct type" do
|
685
745
|
assert_kind_of FooWidget, @foo.versions.last.reify
|
686
746
|
assert_equal @foo.versions[1], PaperTrail::Version.last.previous
|
687
747
|
assert_nil PaperTrail::Version.last.next
|
@@ -689,250 +749,250 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
689
749
|
end
|
690
750
|
end
|
691
751
|
|
692
|
-
|
693
|
-
context 'An item with versions' do
|
752
|
+
context "An item with versions" do
|
694
753
|
setup do
|
695
|
-
@widget = Widget.create :
|
696
|
-
@widget.update_attributes :
|
697
|
-
@widget.update_attributes :
|
754
|
+
@widget = Widget.create name: "Widget"
|
755
|
+
@widget.update_attributes name: "Fidget"
|
756
|
+
@widget.update_attributes name: "Digit"
|
698
757
|
end
|
699
758
|
|
700
|
-
context
|
759
|
+
context "which were created over time" do
|
701
760
|
setup do
|
702
|
-
@created
|
703
|
-
@first_update
|
761
|
+
@created = 2.days.ago
|
762
|
+
@first_update = 1.day.ago
|
704
763
|
@second_update = 1.hour.ago
|
705
|
-
@widget.versions[0].update_attributes :
|
706
|
-
@widget.versions[1].update_attributes :
|
707
|
-
@widget.versions[2].update_attributes :
|
764
|
+
@widget.versions[0].update_attributes created_at: @created
|
765
|
+
@widget.versions[1].update_attributes created_at: @first_update
|
766
|
+
@widget.versions[2].update_attributes created_at: @second_update
|
708
767
|
@widget.update_attribute :updated_at, @second_update
|
709
768
|
end
|
710
769
|
|
711
|
-
should
|
770
|
+
should "return nil for version_at before it was created" do
|
712
771
|
assert_nil @widget.version_at(@created - 1)
|
713
772
|
end
|
714
773
|
|
715
|
-
should
|
716
|
-
assert_equal
|
774
|
+
should "return how it looked when created for version_at its creation" do
|
775
|
+
assert_equal "Widget", @widget.version_at(@created).name
|
717
776
|
end
|
718
777
|
|
719
|
-
should "return how it looked
|
720
|
-
assert_equal
|
778
|
+
should "return how it looked before its first update" do
|
779
|
+
assert_equal "Widget", @widget.version_at(@first_update - 1).name
|
721
780
|
end
|
722
781
|
|
723
|
-
should "return how it looked
|
724
|
-
assert_equal
|
782
|
+
should "return how it looked after its first update" do
|
783
|
+
assert_equal "Fidget", @widget.version_at(@first_update).name
|
725
784
|
end
|
726
785
|
|
727
|
-
should
|
728
|
-
assert_equal
|
786
|
+
should "return how it looked before its second update" do
|
787
|
+
assert_equal "Fidget", @widget.version_at(@second_update - 1).name
|
729
788
|
end
|
730
789
|
|
731
|
-
should
|
732
|
-
assert_equal
|
790
|
+
should "return how it looked after its second update" do
|
791
|
+
assert_equal "Digit", @widget.version_at(@second_update).name
|
733
792
|
end
|
734
793
|
|
735
|
-
should
|
736
|
-
assert_equal
|
794
|
+
should "return the current object for version_at after latest update" do
|
795
|
+
assert_equal "Digit", @widget.version_at(1.day.from_now).name
|
737
796
|
end
|
738
797
|
|
739
|
-
context
|
740
|
-
should
|
741
|
-
# need to add 1 second onto the timestamps before casting to a string,
|
742
|
-
|
743
|
-
assert_equal
|
744
|
-
assert_equal
|
798
|
+
context "passing in a string representation of a timestamp" do
|
799
|
+
should "still return a widget when appropriate" do
|
800
|
+
# need to add 1 second onto the timestamps before casting to a string,
|
801
|
+
# since casting a Time to a string drops the microseconds
|
802
|
+
assert_equal "Widget", @widget.version_at((@created + 1.second).to_s).name
|
803
|
+
assert_equal "Fidget", @widget.version_at((@first_update + 1.second).to_s).name
|
804
|
+
assert_equal "Digit", @widget.version_at((@second_update + 1.second).to_s).name
|
745
805
|
end
|
746
806
|
end
|
747
807
|
end
|
748
808
|
|
749
|
-
context
|
809
|
+
context ".versions_between" do
|
750
810
|
setup do
|
751
|
-
@created
|
752
|
-
@first_update
|
811
|
+
@created = 30.days.ago
|
812
|
+
@first_update = 15.days.ago
|
753
813
|
@second_update = 1.day.ago
|
754
|
-
@widget.versions[0].update_attributes :
|
755
|
-
@widget.versions[1].update_attributes :
|
756
|
-
@widget.versions[2].update_attributes :
|
814
|
+
@widget.versions[0].update_attributes created_at: @created
|
815
|
+
@widget.versions[1].update_attributes created_at: @first_update
|
816
|
+
@widget.versions[2].update_attributes created_at: @second_update
|
757
817
|
@widget.update_attribute :updated_at, @second_update
|
758
818
|
end
|
759
819
|
|
760
|
-
should
|
761
|
-
assert_equal [
|
762
|
-
|
763
|
-
assert_equal
|
764
|
-
|
820
|
+
should "return versions in the time period" do
|
821
|
+
assert_equal ["Fidget"],
|
822
|
+
@widget.versions_between(20.days.ago, 10.days.ago).map(&:name)
|
823
|
+
assert_equal %w(Widget Fidget),
|
824
|
+
@widget.versions_between(45.days.ago, 10.days.ago).map(&:name)
|
825
|
+
assert_equal %w(Fidget Digit Digit),
|
826
|
+
@widget.versions_between(16.days.ago, 1.minute.ago).map(&:name)
|
827
|
+
assert_equal [],
|
828
|
+
@widget.versions_between(60.days.ago, 45.days.ago).map(&:name)
|
765
829
|
end
|
766
830
|
end
|
767
831
|
|
768
|
-
context
|
832
|
+
context "on the first version" do
|
769
833
|
setup { @version = @widget.versions.first }
|
770
834
|
|
771
|
-
should
|
835
|
+
should "have a nil previous version" do
|
772
836
|
assert_nil @version.previous
|
773
837
|
end
|
774
838
|
|
775
|
-
should
|
839
|
+
should "return the next version" do
|
776
840
|
assert_equal @widget.versions[1], @version.next
|
777
841
|
end
|
778
842
|
|
779
|
-
should
|
843
|
+
should "return the correct index" do
|
780
844
|
assert_equal 0, @version.index
|
781
845
|
end
|
782
846
|
end
|
783
847
|
|
784
|
-
context
|
848
|
+
context "on the last version" do
|
785
849
|
setup { @version = @widget.versions.last }
|
786
850
|
|
787
|
-
should
|
851
|
+
should "return the previous version" do
|
788
852
|
assert_equal @widget.versions[@widget.versions.length - 2], @version.previous
|
789
853
|
end
|
790
854
|
|
791
|
-
should
|
855
|
+
should "have a nil next version" do
|
792
856
|
assert_nil @version.next
|
793
857
|
end
|
794
858
|
|
795
|
-
should
|
859
|
+
should "return the correct index" do
|
796
860
|
assert_equal @widget.versions.length - 1, @version.index
|
797
861
|
end
|
798
862
|
end
|
799
863
|
end
|
800
864
|
|
801
|
-
|
802
|
-
context 'An item' do
|
865
|
+
context "An item" do
|
803
866
|
setup do
|
804
|
-
@initial_title =
|
805
|
-
@article = Article.new :
|
867
|
+
@initial_title = "Foobar"
|
868
|
+
@article = Article.new title: @initial_title
|
806
869
|
end
|
807
870
|
|
808
|
-
context
|
871
|
+
context "which is created" do
|
809
872
|
setup { @article.save }
|
810
873
|
|
811
|
-
should
|
874
|
+
should "store fixed meta data" do
|
812
875
|
assert_equal 42, @article.versions.last.answer
|
813
876
|
end
|
814
877
|
|
815
|
-
should
|
816
|
-
assert_equal
|
878
|
+
should "store dynamic meta data which is independent of the item" do
|
879
|
+
assert_equal "31 + 11 = 42", @article.versions.last.question
|
817
880
|
end
|
818
881
|
|
819
|
-
should
|
882
|
+
should "store dynamic meta data which depends on the item" do
|
820
883
|
assert_equal @article.id, @article.versions.last.article_id
|
821
884
|
end
|
822
885
|
|
823
|
-
should
|
886
|
+
should "store dynamic meta data based on a method of the item" do
|
824
887
|
assert_equal @article.action_data_provider_method, @article.versions.last.action
|
825
888
|
end
|
826
889
|
|
827
|
-
should
|
890
|
+
should "store dynamic meta data based on an attribute of the item at creation" do
|
828
891
|
assert_equal @initial_title, @article.versions.last.title
|
829
892
|
end
|
830
893
|
|
831
|
-
|
832
|
-
context 'and updated' do
|
894
|
+
context "and updated" do
|
833
895
|
setup do
|
834
|
-
@article.update_attributes! :
|
896
|
+
@article.update_attributes! content: "Better text.", title: "Rhubarb"
|
835
897
|
end
|
836
898
|
|
837
|
-
should
|
899
|
+
should "store fixed meta data" do
|
838
900
|
assert_equal 42, @article.versions.last.answer
|
839
901
|
end
|
840
902
|
|
841
|
-
should
|
842
|
-
assert_equal
|
903
|
+
should "store dynamic meta data which is independent of the item" do
|
904
|
+
assert_equal "31 + 11 = 42", @article.versions.last.question
|
843
905
|
end
|
844
906
|
|
845
|
-
should
|
907
|
+
should "store dynamic meta data which depends on the item" do
|
846
908
|
assert_equal @article.id, @article.versions.last.article_id
|
847
909
|
end
|
848
910
|
|
849
|
-
should
|
911
|
+
should "store dynamic meta data based on an attribute of the item prior to the update" do
|
850
912
|
assert_equal @initial_title, @article.versions.last.title
|
851
913
|
end
|
852
914
|
end
|
853
915
|
|
854
|
-
|
855
|
-
context 'and destroyed' do
|
916
|
+
context "and destroyed" do
|
856
917
|
setup { @article.destroy }
|
857
918
|
|
858
|
-
should
|
919
|
+
should "store fixed metadata" do
|
859
920
|
assert_equal 42, @article.versions.last.answer
|
860
921
|
end
|
861
922
|
|
862
|
-
should
|
863
|
-
assert_equal
|
923
|
+
should "store dynamic metadata which is independent of the item" do
|
924
|
+
assert_equal "31 + 11 = 42", @article.versions.last.question
|
864
925
|
end
|
865
926
|
|
866
|
-
should
|
927
|
+
should "store dynamic metadata which depends on the item" do
|
867
928
|
assert_equal @article.id, @article.versions.last.article_id
|
868
929
|
end
|
869
930
|
|
870
|
-
should
|
931
|
+
should "store dynamic metadata based on attribute of item prior to destruction" do
|
871
932
|
assert_equal @initial_title, @article.versions.last.title
|
872
933
|
end
|
873
934
|
end
|
874
935
|
end
|
875
936
|
end
|
876
937
|
|
877
|
-
context
|
938
|
+
context "A reified item" do
|
878
939
|
setup do
|
879
|
-
widget = Widget.create :
|
880
|
-
%w( Tom Dick Jane ).each { |name| widget.update_attributes :
|
940
|
+
widget = Widget.create name: "Bob"
|
941
|
+
%w( Tom Dick Jane ).each { |name| widget.update_attributes name: name }
|
881
942
|
@version = widget.versions.last
|
882
943
|
@widget = @version.reify
|
883
944
|
end
|
884
945
|
|
885
|
-
should
|
946
|
+
should "know which version it came from" do
|
886
947
|
assert_equal @version, @widget.version
|
887
948
|
end
|
888
949
|
|
889
|
-
should
|
950
|
+
should "return its previous self" do
|
890
951
|
assert_equal @widget.versions[-2].reify, @widget.previous_version
|
891
952
|
end
|
892
953
|
end
|
893
954
|
|
894
|
-
|
895
|
-
context 'A non-reified item' do
|
955
|
+
context "A non-reified item" do
|
896
956
|
setup { @widget = Widget.new }
|
897
957
|
|
898
|
-
should
|
958
|
+
should "not have a previous version" do
|
899
959
|
assert_nil @widget.previous_version
|
900
960
|
end
|
901
961
|
|
902
|
-
should
|
962
|
+
should "not have a next version" do
|
903
963
|
assert_nil @widget.next_version
|
904
964
|
end
|
905
965
|
|
906
|
-
context
|
966
|
+
context "with versions" do
|
907
967
|
setup do
|
908
968
|
@widget.save
|
909
|
-
%w( Tom Dick Jane ).each { |name| @widget.update_attributes :
|
969
|
+
%w( Tom Dick Jane ).each { |name| @widget.update_attributes name: name }
|
910
970
|
end
|
911
971
|
|
912
|
-
should
|
972
|
+
should "have a previous version" do
|
913
973
|
assert_equal @widget.versions.last.reify.name, @widget.previous_version.name
|
914
974
|
end
|
915
975
|
|
916
|
-
should
|
976
|
+
should "not have a next version" do
|
917
977
|
assert_nil @widget.next_version
|
918
978
|
end
|
919
979
|
end
|
920
980
|
end
|
921
981
|
|
922
|
-
context
|
982
|
+
context "A reified item" do
|
923
983
|
setup do
|
924
|
-
@widget = Widget.create :
|
925
|
-
%w(Tom Dick Jane).each { |name| @widget.update_attributes :
|
926
|
-
@second_widget = @widget.versions[1].reify
|
927
|
-
@last_widget
|
984
|
+
@widget = Widget.create name: "Bob"
|
985
|
+
%w(Tom Dick Jane).each { |name| @widget.update_attributes name: name }
|
986
|
+
@second_widget = @widget.versions[1].reify # first widget is `nil`
|
987
|
+
@last_widget = @widget.versions.last.reify
|
928
988
|
end
|
929
989
|
|
930
|
-
should
|
990
|
+
should "have a previous version" do
|
931
991
|
assert_nil @second_widget.previous_version # `create` events return `nil` for `reify`
|
932
992
|
assert_equal @widget.versions[-2].reify.name, @last_widget.previous_version.name
|
933
993
|
end
|
934
994
|
|
935
|
-
should
|
995
|
+
should "have a next version" do
|
936
996
|
assert_equal @widget.versions[2].reify.name, @second_widget.next_version.name
|
937
997
|
assert_equal @last_widget.next_version.name, @widget.name
|
938
998
|
end
|
@@ -940,178 +1000,226 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
940
1000
|
|
941
1001
|
context ":has_many :through" do
|
942
1002
|
setup do
|
943
|
-
@book = Book.create :
|
944
|
-
@dostoyevsky
|
945
|
-
@solzhenitsyn = Person.create :
|
1003
|
+
@book = Book.create title: "War and Peace"
|
1004
|
+
@dostoyevsky = Person.create name: "Dostoyevsky"
|
1005
|
+
@solzhenitsyn = Person.create name: "Solzhenitsyn"
|
946
1006
|
end
|
947
1007
|
|
948
|
-
should
|
1008
|
+
should "store version on source <<" do
|
949
1009
|
count = PaperTrail::Version.count
|
950
1010
|
@book.authors << @dostoyevsky
|
951
1011
|
assert_equal 1, PaperTrail::Version.count - count
|
952
1012
|
assert_equal PaperTrail::Version.last, @book.authorships.first.versions.first
|
953
1013
|
end
|
954
1014
|
|
955
|
-
should
|
1015
|
+
should "store version on source create" do
|
956
1016
|
count = PaperTrail::Version.count
|
957
|
-
@book.authors.create :
|
1017
|
+
@book.authors.create name: "Tolstoy"
|
958
1018
|
assert_equal 2, PaperTrail::Version.count - count
|
959
|
-
|
1019
|
+
actual = [
|
1020
|
+
PaperTrail::Version.order(:id).to_a[-2].item,
|
1021
|
+
PaperTrail::Version.last.item
|
1022
|
+
]
|
1023
|
+
assert_same_elements [Person.last, Authorship.last], actual
|
960
1024
|
end
|
961
1025
|
|
962
|
-
should
|
1026
|
+
should "store version on join destroy" do
|
963
1027
|
@book.authors << @dostoyevsky
|
964
1028
|
count = PaperTrail::Version.count
|
965
|
-
@book.authorships
|
1029
|
+
@book.authorships.reload.last.destroy
|
966
1030
|
assert_equal 1, PaperTrail::Version.count - count
|
967
1031
|
assert_equal @book, PaperTrail::Version.last.reify.book
|
968
|
-
assert_equal @dostoyevsky, PaperTrail::Version.last.reify.
|
1032
|
+
assert_equal @dostoyevsky, PaperTrail::Version.last.reify.author
|
969
1033
|
end
|
970
1034
|
|
971
|
-
should
|
1035
|
+
should "store version on join clear" do
|
972
1036
|
@book.authors << @dostoyevsky
|
973
1037
|
count = PaperTrail::Version.count
|
974
|
-
@book.authorships
|
1038
|
+
@book.authorships.reload.destroy_all
|
975
1039
|
assert_equal 1, PaperTrail::Version.count - count
|
976
1040
|
assert_equal @book, PaperTrail::Version.last.reify.book
|
977
|
-
assert_equal @dostoyevsky, PaperTrail::Version.last.reify.
|
1041
|
+
assert_equal @dostoyevsky, PaperTrail::Version.last.reify.author
|
978
1042
|
end
|
979
1043
|
end
|
980
1044
|
|
981
|
-
context
|
1045
|
+
context "When an attribute has a custom serializer" do
|
982
1046
|
setup do
|
983
|
-
@person = Person.new(:
|
1047
|
+
@person = Person.new(time_zone: "Samoa")
|
984
1048
|
end
|
985
1049
|
|
986
1050
|
should "be an instance of ActiveSupport::TimeZone" do
|
987
1051
|
assert_equal ActiveSupport::TimeZone, @person.time_zone.class
|
988
1052
|
end
|
989
1053
|
|
990
|
-
context
|
1054
|
+
context "when the model is saved" do
|
991
1055
|
setup do
|
992
1056
|
@changes_before_save = @person.changes.dup
|
993
1057
|
@person.save!
|
994
1058
|
end
|
995
1059
|
|
996
1060
|
# Test for serialization:
|
997
|
-
should
|
998
|
-
|
1061
|
+
should "version.object_changes should store long serialization of TimeZone object" do
|
1062
|
+
len = @person.versions.last.object_changes.length
|
1063
|
+
assert len < 105, "object_changes length was #{len}"
|
999
1064
|
end
|
1065
|
+
|
1000
1066
|
# It should store the serialized value.
|
1001
|
-
should
|
1002
|
-
as_stored_in_version = HashWithIndifferentAccess[
|
1003
|
-
|
1067
|
+
should "version.object_changes attribute should have stored the value from serializer" do
|
1068
|
+
as_stored_in_version = HashWithIndifferentAccess[
|
1069
|
+
YAML.load(@person.versions.last.object_changes)
|
1070
|
+
]
|
1071
|
+
assert_equal [nil, "Samoa"], as_stored_in_version[:time_zone]
|
1004
1072
|
serialized_value = Person::TimeZoneSerializer.dump(@person.time_zone)
|
1005
1073
|
assert_equal serialized_value, as_stored_in_version[:time_zone].last
|
1006
1074
|
end
|
1007
1075
|
|
1008
1076
|
# Tests for unserialization:
|
1009
|
-
should
|
1077
|
+
should "version.changeset should convert attribute to original, unserialized value" do
|
1010
1078
|
unserialized_value = Person::TimeZoneSerializer.load(@person.time_zone)
|
1011
|
-
assert_equal unserialized_value,
|
1079
|
+
assert_equal unserialized_value,
|
1080
|
+
@person.versions.last.changeset[:time_zone].last
|
1012
1081
|
end
|
1082
|
+
|
1013
1083
|
should "record.changes (before save) returns the original, unserialized values" do
|
1014
|
-
assert_equal [NilClass, ActiveSupport::TimeZone],
|
1084
|
+
assert_equal [NilClass, ActiveSupport::TimeZone],
|
1085
|
+
@changes_before_save[:time_zone].map(&:class)
|
1015
1086
|
end
|
1016
|
-
|
1017
|
-
|
1018
|
-
|
1087
|
+
|
1088
|
+
should "version.changeset should be the same as record.changes was before the save" do
|
1089
|
+
actual = @person.versions.last.changeset.delete_if { |k, _v| k.to_sym == :id }
|
1090
|
+
assert_equal @changes_before_save, actual
|
1091
|
+
actual = @person.versions.last.changeset[:time_zone].map(&:class)
|
1092
|
+
assert_equal [NilClass, ActiveSupport::TimeZone], actual
|
1019
1093
|
end
|
1020
1094
|
|
1021
|
-
context
|
1095
|
+
context "when that attribute is updated" do
|
1022
1096
|
setup do
|
1023
1097
|
@attribute_value_before_change = @person.time_zone
|
1024
|
-
@person.assign_attributes(
|
1098
|
+
@person.assign_attributes(time_zone: "Pacific Time (US & Canada)")
|
1025
1099
|
@changes_before_save = @person.changes.dup
|
1026
1100
|
@person.save!
|
1027
1101
|
end
|
1028
1102
|
|
1029
|
-
# Tests for serialization
|
1030
|
-
#
|
1031
|
-
|
1032
|
-
|
1103
|
+
# Tests for serialization
|
1104
|
+
# -----------------------
|
1105
|
+
#
|
1106
|
+
# Before the serialized attributes fix, the object/object_changes value
|
1107
|
+
# that was stored was ridiculously long (58723).
|
1108
|
+
#
|
1109
|
+
# version.object should not have stored the default, ridiculously long
|
1110
|
+
# (to_yaml) serialization of the TimeZone object.
|
1111
|
+
should "object should not store long serialization of TimeZone object" do
|
1112
|
+
len = @person.versions.last.object.length
|
1113
|
+
assert len < 105, "object length was #{len}"
|
1033
1114
|
end
|
1034
|
-
|
1035
|
-
|
1036
|
-
|
1115
|
+
|
1116
|
+
# Need an additional clause to detect what version of ActiveRecord is
|
1117
|
+
# being used for this test because AR4 injects the `updated_at` column
|
1118
|
+
# into the changeset for updates to models.
|
1119
|
+
#
|
1120
|
+
# version.object_changes should not have stored the default,
|
1121
|
+
# ridiculously long (to_yaml) serialization of the TimeZone object
|
1122
|
+
should "object_changes should not store long serialization of TimeZone object" do
|
1123
|
+
max_len = ActiveRecord::VERSION::MAJOR < 4 ? 105 : 118
|
1124
|
+
len = @person.versions.last.object_changes.length
|
1125
|
+
assert len < max_len, "object_changes length was #{len}"
|
1037
1126
|
end
|
1127
|
+
|
1038
1128
|
# But now it stores the short, serialized value.
|
1039
|
-
should
|
1040
|
-
as_stored_in_version = HashWithIndifferentAccess[
|
1041
|
-
|
1129
|
+
should "version.object attribute should have stored value from serializer" do
|
1130
|
+
as_stored_in_version = HashWithIndifferentAccess[
|
1131
|
+
YAML.load(@person.versions.last.object)
|
1132
|
+
]
|
1133
|
+
assert_equal "Samoa", as_stored_in_version[:time_zone]
|
1042
1134
|
serialized_value = Person::TimeZoneSerializer.dump(@attribute_value_before_change)
|
1043
1135
|
assert_equal serialized_value, as_stored_in_version[:time_zone]
|
1044
1136
|
end
|
1045
|
-
|
1046
|
-
|
1047
|
-
|
1137
|
+
|
1138
|
+
should "version.object_changes attribute should have stored value from serializer" do
|
1139
|
+
as_stored_in_version = HashWithIndifferentAccess[
|
1140
|
+
YAML.load(@person.versions.last.object_changes)
|
1141
|
+
]
|
1142
|
+
assert_equal ["Samoa", "Pacific Time (US & Canada)"], as_stored_in_version[:time_zone]
|
1048
1143
|
serialized_value = Person::TimeZoneSerializer.dump(@person.time_zone)
|
1049
1144
|
assert_equal serialized_value, as_stored_in_version[:time_zone].last
|
1050
1145
|
end
|
1051
1146
|
|
1052
1147
|
# Tests for unserialization:
|
1053
|
-
should
|
1148
|
+
should "version.reify should convert attribute to original, unserialized value" do
|
1054
1149
|
unserialized_value = Person::TimeZoneSerializer.load(@attribute_value_before_change)
|
1055
|
-
assert_equal unserialized_value,
|
1150
|
+
assert_equal unserialized_value,
|
1151
|
+
@person.versions.last.reify.time_zone
|
1056
1152
|
end
|
1057
|
-
|
1153
|
+
|
1154
|
+
should "version.changeset should convert attribute to original, unserialized value" do
|
1058
1155
|
unserialized_value = Person::TimeZoneSerializer.load(@person.time_zone)
|
1059
|
-
assert_equal unserialized_value,
|
1156
|
+
assert_equal unserialized_value,
|
1157
|
+
@person.versions.last.changeset[:time_zone].last
|
1060
1158
|
end
|
1159
|
+
|
1061
1160
|
should "record.changes (before save) returns the original, unserialized values" do
|
1062
|
-
assert_equal [ActiveSupport::TimeZone, ActiveSupport::TimeZone],
|
1161
|
+
assert_equal [ActiveSupport::TimeZone, ActiveSupport::TimeZone],
|
1162
|
+
@changes_before_save[:time_zone].map(&:class)
|
1063
1163
|
end
|
1064
|
-
|
1164
|
+
|
1165
|
+
should "version.changeset should be the same as record.changes was before the save" do
|
1065
1166
|
assert_equal @changes_before_save, @person.versions.last.changeset
|
1066
|
-
assert_equal [ActiveSupport::TimeZone, ActiveSupport::TimeZone],
|
1167
|
+
assert_equal [ActiveSupport::TimeZone, ActiveSupport::TimeZone],
|
1168
|
+
@person.versions.last.changeset[:time_zone].map(&:class)
|
1067
1169
|
end
|
1068
|
-
|
1069
1170
|
end
|
1070
1171
|
end
|
1071
1172
|
end
|
1072
1173
|
|
1073
|
-
|
1074
|
-
context 'A new model instance which uses a custom PaperTrail::Version class' do
|
1174
|
+
context "A new model instance which uses a custom PaperTrail::Version class" do
|
1075
1175
|
setup { @post = Post.new }
|
1076
1176
|
|
1077
|
-
context
|
1177
|
+
context "which is then saved" do
|
1078
1178
|
setup { @post.save }
|
1079
|
-
should
|
1080
|
-
should
|
1179
|
+
should "change the number of post versions" do assert_equal 1, PostVersion.count end
|
1180
|
+
should "not change the number of versions" do assert_equal(0, PaperTrail::Version.count) end
|
1081
1181
|
end
|
1082
1182
|
end
|
1083
1183
|
|
1084
|
-
context
|
1184
|
+
context "An existing model instance which uses a custom PaperTrail::Version class" do
|
1085
1185
|
setup { @post = Post.create }
|
1086
|
-
should
|
1186
|
+
should "have one post version" do assert_equal(1, PostVersion.count) end
|
1087
1187
|
|
1088
|
-
context
|
1188
|
+
context "on the first version" do
|
1089
1189
|
setup { @version = @post.versions.first }
|
1090
1190
|
|
1091
|
-
should
|
1191
|
+
should "have the correct index" do
|
1092
1192
|
assert_equal 0, @version.index
|
1093
1193
|
end
|
1094
1194
|
end
|
1095
1195
|
|
1096
|
-
should
|
1196
|
+
should "have versions of the custom class" do
|
1097
1197
|
assert_equal "PostVersion", @post.versions.first.class.name
|
1098
1198
|
end
|
1099
1199
|
|
1100
|
-
context
|
1101
|
-
setup
|
1102
|
-
|
1103
|
-
|
1200
|
+
context "which is modified" do
|
1201
|
+
setup do
|
1202
|
+
@post.update_attributes(content: "Some new content")
|
1203
|
+
end
|
1204
|
+
|
1205
|
+
should "change the number of post versions" do
|
1206
|
+
assert_equal(2, PostVersion.count)
|
1207
|
+
end
|
1208
|
+
|
1209
|
+
should "not change the number of versions" do
|
1210
|
+
assert_equal(0, PaperTrail::Version.count)
|
1211
|
+
end
|
1212
|
+
|
1104
1213
|
should "not have stored changes when object_changes column doesn't exist" do
|
1105
1214
|
assert_nil @post.versions.last.changeset
|
1106
1215
|
end
|
1107
1216
|
end
|
1108
1217
|
end
|
1109
1218
|
|
1110
|
-
|
1111
|
-
context 'An overwritten default accessor' do
|
1219
|
+
context "An overwritten default accessor" do
|
1112
1220
|
setup do
|
1113
|
-
@song = Song.create :
|
1114
|
-
@song.update_attributes :
|
1221
|
+
@song = Song.create length: 4
|
1222
|
+
@song.update_attributes length: 5
|
1115
1223
|
end
|
1116
1224
|
|
1117
1225
|
should 'return "overwritten" value on live instance' do
|
@@ -1121,74 +1229,73 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
1121
1229
|
assert_equal 4, @song.versions.last.reify.length
|
1122
1230
|
end
|
1123
1231
|
|
1124
|
-
context
|
1232
|
+
context "Has a virtual attribute injected into the ActiveModel::Dirty changes" do
|
1125
1233
|
setup do
|
1126
|
-
@song.name =
|
1234
|
+
@song.name = "Good Vibrations"
|
1127
1235
|
@song.save
|
1128
|
-
@song.name =
|
1236
|
+
@song.name = "Yellow Submarine"
|
1129
1237
|
end
|
1130
1238
|
|
1131
|
-
should
|
1132
|
-
assert_equal
|
1239
|
+
should "return persist the changes on the live instance properly" do
|
1240
|
+
assert_equal "Yellow Submarine", @song.name
|
1133
1241
|
end
|
1134
1242
|
should 'return "overwritten" virtual attribute on the reified instance' do
|
1135
|
-
assert_equal
|
1243
|
+
assert_equal "Good Vibrations", @song.versions.last.reify.name
|
1136
1244
|
end
|
1137
1245
|
end
|
1138
1246
|
end
|
1139
1247
|
|
1140
|
-
|
1141
|
-
context 'An unsaved record' do
|
1248
|
+
context "An unsaved record" do
|
1142
1249
|
setup do
|
1143
1250
|
@widget = Widget.new
|
1144
1251
|
@widget.destroy
|
1145
1252
|
end
|
1146
|
-
should
|
1253
|
+
should "not have a version created on destroy" do
|
1147
1254
|
assert @widget.versions.empty?
|
1148
1255
|
end
|
1149
1256
|
end
|
1150
1257
|
|
1151
|
-
context
|
1258
|
+
context "A model with a custom association" do
|
1152
1259
|
setup do
|
1153
1260
|
@doc = Document.create
|
1154
|
-
@doc.update_attributes :
|
1261
|
+
@doc.update_attributes name: "Doc 1"
|
1155
1262
|
end
|
1156
1263
|
|
1157
|
-
should
|
1264
|
+
should "not respond to versions method" do
|
1158
1265
|
assert !@doc.respond_to?(:versions)
|
1159
1266
|
end
|
1160
1267
|
|
1161
|
-
should
|
1268
|
+
should "create a new version record" do
|
1162
1269
|
assert_equal 2, @doc.paper_trail_versions.length
|
1163
1270
|
end
|
1164
1271
|
|
1165
|
-
should
|
1272
|
+
should "respond to `next_version` as normal" do
|
1166
1273
|
assert_equal @doc.paper_trail_versions.last.reify.next_version.name, @doc.name
|
1167
1274
|
end
|
1168
1275
|
|
1169
|
-
should
|
1170
|
-
@doc.update_attributes :
|
1276
|
+
should "respond to `previous_version` as normal" do
|
1277
|
+
@doc.update_attributes name: "Doc 2"
|
1171
1278
|
assert_equal 3, @doc.paper_trail_versions.length
|
1172
|
-
assert_equal
|
1279
|
+
assert_equal "Doc 1", @doc.previous_version.name
|
1173
1280
|
end
|
1174
1281
|
end
|
1175
1282
|
|
1176
|
-
context
|
1177
|
-
context
|
1283
|
+
context "The `on` option" do
|
1284
|
+
context "on create" do
|
1178
1285
|
setup do
|
1179
1286
|
Fluxor.instance_eval <<-END
|
1180
1287
|
has_paper_trail :on => [:create]
|
1181
1288
|
END
|
1182
1289
|
@fluxor = Fluxor.create
|
1183
|
-
@fluxor.update_attributes :
|
1290
|
+
@fluxor.update_attributes name: "blah"
|
1184
1291
|
@fluxor.destroy
|
1185
1292
|
end
|
1186
|
-
should
|
1293
|
+
should "only have a version for the create event" do
|
1187
1294
|
assert_equal 1, @fluxor.versions.length
|
1188
|
-
assert_equal
|
1295
|
+
assert_equal "create", @fluxor.versions.last.event
|
1189
1296
|
end
|
1190
1297
|
end
|
1191
|
-
context
|
1298
|
+
context "on update" do
|
1192
1299
|
setup do
|
1193
1300
|
Fluxor.reset_callbacks :create
|
1194
1301
|
Fluxor.reset_callbacks :update
|
@@ -1197,15 +1304,15 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
1197
1304
|
has_paper_trail :on => [:update]
|
1198
1305
|
END
|
1199
1306
|
@fluxor = Fluxor.create
|
1200
|
-
@fluxor.update_attributes :
|
1307
|
+
@fluxor.update_attributes name: "blah"
|
1201
1308
|
@fluxor.destroy
|
1202
1309
|
end
|
1203
|
-
should
|
1310
|
+
should "only have a version for the update event" do
|
1204
1311
|
assert_equal 1, @fluxor.versions.length
|
1205
|
-
assert_equal
|
1312
|
+
assert_equal "update", @fluxor.versions.last.event
|
1206
1313
|
end
|
1207
1314
|
end
|
1208
|
-
context
|
1315
|
+
context "on destroy" do
|
1209
1316
|
setup do
|
1210
1317
|
Fluxor.reset_callbacks :create
|
1211
1318
|
Fluxor.reset_callbacks :update
|
@@ -1214,15 +1321,15 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
1214
1321
|
has_paper_trail :on => [:destroy]
|
1215
1322
|
END
|
1216
1323
|
@fluxor = Fluxor.create
|
1217
|
-
@fluxor.update_attributes :
|
1324
|
+
@fluxor.update_attributes name: "blah"
|
1218
1325
|
@fluxor.destroy
|
1219
1326
|
end
|
1220
|
-
should
|
1327
|
+
should "only have a version for the destroy event" do
|
1221
1328
|
assert_equal 1, @fluxor.versions.length
|
1222
|
-
assert_equal
|
1329
|
+
assert_equal "destroy", @fluxor.versions.last.event
|
1223
1330
|
end
|
1224
1331
|
end
|
1225
|
-
context
|
1332
|
+
context "on []" do
|
1226
1333
|
setup do
|
1227
1334
|
Fluxor.reset_callbacks :create
|
1228
1335
|
Fluxor.reset_callbacks :update
|
@@ -1231,23 +1338,23 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
1231
1338
|
has_paper_trail :on => []
|
1232
1339
|
END
|
1233
1340
|
@fluxor = Fluxor.create
|
1234
|
-
@fluxor.update_attributes :
|
1341
|
+
@fluxor.update_attributes name: "blah"
|
1235
1342
|
end
|
1236
1343
|
|
1237
1344
|
teardown do
|
1238
1345
|
@fluxor.destroy
|
1239
1346
|
end
|
1240
1347
|
|
1241
|
-
should
|
1348
|
+
should "not have any versions" do
|
1242
1349
|
assert_equal 0, @fluxor.versions.length
|
1243
1350
|
end
|
1244
1351
|
|
1245
|
-
should
|
1352
|
+
should "still respond to touch_with_version" do
|
1246
1353
|
@fluxor.touch_with_version
|
1247
1354
|
assert_equal 1, @fluxor.versions.length
|
1248
1355
|
end
|
1249
1356
|
end
|
1250
|
-
context
|
1357
|
+
context "allows a symbol to be passed" do
|
1251
1358
|
setup do
|
1252
1359
|
Fluxor.reset_callbacks :create
|
1253
1360
|
Fluxor.reset_callbacks :update
|
@@ -1256,54 +1363,54 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
1256
1363
|
has_paper_trail :on => :create
|
1257
1364
|
END
|
1258
1365
|
@fluxor = Fluxor.create
|
1259
|
-
@fluxor.update_attributes :
|
1366
|
+
@fluxor.update_attributes name: "blah"
|
1260
1367
|
@fluxor.destroy
|
1261
1368
|
end
|
1262
|
-
should
|
1369
|
+
should "only have a version for hte create event" do
|
1263
1370
|
assert_equal 1, @fluxor.versions.length
|
1264
|
-
assert_equal
|
1371
|
+
assert_equal "create", @fluxor.versions.last.event
|
1265
1372
|
end
|
1266
1373
|
end
|
1267
1374
|
end
|
1268
1375
|
|
1269
|
-
context
|
1376
|
+
context "A model with column version and custom version_method" do
|
1270
1377
|
setup do
|
1271
|
-
@legacy_widget = LegacyWidget.create(:
|
1378
|
+
@legacy_widget = LegacyWidget.create(name: "foo", version: 2)
|
1272
1379
|
end
|
1273
1380
|
|
1274
|
-
should
|
1381
|
+
should "set version on create" do
|
1275
1382
|
assert_equal 2, @legacy_widget.version
|
1276
1383
|
end
|
1277
1384
|
|
1278
|
-
should
|
1279
|
-
@legacy_widget.update_attributes :
|
1385
|
+
should "allow version updates" do
|
1386
|
+
@legacy_widget.update_attributes version: 3
|
1280
1387
|
assert_equal 3, @legacy_widget.version
|
1281
1388
|
end
|
1282
1389
|
|
1283
|
-
should
|
1390
|
+
should "create a new version record" do
|
1284
1391
|
assert_equal 1, @legacy_widget.versions.size
|
1285
1392
|
end
|
1286
1393
|
end
|
1287
1394
|
|
1288
|
-
context
|
1395
|
+
context "A reified item with a column -version- and custom version_method" do
|
1289
1396
|
setup do
|
1290
|
-
widget = LegacyWidget.create(:
|
1291
|
-
%w( bar baz ).each { |name| widget.update_attributes :
|
1397
|
+
widget = LegacyWidget.create(name: "foo", version: 2)
|
1398
|
+
%w( bar baz ).each { |name| widget.update_attributes name: name }
|
1292
1399
|
@version = widget.versions.last
|
1293
1400
|
@widget = @version.reify
|
1294
1401
|
end
|
1295
1402
|
|
1296
|
-
should
|
1403
|
+
should "know which version it came from" do
|
1297
1404
|
assert_equal @version, @widget.custom_version
|
1298
1405
|
end
|
1299
1406
|
|
1300
|
-
should
|
1407
|
+
should "return its previous self" do
|
1301
1408
|
assert_equal @widget.versions[-2].reify, @widget.previous_version
|
1302
1409
|
end
|
1303
1410
|
end
|
1304
1411
|
|
1305
|
-
context
|
1306
|
-
context
|
1412
|
+
context "custom events" do
|
1413
|
+
context "on create" do
|
1307
1414
|
setup do
|
1308
1415
|
Fluxor.reset_callbacks :create
|
1309
1416
|
Fluxor.reset_callbacks :update
|
@@ -1311,16 +1418,16 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
1311
1418
|
Fluxor.instance_eval <<-END
|
1312
1419
|
has_paper_trail :on => [:create]
|
1313
1420
|
END
|
1314
|
-
@fluxor = Fluxor.new.tap { |model| model.paper_trail_event =
|
1315
|
-
@fluxor.update_attributes :
|
1421
|
+
@fluxor = Fluxor.new.tap { |model| model.paper_trail_event = "created" }
|
1422
|
+
@fluxor.update_attributes name: "blah"
|
1316
1423
|
@fluxor.destroy
|
1317
1424
|
end
|
1318
|
-
should
|
1425
|
+
should "only have a version for the created event" do
|
1319
1426
|
assert_equal 1, @fluxor.versions.length
|
1320
|
-
assert_equal
|
1427
|
+
assert_equal "created", @fluxor.versions.last.event
|
1321
1428
|
end
|
1322
1429
|
end
|
1323
|
-
context
|
1430
|
+
context "on update" do
|
1324
1431
|
setup do
|
1325
1432
|
Fluxor.reset_callbacks :create
|
1326
1433
|
Fluxor.reset_callbacks :update
|
@@ -1328,16 +1435,16 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
1328
1435
|
Fluxor.instance_eval <<-END
|
1329
1436
|
has_paper_trail :on => [:update]
|
1330
1437
|
END
|
1331
|
-
@fluxor = Fluxor.create.tap { |model| model.paper_trail_event =
|
1332
|
-
@fluxor.update_attributes :
|
1438
|
+
@fluxor = Fluxor.create.tap { |model| model.paper_trail_event = "name_updated" }
|
1439
|
+
@fluxor.update_attributes name: "blah"
|
1333
1440
|
@fluxor.destroy
|
1334
1441
|
end
|
1335
|
-
should
|
1442
|
+
should "only have a version for the name_updated event" do
|
1336
1443
|
assert_equal 1, @fluxor.versions.length
|
1337
|
-
assert_equal
|
1444
|
+
assert_equal "name_updated", @fluxor.versions.last.event
|
1338
1445
|
end
|
1339
1446
|
end
|
1340
|
-
context
|
1447
|
+
context "on destroy" do
|
1341
1448
|
setup do
|
1342
1449
|
Fluxor.reset_callbacks :create
|
1343
1450
|
Fluxor.reset_callbacks :update
|
@@ -1345,28 +1452,28 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
1345
1452
|
Fluxor.instance_eval <<-END
|
1346
1453
|
has_paper_trail :on => [:destroy]
|
1347
1454
|
END
|
1348
|
-
@fluxor = Fluxor.create.tap { |model| model.paper_trail_event =
|
1349
|
-
@fluxor.update_attributes :
|
1455
|
+
@fluxor = Fluxor.create.tap { |model| model.paper_trail_event = "destroyed" }
|
1456
|
+
@fluxor.update_attributes name: "blah"
|
1350
1457
|
@fluxor.destroy
|
1351
1458
|
end
|
1352
|
-
should
|
1459
|
+
should "only have a version for the destroy event" do
|
1353
1460
|
assert_equal 1, @fluxor.versions.length
|
1354
|
-
assert_equal
|
1461
|
+
assert_equal "destroyed", @fluxor.versions.last.event
|
1355
1462
|
end
|
1356
1463
|
end
|
1357
1464
|
end
|
1358
1465
|
|
1359
|
-
context
|
1466
|
+
context "`PaperTrail::Config.version_limit` set" do
|
1360
1467
|
setup do
|
1361
1468
|
PaperTrail.config.version_limit = 2
|
1362
|
-
@widget = Widget.create! :
|
1363
|
-
6.times { @widget.update_attribute(:name,
|
1469
|
+
@widget = Widget.create! name: "Henry"
|
1470
|
+
6.times { @widget.update_attribute(:name, FFaker::Lorem.word) }
|
1364
1471
|
end
|
1365
1472
|
|
1366
1473
|
teardown { PaperTrail.config.version_limit = nil }
|
1367
1474
|
|
1368
1475
|
should "limit the number of versions to 3 (2 plus the created at event)" do
|
1369
|
-
assert_equal
|
1476
|
+
assert_equal "create", @widget.versions.first.event
|
1370
1477
|
assert_equal 3, @widget.versions.size
|
1371
1478
|
end
|
1372
1479
|
end
|