administrate-field-active_storage 1.0.5 → 1.0.6
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 +4 -4
- data/README.md +28 -7
- data/administrate-field-active_storage.gemspec +1 -1
- data/app/views/fields/active_storage/_item.html.erb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5ed83e5ceb2aa2fe19f46b3ee5d944ea23e63c407f909e1af479ce947b5badfc
|
4
|
+
data.tar.gz: 1af7048cc72231e962b07e5e28735e75d62e032dceafc822ea18322f023c9d8a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f8b1f9adb9dcedea95e4a4040d709f1c5c4e3b41c4033fa795e76d79d4b933283754db3036bf5d1834fe7aed8d1acf1be4caa555818f8852a3f95a350638032f
|
7
|
+
data.tar.gz: 44d81ee32eef75648670329620b6dc7c3041b9eeb27030bac261a1abd1569d366945da7206b48496004331c0c0e07bf0ef25a7277a371a05fd8c42acd87e35d5
|
data/README.md
CHANGED
@@ -1,13 +1,16 @@
|
|
1
1
|
# Administrate::Field::ActiveStorage
|
2
|
+
|
2
3
|

|
3
4
|

|
4
5
|
|
5
6
|
## Things To Know:
|
7
|
+
|
6
8
|
- To preview pdf files you need to install `mupdf` or `Poppler`.
|
7
9
|
- To preview video files you need to install `ffmpeg`.
|
8
10
|
- To preview Office files as pictures you need to install [activestorage-office-previewer](https://github.com/basecamp/activestorage-office-previewer) by basecamp
|
9
11
|
|
10
12
|
## How To Use:
|
13
|
+
|
11
14
|
Add `administrate-field-active_storage` and `image_processing` to your Gemfile (Rails 6+):
|
12
15
|
|
13
16
|
```ruby
|
@@ -16,6 +19,7 @@ gem "image_processing"
|
|
16
19
|
```
|
17
20
|
|
18
21
|
for Rails 5.x use the following
|
22
|
+
|
19
23
|
```ruby
|
20
24
|
gem "administrate-field-active_storage", "0.1.8"
|
21
25
|
```
|
@@ -27,7 +31,9 @@ $ bundle install
|
|
27
31
|
```
|
28
32
|
|
29
33
|
### `has_one_attached`:
|
34
|
+
|
30
35
|
Assuming your model name is `Model` and field name is `attachment`
|
36
|
+
|
31
37
|
```ruby
|
32
38
|
class ModelDashboard < Administrate::BaseDashboard
|
33
39
|
ATTRIBUTE_TYPES = {
|
@@ -35,10 +41,12 @@ class ModelDashboard < Administrate::BaseDashboard
|
|
35
41
|
}
|
36
42
|
# ...
|
37
43
|
```
|
44
|
+
|
38
45
|
Then add `:attachment` to `FORM_ATTRIBUTES` and `SHOW_PAGE_ATTRIBUTES`.
|
39
46
|
Adding `:attachment` `COLLECTION_ATTRIBUTES` will work but will probably look too big.
|
40
47
|
|
41
48
|
### `has_many_attached`:
|
49
|
+
|
42
50
|
Assuming your model name is `Model` and field name is `attachments` the process is identical the only issue is that the form field isn't being permitted, in order to permit it we apply the following method to the dashboard:
|
43
51
|
|
44
52
|
```ruby
|
@@ -58,12 +66,14 @@ class ModelDashboard < Administrate::BaseDashboard
|
|
58
66
|
super + [:attachments => []]
|
59
67
|
end
|
60
68
|
```
|
69
|
+
|
61
70
|
I know it is not ideal, if you have a workaround please submit a PR.
|
62
71
|
|
63
|
-
Note: Rails 6 introduced a new config to determine the behavior on updates to `has_many_attached`.
|
64
|
-
|
72
|
+
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)
|
73
|
+
|
74
|
+
> 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
75
|
|
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
|
76
|
+
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 behavior 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
77
|
|
68
78
|
```ruby
|
69
79
|
# app/models/concerns/append_to_has_many_attached.rb
|
@@ -91,6 +101,7 @@ end
|
|
91
101
|
```
|
92
102
|
|
93
103
|
And then load this concern in your model:
|
104
|
+
|
94
105
|
```ruby
|
95
106
|
class Model < ApplicationModel
|
96
107
|
include AppendToHasManyAttached['files'] # you can include it before or after, order does not matter, explanation below
|
@@ -102,7 +113,9 @@ end
|
|
102
113
|
Please note, it does not matter if you prepend or include the module, see [original post](https://stackoverflow.com/a/74207496) for details.
|
103
114
|
|
104
115
|
### Prevent N+1 queries
|
116
|
+
|
105
117
|
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
|
118
|
+
|
106
119
|
```ruby
|
107
120
|
module Admin
|
108
121
|
class UsersController < ApplicationController
|
@@ -144,6 +157,7 @@ module Admin
|
|
144
157
|
end
|
145
158
|
end
|
146
159
|
```
|
160
|
+
|
147
161
|
For `has_one_attached` cases, you will use:
|
148
162
|
|
149
163
|
```rb
|
@@ -171,6 +185,7 @@ module Admin
|
|
171
185
|
end
|
172
186
|
end
|
173
187
|
```
|
188
|
+
|
174
189
|
This route can be customized with `destroy_url`. The option expects a `proc` receiving 3 arguments:
|
175
190
|
the Administrate `namespace`, the `resource`, and the `attachment`. The proc can return anything
|
176
191
|
accepted by `link_to`:
|
@@ -239,9 +254,11 @@ Use a named variant for image preview for the `index` and `show` actions, respec
|
|
239
254
|
Named image variants were [added in Rails 7](https://guides.rubyonrails.org/v7.0/active_storage_overview.html#has-one-attached).
|
240
255
|
|
241
256
|
It might be necessary to add to app/assets/config/manifest.js:
|
257
|
+
|
242
258
|
```rb
|
243
259
|
//= link 'administrate-field-active_storage/application.css'
|
244
260
|
```
|
261
|
+
|
245
262
|
When set, this takes precedence over `index_preview_size` and `show_preview_size`.
|
246
263
|
|
247
264
|
Setting this to `false` displays original images instead of variants.
|
@@ -256,15 +273,16 @@ Defaults to `true` if number of attachments is not 1.
|
|
256
273
|
|
257
274
|
### file_field_options
|
258
275
|
|
259
|
-
|
276
|
+
The `file_field_options` is a configuration option that allows you to customize the behavior and appearance of the file input field used for uploading attachments. You can pass various options to `file_field` to control its functionality.
|
260
277
|
|
261
|
-
`multiple` is set by default based on `::ActiveStorage::Attached::Many`, but can be overridden.
|
278
|
+
The `multiple` HTML attribute for file input fields is set by default based on `::ActiveStorage::Attached::Many`, but can be overridden through this option.
|
279
|
+
The `direct` attribute to enable direct uploads is set to read from the field options, but can be overridden through this option.
|
262
280
|
|
263
281
|
#### direct
|
264
282
|
|
265
283
|
Enables direct upload from the browser to the cloud.
|
266
284
|
|
267
|
-
Defaults to `false`.
|
285
|
+
Defaults to `false`.
|
268
286
|
|
269
287
|
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.
|
270
288
|
|
@@ -273,6 +291,7 @@ Don't forget to include [ActiveStorage JavaScript](https://edgeguides.rubyonrail
|
|
273
291
|
You can see translation example [here](https://github.com/Dreamersoul/administrate-field-active_storage/blob/master/config/locales/administrate-field-active_storage.en.yml).
|
274
292
|
|
275
293
|
## Things To Do:
|
294
|
+
|
276
295
|
- [x] upload single file
|
277
296
|
- [x] adding image support through url_for to support 3rd party cloud storage
|
278
297
|
- [x] use html 5 video element for video files
|
@@ -285,10 +304,12 @@ You can see translation example [here](https://github.com/Dreamersoul/administra
|
|
285
304
|
- [x] preview office files as pictures
|
286
305
|
|
287
306
|
## Contribution Guide:
|
288
|
-
|
307
|
+
|
308
|
+
1. contributors are welcome (code, suggestions, and bugs).
|
289
309
|
2. please test your code: `cd test_app && bundle && bundle exec rails test`.
|
290
310
|
3. please document your code.
|
291
311
|
4. add your name to the `contribute.md`.
|
292
312
|
|
293
313
|
---
|
314
|
+
|
294
315
|
Based on the [Administrate::Field::Image](https://github.com/thoughtbot/administrate-field-image) template, and inspired by [Administrate::Field::Paperclip](https://github.com/picandocodigo/administrate-field-paperclip).
|
@@ -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.
|
5
|
+
gem.version = "1.0.6"
|
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"
|
@@ -41,8 +41,8 @@ controlled via a boolean local variable.
|
|
41
41
|
<% destroy_url = field.destroy_url.call(namespace, field.data.record, attachment) %>
|
42
42
|
<div>
|
43
43
|
<%= link_to I18n.t("administrate.fields.active_storage.remove", default: 'Remove'),
|
44
|
-
destroy_url, method: :delete, class: 'remove-attachment-link', data: { confirm: t("administrate.actions.confirm") } %>
|
44
|
+
destroy_url, method: :delete, class: 'remove-attachment-link', data: { confirm: t("administrate.actions.confirm"), turbo_method: :delete, turbo_confirm: t("administrate.actions.confirm") } %>
|
45
45
|
</div>
|
46
46
|
<hr>
|
47
47
|
<% end %>
|
48
|
-
<% end %>
|
48
|
+
<% 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.
|
4
|
+
version: 1.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hamad AlGhanim
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-
|
11
|
+
date: 2025-05-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: administrate
|