rtiss_acts_as_versioned 0.10.3 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,87 @@
1
+ require File.join(File.dirname(__FILE__), 'abstract_unit')
2
+ require File.join(File.dirname(__FILE__), 'fixtures/audited_thing')
3
+
4
+ # Contracts the application relies on in its own callbacks:
5
+ # * attributes assigned in before_destroy end up in the deletion version
6
+ # * restoring? tells a restore apart from a normal create
7
+ class CallbacksTest < ActiveSupport::TestCase
8
+ def setup
9
+ AuditedThing.reset_test_state!
10
+ end
11
+
12
+ def teardown
13
+ AuditedThing.reset_test_state!
14
+ end
15
+
16
+ def test_before_destroy_attributes_are_stored_in_deletion_version
17
+ AuditedThing.current_user = 1
18
+ record = AuditedThing.create!(name: 'Sun')
19
+ assert_equal 1, record.find_version(1).changed_by
20
+
21
+ AuditedThing.current_user = 2
22
+ record.destroy
23
+
24
+ deletion = AuditedThing::Version.where(audited_thing_id: record.id).ordered.last
25
+ assert deletion.deleted_in_original_table
26
+ assert_equal 2, deletion.version
27
+ assert_equal 2, deletion.changed_by, 'deletion version must contain the value assigned in before_destroy'
28
+ assert_equal 1, record.find_version(1).changed_by, 'older versions stay unchanged'
29
+ end
30
+
31
+ def test_restoring_is_true_in_callbacks_during_restore
32
+ AuditedThing.current_user = 1
33
+ record = AuditedThing.create!(name: 'Sun')
34
+ AuditedThing.current_user = 2
35
+ record.destroy
36
+
37
+ AuditedThing.current_user = 3
38
+ AuditedThing.restoring_seen_in_before_create = []
39
+ AuditedThing.restore_deleted(record.id)
40
+
41
+ assert_equal [true], AuditedThing.restoring_seen_in_before_create
42
+ restored = AuditedThing.find(record.id)
43
+ assert_equal 3, restored.changed_by, 'restored record contains the restoring user'
44
+ assert_equal 3, restored.find_version(3).changed_by, 'version of the restore contains the restoring user'
45
+ assert_equal 2, restored.find_version(3).record_restored_from_version
46
+ assert_equal false, restored.restoring?, 'a freshly loaded record is not restoring'
47
+ end
48
+
49
+ def test_restoring_is_reset_after_restore
50
+ record = AuditedThing.create!(name: 'Sun')
51
+ record.destroy
52
+ AuditedThing.restore_deleted(record.id)
53
+
54
+ restored_instance = AuditedThing.last_validated
55
+ assert_equal record.id, restored_instance.id
56
+ assert_equal false, restored_instance.restoring?
57
+ end
58
+
59
+ def test_restoring_is_false_for_normal_create
60
+ AuditedThing.current_user = 1
61
+ record = AuditedThing.create!(name: 'Sun', changed_by: 9)
62
+
63
+ assert_equal [false], AuditedThing.restoring_seen_in_before_create
64
+ assert_equal false, record.restoring?
65
+ assert_equal 9, record.changed_by, 'a normal create keeps an explicitly assigned value'
66
+ end
67
+
68
+ def test_restoring_is_reset_when_restore_fails
69
+ record = AuditedThing.create!(name: 'Sun')
70
+ record.destroy
71
+ AuditedThing.reject_saves = true
72
+
73
+ assert_raises(RuntimeError) { AuditedThing.restore_deleted(record.id) }
74
+ assert_not AuditedThing.exists?(record.id)
75
+ assert_equal false, AuditedThing.last_validated.restoring?
76
+ end
77
+
78
+ def test_deletion_version_is_visible_in_before_create_of_the_version
79
+ record = AuditedThing.create!(name: 'Sun')
80
+ record.update!(name: 'Moon')
81
+ record.destroy
82
+ AuditedThing.restore_deleted(record.id)
83
+
84
+ # versions: create, update, deletion, restore
85
+ assert_equal [false, false, true, false], AuditedThing.deletion_version_seen_in_before_create
86
+ end
87
+ end
@@ -0,0 +1,54 @@
1
+ require File.join(File.dirname(__FILE__), 'abstract_unit')
2
+ require File.join(File.dirname(__FILE__), 'fixtures/good_thing')
3
+
4
+ # Test hook: versions named 'Forbidden' are invalid. Only affects records with exactly this name.
5
+ GoodThing::Version.validate do
6
+ errors.add(:name, 'must not be Forbidden') if name == 'Forbidden'
7
+ end
8
+
9
+ # If the version record can't be saved, the save/destroy/restore of the original record must fail and be rolled back.
10
+ class FailedVersionSaveTest < ActiveSupport::TestCase
11
+ def test_create_raises_and_rolls_back
12
+ count = GoodThing.count
13
+ error = assert_raises(ActiveRecord::RecordNotSaved) { GoodThing.create(name: 'Forbidden') }
14
+ assert_match(/must not be Forbidden/, error.message)
15
+ assert_kind_of GoodThing::Version, error.record
16
+ assert_equal count, GoodThing.count
17
+ end
18
+
19
+ def test_update_raises_and_rolls_back
20
+ thing = GoodThing.create!(name: 'Sun')
21
+ assert_raises(ActiveRecord::RecordNotSaved) { thing.update(name: 'Forbidden') }
22
+ # after the rollback the in-memory object is stale (Rails behaviour), so use a fresh one
23
+ assert_raises(ActiveRecord::RecordNotSaved) { GoodThing.find(thing.id).update!(name: 'Forbidden') }
24
+ assert_equal 'Sun', GoodThing.find(thing.id).name
25
+ assert_equal 1, thing.versions.count
26
+ end
27
+
28
+ def test_save_without_revision_is_not_affected
29
+ thing = GoodThing.create!(name: 'Sun')
30
+ thing.name = 'Forbidden'
31
+ assert thing.save_without_revision
32
+ assert_equal 'Forbidden', GoodThing.find(thing.id).name
33
+ end
34
+
35
+ def test_destroy_raises_and_rolls_back
36
+ thing = GoodThing.create!(name: 'Sun')
37
+ thing.name = 'Forbidden'
38
+ thing.save_without_revision
39
+ assert_raises(ActiveRecord::RecordNotSaved) { thing.destroy }
40
+ assert GoodThing.exists?(thing.id)
41
+ assert_equal 1, thing.versions.count
42
+ end
43
+
44
+ def test_restore_raises_and_rolls_back
45
+ thing = GoodThing.create!(name: 'Sun')
46
+ thing.destroy
47
+ deleted_version = thing.find_version(2)
48
+ deleted_version.update_column(:name, 'Forbidden')
49
+
50
+ assert_raises(ActiveRecord::RecordNotSaved) { GoodThing.restore_deleted(thing.id) }
51
+ assert !GoodThing.exists?(thing.id)
52
+ assert_equal 2, GoodThing::Version.where(good_thing_id: thing.id).count
53
+ end
54
+ end
@@ -0,0 +1,43 @@
1
+ # Test model for application callbacks on destroy and restore, see test/callbacks_test.rb.
2
+ # Mimics an application that records who changed a record (changed_by), similar to a mutator/user id column:
3
+ # AuditedThing.current_user = 42
4
+ # AuditedThing.create!(name: 'Sun') # changed_by => 42
5
+ class AuditedThing < ActiveRecord::Base
6
+ acts_as_versioned
7
+
8
+ # the "logged-in user" of the application
9
+ cattr_accessor :current_user
10
+ # restoring? as seen by each before_create callback, in call order
11
+ cattr_accessor :restoring_seen_in_before_create, default: []
12
+ # makes every validation fail, to test a failing restore
13
+ cattr_accessor :reject_saves, default: false
14
+ # the last instance that was validated (the restored record is not returned by Version#restore)
15
+ cattr_accessor :last_validated
16
+
17
+ validates_presence_of :name
18
+ validate { errors.add(:base, 'rejected') if reject_saves }
19
+ before_validation { self.class.last_validated = self }
20
+
21
+ before_create do
22
+ restoring_seen_in_before_create << restoring?
23
+ # a normal create keeps an explicitly assigned value, a restore records the restoring user
24
+ self.changed_by = current_user if changed_by.nil? || restoring?
25
+ end
26
+
27
+ # in memory only: the deletion version is built from this state
28
+ before_destroy { self.changed_by = current_user }
29
+
30
+ # deletion_version? as seen by each before_create callback of the version class, in call order
31
+ cattr_accessor :deletion_version_seen_in_before_create, default: []
32
+
33
+ # version records are ActiveRecord models too: an application can hook into their creation
34
+ self::Version.before_create { AuditedThing.deletion_version_seen_in_before_create << deletion_version? }
35
+
36
+ def self.reset_test_state!
37
+ self.current_user = nil
38
+ self.restoring_seen_in_before_create = []
39
+ self.deletion_version_seen_in_before_create = []
40
+ self.reject_saves = false
41
+ self.last_validated = nil
42
+ end
43
+ end
@@ -0,0 +1,61 @@
1
+ require File.join(File.dirname(__FILE__), 'abstract_unit')
2
+ require File.join(File.dirname(__FILE__), 'fixtures/page')
3
+ require File.join(File.dirname(__FILE__), 'fixtures/good_thing')
4
+
5
+ # revert_to! must always create a new version with the next version number,
6
+ # also when :if_changed / :if would not create a version for a normal save.
7
+ class RevertTest < ActiveSupport::TestCase
8
+ fixtures :pages, :pages_h, :locked_pages, :locked_pages_revisions, :authors
9
+ set_fixture_class :pages_h => Page::Version
10
+
11
+ def test_revert_to_current_version_with_if_changed
12
+ page = locked_pages(:welcome)
13
+ assert page.revert_to!(24)
14
+ assert_new_version page, 25, reverted_from: 24
15
+ end
16
+
17
+ def test_revert_to_version_with_unchanged_if_changed_attribute
18
+ page = locked_pages(:welcome)
19
+ page.versions.find_by(version: 23).update_column(:title, page.title)
20
+ assert page.revert_to!(23)
21
+ assert_new_version page, 25, reverted_from: 23
22
+ end
23
+
24
+ def test_revert_with_false_if_condition
25
+ Page.feeling_good = false
26
+ page = pages(:welcome)
27
+ assert page.revert_to!(23)
28
+ assert_equal 'Welcome to the weblg', page.reload.title
29
+ assert_new_version page, 25, reverted_from: 23
30
+ ensure
31
+ Page.feeling_good = true
32
+ end
33
+
34
+ def test_revert_to_current_version_without_options
35
+ thing = GoodThing.create!(name: 'Sun')
36
+ thing.update!(name: 'Moon')
37
+ assert thing.revert_to!(2)
38
+ assert_new_version thing, 3, reverted_from: 2
39
+ assert_equal [1, 2, 3], thing.versions.order(:version).pluck(:version)
40
+ end
41
+
42
+ def test_revert_fails_on_validation_error_without_new_version
43
+ thing = GoodThing.create!(name: 'Sun')
44
+ thing.update!(name: 'Moon')
45
+ GoodThing.create!(name: 'Sun')
46
+
47
+ assert !thing.revert_to!(1)
48
+ assert_equal 'Moon', GoodThing.find(thing.id).name
49
+ assert_equal 2, GoodThing.find(thing.id).version
50
+ assert_equal 2, thing.versions.count
51
+ end
52
+
53
+ private
54
+
55
+ def assert_new_version(record, number, reverted_from:)
56
+ assert_equal number, record.class.find(record.id).version
57
+ numbers = record.versions.pluck(record.class.version_column)
58
+ assert_equal numbers.uniq, numbers, 'duplicate version numbers'
59
+ assert_equal reverted_from, record.find_version(number).record_restored_from_version
60
+ end
61
+ end
data/test/schema.rb CHANGED
@@ -109,4 +109,25 @@ ActiveRecord::Schema.define(:version => 1) do
109
109
  end
