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.
Files changed (64) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -1
  3. data/.rubocop.yml +71 -0
  4. data/.rubocop_todo.yml +98 -0
  5. data/.travis.yml +23 -0
  6. data/Appraisals +7 -7
  7. data/CHANGELOG.md +29 -0
  8. data/Gemfile.lock +70 -0
  9. data/README.md +49 -19
  10. data/Rakefile +6 -3
  11. data/acts_as_archival.gemspec +23 -17
  12. data/gemfiles/rails_5.2.gemfile +8 -0
  13. data/gemfiles/{rails_4.2.gemfile → rails_6.0.gemfile} +2 -2
  14. data/gemfiles/{rails_5.0.gemfile → rails_6.1.gemfile} +2 -2
  15. data/lib/acts_as_archival/version.rb +3 -1
  16. data/lib/acts_as_archival.rb +2 -0
  17. data/lib/expected_behavior/acts_as_archival.rb +111 -77
  18. data/lib/expected_behavior/acts_as_archival_active_record_methods.rb +29 -4
  19. data/lib/expected_behavior/association_operation/archive.rb +1 -1
  20. data/lib/expected_behavior/association_operation/base.rb +12 -9
  21. data/lib/expected_behavior/association_operation/unarchive.rb +2 -2
  22. data/script/setup +0 -3
  23. data/test/ambiguous_table_test.rb +3 -1
  24. data/test/application_record_test.rb +5 -3
  25. data/test/associations_test.rb +19 -17
  26. data/test/basic_test.rb +16 -14
  27. data/test/callbacks_test.rb +8 -6
  28. data/test/column_test.rb +8 -6
  29. data/test/deep_nesting_test.rb +6 -4
  30. data/test/fixtures/another_polys_holder.rb +11 -0
  31. data/test/fixtures/application_record.rb +2 -0
  32. data/test/fixtures/application_record_row.rb +2 -0
  33. data/test/fixtures/archival.rb +9 -7
  34. data/test/fixtures/archival_grandkid.rb +2 -0
  35. data/test/fixtures/archival_kid.rb +3 -1
  36. data/test/fixtures/archival_table_name.rb +2 -0
  37. data/test/fixtures/callback_archival_4.rb +2 -0
  38. data/test/fixtures/callback_archival_5.rb +4 -1
  39. data/test/fixtures/exploder.rb +2 -0
  40. data/test/fixtures/independent_archival.rb +2 -0
  41. data/test/fixtures/missing_archive_number.rb +2 -0
  42. data/test/fixtures/missing_archived_at.rb +2 -0
  43. data/test/fixtures/plain.rb +2 -0
  44. data/test/fixtures/poly.rb +3 -1
  45. data/test/fixtures/readonly_when_archived.rb +3 -1
  46. data/test/polymorphic_test.rb +29 -4
  47. data/test/readonly_when_archived_test.rb +6 -4
  48. data/test/relations_test.rb +63 -0
  49. data/test/responds_test.rb +8 -6
  50. data/test/schema.rb +64 -77
  51. data/test/scope_test.rb +28 -23
  52. data/test/test_helper.rb +47 -40
  53. data/test/through_association_test.rb +4 -2
  54. data/test/transaction_test.rb +7 -58
  55. metadata +33 -50
  56. data/gemfiles/rails_4.1.gemfile +0 -8
  57. data/script/db_setup +0 -51
  58. data/test/database.yml +0 -26
  59. data/test/fixtures/mass_attribute_protected.rb +0 -7
  60. data/test/fixtures/mysql_archival.rb +0 -10
  61. data/test/fixtures/mysql_exploder.rb +0 -9
  62. data/test/fixtures/pg_archival.rb +0 -10
  63. data/test/fixtures/pg_exploder.rb +0 -9
  64. data/test/mass_attribute_test.rb +0 -20
@@ -1,11 +1,12 @@
1
1
  require_relative "test_helper"
2
2
 
3
3
  class DeepNestingTest < ActiveSupport::TestCase
