administrate-field-active_storage 0.3.0 → 0.3.5

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: fc5923a984b8ef1046ab8657f3de54cbd68c5c50bc90b9f0aba62d36ed0c0a1b
4
- data.tar.gz: d383f74c54bfd179912f66d612d4ad19c0b4f4da5baf1facd14c1df10d2c2c9f
3
+ metadata.gz: eeae1cb2ea6a2ed7534c917f87c06d73ff22abbb03e299af2253f65abc7f1490
4
+ data.tar.gz: e1b8b00ec8cf58cf084fd916e0afee627110ea991d1fe31a27b617ef2a8919df
5
5
  SHA512:
6
- metadata.gz: a7686831282ed9ffaf94c01cdab6e3f029a7ba93736896982d8255db70bc28f93402f2a9a0f17b820b17258eadbf2f9a294e2e07cc8fd5164dd5d979eb6ad9cb
7
- data.tar.gz: e479ad715183dd81c20b58aec5796f9a06949537729a25e320ea8f0a83b9a056d05cc36b7186da561468fce878284e33dcb8314af33cc38149a18ce34dd22bcd
6
+ metadata.gz: aeec55b49785338dfecebeb33cce2a7da199f3fe0e9c5cb56c9684a069bb0cbd555818d26384c10160a46a4dd63473f759ea255a8a7f6e88d9a8590e1bb3a393
7
+ data.tar.gz: c8950a969fb5503ab53a7cda2a59badd286f0ac4d1166ff0d7e8b42d4b279ad1c0c65ee97ecc28cdaf5bde1cad52d087d10bd4aab5a811fe20b3d7bcf04808d8
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
  /tmp/
10
10
  *.gem
11
11
  .DS_Store
