effective_resources 2.2.8 → 2.2.10

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b2b012e5d5abc1400fabdf14bf5afbbae28bfd8f5963a51274ca39bac1b6a682
4
- data.tar.gz: 37f817c1a4a184a069cf9f2d3b71a4548bd1e55f8496cbb003778c5f10f78e7a
3
+ metadata.gz: 3f5258d38255a23862d15be45d82d15d68596bb42b2f31f88e5159c339e2fae8
4
+ data.tar.gz: c6dba77a6cdb45ec02fead79b8050cee8c482325ec772a53ffde5cea9facc754
5
5
  SHA512:
6
- metadata.gz: bca95eb795e3400ec7beaa81385ca145bc9e0671abe42558db51f7a035c5f45c6051bcc74dc428aca94e7bd12ada62a73a56b223f93d0b83c0c53af3047fb678
7
- data.tar.gz: cd1b657b8b624af16c92edd64e4beb3c73a56121e97f64f44aaf49291efd8f56bc17704fbfe95328a54eb408b97ffd84c29589f71aaf4f9674cfe47bdbf6d4ed
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)
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '2.2.8'.freeze
2
+ VERSION = '2.2.10'.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.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-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