administrate-field-active_storage 1.0.2 → 1.0.4

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: 679096f1bc559346a74c8a88c6ae7df7a540f0660b8ccf729332047f4908b2b7
4
- data.tar.gz: 1de086adcc1ef686c0e28b8d012e5291a56928b2c35503a9915be4720b71391c
3
+ metadata.gz: 8c328387cb1fe2e92dfc18952fc3b91d88a711afa5f962adf9899f58cda1aa8a
4
+ data.tar.gz: 12bb047a9cf65ea4540e19737c0d9907f8e9f042a75548fc79de0871cbad9232
5
5
  SHA512:
6
- metadata.gz: 20b177e400de627190c436537ee31c05cd5bc7407c1c37913f9eec62c14e92bd4393c86bd0d8eec5ab5fb6afec98d237c92b1a1f4a2f9974bde1e7607dfa0129
7
- data.tar.gz: ee7c23703ce310ee931f46dfb3df279296bef5614a0ca99507d275d28fe6b547339f54ff6c262318461dc4096da0e06cb390826749de7b5230fb7096f0c74418
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.1)
4
+ administrate-field-active_storage (1.0.3)
5
5
  administrate (>= 0.2.2)
6
6
  rails (>= 7.0)
7
7
 
@@ -121,23 +121,25 @@ GEM
121
121
  net-imap
122
122
  net-pop
123
123
  net-smtp
124
- marcel (1.0.2)
124
+ marcel (1.0.4)
125
125
  method_source (1.0.0)
126
126
  mini_mime (1.1.5)
127
- mini_portile2 (2.8.5)
128
127
  minitest (5.15.0)
129
- net-imap (0.4.10)
128
+ net-imap (0.4.11)
130
129
  date
131
130
  net-protocol
132
131
  net-pop (0.1.2)
133
132
  net-protocol
134
133
  net-protocol (0.2.2)
135
134
  timeout
136
- net-smtp (0.4.0.1)
135
+ net-smtp (0.5.0)
137
136
  net-protocol
138
- nio4r (2.7.0)
139
- nokogiri (1.13.3)
140
- mini_portile2 (~> 2.8.0)
137
+ nio4r (2.7.3)
138
+ nokogiri (1.13.3-arm64-darwin)
139
+ racc (~> 1.4)
140
+ nokogiri (1.13.3-x86_64-darwin)
141
+ racc (~> 1.4)
142
+ nokogiri (1.13.3-x86_64-linux)
141
143
  racc (~> 1.4)
142
144
  parser (3.0.3.2)
143
145
  ast (~> 2.4.1)
@@ -192,7 +194,9 @@ GEM
192
194
  actionpack (>= 5.2)
193
195
  activesupport (>= 5.2)
194
196
  sprockets (>= 3.0.0)
195
- sqlite3 (1.4.2)
197
+ sqlite3 (1.7.3-arm64-darwin)
198
+ sqlite3 (1.7.3-x86_64-darwin)
199
+ sqlite3 (1.7.3-x86_64-linux)
196
200
  terminal-table (3.0.2)
197
201
  unicode-display_width (>= 1.1.1, < 3)
198
202
  thor (1.1.0)
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.2"
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>
@@ -16,27 +16,14 @@ By default, the attribute is rendered as an image tag.
16
16
  %>
17
17
 
18
18
  <% if field.attached? %>
19
- <% if field.index_display_preview? %>
20
- <% if field.many? %>
21
- <%= render partial: 'fields/active_storage/items',
22
- locals: {
23
- field: field,
24
- variant: field.index_preview_variant,
25
- size: field.index_preview_size,
26
- preview_only: field.index_preview_only?
27
- } %>
28
- <% else %>
29
- <%= render partial: 'fields/active_storage/item',
30
- locals: {
31
- field: field,
32
- attachment: field.data,
33
- variant: field.index_preview_variant,
34
- size: field.index_preview_size,
35
- preview_only: field.index_preview_only?
36
- } %>
37
- <% end %>
38
- <% end %>
39
-
19
+ <%= render partial: "fields/active_storage/items",
20
+ locals: {
21
+ field: field,
22
+ variant: field.index_preview_variant,
23
+ size: field.index_preview_size,
24
+ display_preview: field.index_display_preview?,
25
+ preview_only: field.index_preview_only?
26
+ } %>
40
27
  <% if field.index_display_count? %>