12
+ .vscode/settings.json
@@ -1 +1 @@
1
- 2.6.3
1
+ 2.6.5
data/README.md CHANGED
@@ -11,7 +11,7 @@ Add `administrate-field-active_storage` and `mini_magick` to your Gemfile (rails
11
11
 
12
12
  ```ruby
13
13
  gem 'administrate-field-active_storage'
14
- gem 'mini_magick'
14
+ gem "image_processing"
15
15
  ```
16
16
 
17
17
  for rails 5.x use the following
@@ -63,36 +63,92 @@ I know it is not ideal, if you have a workaround please submit a PR.
63
63
  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
64
64
  ```ruby
65
65
  module Admin
66
- class UsersController < Admin::ApplicationController
66
+ class UsersController < ApplicationController
67
67
  def scoped_resource
68
68
  resource_class.with_attached_avatars
69
69
  end
70
70
  end
71
71
  end
72
-
73
72
  ```
74
73
 
75
74
  ### Removing/Deleting an Attachment
76
- In order to allow the user to delete an attachment using the admin dashboard you need to do the following:
77
- 1. create a controller action with a `delete` route
78
- 2. point the `Field::ActiveStorage` field to that route
79
75
 
80
- here is an example (send the route name as a symbol):
81
- ```ruby
82
- class ModelDashboard < Administrate::BaseDashboard
83
- ATTRIBUTE_TYPES = {
84
- attachment: Field::ActiveStorage.with_options({destroy_path: :custom_active_storage_destroy_path}),
85
- }
86
- # ...
76
+ `Administrate::Field::ActiveStorage` expects the presence of a route
77
+ DELETE `/<namespace>/<resource>/:id/:attachment_name`, which will receive an optional parameter
78
+ `attachment_id` in the case of an `ActiveStorage::Attached::Many`. For instance:
79
+
80
+ ```rb
81
+ # routes.rb
82
+ ...
83
+ namespace :admin do
84
+ ...
85
+ resources :users do
86
+ delete :avatars, on: :member, action: :destroy_avatar
87
+ end
88
+ end
89
+
90
+ # app/controllers/admin/users_controller.rb
91
+ module Admin
92
+ class UsersController < ApplicationController
93
+
94
+ # For illustrative purposes only.
95
+ #
96
+ # **SECURITY NOTICE**: first verify whether current user is authorized to perform the action.
97
+ def destroy_avatar
98
+ avatar = requested_resource.avatars.find(params[:attachment_id])
99
+ avatar.purge
100
+ redirect_back(fallback_location: requested_resource)
101
+ end
102
+ end
103
+ end
87
104
  ```
88
- Your `routes.rb` file must point to a controller action with method `delete` which should contain the following piece of code (you can modify to your own liking).
89
- **FOR SECURITY REASONS** please check if the current user is allowed to remove such file
90
- ```ruby
91
- def remove_attachment
92
- attachment = ActiveStorage::Attachment.find(params[:attachment_id])
93
- attachment.purge
94
- redirect_back(fallback_location: "/")
105
+ For `has_one_attached` cases, you will use:
106
+
107
+ ```rb
108
+ # routes.rb
109
+ ...
110
+ namespace :admin do
111
+ ...
112
+ resources :users do
113
+ delete :avatar, on: :member, action: :destroy_avatar
114
+ end
115
+ end
116
+
117
+ # app/controllers/admin/users_controller.rb
118
+ module Admin
119
+ class UsersController < ApplicationController
120
+
121
+ # For illustrative purposes only.
122
+ #
123
+ # **SECURITY NOTICE**: first verify whether current user is authorized to perform the action.
124
+ def destroy_avatar
125
+ avatar = requested_resource.avatar
126
+ avatar.purge
127
+ redirect_back(fallback_location: requested_resource)
128
+ end
129
+ end
130
+ end
131
+ ```
132
+ This route can be customized with `destroy_url`. The option expects a `proc` receiving 3 arguments:
133
+ the Administrate `namespace`, the `resource`, and the `attachment`. The proc can return anything
134
+ accepted by `link_to`:
135
+
136
+ ```rb
137
+ # routes.rb
138
+ delete :custom_user_avatar_destroy, to: 'users#destroy_avatar'
139
+
140
+ # user_dashboard.rb
141
+ class UserDashboard < Administrate::BaseDashboard
142
+ ATTRIBUTE_TYPES = {
143
+ avatars: Field::ActiveStorage.with_options(
144
+ destroy_url: proc do |namespace, resource, attachment|
145
+ [:custom_user_avatar_destroy, { attachment_id: attachment.id }]
146
+ end
147
+ ),
148
+ # ...
95
149
  end
150
+ # ...
151
+ end
96
152
  ```
97
153
 
98
154
  ## Options
@@ -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 = "0.3.0"
5
+ gem.version = "0.3.5"
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"
@@ -21,7 +21,7 @@ By default, the input is a text field for the image's URL.
21
21
 
22
22
  <div class="field-unit__field">
23
23
  <% if field.attached? %>
24
- <%= render partial: 'fields/active_storage/items', locals: { field: field, removable: field.destroyable? } %>
24
+ <%= render partial: 'fields/active_storage/items', locals: { field: field } %>
25
25
  <% end %>
26
26
 
27
27
  <div>
@@ -25,7 +25,7 @@ By default, the attribute is rendered as an image tag.
25
25
  <%= render partial: 'fields/active_storage/preview',
26
26
  locals: {
27
27
  field: field,
28
- attachment: field.attachments[0],
28
+ attachment: field.data,
29
29
  size: field.index_preview_size
30
30
  } %>
31
31
  <% end %>
@@ -23,21 +23,20 @@ controlled via a boolean local variable.
23
23
  [x, y]
24
24
  Maximum size of the ActiveStorage preview.
25
25
  %>
26
-
27
- <%
28
- # By default we don't allow attachment removal
29
- removable = local_assigns.fetch(:removable, false)
30
- %>
31
-
32
- <div>
33
- <%= render partial: 'fields/active_storage/preview', locals: local_assigns %>
34
- </div>
26
+ <% if field.show_display_preview? %>
27
+ <div>
28
+ <%= render partial: 'fields/active_storage/preview', locals: local_assigns %>
29
+ </div>
30
+ <% end %>
35
31
 
36
32
  <div>
37
- <%= link_to 'Download', field.blob_url(attachment), title: attachment.filename %>
33
+ <%= link_to attachment.filename, field.blob_url(attachment), title: attachment.filename %>
38
34
  </div>
39
35
 
40
- <% if removable %>
41
- <%= link_to 'Remove', field.destroy_path(field, attachment), method: :delete, class: 'remove-attachment-link' %>
36
+ <% if field.destroy_url.present? %>
37
+ <% destroy_url = field.destroy_url.call(namespace, field.data.record, attachment) %>
38
+ <div>
39
+ <%= link_to 'Remove', destroy_url, method: :delete, class: 'remove-attachment-link', data: { confirm: t("administrate.actions.confirm") } %>
40
+ </div>
42
41
  <hr>
43
42
  <% end %>
@@ -13,7 +13,7 @@
13
13
  autobuffer: true,
14
14
  style: "object-fit: contain; width: 100%; height: 100%;") %>
15
15
  <% else %>
16
- <%= video_tag(field.url(attachment), controls: true, autobuffer: true, style: style) %>
16
+ <%= video_tag(field.url(attachment), controls: true, autobuffer: true, style: "object-fit: contain; width: 100%; height: 100%;") %>
17
17
  <% end %>