110
110
 
111
111
  add_index :good_things_h, [:good_thing_id, :version], :unique => true
112
+
113
+ # Test model for application callbacks on destroy and restore (changed_by = who changed the record),
114
+ # see test/callbacks_test.rb
115
+ create_table :audited_things, :force => true do |t|
116
+ t.column :name, :string, :limit => 50, :null => false
117
+ t.column :changed_by, :integer
118
+ t.column :version, :integer, :default => 1
119
+ t.timestamps
120
+ end
121
+
122
+ create_table :audited_things_h, :force => true do |t|
123
+ t.column :audited_thing_id, :integer
124
+ t.column :version, :integer
125
+ t.column :name, :string, :limit => 50
126
+ t.column :changed_by, :integer
127
+ t.timestamps
128
+ t.column :deleted_in_original_table, :boolean
129
+ t.column :record_restored, :integer, :default => nil
130
+ end
131
+
132
+ add_index :audited_things_h, [:audited_thing_id, :version], :unique => true
112
133
  end
@@ -0,0 +1,69 @@
1
+ require File.join(File.dirname(__FILE__), 'abstract_unit')
2
+ require File.join(File.dirname(__FILE__), 'fixtures/good_thing')
3
+
4
+ # Values passed to find_version, restore_deleted and restore_deleted_version
5
+ # often come from request params. They must be bound/cast and never end up in the SQL text.
6
+ class SqlInjectionTest < ActiveSupport::TestCase
7
+ PAYLOAD = '999 OR 1=1'
8
+
9
+ def setup
10
+ @other = GoodThing.create!(name: 'Other', why_good: 'just there')
11
+ @thing = GoodThing.create!(name: 'Sun', why_good: 'Keeps us warm')
12
+ @thing.update!(name: 'Moon')
13
+ end
14
+
15
+ def test_find_version_casts_version
16
+ assert_raises(RuntimeError) { @thing.find_version(PAYLOAD) }
17
+ assert_equal 'Moon', @thing.find_version('2').name
18
+ end
19
+
20
+ def test_find_version_does_not_return_versions_of_other_records
21
+ assert_raises(RuntimeError) { @other.find_version('2 OR 1=1') }
22
+ end
23
+
24
+ def test_restore_deleted_casts_id
25
+ @thing.destroy
26
+ # the deleted record has the highest version in the table, an injected "OR 1=1" would restore it
27
+ assert_raises(ActiveRecord::RecordNotFound) { GoodThing.restore_deleted("0 OR 1=1") }
28
+ assert !GoodThing.exists?(@thing.id)
29
+ end
30
+
31
+ def test_restore_deleted_version_casts_id_and_version
32
+ @thing.destroy
33
+ assert_raises(ActiveRecord::RecordNotFound) { GoodThing.restore_deleted_version(@thing.id, PAYLOAD) }
34
+ assert_raises(ActiveRecord::RecordNotFound) { GoodThing.restore_deleted_version("0 OR 1=1", 1) }
35
+ assert !GoodThing.exists?(@thing.id)
36
+
37
+ GoodThing.restore_deleted_version(@thing.id.to_s, '1')
38
+ assert_equal 'Sun', GoodThing.find(@thing.id).name
39
+ end
40
+
41
+ def test_restore_deleted_with_unknown_id_raises_record_not_found
42
+ assert_raises(ActiveRecord::RecordNotFound) { GoodThing.restore_deleted(0) }
43
+ assert_raises(ActiveRecord::RecordNotFound) { GoodThing.restore_deleted_version(0, 1) }
44
+ end
45
+
46
+ def test_payload_never_appears_in_sql
47
+ @thing.destroy
48
+ sql = capture_sql do
49
+ assert_raises(RuntimeError) { @thing.find_version(PAYLOAD) }
50
+ assert_raises(ActiveRecord::RecordNotFound) { GoodThing.restore_deleted(PAYLOAD) }
51
+ assert_raises(ActiveRecord::RecordNotFound) { GoodThing.restore_deleted_version(PAYLOAD, PAYLOAD) }
52
+ end
53
+ assert sql.any?
54
+ sql.each { |statement| assert_no_match(/OR 1=1/i, statement) }
55
+ end
56
+
57
+ private
58
+
59
+ def capture_sql
60
+ statements = []
61
+ subscriber = ActiveSupport::Notifications.subscribe('sql.active_record') do |*, payload|
62
+ statements << payload[:sql] unless payload[:name] == 'SCHEMA'
63
+ end
64
+ yield
65
+ statements
66
+ ensure
67
+ ActiveSupport::Notifications.unsubscribe(subscriber)
68
+ end
69
+ end
data/test/tiss_test.rb CHANGED
@@ -171,12 +171,131 @@ class TissTest < ActiveSupport::TestCase
171
171
  assert_equal 1, restored.find_version(4).record_restored_from_version