41
28
  <div class="attachments-count">
42
29
  <%= pluralize(field.attachments.count,
@@ -22,8 +22,9 @@ controlled via a boolean local variable.
22
22
  - `preview_only`:
23
23
  If true, show only the previous and not the name or destroy link
24
24
  %>
25
- <% preview_only = local_assigns.fetch(:preview_only, false) %>
26
- <% if field.show_display_preview? && attachment.persisted? %>
25
+ <% preview_only = local_assigns.fetch(:preview_only, false)
26
+ display_preview = local_assigns.fetch(:display_preview, field.show_display_preview?) %>
27
+ <% if display_preview && attachment.persisted? %>
27
28
  <div>
28
29
  <%= render partial: 'fields/active_storage/preview', locals: local_assigns %>
29
30
  </div>
@@ -17,6 +17,7 @@ This partial renders one or more attachments
17
17
  <%
18
18
  variant = local_assigns.fetch(:variant, field.show_preview_variant)
19
19
  size = local_assigns.fetch(:size, field.show_preview_size)
20
+ display_preview = local_assigns.fetch(:display_preview, field.show_display_preview?)
20
21
  preview_only = local_assigns.fetch(:preview_only, false)
21
22
  %>
22
23
 
@@ -28,6 +29,7 @@ This partial renders one or more attachments
28
29
  attachment: attachment,
29
30
  variant: variant,
30
31
  size: size,
32
+ display_preview: display_preview,
31
33
  preview_only: preview_only
32
34
  } %>
33
35
  </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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- administrate-field-active_storage (0.4.1)
4
+ administrate-field-active_storage (1.0.2)
5
5
  administrate (>= 0.2.2)
6
6
  rails (>= 7.0)
7
7
 
@@ -84,18 +84,16 @@ GEM
84
84
  tzinfo (~> 2.0)
85
85
  addressable (2.8.0)
86
86
  public_suffix (>= 2.0.2, < 5.0)
87
- administrate (0.16.0)
88
- actionpack (>= 5.0)
89
- actionview (>= 5.0)
90
- activerecord (>= 5.0)
91
- datetime_picker_rails (~> 0.0.7)
92
- jquery-rails (>= 4.0)
93
- kaminari (>= 1.0)
94
- momentjs-rails (~> 2.8)
87
+ administrate (0.20.1)
88
+ actionpack (>= 6.0, < 8.0)
89
+ actionview (>= 6.0, < 8.0)
90
+ activerecord (>= 6.0, < 8.0)
91
+ jquery-rails (~> 4.6.0)
92
+ kaminari (~> 1.2.2)
95
93
  sassc-rails (~> 2.1)
96
94
  selectize-rails (~> 0.6)
97
95
  base64 (0.2.0)
98
- bigdecimal (3.1.4)
96
+ bigdecimal (3.1.8)
99
97
  bindex (0.8.1)
100
98
  builder (3.2.4)
101
99
  byebug (11.1.3)
@@ -109,28 +107,25 @@ GEM
109
107
  regexp_parser (>= 1.5, < 3.0)
110
108
  xpath (~> 3.2)
111
109
  childprocess (4.1.0)
112
- concurrent-ruby (1.2.2)
110
+ concurrent-ruby (1.2.3)
113
111
  connection_pool (2.4.1)
114
112
  crass (1.0.6)
115
113
  date (3.3.4)
116
- datetime_picker_rails (0.0.7)
117
- momentjs-rails (>= 2.8.1)
118
- drb (2.2.0)
119
- ruby2_keywords
114
+ drb (2.2.1)
120
115
  erubi (1.12.0)
121
- ffi (1.15.4)
116
+ ffi (1.16.3)
122
117
  globalid (1.2.1)
123
118
  activesupport (>= 6.1)
124
- i18n (1.14.1)
119
+ i18n (1.14.5)
125
120
  concurrent-ruby (~> 1.0)
126
121
  image_processing (1.12.2)
127
122
  mini_magick (>= 4.9.5, < 5)
128
123
  ruby-vips (>= 2.0.17, < 3)
