acts_as_archival 0.5.2 → 0.5.3

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.
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Changes!
2
2
 
3
+ ## 0.5.3
4
+ * Major refactoring of archiving/unarchiving logic into nice command classes instead of big ball of ARec voodoo. Thanks, Marten Claes!
5
+
3
6
  ## 0.5.2
4
7
  * More changes to support Rails 4
5
8
  * fix scoping combinations with relations for Rails 4
data/README.md CHANGED
@@ -16,10 +16,14 @@ Additionally, other plugins generally screw with how
16
16
  to destroy records.
17
17
 
18
18
  ## Install
19
- Rails 3.2+ (maybe 3.1?)
19
+ Rails 3.2 and up
20
20
 
21
21
  `gem install acts_as_archival`
22
22
 
23
+ or in your Gemfile
24
+
25
+ `gem "acts_as_archival"`
26
+
23
27
  Rails 3.0x:
24
28
 
25
29
  `rails plugin install http://github.com/expectedbehavior/acts_as_archival.git -r rails3.0x`
@@ -97,8 +101,8 @@ postgres and if not, submit a patch / let us know about it!
97
101
 
98
102
  ## Thanks
99
103
 
100
- ActsAsParanoid and PermanentRecords were both inspirations for this:
101
- http://github.com/technoweenie/acts_as_paranoid
104
+ ActsAsParanoid and PermanentRecords were both inspirations for this:
105
+ http://github.com/technoweenie/acts_as_paranoid
102
106
  http://github.com/fastestforward/permanent_records
103
107
 
104
108
  ## Contributors
@@ -111,6 +115,7 @@ http://github.com/fastestforward/permanent_records
111
115
  * Dave Woodward
112
116
  * Miles Sterrett
113
117
  * James Hill
118
+ * Marten Claes
114
119
 
115
120
  Thanks!
116
121
 
@@ -13,10 +13,14 @@ Gem::Specification.new do |gem|
13
13
  "David Jones",
14
14
  "Dave Woodward",
15
15
  "Miles Sterrett",
16
- "James Hill"]
16
+ "James Hill",
17
+ "Marten Claes"]
17
18
  gem.email = ["joel@expectedbehavior.com",
18
19
  "michael@expectedbehavior.com",
19
- "matt@expectedbehavior.com"]
20
+ "matt@expectedbehavior.com",
21
+ "jason@expectedbehavior.com",
22
+ "tyler@expectedbehavior.com",
23
+ "nathan@expectedbehavior.com"]
20
24
  gem.homepage = "http://github.com/expectedbehavior/acts_as_archival"
21
25
 
22
26
  gem.files = `git ls-files`.split("\n")
@@ -1,5 +1,9 @@
1
1
  require "acts_as_archival/version"
2
2
 
3
+ require "expected_behavior/association_operation/base"
4
+ require "expected_behavior/association_operation/archive"
5
+ require "expected_behavior/association_operation/unarchive"
6
+
3
7
  require "expected_behavior/acts_as_archival"
4
8
  require "expected_behavior/acts_as_archival_active_record_methods"
5
9
 
@@ -1,3 +1,3 @@
1
1
  module ActsAsArchival
2
- VERSION = "0.5.2"
2
+ VERSION = "0.5.3"
3
3
  end
@@ -103,42 +103,11 @@ module ExpectedBehavior
103
103
  end
104
104
 
105
105
  def archive_associations(head_archive_number)
106
- act_only_on_dependent_destroy_associations = Proc.new {|association| association.options[:dependent] == :destroy}
107
- act_on_all_archival_associations(head_archive_number, :archive => true, :association_options => act_only_on_dependent_destroy_associations)
106
+ AssociationOperation::Archive.new(self, head_archive_number).execute
108
107
  end
109
108
 
110
109
  def unarchive_associations(head_archive_number)