4
+
4
5
  test "archiving deeply nested items" do
5
6
  archival = Archival.create!
6
7
  child = archival.archivals.create!
7
8
  grandchild = child.archivals.create!
8
- archival.archive
9
+ archival.archive!
9
10
  assert archival.reload.archived?
10
11
  assert child.reload.archived?
11
12
  assert grandchild.reload.archived?
@@ -15,15 +16,16 @@ class DeepNestingTest < ActiveSupport::TestCase
15
16
 
16
17
  test "unarchiving deeply nested items doesn't blow up" do
17
18
  archival_attributes = {
18
- :archived_at => Time.now,
19
- :archive_number => "test"
19
+ archived_at: Time.now,
20
+ archive_number: "test"
20
21
  }
21
22
  archival = Archival.create!(archival_attributes)
22
23
  child = archival.archivals.create!(archival_attributes)
23
24
  grandchild = child.archivals.create!(archival_attributes)
24
- archival.unarchive
25
+ archival.unarchive!
25
26
  assert_not archival.reload.archived?
26
27
  assert_not child.reload.archived?
27
28
  assert_not grandchild.reload.archived?
28
29
  end
30
+
29
31
  end
@@ -0,0 +1,11 @@
1
+ # name - string
2
+ # archival_id - integer
3
+ # archive_number - string
4
+ # archived_at - datetime
5
+ class AnotherPolysHolder < ActiveRecord::Base
6
+
7
+ acts_as_archival
8
+
9
+ has_many :polys, dependent: :destroy, as: :archiveable
10
+
11
+ end
@@ -1,3 +1,5 @@
1
1
  class ApplicationRecord < ActiveRecord::Base
2
+
2
3
  self.abstract_class = true
4
+
3
5
  end
@@ -1,6 +1,8 @@
1
1
  # Rails 5 introduced a new base class, and this is gonna be used in the tests of that
2
2
  if defined?(ApplicationRecord)
3
3
  class ApplicationRecordRow < ApplicationRecord
4
+
4
5
  acts_as_archival
6
+
5
7
  end
6
8
  end
@@ -3,15 +3,17 @@
3
3
  # archive_number - string
4
4
  # archived_at - datetime
5
5
  class Archival < ActiveRecord::Base
6
+
6
7
  acts_as_archival
7
8
 
8
- has_many :archivals, :dependent => :destroy
9
- has_many :archival_kids, :dependent => :destroy
10
- has_many :archival_grandkids, :dependent => :destroy, :through => :archival_kids
11
- has_many :exploders, :dependent => :destroy
12
- has_many :plains, :dependent => :destroy
13
- has_many :polys, :dependent => :destroy, :as => :archiveable
9
+ has_many :archivals, dependent: :destroy
10
+ has_many :archival_kids, dependent: :destroy
11
+ has_many :archival_grandkids, dependent: :destroy, through: :archival_kids
12
+ has_many :exploders, dependent: :destroy
13
+ has_many :plains, dependent: :destroy
14
+ has_many :polys, dependent: :destroy, as: :archiveable
14
15
  has_many :independent_archivals
15
16
 
16
- scope :bobs, lambda { where(:name => ["Bob", "Bobby", "Robert"]) }
17
+ scope :bobs, -> { where(name: %w[Bob Bobby Robert]) }
18
+
17
19
  end
@@ -2,6 +2,8 @@
2
2
  # archive_number - string
3
3
  # archived_at - datetime
4
4
  class ArchivalGrandkid < ActiveRecord::Base
5
+
5
6
  acts_as_archival
6
7
  belongs_to :archival_kid
8
+
7
9
  end
@@ -2,7 +2,9 @@
2
2
  # archive_number - string
3
3
  # archived_at - datetime
4
4
  class ArchivalKid < ActiveRecord::Base
5
+
5
6
  acts_as_archival
6
7
  belongs_to :archival