172
172
  end
173
173
 
174
+ # record_restored is the plain column (version number or nil), record_restored? the boolean
175
+ def test_record_restored_returns_version_number
176
+ object = create_object('Clouds')
177
+ edit_object(object.id, 'Fog')
178
+ object.destroy
179
+ GoodThing.restore_deleted_version(object.id, 1)
180
+
181
+ restored_version = GoodThing.find(object.id).find_version(4)
182
+ assert_equal 1, restored_version.record_restored
183
+ assert_equal true, restored_version.record_restored?
184
+ assert_equal 1, restored_version.record_restored_from_version
185
+
186
+ normal_version = GoodThing.find(object.id).find_version(2)
187
+ assert_nil normal_version.record_restored
188
+ assert_equal false, normal_version.record_restored?
189
+ assert_nil normal_version.record_restored_from_version
190
+ end
191
+
192
+ # the new version number must come from the highest version, not from the version row with the highest id
193
+ def test_restore_uses_highest_version_not_highest_id
194
+ object = create_object('Stars')
195
+ edit_object(object.id, 'Planets')
196
+ object.destroy
197
+ # e.g. an old version imported later: highest id, low version number
198
+ GoodThing::Version.create!(good_thing_id: object.id, version: 0, name: 'Imported')
199
+
200
+ GoodThing.restore_deleted_version(object.id, 2)
201
+ assert_equal 4, GoodThing.find(object.id).version
202
+ assert_equal [0, 1, 2, 3, 4], GoodThing::Version.where(good_thing_id: object.id).order(:version).pluck(:version)
203
+ end
204
+
205
+ def test_ordered_scope_sorts_by_record_then_version
206
+ first = create_object('Alpha')
207
+ second = create_object('Beta')
208
+ edit_object(first.id, 'Alpha2')
209
+ edit_object(second.id, 'Beta2')
210
+ edit_object(first.id, 'Alpha3')
211
+
212
+ ids = [first.id, second.id]
213
+ expected = [[first.id, 1], [first.id, 2], [first.id, 3], [second.id, 1], [second.id, 2]]
214
+ assert_equal expected, GoodThing::Version.where(good_thing_id: ids).ordered.pluck(:good_thing_id, :version)
215
+ assert_equal [1, 2, 3], first.versions.ordered.pluck(:version)
216
+ assert_equal 3, GoodThing::Version.where(good_thing_id: first.id).ordered.last.version
217
+ end
218
+
219
+ def test_currently_deleted
220
+ deleted = create_object('Deleted')
221
+ deleted.destroy
222
+
223
+ restored = create_object('Restored')
224
+ restored.destroy
225
+ GoodThing.restore_deleted(restored.id)
226
+
227
+ deleted_twice = create_object('DeletedTwice')
228
+ deleted_twice.destroy
229
+ GoodThing.restore_deleted(deleted_twice.id)
230
+ GoodThing.find(deleted_twice.id).destroy
231
+
232
+ never_deleted = create_object('NeverDeleted')
233
+
234
+ ids = [deleted.id, restored.id, deleted_twice.id, never_deleted.id]
235
+ result = GoodThing::Version.currently_deleted.where(good_thing_id: ids).ordered
236
+ assert_equal [[deleted.id, 2], [deleted_twice.id, 4]], result.pluck(:good_thing_id, :version)
237
+ assert result.all?(&:deleted_in_original_table)
238
+
239
+ # usable to restore
240
+ result.find_each(&:restore)
241
+ assert GoodThing.exists?(deleted.id)
242
+ assert GoodThing.exists?(deleted_twice.id)
243
+ assert_empty GoodThing::Version.currently_deleted.where(good_thing_id: ids)
244
+ end
245
+
174
246
  def test_destroy_unsaved_record