129
- io-console (0.6.0)
130
- irb (1.9.0)
131
- rdoc
132
- reline (>= 0.3.8)
133
- jquery-rails (4.4.0)
124
+ io-console (0.7.2)
125
+ irb (1.13.1)
126
+ rdoc (>= 4.0.0)
127
+ reline (>= 0.4.2)
128
+ jquery-rails (4.6.0)
134
129
  rails-dom-testing (>= 1, < 3)
135
130
  railties (>= 4.2.0)
136
131
  thor (>= 0.14, < 2.0)
@@ -158,9 +153,7 @@ GEM
158
153
  matrix (0.4.2)
159
154
  mini_magick (4.12.0)
160
155
  mini_mime (1.1.5)
161
- minitest (5.20.0)
162
- momentjs-rails (2.29.1)
163
- railties (>= 3.1)
156
+ minitest (5.23.0)
164
157
  mutex_m (0.2.0)
165
158
  net-imap (0.4.5)
166
159
  date
@@ -172,28 +165,28 @@ GEM
172
165
  net-smtp (0.4.0)
173
166
  net-protocol
174
167
  nio4r (2.6.0)
175
- nokogiri (1.15.5-arm64-darwin)
168
+ nokogiri (1.16.5-arm64-darwin)
176
169
  racc (~> 1.4)
177
- nokogiri (1.15.5-x86_64-darwin)
170
+ nokogiri (1.16.5-x86_64-darwin)
178
171
  racc (~> 1.4)
179
- nokogiri (1.15.5-x86_64-linux)
172
+ nokogiri (1.16.5-x86_64-linux)
180
173
  racc (~> 1.4)
181
- psych (5.1.1.1)
174
+ psych (5.1.2)
182
175
  stringio
183
176
  public_suffix (4.0.6)
184
177
  puma (5.6.4)
185
178
  nio4r (~> 2.0)
186
179
  racc (1.7.3)
187
- rack (2.2.8)
180
+ rack (3.0.11)
188
181
  rack-mini-profiler (2.3.3)
189
182
  rack (>= 1.2.0)
190
- rack-session (1.0.1)
191
- rack (< 3)
183
+ rack-session (2.0.0)
184
+ rack (>= 3.0.0)
192
185
  rack-test (2.1.0)
193
186
  rack (>= 1.3)
194
- rackup (1.0.0)
195
- rack (< 3)
196
- webrick
187
+ rackup (2.1.0)
188
+ rack (>= 3)
189
+ webrick (~> 1.8)
197
190
  rails (7.1.2)
198
191
  actioncable (= 7.1.2)
199
192
  actionmailbox (= 7.1.2)
@@ -223,16 +216,15 @@ GEM
223
216
  rake (>= 12.2)
224
217
  thor (~> 1.0, >= 1.2.2)
225
218
  zeitwerk (~> 2.6)
226
- rake (13.1.0)
227
- rdoc (6.6.0)
219
+ rake (13.2.1)
220
+ rdoc (6.6.3.1)
228
221
  psych (>= 4.0.0)
229
222
  regexp_parser (2.2.0)
230
- reline (0.4.0)
223
+ reline (0.5.7)
231
224
  io-console (~> 0.5)
232
225
  rexml (3.2.5)
233
226
  ruby-vips (2.2.0)
234
227
  ffi (~> 1.12)
235
- ruby2_keywords (0.0.5)
236
228
  rubyzip (2.3.2)
237
229
  sass-rails (6.0.0)
238
230
  sassc-rails (~> 2.1, >= 2.1.1)
@@ -249,17 +241,19 @@ GEM
249
241
  childprocess (>= 0.5, < 5.0)
250
242
  rexml (~> 3.2, >= 3.2.5)
251
243
  rubyzip (>= 1.2.2)
252
- sprockets (4.0.2)
244
+ sprockets (4.2.1)
253
245
  concurrent-ruby (~> 1.0)
254
- rack (> 1, < 3)
246
+ rack (>= 2.2.4, < 4)
255
247
  sprockets-rails (3.4.2)
256
248
  actionpack (>= 5.2)
257
249
  activesupport (>= 5.2)
258
250
  sprockets (>= 3.0.0)
259
- sqlite3 (1.4.2)
260
- stringio (3.0.9)
261
- thor (1.3.0)
262
- tilt (2.0.10)
251
+ sqlite3 (1.7.3-arm64-darwin)
252
+ sqlite3 (1.7.3-x86_64-darwin)
253
+ sqlite3 (1.7.3-x86_64-linux)
254
+ stringio (3.1.0)
255
+ thor (1.3.1)
256
+ tilt (2.3.0)
263
257
  timeout (0.4.1)