7
- has_many :archival_grandkids, :dependent => :destroy
8
+ has_many :archival_grandkids, dependent: :destroy
9
+
8
10
  end
@@ -2,6 +2,8 @@
2
2
  # archive_number - string
3
3
  # archived_at - datetime
4
4
  class ArchivalTableName < ActiveRecord::Base
5
+
5
6
  self.table_name = "legacy"
6
7
  acts_as_archival
8
+
7
9
  end
@@ -1,4 +1,5 @@
1
1
  class CallbackArchival4 < ActiveRecord::Base
2
+
2
3
  acts_as_archival
3
4
 
4
5
  attr_accessor :set_this_value,
@@ -14,4 +15,5 @@ class CallbackArchival4 < ActiveRecord::Base
14
15
  private def conditional_callback_passer
15
16
  pass_callback || pass_callback.nil?
16
17
  end
18
+
17
19
  end
@@ -1,6 +1,7 @@
1
1
  # necessary for ApplicationRecord
2
2
  if defined?(ApplicationRecord)
3
3
  class CallbackArchival5 < ApplicationRecord
4
+
4
5
  acts_as_archival
5
6
 
6
7
  attr_accessor :set_this_value,
@@ -14,7 +15,9 @@ if defined?(ApplicationRecord)
14
15
  end
15
16
 
16
17
  private def conditional_callback_passer
17
- throw(:abort) unless (pass_callback || pass_callback.nil?)
18
+ # we want to throw only for the value false
19
+ throw(:abort) unless pass_callback || pass_callback.nil?
18
20
  end
21
+
19
22
  end
20
23
  end
@@ -2,6 +2,8 @@
2
2
  # archive_number - string
3
3
  # archived_at - datetime
4
4
  class Exploder < ActiveRecord::Base
5
+
5
6
  acts_as_archival
6
7
  belongs_to :archival
8
+
7
9
  end
@@ -3,6 +3,8 @@
3
3
  # archive_number - string
4
4
  # archived_at - datetime
5
5
  class IndependentArchival < ActiveRecord::Base
6
+
6
7
  acts_as_archival
7
8
  belongs_to :archival
9
+
8
10
  end
@@ -1,5 +1,7 @@
1
1
  # name - string
2
2
  # archived_at - datetime
3
3
  class MissingArchiveNumber < ActiveRecord::Base
4
+
4
5
  acts_as_archival
6
+
5
7
  end
@@ -1,5 +1,7 @@
1
1
  # name - string
2
2
  # archive_number - string
3
3
  class MissingArchivedAt < ActiveRecord::Base
4
+
4
5
  acts_as_archival
6
+
5
7
  end
@@ -1,5 +1,7 @@
1
1
  # name - string
2
2
  # archival_id - integer
3
3
  class Plain < ActiveRecord::Base
4
+
4
5
  belongs_to :archival
6
+
5
7
  end
@@ -3,6 +3,8 @@
3
3
  # archive_number - string
4
4
  # archived_at - datetime
5
5
  class Poly < ActiveRecord::Base
6
+
6
7
  acts_as_archival
7
- belongs_to :archiveable, :polymorphic => true
8
+ belongs_to :archiveable, polymorphic: true
9
+
8
10
  end
@@ -2,5 +2,7 @@
2
2
  # archive_number - string
3
3
  # archived_at - datetime
4
4
  class ReadonlyWhenArchived < ActiveRecord::Base
5
- acts_as_archival :readonly_when_archived => true
5
+
6
+ acts_as_archival readonly_when_archived: true
7
+
6
8
  end
@@ -1,25 +1,50 @@
1
1
  require_relative "test_helper"
2
2
 
3
3
  class PolymorphicTest < ActiveSupport::TestCase
4
+
4
5
  test "archive item with polymorphic association" do
5
6
  archival = Archival.create!
6
7
  poly = archival.polys.create!
7
- archival.archive
8
+ archival.archive!
8
9
 
9
10
  assert archival.reload.archived?
10
11
  assert poly.reload.archived?
11
12
  end
