effective_resources 2.2.8 → 2.2.9

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: b2b012e5d5abc1400fabdf14bf5afbbae28bfd8f5963a51274ca39bac1b6a682
4
- data.tar.gz: 37f817c1a4a184a069cf9f2d3b71a4548bd1e55f8496cbb003778c5f10f78e7a
3
+ metadata.gz: cb2b489df65b7ef3e426fa8e43fe5e142560f861c7c1f7a2d580b3ce864d74d4
4
+ data.tar.gz: 7776fbc00d3ad40f8c8ac6ccff80ff2e114c48b52f47d59ea054ae4bddfaf15d
5
5
  SHA512:
6
- metadata.gz: bca95eb795e3400ec7beaa81385ca145bc9e0671abe42558db51f7a035c5f45c6051bcc74dc428aca94e7bd12ada62a73a56b223f93d0b83c0c53af3047fb678
7
- data.tar.gz: cd1b657b8b624af16c92edd64e4beb3c73a56121e97f64f44aaf49291efd8f56bc17704fbfe95328a54eb408b97ffd84c29589f71aaf4f9674cfe47bdbf6d4ed
6
+ metadata.gz: 1b3d35a468f47af10084426d26d461d5168a6637bfe973b0ba5182bf65cbd1a4804255f956d28a558bc227b4ed986d8ad47eaf39385639db18b9e27afa6a1a24
7
+ data.tar.gz: e9bfd478683a588e25f4a24986d9f695485e4d59451af70415c83852a7fe0f48ec7b7211b000b0541e19e931154df8f2ccf2362e6c785c1fba072dd42120fed7
@@ -0,0 +1,80 @@
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
+ module HasManyPurgable
9
+ extend ActiveSupport::Concern
10
+
11
+ module Base
12
+ def has_many_purgable(*args)
13
+ options = args.extract_options!
14
+ names = Array(args).compact.presence || :all
15
+
16
+ @has_many_purgable_options = options.merge(names: names)
17
+
18
+ include ::HasManyPurgable
19
+ end
20
+
21
+ def has_one_purgable(*args)
22
+ has_many_purgable(*args)
23
+ end
24
+
25
+ end
26
+
27
+ included do
28
+ options = @has_many_purgable_options
29
+ self.send(:define_method, :has_many_purgable_options) { options }
30
+
31
+ attr_accessor :_purge_attached
32
+
33
+ with_options(if: -> { _purge_attached.present? }) do
34
+ before_validation { has_many_purgable_mark_for_destruction }
35
+ after_save { has_many_purgable_purge }
36
+ end
37
+
38
+ end
39
+
40
+ module ClassMethods
41
+ def has_many_purgable?; true; end
42
+ end
43
+
44
+ # All the possible names, merging the actual associations and the given options
45
+ def has_many_purgable_names
46
+ names = has_many_purgable_options.fetch(:names)
47
+
48
+ associations = self.class.reflect_on_all_associations
49
+ .select { |ass| ass.class_name == 'ActiveStorage::Attachment' }
50
+ .map { |ass| ass.name.to_s.chomp('_attachments').chomp('_attachment').to_sym }
51
+
52
+ names == :all ? associations : (names & associations)
53
+ end
54
+
55
+ private
56
+
57
+ # As submitted by the form and permitted by our associations and options
58
+ def has_many_purgable_attachments
59
+ submitted = (Array(_purge_attached) - [nil, '', '0', ' ', 'false', 'f', 'off']).map(&:to_sym)
60
+ submitted & has_many_purgable_names
61
+ end
62
+
63
+ def has_many_purgable_mark_for_destruction
64
+ has_many_purgable_attachments.each do |name|
65
+ Array(public_send(name)).each { |attachment| attachment.mark_for_destruction }
66
+ end
67
+
68
+ true
69
+ end
70
+
71
+ def has_many_purgable_purge
72
+ has_many_purgable_attachments.each do |name|
73
+ Rails.logger.info "[has_many_purgable] Purging #{name} attachments"
74
+ Array(public_send(name)).each { |attachment| attachment.purge }
75
+ end
76
+
77
+ true
78
+ end
79
+
80
+ 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)
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '2.2.8'.freeze
2
+ VERSION = '2.2.9'.freeze
3
3
  end
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.8
4
+ version: 2.2.9
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-23 00:00:00.000000000 Z
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