effective_resources 2.2.8 → 2.2.10
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f5258d38255a23862d15be45d82d15d68596bb42b2f31f88e5159c339e2fae8
|
4
|
+
data.tar.gz: c6dba77a6cdb45ec02fead79b8050cee8c482325ec772a53ffde5cea9facc754
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c0f15eacdd2e1b34c85b57921dd21d01f3dfb1b22e45f17b45c2f0a066add4f0ede0856e649e843a77c016212c5133e88b780980ce48cc30c844a6e0e735149c
|
7
|
+
data.tar.gz: 22088c964406e716aa5d430372813632064278e1a02e1a4fa09f6845a1a8d55b5799a14ce2d29329b624fe59d7d7b1710fcb0437242c345848de21e9bd2a46cb
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# HasManyPurgable
|
2
|
+
#
|
3
|
+
# Mark your model with 'has_many_purgable' or 'has_one_purgable' to allow any has_many or has_one to be purgable
|
4
|
+
# Pass 'has_many_purgable :files, :avatar' to only allow the files and avatar to be purged.
|
5
|
+
# Works with effective_bootstrap file_field to display a Delete file on save checkbox
|
6
|
+
# to submit a _purge_attached array or association names to purge.
|
7
|
+
#
|
8
|
+
# You must permit the attribute _purge_attached: []
|
9
|
+
|
10
|
+
module HasManyPurgable
|
11
|
+
extend ActiveSupport::Concern
|
12
|
+
|
13
|
+
module Base
|
14
|
+
def has_many_purgable(*args)
|
15
|
+
options = args.extract_options!
|
16
|
+
names = Array(args).compact.presence || :all
|
17
|
+
|
18
|
+
@has_many_purgable_options = options.merge(names: names)
|
19
|
+
|
20
|
+
include ::HasManyPurgable
|
21
|
+
end
|
22
|
+
|
23
|
+
def has_one_purgable(*args)
|
24
|
+
has_many_purgable(*args)
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
included do
|
30
|
+
options = @has_many_purgable_options
|
31
|
+
self.send(:define_method, :has_many_purgable_options) { options }
|
32
|
+
|
33
|
+
attr_accessor :_purge_attached
|
34
|
+
|
35
|
+
effective_resource do
|
36
|
+
_purge_attached :permitted_param
|
37
|
+
end
|
38
|
+
|
39
|
+
with_options(if: -> { _purge_attached.present? }) do
|
40
|
+
before_validation { has_many_purgable_mark_for_destruction }
|
41
|
+
after_save { has_many_purgable_purge }
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
module ClassMethods
|
47
|
+
def has_many_purgable?; true; end
|
48
|
+
end
|
49
|
+
|
50
|
+
# All the possible names, merging the actual associations and the given options
|
51
|
+
def has_many_purgable_names
|
52
|
+
names = has_many_purgable_options.fetch(:names)
|
53
|
+
|
54
|
+
associations = self.class.reflect_on_all_associations
|
55
|
+
.select { |ass| ass.class_name == 'ActiveStorage::Attachment' }
|
56
|
+
.map { |ass| ass.name.to_s.chomp('_attachments').chomp('_attachment').to_sym }
|
57
|
+
|
58
|
+
names == :all ? associations : (names & associations)
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
# As submitted by the form and permitted by our associations and options
|
64
|
+
def has_many_purgable_attachments
|
65
|
+
submitted = (Array(_purge_attached) - [nil, '', '0', ' ', 'false', 'f', 'off']).map(&:to_sym)
|
66
|
+
submitted & has_many_purgable_names
|
67
|
+
end
|
68
|
+
|
69
|
+
def has_many_purgable_mark_for_destruction
|
70
|
+
has_many_purgable_attachments.each do |name|
|
71
|
+
Array(public_send(name)).each { |attachment| attachment.mark_for_destruction }
|
72
|
+
end
|
73
|
+
|
74
|
+
true
|
75
|
+
end
|
76
|
+
|
77
|
+
def has_many_purgable_purge
|
78
|
+
has_many_purgable_attachments.each do |name|
|
79
|
+
Rails.logger.info "[has_many_purgable] Purging #{name} attachments"
|
80
|
+
Array(public_send(name)).each { |attachment| attachment.purge }
|
81
|
+
end
|
82
|
+
|
83
|
+
true
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
@@ -29,6 +29,7 @@ module EffectiveResources
|
|
29
29
|
ActiveRecord::Base.extend(ActsAsStatused::Base)
|
30
30
|
ActiveRecord::Base.extend(ActsAsWizard::Base)
|
31
31
|
ActiveRecord::Base.extend(ActsAsPurchasableWizard::Base)
|
32
|
+
ActiveRecord::Base.extend(HasManyPurgable::Base)
|
32
33
|
ActiveRecord::Base.extend(HasManyRichTexts::Base)
|
33
34
|
|
34
35
|
ActiveRecord::Base.extend(EffectiveDeviseUser::Base)
|
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: 2.2.
|
4
|
+
version: 2.2.10
|
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: 2023-01-
|
11
|
+
date: 2023-01-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -165,6 +165,7 @@ files:
|
|
165
165
|
- app/models/concerns/effective_after_commit.rb
|
166
166
|
- app/models/concerns/effective_devise_user.rb
|
167
167
|
- app/models/concerns/effective_resource.rb
|
168
|
+
- app/models/concerns/has_many_purgable.rb
|
168
169
|
- app/models/concerns/has_many_rich_texts.rb
|
169
170
|
- app/models/effective/access_denied.rb
|
170
171
|
- app/models/effective/action_failed.rb
|