12
13
 
14
+ test "does not archive polymorphic association of different item with same id" do
15
+ archival = Archival.create!
16
+ another_polys_holder = AnotherPolysHolder.create!(id: archival.id)
17
+ poly = another_polys_holder.polys.create!
18
+ archival.archive!
19
+
20
+ assert_not poly.reload.archived?
21
+ end
22
+
13
23
  test "unarchive item with polymorphic association" do
14
24
  archive_attributes = {
15
- :archive_number => "test",
16
- :archived_at => Time.now
25
+ archive_number: "test",
26
+ archived_at: Time.now
17
27
  }
18
28
  archival = Archival.create!(archive_attributes)
19
29
  poly = archival.polys.create!(archive_attributes)
20
- archival.unarchive
30
+ archival.unarchive!
21
31
 
22
32
  assert_not archival.reload.archived?
23
33
  assert_not poly.reload.archived?
24
34
  end
35
+
36
+ test "does not unarchive polymorphic association of different item with same id" do
37
+ archive_attributes = {
38
+ archive_number: "test",
39
+ archived_at: Time.now
40
+ }
41
+
42
+ archival = Archival.create!(archive_attributes)
43
+ another_polys_holder = AnotherPolysHolder.create!(archive_attributes.merge(id: archival.id))
44
+ poly = another_polys_holder.polys.create!(archive_attributes)
45
+ archival.unarchive!
46
+
47
+ assert poly.reload.archived?
48
+ end
49
+
25
50
  end
@@ -1,9 +1,10 @@
1
1
  require_relative "test_helper"
2
2
 
3
3
  class ReadonlyWhenArchivedTest < ActiveSupport::TestCase
4
+
4
5
  test "acts_as_archival objects can normally be altered after archive" do
5
- archival = Archival.create!(:name => "original")
6
- archival.archive
6
+ archival = Archival.create!(name: "original")
7
+ archival.archive!
7
8
  archival.name = "updated"
8
9
  archival.save!
9
10
 
@@ -11,12 +12,13 @@ class ReadonlyWhenArchivedTest < ActiveSupport::TestCase
11
12
  end
12
13
 
13
14
  test "acts_as_archival marked as readonly_when_archived cannot be updated after archive" do
14
- archival = ReadonlyWhenArchived.create!(:name => "original")
15
- archival.archive
15
+ archival = ReadonlyWhenArchived.create!(name: "original")
16
+ archival.archive!
16
17
  archival.name = "updated"
17
18
 
18
19
  assert_not archival.save
19
20
  assert_equal "Cannot modify an archived record.",
20
21
  archival.errors.full_messages.first
21
22
  end
23
+
22
24
  end
@@ -0,0 +1,63 @@
1
+ require_relative "test_helper"
2
+
3
+ class RelationsTest < ActiveSupport::TestCase
4
+
5
+ test "archive_all! archives all records in an AR Association" do
6
+ 3.times { Archival.create! }
7
+
8
+ archivals = Archival.all
9
+ archivals.archive_all!
10
+ assert archivals.first.archived?
11
+ assert archivals.last.archived?
12
+ end
13
+
14
+ test "archive_all! archives all records with the same archival number" do
15
+ 3.times { Archival.create! }
16
+
17
+ archivals = Archival.all
18
+ archivals.archive_all!
19
+ assert_equal archivals.first.archive_number, archivals.last.archive_number
20
+ end
21
+
22
+ test "archive_all! archives children records" do
23
+ 3.times do
24
+ parent = Archival.create!
25
+ 2.times do
26
+ parent.archivals.create!
27
+ end
28
+ end
29
+
30
+ parents = Archival.all
31
+ parents.archive_all!
32
+
33
+ assert parents.first.archivals.first.archived?
34
+ assert parents.first.archivals.last.archived?
35
+ end
36
+
37
+ test "unarchive_all! unarchives all records in an AR Assocation" do
38
+ 3.times { Archival.create! }
39
+
40
+ archivals = Archival.all
41
+ archivals.archive_all!
42
+ archivals.unarchive_all!
43
+ assert_not archivals.first.archived?
44
+ assert_not archivals.last.archived?
45
+ end
46
+
47
+ test "unarchive_all! unarchives children records" do
48
+ 3.times do
49
+ parent = Archival.create!
50
+ 2.times do
51
+ parent.archivals.create!
52
+ end
53
+ end
54
+
55
+ parents = Archival.all
56
+ parents.archive_all!
57
+ parents.unarchive_all!
58
+
59
+ assert_not parents.first.archivals.first.archived?
60
+ assert_not parents.first.archivals.last.archived?
61
+ end
62
+
63
+ end
@@ -1,13 +1,15 @@
1
1
  require_relative "test_helper"