175
247
  object = GoodThing.new(name: "Never saved")
176
248
  assert_nothing_raised { object.destroy }
177
249
  assert_equal(-1, object.highest_version)
178
250
  end
179
251
 
252
+ # Rails runs the destroy callbacks again, but nothing is deleted
253
+ def test_destroy_twice_writes_one_deletion_version
254
+ object = create_object('Twice')
255
+ object.destroy
256
+ assert_nothing_raised { object.destroy }
257
+
258
+ assert_equal [false, true], versions_of(object.id).map(&:deleted_in_original_table)
259
+ end
260
+
261
+ # the second process waits for the row lock, its DELETE then affects no row
262
+ def test_concurrent_destroy_writes_one_deletion_version
263
+ object = create_object('Concurrent')
264
+ first = GoodThing.find(object.id)
265
+ second = GoodThing.find(object.id)
266
+
267
+ first.destroy
268
+ assert_nothing_raised { second.destroy }
269
+
270
+ assert_equal [false, true], versions_of(object.id).map(&:deleted_in_original_table)
271
+ end
272
+
273
+ def test_destroy_unsaved_record_with_id_writes_no_version
274
+ object = GoodThing.new(name: 'Never saved', id: 987_654)
275
+ assert_nothing_raised { object.destroy }
276
+ assert_empty versions_of(987_654)
277
+ end
278
+
279
+ # fail-open: without the information from destroy_row the deletion is versioned as before
280
+ def test_deletion_version_is_written_without_destroy_row_information
281
+ object = create_object('Fail open')
282
+ object.define_singleton_method(:destroy_row) { self.class.where(id: id).delete_all }
283
+ object.destroy
284
+
285
+ assert_not GoodThing.exists?(object.id)
286
+ assert_equal [false, true], versions_of(object.id).map(&:deleted_in_original_table)
287
+ end
288
+
289
+ def test_deletion_version?
290
+ object = create_object('Deletion version')
291
+ object.update!(why_good: 'changed')
292
+ object.destroy
293
+ GoodThing.restore_deleted(object.id)
294
+
295
+ # versions: create, update, deletion, restore
296
+ assert_equal [false, false, true, false], versions_of(object.id).map(&:deletion_version?)
297
+ end
298
+
180
299
  private
