acts_as_archival 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -1
- data/CHANGELOG.md +5 -0
- data/lib/acts_as_archival/version.rb +1 -1
- data/lib/expected_behavior/acts_as_archival.rb +13 -16
- data/test/ambiguous_table_test.rb +2 -2
- data/test/basic_test.rb +2 -2
- data/test/fixtures/archival.rb +2 -0
- data/test/mass_attribute_test.rb +12 -10
- data/test/scope_test.rb +24 -1
- data/test/test_helper.rb +8 -4
- metadata +1 -1
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# Changes!
|
2
2
|
|
3
|
+
## 0.5.2
|
4
|
+
* More changes to support Rails 4
|
5
|
+
* fix scoping combinations with relations for Rails 4
|
6
|
+
* BREAKING CHANGE (possibly): removed the scope constants, so if you were using them, you should stop
|
7
|
+
|
3
8
|
## 0.5.1
|
4
9
|
* update to use .table_name for archived scope
|
5
10
|
|
@@ -2,9 +2,6 @@ module ExpectedBehavior
|
|
2
2
|
module ActsAsArchival
|
3
3
|
require 'digest/md5'
|
4
4
|
|
5
|
-
ARCHIVED_CONDITIONS = lambda { |zelf| %Q{#{zelf.table_name}.archived_at IS NOT NULL AND #{zelf.table_name}.archive_number IS NOT NULL} }
|
6
|
-
UNARCHIVED_CONDITIONS = { :archived_at => nil, :archive_number => nil }
|
7
|
-
|
8
5
|
MissingArchivalColumnError = Class.new(ActiveRecord::ActiveRecordError) unless defined?(MissingArchivalColumnError) == 'constant' && MissingArchivalColumnError.class == Class
|
9
6
|
CouldNotArchiveError = Class.new(ActiveRecord::ActiveRecordError) unless defined?(CouldNotArchiveError) == 'constant' && CouldNotArchiveError.class == Class
|
10
7
|
CouldNotUnarchiveError = Class.new(ActiveRecord::ActiveRecordError) unless defined?(CouldNotUnarchiveError) == 'constant' && CouldNotUnarchiveError.class == Class
|
@@ -21,9 +18,9 @@ module ExpectedBehavior
|
|
21
18
|
before_validation :raise_if_not_archival
|
22
19
|
validate :readonly_when_archived if options[:readonly_when_archived]
|
23
20
|
|
24
|
-
scope :archived,
|
25
|
-
scope :unarchived, :
|
26
|
-
scope :archived_from_archive_number, lambda { |head_archive_number|
|
21
|
+
scope :archived, lambda { where %Q{#{self.table_name}.archived_at IS NOT NULL AND #{self.table_name}.archive_number IS NOT NULL} }
|
22
|
+
scope :unarchived, lambda { where(:archived_at => nil, :archive_number => nil) }
|
23
|
+
scope :archived_from_archive_number, lambda { |head_archive_number| where(['archived_at IS NOT NULL AND archive_number = ?', head_archive_number]) }
|
27
24
|
|
28
25
|
callbacks = ['archive','unarchive']
|
29
26
|
define_callbacks *[callbacks, {:terminator => 'result == false'}].flatten
|
@@ -86,15 +83,15 @@ module ExpectedBehavior
|
|
86
83
|
def unarchive(head_archive_number=nil)
|
87
84
|
self.class.transaction do
|
88
85
|
begin
|
89
|
-
run_callbacks :unarchive
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
86
|
+
run_callbacks :unarchive do
|
87
|
+
if self.archived?
|
88
|
+
head_archive_number ||= self.archive_number
|
89
|
+
self.archived_at = nil
|
90
|
+
self.archive_number = nil
|
91
|
+
self.save!
|
92
|
+
self.unarchive_associations(head_archive_number)
|
93
|
+
end
|
96
94
|
end
|
97
|
-
run_callbacks :unarchive, :after
|
98
95
|
return true
|
99
96
|
rescue => e
|
100
97
|
ActiveRecord::Base.logger.try(:debug, e.message)
|
@@ -130,13 +127,13 @@ module ExpectedBehavior
|
|
130
127
|
def act_on_a_related_archival(klass, key_name, id, head_archive_number, options={})
|
131
128
|
return if options.length == 0 || (!options[:archive] && !options[:unarchive])
|
132
129
|
if options[:archive]
|
133
|
-
klass.unarchived.
|
130
|
+
klass.unarchived.where(["#{key_name} = ?", id]).each do |related_record|
|
134
131
|
unless related_record.archive(head_archive_number)
|
135
132
|
raise ActiveRecord::Rollback
|
136
133
|
end
|
137
134
|
end
|
138
135
|
else
|
139
|
-
klass.archived.
|
136
|
+
klass.archived.where(["#{key_name} = ? AND archive_number = ?", id, head_archive_number]).each do |related_record|
|
140
137
|
unless related_record.unarchive(head_archive_number)
|
141
138
|
raise ActiveRecord::Rollback
|
142
139
|
end
|
@@ -3,10 +3,10 @@ require_relative "test_helper"
|
|
3
3
|
class AmbiguousTableTest < ActiveSupport::TestCase
|
4
4
|
# test against the problem fixed in http://github.com/DarkTatka/acts_as_archival/commit/63d0a2532a15d7a6ab41d081e1591108a5ea9b37
|
5
5
|
test "no ambiguous table problem" do
|
6
|
-
archival = Archival.create!
|
6
|
+
archival = Archival.create!
|
7
7
|
child = archival.archivals.create!
|
8
8
|
child.archive
|
9
9
|
|
10
|
-
assert_equal 1, Archival.unarchived.
|
10
|
+
assert_equal 1, Archival.unarchived.joins(:archivals).count
|
11
11
|
end
|
12
12
|
end
|
data/test/basic_test.rb
CHANGED
@@ -40,8 +40,8 @@ class BasicTest < ActiveSupport::TestCase
|
|
40
40
|
archival.archive
|
41
41
|
sleep(0.001)
|
42
42
|
after = DateTime.now
|
43
|
-
|
44
|
-
|
43
|
+
assert before < archival.archived_at.to_datetime
|
44
|
+
assert after > archival.archived_at.to_datetime
|
45
45
|
end
|
46
46
|
|
47
47
|
test "archive sets the archive number to the md5 hexdigest for the model and id that is archived" do
|
data/test/fixtures/archival.rb
CHANGED
data/test/mass_attribute_test.rb
CHANGED
@@ -1,18 +1,20 @@
|
|
1
1
|
require_relative "test_helper"
|
2
2
|
|
3
3
|
class MassAttributeTest < ActiveSupport::TestCase
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
if $require_mass_protection
|
5
|
+
test "archive works when attr_accessible present" do
|
6
|
+
archival = MassAttributeProtected.create(:color => "pink")
|
7
|
+
archival.archive
|
7
8
|
|
8
|
-
|
9
|
-
|
9
|
+
assert archival.reload.archived?
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
test "unarchive works when attr_accessible present" do
|
13
|
+
archival = MassAttributeProtected.create(:color => "pink")
|
14
|
+
archival.archive
|
15
|
+
archival.unarchive
|
15
16
|
|
16
|
-
|
17
|
+
assert_not archival.reload.archived?
|
18
|
+
end
|
17
19
|
end
|
18
20
|
end
|
data/test/scope_test.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require_relative "test_helper"
|
2
2
|
|
3
|
-
class
|
3
|
+
class ScopeTest < ActiveSupport::TestCase
|
4
4
|
test "simple unarchived scope" do
|
5
5
|
Archival.create!
|
6
6
|
Archival.create!
|
@@ -52,4 +52,27 @@ class ActsAsArchivalTest < ActiveSupport::TestCase
|
|
52
52
|
assert_equal "SELECT `legacy`.* FROM `legacy` WHERE (legacy.archived_at IS NOT NULL AND legacy.archive_number IS NOT NULL)", ArchivalTableName.archived.to_sql
|
53
53
|
assert_equal "SELECT `legacy`.* FROM `legacy` WHERE `legacy`.`archived_at` IS NULL AND `legacy`.`archive_number` IS NULL", ArchivalTableName.unarchived.to_sql
|
54
54
|
end
|
55
|
+
|
56
|
+
test "combines with other scope properly" do
|
57
|
+
Archival.create!(:name => "Robert")
|
58
|
+
Archival.create!(:name => "Bobby")
|
59
|
+
Archival.create!(:name => "Sue")
|
60
|
+
Archival.create!(:name => "Bob").archive
|
61
|
+
assert_equal 3, Archival.bobs.count
|
62
|
+
assert_equal 3, Archival.unarchived.count
|
63
|
+
assert_equal 2, Archival.bobs.unarchived.count
|
64
|
+
assert_equal 2, Archival.unarchived.bobs.count
|
65
|
+
assert_equal 1, Archival.bobs.archived.count
|
66
|
+
assert_equal 1, Archival.archived.bobs.count
|
67
|
+
end
|
68
|
+
|
69
|
+
test "scopes combine with relations correctly" do
|
70
|
+
parent = Archival.create!
|
71
|
+
parent.archivals.create!
|
72
|
+
parent.archivals.create!
|
73
|
+
parent.archivals.create!.archive
|
74
|
+
assert_equal 3, parent.archivals.count
|
75
|
+
assert_equal 1, parent.archivals.archived.count
|
76
|
+
assert_equal 2, parent.archivals.unarchived.count
|
77
|
+
end
|
55
78
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
2
2
|
require "bundler/setup"
|
3
3
|
require "test/unit"
|
4
|
+
|
4
5
|
require "active_record"
|
5
6
|
require "assertions"
|
6
7
|
require "database_cleaner"
|
8
|
+
|
7
9
|
require "acts_as_archival"
|
8
10
|
|
9
11
|
def prepare_for_tests
|
@@ -43,19 +45,21 @@ def require_test_classes
|
|
43
45
|
inflect.irregular "poly", "polys"
|
44
46
|
end
|
45
47
|
|
46
|
-
[:archival,
|
48
|
+
fixtures = [:archival,
|
47
49
|
:archival_kid,
|
48
50
|
:archival_grandkid,
|
49
51
|
:archival_table_name,
|
50
52
|
:exploder,
|
51
53
|
:independent_archival,
|
52
|
-
:
|
54
|
+
:plain,
|
53
55
|
:missing_archived_at,
|
54
56
|
:missing_archive_number,
|
55
57
|
:plain,
|
56
58
|
:poly,
|
57
|
-
:readonly_when_archived
|
58
|
-
|
59
|
+
:readonly_when_archived]
|
60
|
+
$require_mass_protection = ActiveModel.constants.include?(:MassAssignmentSecurity)
|
61
|
+
fixtures << :mass_attribute_protected if $require_mass_protection
|
62
|
+
fixtures.each {|test_class_file| require_relative "fixtures/#{test_class_file}"}
|
59
63
|
end
|
60
64
|
|
61
65
|
prepare_for_tests
|