18
18
  <% elsif attachment.audio? %>
19
19
  <%= audio_tag(field.url(attachment), autoplay: false, controls: true) %>
@@ -17,5 +17,11 @@ module Admin
17
17
 
18
18
  # See https://administrate-prototype.herokuapp.com/customizing_controller_actions
19
19
  # for more information
20
+
21
+ def destroy_avatar
22
+ avatar = requested_resource.avatars.find(params[:attachment_id])
23
+ avatar.purge
24
+ redirect_back(fallback_location: requested_resource)
25
+ end
20
26
  end
21
27
  end
@@ -1,7 +1,7 @@
1
1
  class UsersController < ApplicationController
2
- def remove_attachment
3
- attachment = ActiveStorage::Attachment.find(params[:attachment_id])
4
- attachment.purge
5
- redirect_back(fallback_location: "/")
2
+ def destroy_avatar
3
+ avatar = requested_resource.avatars.find(params[:attachment_id])
4
+ avatar.purge
5
+ redirect_back(fallback_location: requested_resource)
6
6
  end
7
7
  end
@@ -10,7 +10,9 @@ class UserDashboard < Administrate::BaseDashboard
10
10
  ATTRIBUTE_TYPES = {
11
11
  id: Field::Number,
12
12
  name: Field::String,
13
- avatars: Field::ActiveStorage.with_options({:destroy_path => :custom_active_record_remove_path, :show_in_index => true}),
13
+ avatars: Field::ActiveStorage.with_options(
14
+ show_in_index: true
15
+ ),
14
16
  created_at: Field::DateTime,
15
17
  updated_at: Field::DateTime
16
18
  }.freeze
@@ -1,9 +1,10 @@
1
1
  Rails.application.routes.draw do
2
2
  namespace :admin do
3
- resources :users
3
+ resources :users do
4
+ delete :avatar, on: :member, action: :destroy_avatar
5
+ end
4
6
 
5
7
  root to: "users#index"
6
8
  end
7
- delete "custom_active_record_remove", to: 'users#remove_avatar'
8
9
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
9
10
  end
@@ -7,10 +7,6 @@ module Administrate
7
7
  class Engine < ::Rails::Engine
8
8
  end
9
9
 
10
- def destroyable?
11
- options.key?(:destroy_path)
12
- end
13
-
14
10
  def index_display_preview?
15
11
  options.fetch(:index_display_preview, true)
16
12
  end
@@ -20,7 +16,7 @@ module Administrate
20
16
  end
21
17
 
22
18
  def index_display_count?
23
- options.fetch(:index_display_count) { attachments.count != 1 }
19
+ options.fetch(:index_display_count) { attached? && attachments.count != 1 }
24
20
  end
25
21
 
26
22
  def show_display_preview?
@@ -32,17 +28,16 @@ module Administrate
32
28
  end
33
29
 
34
30
  def many?
35
- # find a way to use instance_of
36
- data.class.name == 'ActiveStorage::Attached::Many'
31
+ data.is_a? ::ActiveStorage::Attached::Many
37
32
  end
38
33
 
39
34
  def direct?
40
35
  options.fetch(:direct_upload, false)
41
36
  end
42
37
 
43
- # def destroy_path?
44
- # options.fetch(:destroy_path, false).present?
45
- # end
38
+ def destroy_url
39
+ options.fetch(:destroy_url, nil)
40
+ end
46
41
 
47
42
  # currently we are using Rails.application.routes.url_helpers
48
43
  # without including the namespace because it runs into an
@@ -66,22 +61,17 @@ module Administrate
66
61
  Rails.application.routes.url_helpers.rails_blob_path(attachment, disposition: :attachment, only_path: true)
67
62
  end
68
63
 
69
- def destroy_path(field, attachment)
70
- destroy_path_helper = options.fetch(:destroy_path)
71
- record_id = field.data.record.id
72
- attachment_id = attachment.id
73
- Rails.application.routes.url_helpers.send(destroy_path_helper, {:record_id => record_id, :attachment_id => attachment_id})
74
- end
75
-
76
64
  def can_add_attachment?
77
- many? || attachments.empty?
65
+ many? || attachments.blank?
78
66
  end
79
67
 
80
68
  def attached?
81
69
  data.present? && data.attached?
82
70
  end
83
71
 
84
- delegate :attachments, to: :data
72
+ def attachments
73
+ data.attachments if attached?
74
+ end
85
75
  end
86
76
  end
87
77
  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: 0.3.0
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hamad AlGhanim
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-13 00:00:00.000000000 Z
11
+ date: 2020-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: administrate