effective_resources 2.2.7 → 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: 6819b26bada9a724fa3389143b46e3d39988040cc58430440679672d8a437114
4
- data.tar.gz: 604f9712d29693fd48ea06d357ac7f84a0ff07b717316f6378b3822db5fbdc87
3
+ metadata.gz: cb2b489df65b7ef3e426fa8e43fe5e142560f861c7c1f7a2d580b3ce864d74d4
4
+ data.tar.gz: 7776fbc00d3ad40f8c8ac6ccff80ff2e114c48b52f47d59ea054ae4bddfaf15d
5
5
  SHA512:
6
- metadata.gz: 756414efa78b9f5517699e5cb61de0ea62de0f0180efadb24ee7ffb065985441e17f389de7b2452346946024da12f1131cd3fee3766d633a6b2956ba705f9bb1
7
- data.tar.gz: a5d1dd0200866bfd0b7b1970d599203a72b5c9f3d08f57931e7679d8b0eb61f3f769921d3c7c634a9d250c73210f544468a9845aba368e60e0ad00f0be03cbce
6
+ metadata.gz: 1b3d35a468f47af10084426d26d461d5168a6637bfe973b0ba5182bf65cbd1a4804255f956d28a558bc227b4ed986d8ad47eaf39385639db18b9e27afa6a1a24
7
+ data.tar.gz: e9bfd478683a588e25f4a24986d9f695485e4d59451af70415c83852a7fe0f48ec7b7211b000b0541e19e931154df8f2ccf2362e6c785c1fba072dd42120fed7
@@ -49,7 +49,7 @@ module Effective
49
49
 
50
50
  existing = resource_scope.in_progress.order(:id).where.not(id: resource).first
51
51
  return unless existing.present?
52
- return if (existing.id > resource.id) # Otherwise we get an infinite loop
52
+ return if resource.persisted? && (existing.id > resource.id) # Otherwise we get an infinite loop
53
53
 
54
54
  flash[:success] = "You have been redirected to your in-progress wizard"
55
55
  redirect_to resource_wizard_path(existing, existing.next_step)
@@ -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.7'.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.7
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-20 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