administrate-field-active_storage 1.0.3 → 1.0.4

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: 12fb995471a84f218352f4c630c6de6088c1eec7b975671f42125a1f91fe05d5
4
- data.tar.gz: 797c67a7742599097b761314a457f1f53f3153aeb5c6aa72c4556cfd0a86d449
3
+ metadata.gz: 8c328387cb1fe2e92dfc18952fc3b91d88a711afa5f962adf9899f58cda1aa8a
4
+ data.tar.gz: 12bb047a9cf65ea4540e19737c0d9907f8e9f042a75548fc79de0871cbad9232
5
5
  SHA512:
6
- metadata.gz: 3d285fd017385d5ce56a18a2ef464b5ec5d981fa065fe8a59706db2b7716676d81fbff44909a4b1543cfb70821c2368057184d0095bf6aaaafcf3e9139ad1b8e
7
- data.tar.gz: 6ccbf0a5119607bda9110ace878002ba9a8fd40ae39573c112c96f14cc69c0fa54855ddf28bc5ce7e437776532a85f2d3f5c44cea8373b4a695176cd78000493
6
+ metadata.gz: 05e3e458a12c3abac03e1869cffbe00adafc015adef2e07bc7606adfd5afdf1e592efdceb399c92d42167f14e20164fab2b35098cb719253ccf0bb1b04ca125b
7
+ data.tar.gz: 5f16cda52260f09e57c7974de9671985501bf0457fce31d0589d9941adebcd6444ed56b201a52be6d4ed78de47754396157d8e9d0d209718925b0222dbb21827
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- administrate-field-active_storage (1.0.2)
4
+ administrate-field-active_storage (1.0.3)
5
5
  administrate (>= 0.2.2)
6
6
  rails (>= 7.0)
7
7
 
data/README.md CHANGED
@@ -54,7 +54,7 @@ class ModelDashboard < Administrate::BaseDashboard
54
54
  }
55
55
 
56
56
  # permitted for has_many_attached
57
- def permitted_attributes
57
+ def permitted_attributes(_action = nil)
58
58
  super + [:attachments => []]
59
59
  end
60
60
  ```
@@ -63,6 +63,43 @@ I know it is not ideal, if you have a workaround please submit a PR.
63
63
  Note: Rails 6 introduced a new config to determine the behavior on updates to `has_many_attached`. Setting `Rails.application.config.active_storage.replace_on_assign_to_many` to `true` will overwrite any existing values (purging the old ones), and setting it to `false` will append the new values. Please note that this configuation was [deprecated with Rails 7.1](https://github.com/rails/rails/blob/v7.0.2.3/activestorage/lib/active_storage/attached/model.rb#L150)
64
64
  >config.active_storage.replace_on_assign_to_many is deprecated and will be removed in Rails 7.1. Make sure that your code works well with config.active_storage.replace_on_assign_to_many set to true before upgrading. To append new attachables to the Active Storage association, prefer using attach. Using association setter would result in purging the existing attached attachments and replacing them with new ones.
65
65
 
66
+ This means that in Rails 7 for `has_many_attached`, whenever a form is submitted, all associations to existing attachments are being removed without the ActiveStorage objects being deleted. This is undesired behaviour because you have to re attach every time you update an object and it will result in orphaned ActiveStorage objects. To fix this and to add the ability to add more attachments to an existing set of attachments, follow this [workaround](https://stackoverflow.com/a/74207496). In short, you can create a concern:
67
+
68
+ ```ruby
69
+ # app/models/concerns/append_to_has_many_attached.rb
70
+ module AppendToHasManyAttached
71
+ def self.[](fields)
72
+ Module.new do
73
+ extend ActiveSupport::Concern
74
+
75
+ fields = Array(fields).compact_blank # will always return an array ( worst case is an empty array)
76
+
77
+ fields.each do |field|
78
+ field = field.to_s # We need the string version
79
+ define_method :"#{field}=" do |attachables|
80
+ attachables = Array(attachables).compact_blank
81
+
82
+ if attachables.any?
83
+ attachment_changes[field] =
84
+ ActiveStorage::Attached::Changes::CreateMany.new(field, self, public_send(field).public_send(:blobs) + attachables)
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ ```
92
+
93
+ And then load this concern in your model:
94
+ ```ruby
95
+ class Model < ApplicationModel
96
+ include AppendToHasManyAttached['files'] # you can include it before or after, order does not matter, explanation below
97
+
98
+ has_many_attached :files
99
+ end
100
+ ```
101
+
102
+ Please note, it does not matter if you prepend or include the module, see [original post](https://stackoverflow.com/a/74207496) for details.
66
103
 
67
104
  ### Prevent N+1 queries
68
105
  In order to prevent N+1 queries from active storage you have to modify your admin model controller, below an example for a model called `User` and with attached avatars
@@ -217,11 +254,17 @@ Displays the number of attachments in the `index` action.
217
254
 
218
255
  Defaults to `true` if number of attachments is not 1.
219
256
 
220
- ### direct_upload
257
+ ### file_field_options
258
+
259
+ Configure any options to give to `file_field`.
260
+
261
+ `multiple` is set by default based on `::ActiveStorage::Attached::Many`, but can be overridden.
262
+
263
+ #### direct
221
264
 
222
265
  Enables direct upload from the browser to the cloud.
223
266
 
224
- Defaults to `false`.
267
+ Defaults to `false`. Configure with `file_field_options: { direct: true }`.
225
268
 
226
269
  Don't forget to include [ActiveStorage JavaScript](https://edgeguides.rubyonrails.org/active_storage_overview.html#direct-uploads). You can use `rails generate administrate:assets:javascripts` to be able to customize Administrate JavaScripts in your application.
227
270
 
@@ -2,7 +2,7 @@ $:.push File.expand_path("../lib", __FILE__)
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = "administrate-field-active_storage"
5
- gem.version = "1.0.3"
5
+ gem.version = "1.0.4"
6
6
  gem.authors = ["Hamad AlGhanim"]
7
7
  gem.email = ["hamadyalghanim@gmail.com"]
8
8
  gem.homepage = "https://github.com/Dreamersoul/administrate-field-active_storage"
@@ -29,6 +29,6 @@ By default, the input is a text field for the image's URL.
29
29
  I18n.t("administrate.fields.active_storage.add", default: 'Add') :
30
30
  I18n.t("administrate.fields.active_storage.replace", default: 'Replace')
31
31
  %>
32
- <%= f.file_field field.attribute, multiple: field.many?, direct_upload: field.direct? %>
32
+ <%= f.file_field field.attribute, field.file_field_options %>
33
33
  </div>
34
34
  </div>
@@ -40,6 +40,13 @@ module Administrate
40
40
  options.fetch(:show_preview_variant, nil)
41
41
  end
42
42
 
43
+ def file_field_options
44
+ {
45
+ direct_upload: field.direct?,
46
+ multiple: field.many?,
47
+ }.merge options.fetch(:file_field_options, {})
48
+ end
49
+
43
50
  def many?
44
51
  data.is_a? ::ActiveStorage::Attached::Many
45
52
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: administrate-field-active_storage
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hamad AlGhanim
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-17 00:00:00.000000000 Z
11
+ date: 2025-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: administrate