264
258
  tzinfo (2.0.6)
265
259
  concurrent-ruby (~> 1.0)
@@ -274,7 +268,7 @@ GEM
274
268
  websocket-extensions (0.1.5)
275
269
  xpath (3.2.0)
276
270
  nokogiri (~> 1.8)
277
- zeitwerk (2.6.12)
271
+ zeitwerk (2.6.14)
278
272
 
279
273
  PLATFORMS
280
274
  arm64-darwin-23
@@ -42,5 +42,11 @@ module Admin
42
42
 
43
43
  # See https://administrate-prototype.herokuapp.com/customizing_controller_actions
44
44
  # for more information
45
+
46
+ def destroy_other_image
47
+ other_image = requested_resource.other_images.find(params[:attachment_id])
48
+ other_image.purge
49
+ redirect_back(fallback_location: requested_resource)
50
+ end
45
51
  end
46
52
  end
@@ -10,6 +10,11 @@ class PostDashboard < Administrate::BaseDashboard
10
10
  ATTRIBUTE_TYPES = {
11
11
  id: Field::Number,
12
12
  cover_image: Field::ActiveStorage,
13
+ other_images: Field::ActiveStorage.with_options(
14
+ destroy_url: proc do |namespace, resource, attachment|
15
+ [:other_image_admin_post, { id: resource.id, attachment_id: attachment.id }]
16
+ end
17
+ ),
13
18
  title: Field::String,
14
19
  created_at: Field::DateTime,
15
20
  updated_at: Field::DateTime,
@@ -24,6 +29,7 @@ class PostDashboard < Administrate::BaseDashboard
24
29
  id
25
30
  title
26
31
  cover_image
32
+ other_images
27
33
  created_at
28
34
  ].freeze
29
35
 
@@ -33,6 +39,7 @@ class PostDashboard < Administrate::BaseDashboard
33
39
  id
34
40
  title
35
41
  cover_image
42
+ other_images
36
43
  created_at
37
44
  updated_at
38
45
  ].freeze
@@ -43,6 +50,7 @@ class PostDashboard < Administrate::BaseDashboard
43
50
  FORM_ATTRIBUTES = %i[
44
51
  title
45
52
  cover_image
53
+ other_images
46
54
  ].freeze
47
55
 
48
56
  # COLLECTION_FILTERS
@@ -63,4 +71,8 @@ class PostDashboard < Administrate::BaseDashboard
63
71
  # def display_resource(post)
64
72
  # "Post ##{post.id}"
65
73
  # end
74
+
75
+ def permitted_attributes(action = nil)
76
+ super + [:other_images => []]
77
+ end
66
78
  end
@@ -1,5 +1,6 @@
1
1
  class Post < ApplicationRecord
2
2
  has_one_attached :cover_image
3
+ has_many_attached :other_images
3
4
 
4
5
  validates :title, presence: true
5
6
  end
@@ -2,7 +2,9 @@ Rails.application.routes.draw do
2
2
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
3
3
 
4
4
  namespace :admin do
5
- resources :posts
5
+ resources :posts do
6
+ delete :other_image, on: :member, action: :destroy_other_image
7
+ end
6
8
 
7
9
  root to: "posts#index"
8
10
  end
@@ -10,7 +10,7 @@
10
10
  #
11
11
  # It's strongly recommended that you check this file into your version control system.
12
12
 
13
- ActiveRecord::Schema.define(version: 2023_11_17_224946) do
13
+ ActiveRecord::Schema[7.1].define(version: 2023_11_17_224946) do
14
14
  create_table "active_storage_attachments", force: :cascade do |t|
15
15
  t.string "name", null: false
16
16
  t.string "record_type", null: false
@@ -40,9 +40,9 @@ ActiveRecord::Schema.define(version: 2023_11_17_224946) do
40
40
  end
41
41
 
42
42
  create_table "posts", force: :cascade do |t|
43
- t.string "title"
44
43
  t.datetime "created_at", null: false
45
44
  t.datetime "updated_at", null: false
45
+ t.string "title"
46
46
  end
47
47
 
48
48
  add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
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.2
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-04-30 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