181
300
 
182
301
  def create_object(name)
@@ -242,6 +361,10 @@ class TissTest < ActiveSupport::TestCase
242
361
  GoodThing.restore_deleted(id)
243
362
  end
244
363
 
364
+ def versions_of(id)
365
+ GoodThing::Version.where(good_thing_id: id).order(:version).to_a
366
+ end
367
+
245
368
  def restored_record_available_in_original_table?(id)
246
369
  GoodThing.exists?(id: id)
247
370
  end
@@ -350,28 +350,28 @@ class VersionedTest < ActiveSupport::TestCase
350
350
  end
351
351
 
352
352
  def test_without_locking_temporarily_disables_optimistic_locking
353
- enabled1 = false
353
+ enabled1 = nil
354
354
  block_called = false
355
-
356
- ActiveRecord::Base.lock_optimistically = true
355
+
356
+ assert LockedPage.locking_enabled?
357
357
  LockedPage.without_locking do
358
- enabled1 = ActiveRecord::Base.lock_optimistically
358
+ enabled1 = LockedPage.locking_enabled?
359
359
  block_called = true
360
360
  end
361
- enabled2 = ActiveRecord::Base.lock_optimistically
362
-
361
+
363
362
  assert block_called
364
363
  assert !enabled1
365
- assert enabled2
364
+ assert LockedPage.locking_enabled?
365
+ assert ActiveRecord::Base.lock_optimistically
366
366
  end
