acts_as_archival 1.1.1 → 2.0.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.
- checksums.yaml +5 -5
- data/.gitignore +1 -1
- data/.rubocop.yml +71 -0
- data/.rubocop_todo.yml +98 -0
- data/.travis.yml +23 -0
- data/Appraisals +7 -7
- data/CHANGELOG.md +29 -0
- data/Gemfile.lock +70 -0
- data/README.md +49 -19
- data/Rakefile +6 -3
- data/acts_as_archival.gemspec +23 -17
- data/gemfiles/rails_5.2.gemfile +8 -0
- data/gemfiles/{rails_4.2.gemfile → rails_6.0.gemfile} +2 -2
- data/gemfiles/{rails_5.0.gemfile → rails_6.1.gemfile} +2 -2
- data/lib/acts_as_archival/version.rb +3 -1
- data/lib/acts_as_archival.rb +2 -0
- data/lib/expected_behavior/acts_as_archival.rb +111 -77
- data/lib/expected_behavior/acts_as_archival_active_record_methods.rb +29 -4
- data/lib/expected_behavior/association_operation/archive.rb +1 -1
- data/lib/expected_behavior/association_operation/base.rb +12 -9
- data/lib/expected_behavior/association_operation/unarchive.rb +2 -2
- data/script/setup +0 -3
- data/test/ambiguous_table_test.rb +3 -1
- data/test/application_record_test.rb +5 -3
- data/test/associations_test.rb +19 -17
- data/test/basic_test.rb +16 -14
- data/test/callbacks_test.rb +8 -6
- data/test/column_test.rb +8 -6
- data/test/deep_nesting_test.rb +6 -4
- data/test/fixtures/another_polys_holder.rb +11 -0
- data/test/fixtures/application_record.rb +2 -0
- data/test/fixtures/application_record_row.rb +2 -0
- data/test/fixtures/archival.rb +9 -7
- data/test/fixtures/archival_grandkid.rb +2 -0
- data/test/fixtures/archival_kid.rb +3 -1
- data/test/fixtures/archival_table_name.rb +2 -0
- data/test/fixtures/callback_archival_4.rb +2 -0
- data/test/fixtures/callback_archival_5.rb +4 -1
- data/test/fixtures/exploder.rb +2 -0
- data/test/fixtures/independent_archival.rb +2 -0
- data/test/fixtures/missing_archive_number.rb +2 -0
- data/test/fixtures/missing_archived_at.rb +2 -0
- data/test/fixtures/plain.rb +2 -0
- data/test/fixtures/poly.rb +3 -1
- data/test/fixtures/readonly_when_archived.rb +3 -1
- data/test/polymorphic_test.rb +29 -4
- data/test/readonly_when_archived_test.rb +6 -4
- data/test/relations_test.rb +63 -0
- data/test/responds_test.rb +8 -6
- data/test/schema.rb +64 -77
- data/test/scope_test.rb +28 -23
- data/test/test_helper.rb +47 -40
- data/test/through_association_test.rb +4 -2
- data/test/transaction_test.rb +7 -58
- metadata +33 -50
- data/gemfiles/rails_4.1.gemfile +0 -8
- data/script/db_setup +0 -51
- data/test/database.yml +0 -26
- data/test/fixtures/mass_attribute_protected.rb +0 -7
- data/test/fixtures/mysql_archival.rb +0 -10
- data/test/fixtures/mysql_exploder.rb +0 -9
- data/test/fixtures/pg_archival.rb +0 -10
- data/test/fixtures/pg_exploder.rb +0 -9
- data/test/mass_attribute_test.rb +0 -20
@@ -1,113 +1,127 @@
|
|
1
1
|
module ExpectedBehavior
|
2
2
|
module ActsAsArchival
|
3
|
-
require 'digest/md5'
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
require "digest/md5"
|
5
|
+
|
6
|
+
unless defined?(MissingArchivalColumnError) == "constant" && MissingArchivalColumnError.class == Class
|
7
|
+
MissingArchivalColumnError = Class.new(ActiveRecord::ActiveRecordError)
|
8
|
+
end
|
9
|
+
unless defined?(CouldNotArchiveError) == "constant" && CouldNotArchiveError.class == Class
|
10
|
+
CouldNotArchiveError = Class.new(ActiveRecord::ActiveRecordError)
|
11
|
+
end
|
12
|
+
unless defined?(CouldNotUnarchiveError) == "constant" && CouldNotUnarchiveError.class == Class
|
13
|
+
CouldNotUnarchiveError = Class.new(ActiveRecord::ActiveRecordError)
|
14
|
+
end
|
8
15
|
|
9
16
|
def self.included(base)
|
10
17
|
base.extend ActMethods
|
11
18
|
end
|
12
19
|
|
13
20
|
module ActMethods
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
21
|
+
|
22
|
+
def acts_as_archival(options = {})
|
23
|
+
return if included_modules.include?(InstanceMethods)
|
24
|
+
|
25
|
+
include InstanceMethods
|
26
|
+
|
27
|
+
setup_validations(options)
|
28
|
+
|
29
|
+
setup_scopes
|
30
|
+
|
31
|
+
setup_callbacks
|
32
|
+
end
|
33
|
+
|
34
|
+
private def setup_validations(options)
|
35
|
+
before_validation :raise_if_not_archival
|
36
|
+
validate :readonly_when_archived if options[:readonly_when_archived]
|
37
|
+
end
|
38
|
+
|
39
|
+
private def setup_scopes
|
40
|
+
scope :archived, -> { where.not(archived_at: nil).where.not(archive_number: nil) }
|
41
|
+
scope :unarchived, -> { where(archived_at: nil, archive_number: nil) }
|
42
|
+
scope :archived_from_archive_number, (lambda do |head_archive_number|
|
43
|
+
where(["archived_at IS NOT NULL AND archive_number = ?", head_archive_number])
|
44
|
+
end)
|
45
|
+
end
|
46
|
+
|
47
|
+
private def setup_callbacks
|
48
|
+
callbackable_actions = %w[archive unarchive]
|
49
|
+
|
50
|
+
setup_activerecord_callbacks(callbackable_actions)
|
51
|
+
|
52
|
+
define_callback_dsl_methods(callbackable_actions)
|
53
|
+
end
|
54
|
+
|
55
|
+
private def setup_activerecord_callbacks(callbackable_actions)
|
56
|
+
define_callbacks(*[callbackable_actions].flatten)
|
57
|
+
end
|
58
|
+
|
59
|
+
private def define_callback_dsl_methods(callbackable_actions)
|
60
|
+
callbackable_actions.each do |action|
|
61
|
+
%w[before after].each do |callbackable_type|
|
62
|
+
define_callback_dsl_method(callbackable_type, action)
|
44
63
|
end
|
45
64
|
end
|
46
65
|
end
|
47
66
|
|
67
|
+
private def define_callback_dsl_method(callbackable_type, action)
|
68
|
+
# rubocop:disable Security/Eval
|
69
|
+
eval <<-end_callbacks
|
70
|
+
unless defined?(#{callbackable_type}_#{action})
|
71
|
+
def #{callbackable_type}_#{action}(*args, &blk)
|
72
|
+
set_callback(:#{action}, :#{callbackable_type}, *args, &blk)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end_callbacks
|
76
|
+
# rubocop:enable Security/Eval
|
77
|
+
end
|
78
|
+
|
48
79
|
end
|
49
80
|
|
50
81
|
module InstanceMethods
|
51
82
|
|
52
83
|
def readonly_when_archived
|
53
|
-
|
54
|
-
|
55
|
-
|
84
|
+
readonly_attributes_changed = archived? && changed? && !archived_at_changed? && !archive_number_changed?
|
85
|
+
return unless readonly_attributes_changed
|
86
|
+
|
87
|
+
errors.add(:base, "Cannot modify an archived record.")
|
56
88
|
end
|
57
89
|
|
58
90
|
def raise_if_not_archival
|
59
91
|
missing_columns = []
|
60
|
-
missing_columns << "archive_number" unless
|
61
|
-
missing_columns << "archived_at" unless
|
62
|
-
|
92
|
+
missing_columns << "archive_number" unless respond_to?(:archive_number)
|
93
|
+
missing_columns << "archived_at" unless respond_to?(:archived_at)
|
94
|
+
return if missing_columns.blank?
|
95
|
+
|
96
|
+
raise MissingArchivalColumnError.new("Add '#{missing_columns.join "', '"}' column(s) to '#{self.class.name}' to make it archival")
|
63
97
|
end
|
64
98
|
|
65
99
|
def archived?
|
66
|
-
!!(
|
100
|
+
!!(archived_at? && archive_number)
|
67
101
|
end
|
68
102
|
|
69
|
-
def archive(head_archive_number=nil)
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
self.archive_number = head_archive_number
|
78
|
-
self.save!
|
79
|
-
end
|
80
|
-
end
|
81
|
-
return !!success
|
82
|
-
rescue => e
|
83
|
-
ActiveRecord::Base.logger.try(:debug, e.message)
|
84
|
-
ActiveRecord::Base.logger.try(:debug, e.backtrace)
|
85
|
-
raise ActiveRecord::Rollback
|
103
|
+
def archive!(head_archive_number = nil)
|
104
|
+
execute_archival_action(:archive) do
|
105
|
+
unless archived?
|
106
|
+
head_archive_number ||= Digest::MD5.hexdigest("#{self.class.name}#{id}")
|
107
|
+
archive_associations(head_archive_number)
|
108
|
+
self.archived_at = DateTime.now
|
109
|
+
self.archive_number = head_archive_number
|
110
|
+
save!
|
86
111
|
end
|
87
112
|
end
|
88
|
-
false
|
89
113
|
end
|
90
114
|
|
91
|
-
def unarchive(head_archive_number=nil)
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
self.save!
|
100
|
-
self.unarchive_associations(head_archive_number)
|
101
|
-
end
|
102
|
-
end
|
103
|
-
return !!success
|
104
|
-
rescue => e
|
105
|
-
ActiveRecord::Base.logger.try(:debug, e.message)
|
106
|
-
ActiveRecord::Base.logger.try(:debug, e.backtrace)
|
107
|
-
raise ActiveRecord::Rollback
|
115
|
+
def unarchive!(head_archive_number = nil)
|
116
|
+
execute_archival_action(:unarchive) do
|
117
|
+
if archived?
|
118
|
+
head_archive_number ||= archive_number
|
119
|
+
self.archived_at = nil
|
120
|
+
self.archive_number = nil
|
121
|
+
save!
|
122
|
+
unarchive_associations(head_archive_number)
|
108
123
|
end
|
109
124
|
end
|
110
|
-
false
|
111
125
|
end
|
112
126
|
|
113
127
|
def archive_associations(head_archive_number)
|
@@ -117,6 +131,26 @@ module ExpectedBehavior
|
|
117
131
|
def unarchive_associations(head_archive_number)
|
118
132
|
AssociationOperation::Unarchive.new(self, head_archive_number).execute
|
119
133
|
end
|
134
|
+
|
135
|
+
private def execute_archival_action(action)
|
136
|
+
self.class.transaction do
|
137
|
+
begin
|
138
|
+
success = run_callbacks(action) { yield }
|
139
|
+
return !!success
|
140
|
+
rescue => e
|
141
|
+
handle_archival_action_exception(e)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
false
|
145
|
+
end
|
146
|
+
|
147
|
+
private def handle_archival_action_exception(exception)
|
148
|
+
ActiveRecord::Base.logger.try(:debug, exception.message)
|
149
|
+
ActiveRecord::Base.logger.try(:debug, exception.backtrace)
|
150
|
+
raise ActiveRecord::Rollback
|
151
|
+
end
|
152
|
+
|
120
153
|
end
|
154
|
+
|
121
155
|
end
|
122
156
|
end
|
@@ -1,20 +1,45 @@
|
|
1
1
|
module ExpectedBehavior
|
2
2
|
module ActsAsArchivalActiveRecordMethods
|
3
|
+
|
3
4
|
def self.included(base)
|
4
5
|
base.extend ARClassMethods
|
5
6
|
base.send :include, ARInstanceMethods
|
6
7
|
end
|
7
8
|
|
8
9
|
module ARClassMethods
|
9
|
-
|
10
|
-
|
10
|
+
|
11
|
+
def archival?
|
12
|
+
included_modules.include?(ExpectedBehavior::ActsAsArchival::InstanceMethods)
|
11
13
|
end
|
14
|
+
|
12
15
|
end
|
13
16
|
|
14
17
|
module ARInstanceMethods
|
15
|
-
|
16
|
-
|
18
|
+
|
19
|
+
def archival?
|
20
|
+
self.class.archival?
|
17
21
|
end
|
22
|
+
|
18
23
|
end
|
24
|
+
|
25
|
+
module ARRelationMethods
|
26
|
+
|
27
|
+
def archive_all!
|
28
|
+
error_message = "The #{klass} must implement 'act_on_archivals' in order to call `archive_all!`"
|
29
|
+
raise NotImplementedError.new(error_message) unless archival?
|
30
|
+
|
31
|
+
head_archive_number = Digest::MD5.hexdigest("#{klass}#{Time.now.utc.to_i}")
|
32
|
+
each { |record| record.archive!(head_archive_number) }.tap { reset }
|
33
|
+
end
|
34
|
+
|
35
|
+
def unarchive_all!
|
36
|
+
error_message = "The #{klass} must implement 'act_on_archivals' in order to call `unarchive_all!`"
|
37
|
+
raise NotImplementedError.new(error_message) unless archival?
|
38
|
+
|
39
|
+
each(&:unarchive!).tap { reset }
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
19
44
|
end
|
20
45
|
end
|
@@ -7,7 +7,7 @@ module ExpectedBehavior
|
|
7
7
|
|
8
8
|
def act_on_archivals(archivals)
|
9
9
|
archivals.unarchived.find_each do |related_record|
|
10
|
-
raise ActiveRecord::Rollback unless related_record.archive(head_archive_number)
|
10
|
+
raise ActiveRecord::Rollback unless related_record.archive!(head_archive_number)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
@@ -1,8 +1,8 @@
|
|
1
1
|
module ExpectedBehavior
|
2
2
|
module ActsAsArchival
|
3
3
|
module AssociationOperation
|
4
|
-
|
5
4
|
class Base
|
5
|
+
|
6
6
|
attr_reader :model, :head_archive_number
|
7
7
|
|
8
8
|
def initialize(model, head_archive_number)
|
@@ -19,33 +19,36 @@ module ExpectedBehavior
|
|
19
19
|
protected
|
20
20
|
|
21
21
|
def each_archivable_association
|
22
|
-
|
22
|
+
model.class.reflect_on_all_associations.each do |association|
|
23
23
|
yield(association) if archivable_association?(association)
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
27
|
def archivable_association?(association)
|
28
28
|
association.macro.to_s =~ /^has/ &&
|
29
|
-
association.klass.
|
29
|
+
association.klass.archival? &&
|
30
30
|
association.options[:through].nil?
|
31
31
|
end
|
32
32
|
|
33
|
-
def association_conditions_met?(
|
33
|
+
def association_conditions_met?(_association)
|
34
34
|
true
|
35
35
|
end
|
36
36
|
|
37
37
|
def act_on_association(association)
|
38
38
|
key = association.respond_to?(:foreign_key) ? association.foreign_key : association.primary_key_name
|
39
|
-
|
39
|
+
scope_conditions = { key => model.id }
|
40
|
+
# polymorphic associations need a type so we don't accidentally act on multiple associated objects
|
41
|
+
# that have the same ID
|
42
|
+
scope_conditions[association.type] = model.class.base_class.name if association.type
|
43
|
+
scope = association.klass.where(scope_conditions)
|
40
44
|
act_on_archivals(scope)
|
41
45
|
end
|
42
46
|
|
43
|
-
def act_on_archivals(
|
44
|
-
raise NotImplementedError
|
45
|
-
"The #{self.class} hasn't implemented 'act_on_archivals(scope)'"
|
47
|
+
def act_on_archivals(_scope)
|
48
|
+
raise NotImplementedError.new("The #{self.class} hasn't implemented 'act_on_archivals(scope)'")
|
46
49
|
end
|
47
|
-
end
|
48
50
|
|
51
|
+
end
|
49
52
|
end
|
50
53
|
end
|
51
54
|
end
|
@@ -6,8 +6,8 @@ module ExpectedBehavior
|
|
6
6
|
protected
|
7
7
|
|
8
8
|
def act_on_archivals(scope)
|
9
|
-
scope.archived.where(:
|
10
|
-
raise ActiveRecord::Rollback unless related_record.unarchive(head_archive_number)
|
9
|
+
scope.archived.where(archive_number: head_archive_number).find_each do |related_record|
|
10
|
+
raise ActiveRecord::Rollback unless related_record.unarchive!(head_archive_number)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
data/script/setup
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
require_relative "test_helper"
|
2
2
|
|
3
3
|
class AmbiguousTableTest < ActiveSupport::TestCase
|
4
|
+
|
4
5
|
test "no ambiguous table problem" do
|
5
6
|
archival = Archival.create!
|
6
7
|
child = archival.archivals.create!
|
7
|
-
child.archive
|
8
|
+
child.archive!
|
8
9
|
|
9
10
|
# this is a bug fix for a problem wherein table names weren't being
|
10
11
|
# namespaced, so if a table joined against itself, incorrect SQL was
|
11
12
|
# generated
|
12
13
|
assert_equal 1, Archival.unarchived.joins(:archivals).count
|
13
14
|
end
|
15
|
+
|
14
16
|
end
|
@@ -3,16 +3,18 @@ require_relative "test_helper"
|
|
3
3
|
# Rails 5 introduced a new base class, and this is gonna test that
|
4
4
|
if defined?(ApplicationRecord)
|
5
5
|
class ApplicationRecordTest < ActiveSupport::TestCase
|
6
|
+
|
6
7
|
test "archive archives the record" do
|
7
8
|
archival = ApplicationRecordRow.create!
|
8
|
-
archival.archive
|
9
|
+
archival.archive!
|
9
10
|
assert archival.reload.archived?
|
10
11
|
end
|
11
12
|
|
12
13
|
test "unarchive unarchives archival records" do
|
13
|
-
archival = ApplicationRecordRow.create!(:
|
14
|
-
archival.unarchive
|
14
|
+
archival = ApplicationRecordRow.create!(archived_at: Time.now, archive_number: 1)
|
15
|
+
archival.unarchive!
|
15
16
|
assert_not archival.reload.archived?
|
16
17
|
end
|
18
|
+
|
17
19
|
end
|
18
20
|
end
|
data/test/associations_test.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
require_relative "test_helper"
|
2
2
|
|
3
3
|
class AssociationsTest < ActiveSupport::TestCase
|
4
|
+
|
4
5
|
test "archive archives 'has_' associated archival objects that are dependent destroy" do
|
5
6
|
archival = Archival.create!
|
6
7
|
child = archival.archivals.create!
|
7
|
-
archival.archive
|
8
|
+
archival.archive!
|
8
9
|
|
9
10
|
assert archival.reload.archived?
|
10
11
|
assert child.reload.archived?
|
@@ -13,7 +14,7 @@ class AssociationsTest < ActiveSupport::TestCase
|
|
13
14
|
test "archive acts on all objects in the 'has_' relationship" do
|
14
15
|
archival = Archival.create!
|
15
16
|
children = [archival.archivals.create!, archival.archivals.create!]
|
16
|
-
archival.archive
|
17
|
+
archival.archive!
|
17
18
|
|
18
19
|
assert archival.reload.archived?
|
19
20
|
assert children.map(&:reload).all?(&:archived?)
|
@@ -23,8 +24,8 @@ class AssociationsTest < ActiveSupport::TestCase
|
|
23
24
|
archival = Archival.create!
|
24
25
|
archival.archivals.create!
|
25
26
|
prearchived_child = archival.archivals.create!
|
26
|
-
prearchived_child.archive
|
27
|
-
archival.archive
|
27
|
+
prearchived_child.archive!
|
28
|
+
archival.archive!
|
28
29
|
|
29
30
|
assert_not_equal archival.archive_number, prearchived_child.reload.archive_number
|
30
31
|
end
|
@@ -32,7 +33,7 @@ class AssociationsTest < ActiveSupport::TestCase
|
|
32
33
|
test "archive does not archive 'has_' associated archival objects that are not dependent destroy" do
|
33
34
|
archival = Archival.create!
|
34
35
|
non_dependent_child = archival.independent_archivals.create!
|
35
|
-
archival.archive
|
36
|
+
archival.archive!
|
36
37
|
|
37
38
|
assert archival.reload.archived?
|
38
39
|
assert_not non_dependent_child.reload.archived?
|
@@ -41,7 +42,7 @@ class AssociationsTest < ActiveSupport::TestCase
|
|
41
42
|
test "archive doesn't do anything to associated dependent destroy models that are non-archival" do
|
42
43
|
archival = Archival.create!
|
43
44
|
plain = archival.plains.create!
|
44
|
-
archival.archive
|
45
|
+
archival.archive!
|
45
46
|
|
46
47
|
assert archival.archived?
|
47
48
|
assert plain.reload
|
@@ -50,7 +51,7 @@ class AssociationsTest < ActiveSupport::TestCase
|
|
50
51
|
test "archive sets the object hierarchy to all have the same archive_number" do
|
51
52
|
archival = Archival.create!
|
52
53
|
child = archival.archivals.create!
|
53
|
-
archival.archive
|
54
|
+
archival.archive!
|
54
55
|
expected_digest = Digest::MD5.hexdigest("Archival#{archival.id}")
|
55
56
|
|
56
57
|
assert_equal expected_digest, archival.archive_number
|
@@ -60,8 +61,8 @@ class AssociationsTest < ActiveSupport::TestCase
|
|
60
61
|
test "unarchive acts on child objects" do
|
61
62
|
archival = Archival.create!
|
62
63
|
child = archival.archivals.create!
|
63
|
-
archival.archive
|
64
|
-
archival.unarchive
|
64
|
+
archival.archive!
|
65
|
+
archival.unarchive!
|
65
66
|
|
66
67
|
assert_not archival.archived?
|
67
68
|
assert_not child.reload.archived?
|
@@ -71,9 +72,9 @@ class AssociationsTest < ActiveSupport::TestCase
|
|
71
72
|
archival = Archival.create!
|
72
73
|
child = archival.archivals.create!
|
73
74
|
prearchived_child = archival.archivals.create!
|
74
|
-
prearchived_child.archive
|
75
|
-
archival.archive
|
76
|
-
archival.unarchive
|
75
|
+
prearchived_child.archive!
|
76
|
+
archival.archive!
|
77
|
+
archival.unarchive!
|
77
78
|
|
78
79
|
assert_not archival.archived?
|
79
80
|
assert_not child.reload.archived?
|
@@ -83,9 +84,9 @@ class AssociationsTest < ActiveSupport::TestCase
|
|
83
84
|
test "unarchive acts on 'has_' associated non-dependent_destroy objects" do
|
84
85
|
archival = Archival.create!
|
85
86
|
independent = archival.independent_archivals.create!
|
86
|
-
archival.archive
|
87
|
-
independent.archive(archival.archive_number)
|
88
|
-
archival.unarchive
|
87
|
+
archival.archive!
|
88
|
+
independent.archive!(archival.archive_number)
|
89
|
+
archival.unarchive!
|
89
90
|
|
90
91
|
assert_not archival.reload.archived?
|
91
92
|
assert_not independent.reload.archived?
|
@@ -94,9 +95,10 @@ class AssociationsTest < ActiveSupport::TestCase
|
|
94
95
|
test "unarchive doesn't unarchive associated objects if the head object is already unarchived" do
|
95
96
|
archival = Archival.create!
|
96
97
|
prearchived_child = archival.archivals.create!
|
97
|
-
prearchived_child.archive
|
98
|
-
archival.unarchive
|
98
|
+
prearchived_child.archive!
|
99
|
+
archival.unarchive!
|
99
100
|
|
100
101
|
assert prearchived_child.reload.archived?
|
101
102
|
end
|
103
|
+
|
102
104
|
end
|
data/test/basic_test.rb
CHANGED
@@ -1,64 +1,66 @@
|
|
1
1
|
require_relative "test_helper"
|
2
2
|
|
3
3
|
class BasicTest < ActiveSupport::TestCase
|
4
|
+
|
4
5
|
test "archive archives the record" do
|
5
6
|
archival = Archival.create!
|
6
|
-
archival.archive
|
7
|
+
archival.archive!
|
7
8
|
assert_equal true, archival.reload.archived?
|
8
9
|
end
|
9
10
|
|
10
11
|
test "unarchive unarchives archival records" do
|
11
|
-
archival = Archival.create!(:
|
12
|
-
archival.unarchive
|
12
|
+
archival = Archival.create!(archived_at: Time.now, archive_number: 1)
|
13
|
+
archival.unarchive!
|
13
14
|
assert_equal false, archival.reload.archived?
|
14
15
|
end
|
15
16
|
|
16
17
|
test "archive returns true on success" do
|
17
18
|
normal = Archival.create!
|
18
|
-
assert_equal true, normal.archive
|
19
|
+
assert_equal true, normal.archive!
|
19
20
|
end
|
20
21
|
|
21
22
|
test "archive returns false on failure" do
|
22
23
|
readonly = Archival.create!
|
23
24
|
readonly.readonly!
|
24
|
-
assert_equal false, readonly.archive
|
25
|
+
assert_equal false, readonly.archive!
|
25
26
|
end
|
26
27
|
|
27
28
|
test "unarchive returns true on success" do
|
28
|
-
normal = Archival.create!(:
|
29
|
-
assert_equal true, normal.unarchive
|
29
|
+
normal = Archival.create!(archived_at: Time.now, archive_number: "1")
|
30
|
+
assert_equal true, normal.unarchive!
|
30
31
|
end
|
31
32
|
|
32
33
|
test "unarchive returns false on failure" do
|
33
|
-
readonly = Archival.create!(:
|
34
|
+
readonly = Archival.create!(archived_at: Time.now, archive_number: "1")
|
34
35
|
readonly.readonly!
|
35
|
-
assert_equal false, readonly.unarchive
|
36
|
+
assert_equal false, readonly.unarchive!
|
36
37
|
end
|
37
38
|
|
38
39
|
test "archive sets archived_at to the time of archiving" do
|
39
40
|
archival = Archival.create!
|
40
41
|
before = DateTime.now
|
41
42
|
sleep(0.001)
|
42
|
-
archival.archive
|
43
|
+
archival.archive!
|
43
44
|
sleep(0.001)
|
44
45
|
after = DateTime.now
|
45
46
|
assert before < archival.archived_at.to_datetime
|
46
|
-
assert after
|
47
|
+
assert after > archival.archived_at.to_datetime
|
47
48
|
end
|
48
49
|
|
49
50
|
test "archive sets the archive number to the md5 hexdigest for the model and id that is archived" do
|
50
51
|
archival = Archival.create!
|
51
|
-
archival.archive
|
52
|
+
archival.archive!
|
52
53
|
expected_digest = Digest::MD5.hexdigest("#{archival.class.name}#{archival.id}")
|
53
54
|
assert_equal expected_digest, archival.archive_number
|
54
55
|
end
|
55
56
|
|
56
57
|
test "archive on archived object doesn't alter the archive_number" do
|
57
58
|
archived = Archival.create
|
58
|
-
archived.archive
|
59
|
+
archived.archive!
|
59
60
|
initial_number = archived.archive_number
|
60
|
-
archived.reload.archive
|
61
|
+
archived.reload.archive!
|
61
62
|
second_number = archived.archive_number
|
62
63
|
assert_equal initial_number, second_number
|
63
64
|
end
|
65
|
+
|
64
66
|
end
|
data/test/callbacks_test.rb
CHANGED
@@ -1,21 +1,22 @@
|
|
1
1
|
require_relative "test_helper"
|
2
2
|
|
3
3
|
class CallbacksTest < ActiveSupport::TestCase
|
4
|
+
|
4
5
|
if defined?(ApplicationRecord)
|
5
6
|
test "can set a value as part of archiving" do
|
6
7
|
archival = CallbackArchival5.create
|
7
8
|
archival.set_this_value = "a test string"
|
8
9
|
assert_nil archival.settable_field
|
9
|
-
archival.archive
|
10
|
+
archival.archive!
|
10
11
|
assert_equal "a test string", archival.reload.settable_field
|
11
12
|
end
|
12
13
|
|
13
14
|
test "can be halted" do
|
14
|
-
archival =
|
15
|
+
archival = CallbackArchival5.create
|
15
16
|
archival.set_this_value = "a test string"
|
16
17
|
archival.pass_callback = false
|
17
18
|
assert_nil archival.settable_field
|
18
|
-
archival.archive
|
19
|
+
archival.archive!
|
19
20
|
assert_nil archival.reload.settable_field
|
20
21
|
end
|
21
22
|
else
|
@@ -23,17 +24,18 @@ class CallbacksTest < ActiveSupport::TestCase
|
|
23
24
|
archival = CallbackArchival4.create
|
24
25
|
archival.set_this_value = "a test string"
|
25
26
|
assert_nil archival.settable_field
|
26
|
-
archival.archive
|
27
|
+
archival.archive!
|
27
28
|
assert_equal "a test string", archival.reload.settable_field
|
28
29
|
end
|
29
30
|
|
30
31
|
test "can be halted" do
|
31
|
-
archival =
|
32
|
+
archival = CallbackArchival4.create
|
32
33
|
archival.set_this_value = "a test string"
|
33
34
|
archival.pass_callback = false
|
34
35
|
assert_nil archival.settable_field
|
35
|
-
archival.archive
|
36
|
+
archival.archive!
|
36
37
|
assert_nil archival.reload.settable_field
|
37
38
|
end
|
38
39
|
end
|
40
|
+
|
39
41
|
end
|
data/test/column_test.rb
CHANGED
@@ -1,15 +1,17 @@
|
|
1
1
|
require_relative "test_helper"
|
2
2
|
|
3
3
|
class ColumnTest < ActiveSupport::TestCase
|
4
|
+
|
4
5
|
test "acts_as_archival raises during create if missing archived_at column" do
|
5
|
-
assert_raises(ExpectedBehavior::ActsAsArchival::MissingArchivalColumnError)
|
6
|
-
MissingArchivedAt.create!(:
|
7
|
-
|
6
|
+
assert_raises(ExpectedBehavior::ActsAsArchival::MissingArchivalColumnError) do
|
7
|
+
MissingArchivedAt.create!(name: "foo-foo")
|
8
|
+
end
|
8
9
|
end
|
9
10
|
|
10
11
|
test "acts_as_archival raises during create if missing archive_number column" do
|
11
|
-
assert_raises(ExpectedBehavior::ActsAsArchival::MissingArchivalColumnError)
|
12
|
-
MissingArchiveNumber.create!(:
|
13
|
-
|
12
|
+
assert_raises(ExpectedBehavior::ActsAsArchival::MissingArchivalColumnError) do
|
13
|
+
MissingArchiveNumber.create!(name: "rover")
|
14
|
+
end
|
14
15
|
end
|
16
|
+
|
15
17
|
end
|