111
- act_on_all_archival_associations(head_archive_number, :unarchive => true)
112
- end
113
-
114
- def act_on_all_archival_associations(head_archive_number, options={})
115
- return if options.length == 0
116
- options[:association_options] ||= Proc.new { true }
117
- self.class.reflect_on_all_associations.each do |association|
118
- if (association.macro.to_s =~ /^has/ && association.klass.is_archival? &&
119
- options[:association_options].call(association) &&
120
- association.options[:through].nil?)
121
- association_key = association.respond_to?(:foreign_key) ? association.foreign_key : association.primary_key_name
122
- act_on_a_related_archival(association.klass, association_key, id, head_archive_number, options)
123
- end
124
- end
125
- end
126
-
127
- def act_on_a_related_archival(klass, key_name, id, head_archive_number, options={})
128
- return if options.length == 0 || (!options[:archive] && !options[:unarchive])
129
- if options[:archive]
130
- klass.unarchived.where(["#{key_name} = ?", id]).each do |related_record|
131
- unless related_record.archive(head_archive_number)
132
- raise ActiveRecord::Rollback
133
- end
134
- end
135
- else
136
- klass.archived.where(["#{key_name} = ? AND archive_number = ?", id, head_archive_number]).each do |related_record|
137
- unless related_record.unarchive(head_archive_number)
138
- raise ActiveRecord::Rollback
139
- end
140
- end
141
- end
110
+ AssociationOperation::Unarchive.new(self, head_archive_number).execute
142
111
  end
143
112
  end
144
113
  end
@@ -0,0 +1,21 @@
1
+ module ExpectedBehavior
2
+ module ActsAsArchival
3
+ module AssociationOperation
4
+ class Archive < Base
5
+
6
+ protected
7
+
8
+ def act_on_archivals(archivals)
9
+ archivals.unarchived.find_each do |related_record|
10
+ raise ActiveRecord::Rollback unless related_record.archive(head_archive_number)
11
+ end
12
+ end
13
+
14
+ def association_conditions_met?(association)
15
+ association.options[:dependent] == :destroy
16
+ end
17
+
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,51 @@
1
+ module ExpectedBehavior
2
+ module ActsAsArchival
3
+ module AssociationOperation
4
+
5
+ class Base
6
+ attr_reader :model, :head_archive_number
7
+
8
+ def initialize(model, head_archive_number)
9
+ @model = model
10
+ @head_archive_number = head_archive_number
11
+ end
12
+
13
+ def execute
14
+ each_archivable_association do |association|
15
+ act_on_association(association) if association_conditions_met? association
16
+ end
17
+ end
18
+
19
+ protected
20
+
21
+ def each_archivable_association
22
+ self.model.class.reflect_on_all_associations.each do |association|
23
+ yield(association) if archivable_association?(association)
24
+ end
25
+ end
26
+
27
+ def archivable_association?(association)
28
+ association.macro.to_s =~ /^has/ &&
29
+ association.klass.is_archival? &&
30
+ association.options[:through].nil?
31
+ end
32
+
33
+ def association_conditions_met?(association)
34
+ true
35
+ end
36
+
37
+ def act_on_association(association)
38
+ key = association.respond_to?(:foreign_key) ? association.foreign_key : association.primary_key_name
39
+ scope = association.klass.where(key => model.id)
40
+ act_on_archivals(scope)
41
+ end
42
+
43
+ def act_on_archivals(scope)
44
+ raise NotImplementedError,
45
+ "The #{self.class} hasn't implemented 'act_on_archivals(scope)'"
46
+ end
47
+ end
48
+
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,17 @@
1
+ module ExpectedBehavior
2
+ module ActsAsArchival
3
+ module AssociationOperation
4
+ class Unarchive < Base
5
+
6
+ protected
7
+
8
+ def act_on_archivals(scope)
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
+ end
12
+ end
13
+
14
+ end
15
+ end
16
+ end
17
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_archival
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,6 +13,7 @@ authors:
13
13
  - Dave Woodward
14
14
  - Miles Sterrett
15
15
  - James Hill
16
+ - Marten Claes
16
17
  autorequire:
17
18
  bindir: bin
18
19
  cert_chain: []
@@ -158,6 +159,9 @@ email:
158
159
  - joel@expectedbehavior.com
159
160
  - michael@expectedbehavior.com
160
161
  - matt@expectedbehavior.com
162
+ - jason@expectedbehavior.com
163
+ - tyler@expectedbehavior.com
164
+ - nathan@expectedbehavior.com
161
165
  executables: []
162
166
  extensions: []
163
167
  extra_rdoc_files: []
@@ -176,6 +180,9 @@ files:
176
180
  - lib/acts_as_archival/version.rb
177
181
  - lib/expected_behavior/acts_as_archival.rb
178
182
  - lib/expected_behavior/acts_as_archival_active_record_methods.rb
183
+ - lib/expected_behavior/association_operation/archive.rb
184
+ - lib/expected_behavior/association_operation/base.rb
185
+ - lib/expected_behavior/association_operation/unarchive.rb
179
186
  - test/ambiguous_table_test.rb
180
187
  - test/associations_test.rb
181
188
  - test/basic_test.rb