effective_resources 1.3.1 → 1.3.2
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 +4 -4
- data/app/controllers/concerns/effective/crud_controller.rb +1 -1
- data/app/controllers/concerns/effective/crud_controller/dsl.rb +1 -1
- data/app/models/concerns/acts_as_archived.rb +84 -6
- data/lib/effective_resources/engine.rb +1 -1
- data/lib/effective_resources/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eddc7cf600ae3743cb4a686c5ddbff6251f70660568de4f6754a2e13e52b3ea9
|
4
|
+
data.tar.gz: 1d571d8f411aa63166f677446ced8b5d5f3f40f96c3aa2b47d29f08c704655c5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71aa4be47daebc507cf4136e0deadcf74ec0ed60399a5ec5200ea1ef31c215e2767e7a323ae060ce4d350bf1ca3d25bad64a66f2defa0b531e91e5ab9c009887
|
7
|
+
data.tar.gz: 1d5df0cc8e833a2a89dffb13ba65aaa8f84930ac203007912076545c4e431b43e4cd00fb081bcb20528137352bd6e2f0a83290932ba8c8d2f66d12ef341312e7
|
@@ -82,7 +82,7 @@ module Effective
|
|
82
82
|
# Returns an ActiveRecord relation based on the computed value of `resource_scope` dsl method
|
83
83
|
def resource_scope # Thing
|
84
84
|
@_effective_resource_relation ||= (
|
85
|
-
relation = case @_effective_resource_scope # If this was initialized by the resource_scope
|
85
|
+
relation = case @_effective_resource_scope # If this was initialized by the resource_scope before_action
|
86
86
|
when ActiveRecord::Relation
|
87
87
|
@_effective_resource_scope
|
88
88
|
when Hash
|
@@ -80,7 +80,7 @@ module Effective
|
|
80
80
|
raise 'expected a proc or block' unless (obj.respond_to?(:call) || block_given?)
|
81
81
|
|
82
82
|
instance_exec do
|
83
|
-
|
83
|
+
prepend_before_action(opts) { @_effective_resource_scope ||= instance_exec(&(block_given? ? block : obj)) }
|
84
84
|
end
|
85
85
|
end
|
86
86
|
|
@@ -8,6 +8,7 @@
|
|
8
8
|
# class Thing < ApplicationRecord
|
9
9
|
# has_many :comments
|
10
10
|
# acts_as_archivable cascade: :comments
|
11
|
+
# acts_as_archivable cascade: :comments, strategy: :archive|:archive_all|:active_job
|
11
12
|
# end
|
12
13
|
|
13
14
|
# Each controller needs its own archive and unarchive action.
|
@@ -28,15 +29,20 @@ module ActsAsArchived
|
|
28
29
|
extend ActiveSupport::Concern
|
29
30
|
|
30
31
|
module ActiveRecord
|
31
|
-
def acts_as_archived(cascade: [])
|
32
|
+
def acts_as_archived(cascade: [], strategy: :archive)
|
32
33
|
|
33
34
|
cascade = Array(cascade).compact
|
35
|
+
strategy = strategy
|
34
36
|
|
35
37
|
if cascade.any? { |obj| !obj.kind_of?(Symbol) }
|
36
38
|
raise 'expected cascade to be an Array of has_many symbols'
|
37
39
|
end
|
38
40
|
|
39
|
-
|
41
|
+
unless [:archive, :archive_all, :active_job].include?(strategy)
|
42
|
+
raise 'expected strategy to be :archive, :archive_all, or :active_job'
|
43
|
+
end
|
44
|
+
|
45
|
+
@acts_as_archived_options = { cascade: cascade, strategy: strategy }
|
40
46
|
|
41
47
|
include ::ActsAsArchived
|
42
48
|
end
|
@@ -61,6 +67,8 @@ module ActsAsArchived
|
|
61
67
|
end
|
62
68
|
|
63
69
|
included do
|
70
|
+
define_callbacks :archive, :unarchive # ActiveSupport::Callbacks
|
71
|
+
|
64
72
|
scope :archived, -> { where(archived: true) }
|
65
73
|
scope :unarchived, -> { where(archived: [false, nil]) }
|
66
74
|
|
@@ -74,26 +82,96 @@ module ActsAsArchived
|
|
74
82
|
|
75
83
|
module ClassMethods
|
76
84
|
def acts_as_archived?; true; end
|
85
|
+
|
86
|
+
# before_archive(if: -> { persisted? })
|
87
|
+
def before_archive(*filters, &blk)
|
88
|
+
set_callback(:archive, :before, *filters, &blk)
|
89
|
+
end
|
90
|
+
|
91
|
+
def after_archive(*filters, &blk)
|
92
|
+
set_callback(:archive, :after, *filters, &blk)
|
93
|
+
end
|
94
|
+
|
95
|
+
def before_unarchive(*filters, &blk)
|
96
|
+
set_callback(:unarchive, :before, *filters, &blk)
|
97
|
+
end
|
98
|
+
|
99
|
+
def after_unarchive(*filters, &blk)
|
100
|
+
set_callback(:unarchive, :after, *filters, &blk)
|
101
|
+
end
|
102
|
+
|
103
|
+
# after_commit(if: :just_archived?)
|
104
|
+
# after_commit(if: :just_unarchived?)
|
77
105
|
end
|
78
106
|
|
79
107
|
# Instance methods
|
80
108
|
def archive!
|
109
|
+
return true if archived?
|
110
|
+
|
111
|
+
strategy = acts_as_archived_options[:strategy]
|
112
|
+
cascade = acts_as_archived_options[:cascade]
|
113
|
+
|
81
114
|
transaction do
|
82
|
-
|
83
|
-
|
115
|
+
run_callbacks :archive do
|
116
|
+
update!(archived: true) # Runs validations
|
117
|
+
|
118
|
+
if strategy == :archive_all
|
119
|
+
cascade.each { |associated| public_send(associated).update_all(archived: true) }
|
120
|
+
end
|
121
|
+
|
122
|
+
if strategy == :archive
|
123
|
+
cascade.each { |associated| Array(public_send(associated)).each { |resource| resource.archive! } }
|
124
|
+
end
|
125
|
+
end
|
84
126
|
end
|
127
|
+
|
128
|
+
if strategy == :active_job
|
129
|
+
ActsAsArchivedArchiveJob.perform_later(self)
|
130
|
+
end
|
131
|
+
|
132
|
+
true
|
85
133
|
end
|
86
134
|
|
87
135
|
def unarchive!
|
136
|
+
return true unless archived?
|
137
|
+
|
138
|
+
strategy = acts_as_archived_options[:strategy]
|
139
|
+
cascade = acts_as_archived_options[:cascade]
|
140
|
+
|
88
141
|
transaction do
|
89
|
-
|
90
|
-
|
142
|
+
run_callbacks :unarchive do
|
143
|
+
update!(archived: false) # Runs validations
|
144
|
+
|
145
|
+
if strategy == :archive_all
|
146
|
+
cascade.each { |associated| public_send(associated).update_all(archived: false) }
|
147
|
+
end
|
148
|
+
|
149
|
+
if strategy == :archive
|
150
|
+
cascade.each { |associated| Array(public_send(associated)).each { |resource| resource.unarchive! } }
|
151
|
+
end
|
152
|
+
end
|
91
153
|
end
|
154
|
+
|
155
|
+
if strategy == :active_job
|
156
|
+
ActsAsArchivedUnarchiveJob.perform_later(self)
|
157
|
+
end
|
158
|
+
|
159
|
+
true
|
92
160
|
end
|
93
161
|
|
94
162
|
def destroy
|
95
163
|
archive!
|
96
164
|
end
|
97
165
|
|
166
|
+
private
|
167
|
+
|
168
|
+
def just_archived?
|
169
|
+
previous_changes[:archived] && archived?
|
170
|
+
end
|
171
|
+
|
172
|
+
def just_unarchived?
|
173
|
+
previous_changes[:archived] && !archived?
|
174
|
+
end
|
175
|
+
|
98
176
|
end
|
99
177
|
|
@@ -2,7 +2,7 @@ module EffectiveResources
|
|
2
2
|
class Engine < ::Rails::Engine
|
3
3
|
engine_name 'effective_resources'
|
4
4
|
|
5
|
-
config.autoload_paths += Dir["#{config.root}/lib/", "#{config.root}/app/controllers/concerns/effective/"]
|
5
|
+
config.autoload_paths += Dir["#{config.root}/lib/", "#{config.root}/jobs/", "#{config.root}/app/controllers/concerns/effective/"]
|
6
6
|
|
7
7
|
# Set up our default configuration options.
|
8
8
|
initializer 'effective_resources.defaults', before: :load_config_initializers do |app|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: effective_resources
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Code and Effect
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-05
|
11
|
+
date: 2019-06-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|