2
2
 
3
3
  class RespondsTest < ActiveSupport::TestCase
4
- test "archival class responds correctly to 'is_archival?'" do
5
- assert Archival.is_archival?
6
- assert_not Plain.is_archival?
4
+
5
+ test "archival class responds correctly to 'archival?'" do
6
+ assert Archival.archival?
7
+ assert_not Plain.archival?
7
8
  end
8
9
 
9
- test "archival object responds correctly to 'is_archival?'" do
10
- assert Archival.new.is_archival?
11
- assert_not Plain.new.is_archival?
10
+ test "archival object responds correctly to 'archival?'" do
11
+ assert Archival.new.archival?
12
+ assert_not Plain.new.archival?
12
13
  end
14
+
13
15
  end
data/test/schema.rb CHANGED
@@ -1,103 +1,90 @@
1
- # Rails 4.0 and before do not deal correctly with newer versions of mysql, so we're
2
- # gonna force a non-null primary ID for old versions of Rails just as new ones do.
3
- if (ActiveRecord.version <=> Gem::Version.new("4.1.0")) < 0
4
- class ActiveRecord::ConnectionAdapters::Mysql2Adapter
5
- NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
1
+ ActiveRecord::Schema.define(version: 1) do
2
+ create_table :another_polys_holders, force: true do |t|
3
+ t.column :name, :string
4
+ t.column :archival_id, :integer
5
+ t.column :archive_number, :string
6
+ t.column :archived_at, :datetime
6
7
  end
7
- end
8
8
 
9
- ActiveRecord::Schema.define(:version => 1) do
10
- create_table :archivals, :force => true do |t|
9
+ create_table :archivals, force: true do |t|
11
10
  t.column :name, :string
12
11
  t.column :archival_id, :integer
13
12
  t.column :archive_number, :string
14
13
  t.column :archived_at, :datetime
15
14
  end
16
15
 
17
- create_table :exploders, :force => true do |t|
16
+ create_table :exploders, force: true do |t|
18
17
  t.column :archival_id, :integer
19
18
  t.column :archive_number, :string
20
19
  t.column :archived_at, :datetime
21
20
  end
22
21
 
23
- ###
24
- # The classes above are used to test database-specific
25
- # things in PG and MySQL, and we don't need to do all
26
- # the tests there.
27
- if "SQLite" == ActiveRecord::Base.connection.adapter_name
28
- create_table :archival_kids, :force => true do |t|
29
- t.column :archival_id, :integer
30
- t.column :archive_number, :string
31
- t.column :archived_at, :datetime
32
- end
33
-
34
- create_table :archival_grandkids, :force => true do |t|
35
- t.column :archival_kid_id, :integer
36
- t.column :archive_number, :string
37
- t.column :archived_at, :datetime
38
- end
22
+ create_table :archival_kids, force: true do |t|
23
+ t.column :archival_id, :integer
24
+ t.column :archive_number, :string
25
+ t.column :archived_at, :datetime
26
+ end
39
27
 