367
-
367
+
368
368
  def test_without_locking_reverts_optimistic_locking_settings_if_block_raises_exception
369
369
  assert_raises(RuntimeError) do
370
370
  LockedPage.without_locking do
371
371
  raise RuntimeError, "oh noes"
372
372
  end
373
373
  end
374
- assert ActiveRecord::Base.lock_optimistically
374
+ assert LockedPage.locking_enabled?
375
375
  end
376
376
 
377
377
  def test_deleted_in_original_table
@@ -0,0 +1,86 @@
1
+ require File.join(File.dirname(__FILE__), 'abstract_unit')
2
+ require File.join(File.dirname(__FILE__), 'fixtures/good_thing')
3
+ require File.join(File.dirname(__FILE__), 'fixtures/page')
4
+
5
+ # without_revision and without_locking must only affect the current thread and must be nestable.
6
+ class WithoutRevisionAndLockingTest < ActiveSupport::TestCase
7
+ fixtures :locked_pages, :locked_pages_revisions
8
+
9
+ def test_without_revision_suppresses_versions
10
+ thing = GoodThing.create!(name: 'Sun')
11
+ GoodThing.without_revision { thing.update!(name: 'Moon') }
12
+ assert_equal 1, thing.versions.count
13
+ assert_equal 'Moon', thing.reload.name
14
+ end
15
+
16
+ def test_nested_without_revision_restores_versioning
17
+ thing = GoodThing.create!(name: 'Sun')
18
+ GoodThing.without_revision do
19
+ GoodThing.without_revision { thing.update!(name: 'Moon') }
20
+ assert GoodThing.without_revision?
21
+ thing.save_without_revision
22
+ end
23
+ assert !GoodThing.without_revision?
24
+
25
+ thing.update!(name: 'Stars')
26
+ assert_equal 2, thing.versions.count
27
+ end
28
+
29
+ def test_without_revision_is_reset_after_exception
30
+ thing = GoodThing.create!(name: 'Sun')
31
+ assert_raises(RuntimeError) do
32
+ GoodThing.without_revision { raise 'oh noes' }
33
+ end
34
+ assert !GoodThing.without_revision?
35
+
36
+ thing.update!(name: 'Moon')
37
+ assert_equal 2, thing.versions.count
38
+ end
39
+
40
+ def test_without_revision_only_affects_the_given_class_and_subclasses
41
+ Page.without_revision do
42
+ assert Page.without_revision?
43
+ assert !GoodThing.without_revision?
44
+ end
45
+ LockedPage.without_revision do
46
+ assert SpecialLockedPage.without_revision?
47
+ end
48
+ SpecialLockedPage.without_revision do
49
+ assert !LockedPage.without_revision?
50
+ end
51
+ end
52
+
53
+ def test_without_revision_does_not_affect_other_threads
54
+ assert_equal [true, false], in_block_and_other_thread(GoodThing.method(:without_revision)) { GoodThing.without_revision? }
55
+ end
56
+
57
+ def test_without_locking_does_not_affect_other_threads
58
+ assert_equal [false, true], in_block_and_other_thread(LockedPage.method(:without_locking)) { !!LockedPage.locking_enabled? }
59
+ end
60
+
61
+ def test_without_locking_allows_saving_stale_records
62
+ page = LockedPage.find(locked_pages(:welcome).id)
63
+ stale = LockedPage.find(page.id)
64
+ page.update!(title: 'fresh')
65
+
66
+ stale.title = 'stale'
67
+ assert_raises(ActiveRecord::StaleObjectError) { stale.save! }
68
+
69
+ LockedPage.without_locking { stale.save! }
70
+ assert_equal 'stale', page.reload.title
71
+ end
72
+
73
+ private
74
+
75
+ # Calls the check inside the block (in this thread) and in a second thread while the block is still running.
76
+ # The second thread does not touch the database.
77
+ def in_block_and_other_thread(wrapper, &check)
78
+ inside = nil
79
+ other = nil
80
+ wrapper.call do
81
+ inside = check.call
82
+ other = Thread.new { check.call }.value
83
+ end
84
+ [inside, other]
85
+ end
86
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rtiss_acts_as_versioned
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.3
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rick Olson
@@ -9,7 +9,7 @@ authors:
9
9
  - Igor Jancev
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2026-11-26 00:00:00.000000000 Z
12
+ date: 2026-12-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -67,13 +67,16 @@ dependencies:
67
67
  - - ">="
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
- description: "Add simple versioning to ActiveRecord models (TISS version).\n\nEach
71
- model has a to-many model named mymodel_h which records all changes \n(including
72
- destroys but not deletes) made to the model. This is the version\nused by http://tiss.tuwien.ac.at
73
- and substantially differs from the original\nversion. If you want to use acts_as_versioned
74
- in your project we recommend\nto use technoweenie's version (can be found also on
75
- github)"
76
- email: igor.jancev@tuwien.ac.at
70
+ description: |-
71
+ Add simple versioning to ActiveRecord models (TISS version).
72
+
73
+ Every change to a record saves a full copy of the record in a separate history
74
+ table (Model::Version). Destroys are versioned too (deletes are not), so destroyed
75
+ records can be restored with the same id.
76
+
77
+ This fork is used by the TISS-Team at TU Wien and substantially differs from the
78
+ original. If you need a general-purpose versioning gem, consider technoweenie's
79
+ acts_as_versioned (on GitHub) or one of the actively maintained alternatives.
77
80
  executables: []
78
81
  extensions: []
79
82
  extra_rdoc_files:
@@ -92,7 +95,10 @@ files:
92
95
  - mise.toml.example
93
96
  - rtiss_acts_as_versioned.gemspec
94
97
  - test/abstract_unit.rb
98
+ - test/callbacks_test.rb
95
99
  - test/database.yml
100
+ - test/failed_version_save_test.rb
101
+ - test/fixtures/audited_thing.rb
96
102
  - test/fixtures/authors.yml
97
103
  - test/fixtures/good_thing.rb
98
104
  - test/fixtures/landmark.rb
@@ -106,9 +112,12 @@ files:
106
112
  - test/fixtures/pages_h.yml
107
113
  - test/fixtures/widget.rb
108
114
  - test/migration_test.rb
115
+ - test/revert_test.rb
109
116
  - test/schema.rb
117
+ - test/sql_injection_test.rb
110
118
  - test/tiss_test.rb
111
119
  - test/versioned_test.rb
120
+ - test/without_revision_and_locking_test.rb
112
121
  homepage: http://github.com/rtiss/rtiss_acts_as_versioned
113
122
  licenses: []
114
123
  metadata: {}