acts_as_archival 1.1.1 → 1.2.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 (57) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +81 -0
  3. data/.rubocop_todo.yml +7 -0
  4. data/.travis.yml +34 -0
  5. data/Appraisals +5 -2
  6. data/CHANGELOG.md +8 -0
  7. data/README.md +14 -7
  8. data/Rakefile +6 -3
  9. data/acts_as_archival.gemspec +3 -4
  10. data/gemfiles/rails_4.1.gemfile +0 -1
  11. data/gemfiles/rails_5.0.gemfile +1 -1
  12. data/gemfiles/rails_5.1.beta.gemfile +7 -0
  13. data/lib/acts_as_archival/version.rb +3 -1
  14. data/lib/expected_behavior/acts_as_archival.rb +115 -77
  15. data/lib/expected_behavior/acts_as_archival_active_record_methods.rb +22 -2
  16. data/lib/expected_behavior/association_operation/base.rb +7 -8
  17. data/lib/expected_behavior/association_operation/unarchive.rb +1 -1
  18. data/script/setup +0 -3
  19. data/test/ambiguous_table_test.rb +2 -0
  20. data/test/application_record_test.rb +3 -1
  21. data/test/associations_test.rb +2 -0
  22. data/test/basic_test.rb +6 -4
  23. data/test/callbacks_test.rb +4 -2
  24. data/test/column_test.rb +8 -6
  25. data/test/deep_nesting_test.rb +4 -2
  26. data/test/fixtures/application_record.rb +2 -0
  27. data/test/fixtures/application_record_row.rb +2 -0
  28. data/test/fixtures/archival.rb +9 -7
  29. data/test/fixtures/archival_grandkid.rb +2 -0
  30. data/test/fixtures/archival_kid.rb +3 -1
  31. data/test/fixtures/archival_table_name.rb +2 -0
  32. data/test/fixtures/callback_archival_4.rb +2 -0
  33. data/test/fixtures/callback_archival_5.rb +4 -1
  34. data/test/fixtures/exploder.rb +2 -0
  35. data/test/fixtures/independent_archival.rb +2 -0
  36. data/test/fixtures/missing_archive_number.rb +2 -0
  37. data/test/fixtures/missing_archived_at.rb +2 -0
  38. data/test/fixtures/plain.rb +2 -0
  39. data/test/fixtures/poly.rb +3 -1
  40. data/test/fixtures/readonly_when_archived.rb +3 -1
  41. data/test/polymorphic_test.rb +4 -2
  42. data/test/readonly_when_archived_test.rb +4 -2
  43. data/test/responds_test.rb +22 -4
  44. data/test/schema.rb +59 -79
  45. data/test/scope_test.rb +10 -6
  46. data/test/test_helper.rb +46 -40
  47. data/test/through_association_test.rb +3 -1
  48. data/test/transaction_test.rb +2 -53
  49. metadata +16 -41
  50. data/script/db_setup +0 -51
  51. data/test/database.yml +0 -26
  52. data/test/fixtures/mass_attribute_protected.rb +0 -7
  53. data/test/fixtures/mysql_archival.rb +0 -10
  54. data/test/fixtures/mysql_exploder.rb +0 -9
  55. data/test/fixtures/pg_archival.rb +0 -10
  56. data/test/fixtures/pg_exploder.rb +0 -9
  57. data/test/mass_attribute_test.rb +0 -20
@@ -1,20 +1,40 @@
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
10
+
11
+ def archival?
12
+ included_modules.include?(ExpectedBehavior::ActsAsArchival::InstanceMethods)
13
+ end
14
+
15
+ # rubocop:disable Style/PredicateName
9
16
  def is_archival?
10
- self.included_modules.include?(ExpectedBehavior::ActsAsArchival::InstanceMethods)
17
+ ActiveSupport::Deprecation.warn(".is_archival? is deprecated in favor of .archival?")
18
+ archival?
11
19
  end
20
+ # rubocop:enable Style/PredicateName
21
+
12
22
  end
13
23
 
14
24
  module ARInstanceMethods
25
+
26
+ def archival?
27
+ self.class.archival?
28
+ end
29
+
30
+ # rubocop:disable Style/PredicateName
15
31
  def is_archival?
16
- self.class.is_archival?
32
+ ActiveSupport::Deprecation.warn("#is_archival? is deprecated in favor of #archival?")
33
+ archival?
17
34
  end
35
+ # rubocop:enable Style/PredicateName
36
+
18
37
  end
38
+
19
39
  end
20
40
  end
@@ -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,18 +19,18 @@ module ExpectedBehavior
19
19
  protected
20
20
 
21
21
  def each_archivable_association
22
- self.model.class.reflect_on_all_associations.each do |association|
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.is_archival? &&
29
+ association.klass.archival? &&
30
30
  association.options[:through].nil?
31
31
  end
32
32
 
33
- def association_conditions_met?(association)
33
+ def association_conditions_met?(_association)
34
34
  true
35
35
  end
36
36
 
@@ -40,12 +40,11 @@ module ExpectedBehavior
40
40
  act_on_archivals(scope)
41
41
  end
42
42
 
43
- def act_on_archivals(scope)
44
- raise NotImplementedError,
45
- "The #{self.class} hasn't implemented 'act_on_archivals(scope)'"
43
+ def act_on_archivals(_scope)
44
+ raise NotImplementedError.new("The #{self.class} hasn't implemented 'act_on_archivals(scope)'")
46
45
  end
