paper_trail 4.0.0 → 5.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/CONTRIBUTING.md +105 -0
- data/.github/ISSUE_TEMPLATE.md +13 -0
- data/.gitignore +2 -0
- data/.rubocop.yml +100 -0
- data/.rubocop_todo.yml +14 -0
- data/.travis.yml +11 -10
- data/Appraisals +37 -0
- data/CHANGELOG.md +173 -8
- data/Gemfile +1 -1
- data/README.md +641 -470
- data/Rakefile +19 -19
- data/doc/bug_report_template.rb +71 -0
- data/doc/warning_about_not_setting_whodunnit.md +32 -0
- data/gemfiles/ar3.gemfile +18 -0
- 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 +3 -1
- 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 +53 -5
- data/lib/paper_trail/attribute_serializers/README.md +10 -0
- data/lib/paper_trail/attribute_serializers/cast_attribute_serializer.rb +58 -0
- data/lib/paper_trail/attribute_serializers/legacy_active_record_shim.rb +48 -0
- data/lib/paper_trail/attribute_serializers/object_attribute.rb +39 -0
- data/lib/paper_trail/attribute_serializers/object_changes_attribute.rb +42 -0
- data/lib/paper_trail/cleaner.rb +41 -18
- data/lib/paper_trail/config.rb +42 -26
- 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/active_record.rb +2 -2
- data/lib/paper_trail/frameworks/cucumber.rb +1 -0
- data/lib/paper_trail/frameworks/rails/controller.rb +50 -14
- data/lib/paper_trail/frameworks/rails/engine.rb +6 -1
- data/lib/paper_trail/frameworks/rails.rb +2 -7
- data/lib/paper_trail/frameworks/rspec/helpers.rb +3 -1
- data/lib/paper_trail/frameworks/rspec.rb +5 -5
- data/lib/paper_trail/frameworks/sinatra.rb +8 -5
- data/lib/paper_trail/has_paper_trail.rb +381 -221
- data/lib/paper_trail/record_history.rb +57 -0
- data/lib/paper_trail/reifier.rb +450 -0
- data/lib/paper_trail/serializers/json.rb +7 -7
- data/lib/paper_trail/serializers/yaml.rb +31 -12
- data/lib/paper_trail/version_association_concern.rb +6 -2
- data/lib/paper_trail/version_concern.rb +200 -287
- data/lib/paper_trail/version_number.rb +6 -9
- data/lib/paper_trail.rb +169 -137
- data/paper_trail.gemspec +41 -43
- 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 +23 -6
- data/spec/models/boolit_spec.rb +8 -8
- data/spec/models/callback_modifier_spec.rb +96 -0
- 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 +50 -28
- 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 +40 -11
- 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 +86 -55
- 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 +3 -4
- data/spec/paper_trail/config_spec.rb +33 -0
- 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 +42 -17
- 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 +45 -0
- data/test/dummy/app/models/car.rb +3 -0
- data/test/dummy/app/models/chapter.rb +9 -0
- data/test/dummy/app/models/citation.rb +5 -0
- 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 +5 -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/paragraph.rb +5 -0
- 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 +5 -0
- data/test/dummy/app/models/section.rb +6 -0
- 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/application.rb +20 -9
- data/test/dummy/config/boot.rb +5 -5
- data/test/dummy/config/database.postgres.yml +1 -1
- 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 +4 -3
- 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/config.ru +1 -1
- data/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb +148 -68
- data/test/dummy/db/schema.rb +119 -31
- 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 +78 -18
- data/test/time_travel_helper.rb +1 -15
- data/test/unit/associations_test.rb +1016 -0
- data/test/unit/cleaner_test.rb +66 -60
- data/test/unit/inheritance_column_test.rb +19 -19
- data/test/unit/model_test.rb +646 -1071
- data/test/unit/protected_attrs_test.rb +19 -14
- data/test/unit/serializer_test.rb +44 -43
- data/test/unit/serializers/json_test.rb +28 -21
- 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 +16 -14
- data/test/unit/timestamp_test.rb +10 -12
- data/test/unit/version_test.rb +88 -70
- metadata +166 -72
- data/gemfiles/3.0.gemfile +0 -52
data/spec/models/version_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
require
|
1
|
+
require "rails_helper"
|
2
2
|
|
3
|
-
describe PaperTrail::Version, :
|
3
|
+
describe PaperTrail::Version, type: :model do
|
4
4
|
it "should include the `VersionConcern` module to get base functionality" do
|
5
5
|
expect(PaperTrail::Version).to include(PaperTrail::VersionConcern)
|
6
6
|
end
|
@@ -13,8 +13,8 @@ describe PaperTrail::Version, :type => :model do
|
|
13
13
|
it { is_expected.to have_db_column(:object).of_type(:text) }
|
14
14
|
it { is_expected.to have_db_column(:created_at).of_type(:datetime) }
|
15
15
|
|
16
|
-
describe "object_changes column", :
|
17
|
-
let(:widget) { Widget.create!(:
|
16
|
+
describe "object_changes column", versioning: true do
|
17
|
+
let(:widget) { Widget.create!(name: "Dashboard") }
|
18
18
|
let(:value) { widget.versions.last.object_changes }
|
19
19
|
|
20
20
|
context "serializer is YAML" do
|
@@ -43,7 +43,7 @@ describe PaperTrail::Version, :type => :model do
|
|
43
43
|
|
44
44
|
describe "Methods" do
|
45
45
|
describe "Instance" do
|
46
|
-
subject { PaperTrail::Version.new
|
46
|
+
subject { PaperTrail::Version.new }
|
47
47
|
|
48
48
|
describe '#paper_trail_originator' do
|
49
49
|
it { is_expected.to respond_to(:paper_trail_originator) }
|
@@ -56,12 +56,12 @@ describe PaperTrail::Version, :type => :model do
|
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
-
context "Has previous version", :
|
60
|
-
let(:name) {
|
61
|
-
let(:widget) { Widget.create!(:
|
59
|
+
context "Has previous version", versioning: true do
|
60
|
+
let(:name) { FFaker::Name.name }
|
61
|
+
let(:widget) { Widget.create!(name: FFaker::Name.name) }
|
62
62
|
before do
|
63
|
-
widget.versions.first.update_attributes!(:
|
64
|
-
widget.update_attributes!(:
|
63
|
+
widget.versions.first.update_attributes!(whodunnit: name)
|
64
|
+
widget.update_attributes!(name: FFaker::Name.first_name)
|
65
65
|
end
|
66
66
|
subject { widget.versions.last }
|
67
67
|
|
@@ -76,13 +76,13 @@ describe PaperTrail::Version, :type => :model do
|
|
76
76
|
describe "#originator" do
|
77
77
|
it { is_expected.to respond_to(:originator) }
|
78
78
|
|
79
|
-
it
|
79
|
+
it "should set the invoke `paper_trail_originator`" do
|
80
80
|
allow(ActiveSupport::Deprecation).to receive(:warn)
|
81
81
|
is_expected.to receive(:paper_trail_originator)
|
82
82
|
subject.originator
|
83
83
|
end
|
84
84
|
|
85
|
-
it
|
85
|
+
it "should display a deprecation warning" do
|
86
86
|
expect(ActiveSupport::Deprecation).to receive(:warn).
|
87
87
|
with(/Use paper_trail_originator instead of originator/)
|
88
88
|
subject.originator
|
@@ -90,9 +90,10 @@ describe PaperTrail::Version, :type => :model do
|
|
90
90
|
end
|
91
91
|
|
92
92
|
describe '#terminator' do
|
93
|
-
|
93
|
+
let(:attributes) { { whodunnit: FFaker::Name.first_name } }
|
94
|
+
subject { PaperTrail::Version.new attributes }
|
94
95
|
|
95
|
-
|
96
|
+
it { is_expected.to respond_to(:terminator) }
|
96
97
|
|
97
98
|
it "is an alias for the `whodunnit` attribute" do
|
98
99
|
expect(subject.terminator).to eq(attributes[:whodunnit])
|
@@ -110,10 +111,10 @@ describe PaperTrail::Version, :type => :model do
|
|
110
111
|
|
111
112
|
describe "Class" do
|
112
113
|
column_overrides = [false]
|
113
|
-
if ENV[
|
114
|
-
column_overrides <<
|
114
|
+
if ENV["DB"] == "postgres" && ::ActiveRecord::VERSION::MAJOR >= 4
|
115
|
+
column_overrides << "json"
|
115
116
|
# 'jsonb' column types are only supported for ActiveRecord 4.2+
|
116
|
-
column_overrides <<
|
117
|
+
column_overrides << "jsonb" if ::ActiveRecord::VERSION::STRING >= "4.2"
|
117
118
|
end
|
118
119
|
|
119
120
|
column_overrides.shuffle.each do |override|
|
@@ -121,9 +122,13 @@ describe PaperTrail::Version, :type => :model do
|
|
121
122
|
before do
|
122
123
|
if override
|
123
124
|
ActiveRecord::Base.connection.execute("SAVEPOINT pgtest;")
|
124
|
-
%w
|
125
|
-
ActiveRecord::Base.connection.execute(
|
126
|
-
|
125
|
+
%w(object object_changes).each do |column|
|
126
|
+
ActiveRecord::Base.connection.execute(
|
127
|
+
"ALTER TABLE versions DROP COLUMN #{column};"
|
128
|
+
)
|
129
|
+
ActiveRecord::Base.connection.execute(
|
130
|
+
"ALTER TABLE versions ADD COLUMN #{column} #{override};"
|
131
|
+
)
|
127
132
|
end
|
128
133
|
PaperTrail::Version.reset_column_information
|
129
134
|
end
|
@@ -140,92 +145,131 @@ describe PaperTrail::Version, :type => :model do
|
|
140
145
|
|
141
146
|
context "invalid arguments" do
|
142
147
|
it "should raise an error" do
|
143
|
-
expect {
|
144
|
-
|
148
|
+
expect {
|
149
|
+
PaperTrail::Version.where_object(:foo)
|
150
|
+
}.to raise_error(ArgumentError)
|
151
|
+
expect {
|
152
|
+
PaperTrail::Version.where_object([])
|
153
|
+
}.to raise_error(ArgumentError)
|
145
154
|
end
|
146
155
|
end
|
147
156
|
|
148
|
-
context "valid arguments", :
|
157
|
+
context "valid arguments", versioning: true do
|
149
158
|
let(:widget) { Widget.new }
|
150
|
-
let(:name) {
|
159
|
+
let(:name) { FFaker::Name.first_name }
|
151
160
|
let(:int) { rand(10) + 1 }
|
152
161
|
|
153
162
|
before do
|
154
|
-
widget.update_attributes!(:
|
155
|
-
widget.update_attributes!(:
|
156
|
-
widget.update_attributes!(:
|
163
|
+
widget.update_attributes!(name: name, an_integer: int)
|
164
|
+
widget.update_attributes!(name: "foobar", an_integer: 100)
|
165
|
+
widget.update_attributes!(name: FFaker::Name.last_name, an_integer: 15)
|
157
166
|
end
|
158
167
|
|
159
168
|
context "`serializer == YAML`" do
|
160
|
-
specify
|
169
|
+
specify do
|
170
|
+
expect(PaperTrail.serializer).to be PaperTrail::Serializers::YAML
|
171
|
+
end
|
161
172
|
|
162
173
|
it "should be able to locate versions according to their `object` contents" do
|
163
|
-
expect(
|
164
|
-
|
174
|
+
expect(
|
175
|
+
PaperTrail::Version.where_object(name: name)
|
176
|
+
).to eq([widget.versions[1]])
|
177
|
+
expect(
|
178
|
+
PaperTrail::Version.where_object(an_integer: 100)
|
179
|
+
).to eq([widget.versions[2]])
|
165
180
|
end
|
166
181
|
end
|
167
182
|
|
168
|
-
context "
|
169
|
-
before(:all)
|
170
|
-
|
183
|
+
context "JSON serializer" do
|
184
|
+
before(:all) do
|
185
|
+
PaperTrail.serializer = PaperTrail::Serializers::JSON
|
186
|
+
end
|
187
|
+
|
188
|
+
specify do
|
189
|
+
expect(PaperTrail.serializer).to be PaperTrail::Serializers::JSON
|
190
|
+
end
|
171
191
|
|
172
192
|
it "should be able to locate versions according to their `object` contents" do
|
173
|
-
expect(
|
174
|
-
|
193
|
+
expect(
|
194
|
+
PaperTrail::Version.where_object(name: name)
|
195
|
+
).to eq([widget.versions[1]])
|
196
|
+
expect(
|
197
|
+
PaperTrail::Version.where_object(an_integer: 100)
|
198
|
+
).to eq([widget.versions[2]])
|
175
199
|
end
|
176
200
|
|
177
|
-
after(:all)
|
201
|
+
after(:all) do
|
202
|
+
PaperTrail.serializer = PaperTrail::Serializers::YAML
|
203
|
+
end
|
178
204
|
end
|
179
205
|
end
|
180
206
|
end
|
181
207
|
|
182
208
|
describe '#where_object_changes' do
|
183
|
-
it { expect(PaperTrail::Version).to respond_to(:where_object_changes) }
|
184
|
-
|
185
209
|
context "invalid arguments" do
|
186
210
|
it "should raise an error" do
|
187
|
-
expect {
|
188
|
-
|
211
|
+
expect {
|
212
|
+
PaperTrail::Version.where_object_changes(:foo)
|
213
|
+
}.to raise_error(ArgumentError)
|
214
|
+
expect {
|
215
|
+
PaperTrail::Version.where_object_changes([])
|
216
|
+
}.to raise_error(ArgumentError)
|
189
217
|
end
|
190
218
|
end
|
191
219
|
|
192
|
-
context "valid arguments", :
|
220
|
+
context "valid arguments", versioning: true do
|
193
221
|
let(:widget) { Widget.new }
|
194
|
-
let(:name) {
|
222
|
+
let(:name) { FFaker::Name.first_name }
|
195
223
|
let(:int) { rand(5) + 2 }
|
196
224
|
|
197
225
|
before do
|
198
|
-
widget.update_attributes!(:
|
199
|
-
widget.update_attributes!(:
|
200
|
-
widget.update_attributes!(:
|
226
|
+
widget.update_attributes!(name: name, an_integer: 0)
|
227
|
+
widget.update_attributes!(name: "foobar", an_integer: 77)
|
228
|
+
widget.update_attributes!(name: FFaker::Name.last_name, an_integer: int)
|
201
229
|
end
|
202
230
|
|
203
|
-
context "
|
231
|
+
context "YAML serializer" do
|
204
232
|
specify { expect(PaperTrail.serializer).to be PaperTrail::Serializers::YAML }
|
205
233
|
|
206
|
-
it "
|
207
|
-
expect(
|
208
|
-
|
209
|
-
|
234
|
+
it "locates versions according to their `object_changes` contents" do
|
235
|
+
expect(
|
236
|
+
widget.versions.where_object_changes(name: name)
|
237
|
+
).to eq(widget.versions[0..1])
|
238
|
+
expect(
|
239
|
+
widget.versions.where_object_changes(an_integer: 77)
|
240
|
+
).to eq(widget.versions[1..2])
|
241
|
+
expect(
|
242
|
+
widget.versions.where_object_changes(an_integer: int)
|
243
|
+
).to eq([widget.versions.last])
|
210
244
|
end
|
211
245
|
|
212
|
-
it "
|
213
|
-
expect(
|
246
|
+
it "handles queries for multiple attributes" do
|
247
|
+
expect(
|
248
|
+
widget.versions.where_object_changes(an_integer: 77, name: "foobar")
|
249
|
+
).to eq(widget.versions[1..2])
|
214
250
|
end
|
215
251
|
end
|
216
252
|
|
217
|
-
context "
|
253
|
+
context "JSON serializer" do
|
218
254
|
before(:all) { PaperTrail.serializer = PaperTrail::Serializers::JSON }
|
219
255
|
specify { expect(PaperTrail.serializer).to be PaperTrail::Serializers::JSON }
|
220
256
|
|
221
|
-
it "
|
222
|
-
expect(
|
223
|
-
|
224
|
-
|
257
|
+
it "locates versions according to their `object_changes` contents" do
|
258
|
+
expect(
|
259
|
+
widget.versions.where_object_changes(name: name)
|
260
|
+
).to eq(widget.versions[0..1])
|
261
|
+
expect(
|
262
|
+
widget.versions.where_object_changes(an_integer: 77)
|
263
|
+
).to eq(widget.versions[1..2])
|
264
|
+
expect(
|
265
|
+
widget.versions.where_object_changes(an_integer: int)
|
266
|
+
).to eq([widget.versions.last])
|
225
267
|
end
|
226
268
|
|
227
|
-
it "
|
228
|
-
expect(
|
269
|
+
it "handles queries for multiple attributes" do
|
270
|
+
expect(
|
271
|
+
widget.versions.where_object_changes(an_integer: 77, name: "foobar")
|
272
|
+
).to eq(widget.versions[1..2])
|
229
273
|
end
|
230
274
|
|
231
275
|
after(:all) { PaperTrail.serializer = PaperTrail::Serializers::YAML }
|
data/spec/models/widget_spec.rb
CHANGED
@@ -1,63 +1,66 @@
|
|
1
|
-
require
|
1
|
+
require "rails_helper"
|
2
2
|
|
3
|
-
describe Widget, :
|
4
|
-
describe
|
3
|
+
describe Widget, type: :model do
|
4
|
+
describe "`be_versioned` matcher" do
|
5
5
|
it { is_expected.to be_versioned }
|
6
6
|
end
|
7
7
|
|
8
|
-
let(:widget) { Widget.create! :
|
8
|
+
let(:widget) { Widget.create! name: "Bob", an_integer: 1 }
|
9
9
|
|
10
|
-
describe
|
10
|
+
describe "`have_a_version_with` matcher", versioning: true do
|
11
11
|
before do
|
12
|
-
widget.update_attributes!(:
|
13
|
-
widget.update_attributes!(:
|
14
|
-
widget.update_attributes!(:
|
12
|
+
widget.update_attributes!(name: "Leonard", an_integer: 1)
|
13
|
+
widget.update_attributes!(name: "Tom")
|
14
|
+
widget.update_attributes!(name: "Bob")
|
15
15
|
end
|
16
16
|
|
17
17
|
it "is possible to do assertions on versions" do
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
expect(widget).to have_a_version_with name: "Leonard", an_integer: 1
|
19
|
+
expect(widget).to have_a_version_with an_integer: 1
|
20
|
+
expect(widget).to have_a_version_with name: "Tom"
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
describe "
|
25
|
-
context
|
26
|
-
it
|
24
|
+
describe "versioning option" do
|
25
|
+
context "enabled", versioning: true do
|
26
|
+
it "should enable versioning" do
|
27
27
|
expect(widget.versions.size).to eq(1)
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
-
context
|
32
|
-
it
|
31
|
+
context "disabled (default)" do
|
32
|
+
it "should not enable versioning" do
|
33
33
|
expect(widget.versions.size).to eq(0)
|
34
34
|
end
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
-
describe "Callbacks", :
|
38
|
+
describe "Callbacks", versioning: true do
|
39
39
|
describe :before_save do
|
40
|
-
context
|
41
|
-
before { widget.update_attributes!(:
|
40
|
+
context ":on => :update" do
|
41
|
+
before { widget.update_attributes!(name: "Foobar") }
|
42
42
|
|
43
43
|
subject { widget.versions.last.reify }
|
44
44
|
|
45
|
-
it "
|
46
|
-
|
45
|
+
it "resets value for timestamp attrs for update so that value gets updated properly" do
|
46
|
+
# Travel 1 second because MySQL lacks sub-second resolution
|
47
|
+
Timecop.travel(1) do
|
48
|
+
expect { subject.save! }.to change(subject, :updated_at)
|
49
|
+
end
|
47
50
|
end
|
48
51
|
end
|
49
52
|
end
|
50
53
|
|
51
54
|
describe :after_create do
|
52
|
-
let(:widget) { Widget.create!(:
|
55
|
+
let(:widget) { Widget.create!(name: "Foobar", created_at: Time.now - 1.week) }
|
53
56
|
|
54
|
-
it "corresponding version should use the widget's `
|
55
|
-
expect(widget.versions.last.created_at.to_i).to eq(widget.
|
57
|
+
it "corresponding version should use the widget's `updated_at`" do
|
58
|
+
expect(widget.versions.last.created_at.to_i).to eq(widget.updated_at.to_i)
|
56
59
|
end
|
57
60
|
end
|
58
61
|
|
59
62
|
describe :after_update do
|
60
|
-
before { widget.update_attributes!(:
|
63
|
+
before { widget.update_attributes!(name: "Foobar", updated_at: Time.now + 1.week) }
|
61
64
|
|
62
65
|
subject { widget.versions.last.reify }
|
63
66
|
|
@@ -87,13 +90,13 @@ describe Widget, :type => :model do
|
|
87
90
|
end
|
88
91
|
|
89
92
|
describe :after_rollback do
|
90
|
-
let(:rolled_back_name) {
|
93
|
+
let(:rolled_back_name) { "Big Moo" }
|
91
94
|
|
92
95
|
before do
|
93
96
|
begin
|
94
97
|
widget.transaction do
|
95
|
-
widget.update_attributes!(:
|
96
|
-
widget.update_attributes!(:
|
98
|
+
widget.update_attributes!(name: rolled_back_name)
|
99
|
+
widget.update_attributes!(name: Widget::EXCLUDED_NAME)
|
97
100
|
end
|
98
101
|
rescue ActiveRecord::RecordInvalid
|
99
102
|
widget.reload
|
@@ -102,31 +105,47 @@ describe Widget, :type => :model do
|
|
102
105
|
end
|
103
106
|
end
|
104
107
|
|
105
|
-
it
|
108
|
+
it "does not create an event for changes that did not happen" do
|
106
109
|
widget.versions.map(&:changeset).each do |changeset|
|
107
|
-
expect(changeset.fetch(
|
110
|
+
expect(changeset.fetch("name", [])).to_not include(rolled_back_name)
|
108
111
|
end
|
109
112
|
end
|
113
|
+
|
114
|
+
it "has not yet loaded the assocation" do
|
115
|
+
expect(widget.versions).to_not be_loaded
|
116
|
+
end
|
110
117
|
end
|
111
118
|
end
|
112
119
|
|
113
|
-
describe "Association", :
|
120
|
+
describe "Association", versioning: true do
|
114
121
|
describe "sort order" do
|
115
122
|
it "should sort by the timestamp order from the `VersionConcern`" do
|
116
123
|
expect(widget.versions.to_sql).to eq(
|
117
|
-
widget.versions.reorder(PaperTrail::Version.timestamp_sort_order).to_sql
|
124
|
+
widget.versions.reorder(PaperTrail::Version.timestamp_sort_order).to_sql
|
125
|
+
)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
if defined?(ActiveRecord::IdentityMap) && ActiveRecord::IdentityMap.respond_to?(:without)
|
131
|
+
describe "IdentityMap", versioning: true do
|
132
|
+
it "should not clobber the IdentityMap when reifying" do
|
133
|
+
widget.update_attributes name: "Henry", created_at: Time.now - 1.day
|
134
|
+
widget.update_attributes name: "Harry"
|
135
|
+
expect(ActiveRecord::IdentityMap).to receive(:without).once
|
136
|
+
widget.versions.last.reify
|
118
137
|
end
|
119
138
|
end
|
120
139
|
end
|
121
140
|
|
122
141
|
describe "Methods" do
|
123
|
-
describe "Instance", :
|
142
|
+
describe "Instance", versioning: true do
|
124
143
|
describe '#paper_trail_originator' do
|
125
144
|
it { is_expected.to respond_to(:paper_trail_originator) }
|
126
145
|
|
127
146
|
describe "return value" do
|
128
|
-
let(:orig_name) {
|
129
|
-
let(:new_name) {
|
147
|
+
let(:orig_name) { FFaker::Name.name }
|
148
|
+
let(:new_name) { FFaker::Name.name }
|
130
149
|
before { PaperTrail.whodunnit = orig_name }
|
131
150
|
|
132
151
|
context "accessed from live model instance" do
|
@@ -134,16 +153,16 @@ describe Widget, :type => :model do
|
|
134
153
|
|
135
154
|
it "should return the originator for the model at a given state" do
|
136
155
|
expect(widget.paper_trail_originator).to eq(orig_name)
|
137
|
-
widget.whodunnit(new_name) { |w| w.update_attributes(:
|
156
|
+
widget.whodunnit(new_name) { |w| w.update_attributes(name: "Elizabeth") }
|
138
157
|
expect(widget.paper_trail_originator).to eq(new_name)
|
139
158
|
end
|
140
159
|
end
|
141
160
|
|
142
161
|
context "accessed from a reified model instance" do
|
143
162
|
before do
|
144
|
-
widget.update_attributes(:
|
163
|
+
widget.update_attributes(name: "Andy")
|
145
164
|
PaperTrail.whodunnit = new_name
|
146
|
-
widget.update_attributes(:
|
165
|
+
widget.update_attributes(name: "Elizabeth")
|
147
166
|
end
|
148
167
|
|
149
168
|
context "default behavior (no `options[:dup]` option passed in)" do
|
@@ -159,7 +178,7 @@ describe Widget, :type => :model do
|
|
159
178
|
end
|
160
179
|
|
161
180
|
context "creating a new instance (`options[:dup] == true`)" do
|
162
|
-
let(:reified_widget) { widget.versions[1].reify(:
|
181
|
+
let(:reified_widget) { widget.versions[1].reify(dup: true) }
|
163
182
|
|
164
183
|
it "should return the appropriate originator" do
|
165
184
|
expect(reified_widget.paper_trail_originator).to eq(orig_name)
|
@@ -178,13 +197,13 @@ describe Widget, :type => :model do
|
|
178
197
|
|
179
198
|
it { is_expected.to respond_to(:originator) }
|
180
199
|
|
181
|
-
it
|
200
|
+
it "should set the invoke `paper_trail_originator`" do
|
182
201
|
allow(::ActiveSupport::Deprecation).to receive(:warn)
|
183
202
|
is_expected.to receive(:paper_trail_originator)
|
184
203
|
subject.originator
|
185
204
|
end
|
186
205
|
|
187
|
-
it
|
206
|
+
it "should display a deprecation warning" do
|
188
207
|
expect(::ActiveSupport::Deprecation).to receive(:warn).
|
189
208
|
with(/Use paper_trail_originator instead of originator/)
|
190
209
|
subject.originator
|
@@ -196,7 +215,7 @@ describe Widget, :type => :model do
|
|
196
215
|
|
197
216
|
context "Timestamp argument is AFTER object has been destroyed" do
|
198
217
|
before do
|
199
|
-
widget.update_attribute(:name,
|
218
|
+
widget.update_attribute(:name, "foobar")
|
200
219
|
widget.destroy
|
201
220
|
end
|
202
221
|
|
@@ -211,13 +230,15 @@ describe Widget, :type => :model do
|
|
211
230
|
|
212
231
|
context "no block given" do
|
213
232
|
it "should raise an error" do
|
214
|
-
expect {
|
233
|
+
expect {
|
234
|
+
widget.whodunnit("Ben")
|
235
|
+
}.to raise_error(ArgumentError, "expected to receive a block")
|
215
236
|
end
|
216
237
|
end
|
217
238
|
|
218
239
|
context "block given" do
|
219
|
-
let(:orig_name) {
|
220
|
-
let(:new_name) {
|
240
|
+
let(:orig_name) { FFaker::Name.name }
|
241
|
+
let(:new_name) { FFaker::Name.name }
|
221
242
|
|
222
243
|
before do
|
223
244
|
PaperTrail.whodunnit = orig_name
|
@@ -227,19 +248,23 @@ describe Widget, :type => :model do
|
|
227
248
|
it "should modify value of `PaperTrail.whodunnit` while executing the block" do
|
228
249
|
widget.whodunnit(new_name) do
|
229
250
|
expect(PaperTrail.whodunnit).to eq(new_name)
|
230
|
-
widget.update_attributes(:
|
251
|
+
widget.update_attributes(name: "Elizabeth")
|
231
252
|
end
|
232
253
|
expect(widget.versions.last.whodunnit).to eq(new_name)
|
233
254
|
end
|
234
255
|
|
235
|
-
|
236
|
-
|
237
|
-
|
256
|
+
context "after executing the block" do
|
257
|
+
it "reverts value of whodunnit to previous value" do
|
258
|
+
widget.whodunnit(new_name) { |w| w.update_attributes(name: "Elizabeth") }
|
259
|
+
expect(PaperTrail.whodunnit).to eq(orig_name)
|
260
|
+
end
|
238
261
|
end
|
239
262
|
|
240
263
|
context "error within block" do
|
241
|
-
it "
|
242
|
-
expect {
|
264
|
+
it "still reverts the whodunnit value to previous value" do
|
265
|
+
expect {
|
266
|
+
widget.whodunnit(new_name) { raise }
|
267
|
+
}.to raise_error(RuntimeError)
|
243
268
|
expect(PaperTrail.whodunnit).to eq(orig_name)
|
244
269
|
end
|
245
270
|
end
|
@@ -251,13 +276,19 @@ describe Widget, :type => :model do
|
|
251
276
|
|
252
277
|
it "creates a version" do
|
253
278
|
count = widget.versions.size
|
254
|
-
|
279
|
+
# Travel 1 second because MySQL lacks sub-second resolution
|
280
|
+
Timecop.travel(1) do
|
281
|
+
widget.touch_with_version
|
282
|
+
end
|
255
283
|
expect(widget.versions.size).to eq(count + 1)
|
256
284
|
end
|
257
285
|
|
258
286
|
it "increments the `:updated_at` timestamp" do
|
259
287
|
time_was = widget.updated_at
|
260
|
-
|
288
|
+
# Travel 1 second because MySQL lacks sub-second resolution
|
289
|
+
Timecop.travel(1) do
|
290
|
+
widget.touch_with_version
|
291
|
+
end
|
261
292
|
expect(widget.updated_at).to be > time_was
|
262
293
|
end
|
263
294
|
end
|
@@ -275,7 +306,7 @@ describe Widget, :type => :model do
|
|
275
306
|
describe '#paper_trail_off!' do
|
276
307
|
it { is_expected.to respond_to(:paper_trail_off!) }
|
277
308
|
|
278
|
-
it
|
309
|
+
it "should set the `paper_trail_enabled_for_model?` to `false`" do
|
279
310
|
expect(subject.paper_trail_enabled_for_model?).to be true
|
280
311
|
subject.paper_trail_off!
|
281
312
|
expect(subject.paper_trail_enabled_for_model?).to be false
|
@@ -287,7 +318,7 @@ describe Widget, :type => :model do
|
|
287
318
|
|
288
319
|
it { is_expected.to respond_to(:paper_trail_on!) }
|
289
320
|
|
290
|
-
it
|
321
|
+
it "should set the `paper_trail_enabled_for_model?` to `true`" do
|
291
322
|
expect(subject.paper_trail_enabled_for_model?).to be false
|
292
323
|
subject.paper_trail_on!
|
293
324
|
expect(subject.paper_trail_enabled_for_model?).to be true
|
@@ -1,30 +1,29 @@
|
|
1
|
-
require
|
1
|
+
require "rails_helper"
|
2
2
|
|
3
3
|
describe PaperTrail::VersionConcern do
|
4
|
+
before(:all) { require "support/alt_db_init" }
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
it 'allows included class to have different connections' do
|
6
|
+
it "allows included class to have different connections" do
|
8
7
|
expect(Foo::Version.connection).not_to eq(Bar::Version.connection)
|
9
8
|
end
|
10
9
|
|
11
|
-
it
|
10
|
+
it "allows custom version class to share connection with superclass" do
|
12
11
|
expect(Foo::Version.connection).to eq(Foo::Document.connection)
|
13
12
|
expect(Bar::Version.connection).to eq(Bar::Document.connection)
|
14
13
|
end
|
15
14
|
|
16
|
-
it
|
17
|
-
expect(Foo::Document.version_class_name).to eq(
|
18
|
-
expect(Bar::Document.version_class_name).to eq(
|
15
|
+
it "can be used with class_name option" do
|
16
|
+
expect(Foo::Document.version_class_name).to eq("Foo::Version")
|
17
|
+
expect(Bar::Document.version_class_name).to eq("Bar::Version")
|
19
18
|
end
|
20
19
|
|
21
|
-
describe
|
20
|
+
describe "persistence", versioning: true do
|
22
21
|
before do
|
23
|
-
@foo_doc = Foo::Document.create!(:
|
24
|
-
@bar_doc = Bar::Document.create!(:
|
22
|
+
@foo_doc = Foo::Document.create!(name: "foobar")
|
23
|
+
@bar_doc = Bar::Document.create!(name: "raboof")
|
25
24
|
end
|
26
25
|
|
27
|
-
it
|
26
|
+
it "should store versions in the correct corresponding db location" do
|
28
27
|
expect(@foo_doc.versions.first).to be_instance_of(Foo::Version)
|
29
28
|
expect(@bar_doc.versions.first).to be_instance_of(Bar::Version)
|
30
29
|
end
|
@@ -1,7 +1,6 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe PaperTrail::VERSION do
|
4
|
-
|
5
4
|
describe "Constants" do
|
6
5
|
subject { PaperTrail::VERSION }
|
7
6
|
|
@@ -29,11 +28,11 @@ describe PaperTrail::VERSION do
|
|
29
28
|
|
30
29
|
it "should join the numbers into a period separated string" do
|
31
30
|
expect(subject::STRING).to eq(
|
32
|
-
[subject::MAJOR, subject::MINOR, subject::TINY, subject::PRE].compact.join(
|
31
|
+
[subject::MAJOR, subject::MINOR, subject::TINY, subject::PRE].compact.join(".")
|
32
|
+
)
|
33
33
|
end
|
34
34
|
end
|
35
35
|
end
|
36
|
-
|
37
36
|
end
|
38
37
|
|
39
38
|
describe PaperTrail do
|