40
- create_table :independent_archivals, :force => true do |t|
41
- t.column :name, :string
42
- t.column :archival_id, :integer
43
- t.column :archive_number, :string
44
- t.column :archived_at, :datetime
45
- end
28
+ create_table :archival_grandkids, force: true do |t|
29
+ t.column :archival_kid_id, :integer
30
+ t.column :archive_number, :string
31
+ t.column :archived_at, :datetime
32
+ end
46
33
 
47
- create_table :plains, :force => true do |t|
48
- t.column :name, :string
49
- t.column :archival_id, :integer
50
- end
34
+ create_table :independent_archivals, force: true do |t|
35
+ t.column :name, :string
36
+ t.column :archival_id, :integer
37
+ t.column :archive_number, :string
38
+ t.column :archived_at, :datetime
39
+ end
51
40
 
52
- create_table :mass_attribute_protecteds, :force => true do |t|
53
- t.column :name, :string
54
- t.column :archive_number, :string
55
- t.column :archived_at, :datetime
56
- end
41
+ create_table :plains, force: true do |t|
42
+ t.column :name, :string
43
+ t.column :archival_id, :integer
44
+ end
57
45
 
58
- create_table :readonly_when_archiveds, :force => true do |t|
59
- t.column :name, :string
60
- t.column :archive_number, :string
61
- t.column :archived_at, :datetime
62
- end
46
+ create_table :readonly_when_archiveds, force: true do |t|
47
+ t.column :name, :string
48
+ t.column :archive_number, :string
49
+ t.column :archived_at, :datetime
50
+ end
63
51
 
64
- create_table :missing_archived_ats, :force => true do |t|
65
- t.column :name, :string
66
- t.column :archive_number, :string
67
- end
52
+ create_table :missing_archived_ats, force: true do |t|
53
+ t.column :name, :string
54
+ t.column :archive_number, :string
55
+ end
68
56
 
69
- create_table :missing_archive_numbers, :force => true do |t|
70
- t.column :name, :string
71
- t.column :archived_at, :datetime
72
- end
57
+ create_table :missing_archive_numbers, force: true do |t|
58
+ t.column :name, :string
59
+ t.column :archived_at, :datetime
60
+ end
73
61
 
74
- create_table :polys, :force => true do |t|
75
- t.references :archiveable, :polymorphic => true
76
- t.column :archive_number, :string
77
- t.column :archived_at, :datetime
78
- end
62
+ create_table :polys, force: true do |t|
63
+ t.references :archiveable, polymorphic: true
64
+ t.column :archive_number, :string
65
+ t.column :archived_at, :datetime
66
+ end
79
67
 
80
- create_table :legacy, :force => true do |t|
81
- t.column :name, :string
82
- t.column :archive_number, :string
83
- t.column :archived_at, :datetime
84
- end
68
+ create_table :legacy, force: true do |t|
69
+ t.column :name, :string
70
+ t.column :archive_number, :string
71
+ t.column :archived_at, :datetime
72
+ end
85
73
 
86
- create_table :application_record_rows, :force => true do |t|
87
- t.column :archive_number, :string
88
- t.column :archived_at, :datetime
89
- end
74
+ create_table :application_record_rows, force: true do |t|
75
+ t.column :archive_number, :string
76
+ t.column :archived_at, :datetime
77
+ end
90
78
 
91
- create_table :callback_archival4s, :force => true do |t|
92
- t.column :settable_field, :string
93
- t.column :archive_number, :string
94
- t.column :archived_at, :datetime
95
- end
79
+ create_table :callback_archival4s, force: true do |t|
80
+ t.column :settable_field, :string
81
+ t.column :archive_number, :string
82
+ t.column :archived_at, :datetime
83
+ end
96
84
 
97
- create_table :callback_archival5s, :force => true do |t|
98
- t.column :settable_field, :string
99
- t.column :archive_number, :string
100
- t.column :archived_at, :datetime
101
- end
85
+ create_table :callback_archival5s, force: true do |t|
86
+ t.column :settable_field, :string
87
+ t.column :archive_number, :string
88
+ t.column :archived_at, :datetime
102
89
  end
103
90
  end