47
- end
48
46
 
47
+ end
49
48
  end
50
49
  end
51
50
  end
@@ -6,7 +6,7 @@ module ExpectedBehavior
6
6
  protected
7
7
 
8
8
  def act_on_archivals(scope)
9
- scope.archived.where(:archive_number => head_archive_number).find_each do |related_record|
9
+ scope.archived.where(archive_number: head_archive_number).find_each do |related_record|
10
10
  raise ActiveRecord::Rollback unless related_record.unarchive(head_archive_number)
11
11
  end
12
12
  end
data/script/setup CHANGED
@@ -5,8 +5,5 @@ cd "`dirname \"$0\"`/.."
5
5
  echo "\n---=== Bundling ===---"
6
6
  bundle install
7
7
 
8
- echo "\n---=== Setting up test databases ===---"
9
- script/db_setup
10
-
11
8
  echo "\n---=== Setting up various rails versions for testing ===---"
12
9
  bundle exec appraisal install
@@ -1,6 +1,7 @@
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!
@@ -11,4 +12,5 @@ class AmbiguousTableTest < ActiveSupport::TestCase
11
12
  # generated
12
13
  assert_equal 1, Archival.unarchived.joins(:archivals).count
13
14
  end
15
+
14
16
  end
@@ -3,6 +3,7 @@ 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
9
  archival.archive
@@ -10,9 +11,10 @@ if defined?(ApplicationRecord)
10
11
  end
11
12
 
12
13
  test "unarchive unarchives archival records" do
13
- archival = ApplicationRecordRow.create!(:archived_at => Time.now, :archive_number => 1)
14
+ archival = ApplicationRecordRow.create!(archived_at: Time.now, archive_number: 1)
14
15
  archival.unarchive
15
16
  assert_not archival.reload.archived?
16
17
  end
18
+
17
19
  end
18
20
  end
@@ -1,6 +1,7 @@
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!
@@ -99,4 +100,5 @@ class AssociationsTest < ActiveSupport::TestCase
99
100
 
100
101
  assert prearchived_child.reload.archived?
101
102
  end
103
+
102
104
  end
data/test/basic_test.rb CHANGED
@@ -1,6 +1,7 @@
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
7
  archival.archive
@@ -8,7 +9,7 @@ class BasicTest < ActiveSupport::TestCase
8
9
  end
9
10
 
10
11
  test "unarchive unarchives archival records" do
11
- archival = Archival.create!(:archived_at => Time.now, :archive_number => 1)
12
+ archival = Archival.create!(archived_at: Time.now, archive_number: 1)
12
13
  archival.unarchive
13
14
  assert_equal false, archival.reload.archived?
14
15
  end
@@ -25,12 +26,12 @@ class BasicTest < ActiveSupport::TestCase
25
26
  end
26
27
 
27
28
  test "unarchive returns true on success" do
28
- normal = Archival.create!(:archived_at => Time.now, :archive_number => "1")
29
+ normal = Archival.create!(archived_at: Time.now, archive_number: "1")
29
30
  assert_equal true, normal.unarchive
30
31
  end
31
32
 
32
33
  test "unarchive returns false on failure" do
33
- readonly = Archival.create!(:archived_at => Time.now, :archive_number => "1")
34
+ readonly = Archival.create!(archived_at: Time.now, archive_number: "1")
34
35
  readonly.readonly!
35
36
  assert_equal false, readonly.unarchive
36
37
  end
@@ -43,7 +44,7 @@ class BasicTest < ActiveSupport::TestCase
43
44
  sleep(0.001)
44
45
  after = DateTime.now
45
46
  assert before < archival.archived_at.to_datetime
46
- assert after > archival.archived_at.to_datetime
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
@@ -61,4 +62,5 @@ class BasicTest < ActiveSupport::TestCase
61
62
  second_number = archived.archive_number
62
63
  assert_equal initial_number, second_number
63
64
  end
65
+
64
66
  end
@@ -1,6 +1,7 @@
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
@@ -11,7 +12,7 @@ class CallbacksTest < ActiveSupport::TestCase
11
12
  end
12
13
 
13
14
  test "can be halted" do
14
- archival = archival = CallbackArchival5.create
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
@@ -28,7 +29,7 @@ class CallbacksTest < ActiveSupport::TestCase
28
29
  end
29
30
 
30
31
  test "can be halted" do
31
- archival = archival = CallbackArchival4.create
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
@@ -36,4 +37,5 @@ class CallbacksTest < ActiveSupport::TestCase
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!(:name => "foo-foo")
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!(:name => "rover")
13
- }
12
+ assert_raises(ExpectedBehavior::ActsAsArchival::MissingArchivalColumnError) do
13
+ MissingArchiveNumber.create!(name: "rover")
14
+ end
14
15
  end
16
+
15
17
  end
@@ -1,6 +1,7 @@
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!
@@ -15,8 +16,8 @@ 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)
@@ -26,4 +27,5 @@ class DeepNestingTest < ActiveSupport::TestCase
26
27
  assert_not child.reload.archived?
27
28
  assert_not grandchild.reload.archived?
28
29
  end
30
+
29
31
  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