administrate-field-active_storage 0.3.0 → 0.3.1

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: 1ea48021b9c4480f394cc61655e5e0a51c8ee6f9723f97902902642668a9ea88
4
+ data.tar.gz: b9691a2f4b65e00a559dbd5ec52e73b1bac589e98403a6f135fdcd0f09953bff
5
5
  SHA512:
6
- metadata.gz: a7686831282ed9ffaf94c01cdab6e3f029a7ba93736896982d8255db70bc28f93402f2a9a0f17b820b17258eadbf2f9a294e2e07cc8fd5164dd5d979eb6ad9cb
7
- data.tar.gz: e479ad715183dd81c20b58aec5796f9a06949537729a25e320ea8f0a83b9a056d05cc36b7186da561468fce878284e33dcb8314af33cc38149a18ce34dd22bcd
6
+ metadata.gz: c5409c4400e684e023b287144b7415f51a77d6800df33c17eeb6f805de5381900e1d4947413e3d64939e85fd2009c8ff6ebeecbcde864017fc2c99f11c668650
7
+ data.tar.gz: cf96c0b3b886a87fbe4774c2b2e3658d56c64375b34eebcb1d2ff3240dafb9ccf0a96debab40b3ce675b7869b6dd5cc6d0cc114f11368bcea27da35f5e8e3c23
data/README.md CHANGED
@@ -63,38 +63,70 @@ 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
+
106
+ This route can be customized with `destroy_url`. The option expects a `proc` receiving 3 arguments:
107
+ the Administrate `namespace`, the `resource`, and the `attachment`. The proc can return anything
108
+ accepted by `link_to`:
109
+
110
+ ```rb
111
+ # routes.rb
112
+ delete :custom_user_avatar_destroy, to: 'users#destroy_avatar'
113
+
114
+ # user_dashboard.rb
115
+ class UserDashboard < Administrate::BaseDashboard
116
+ ATTRIBUTE_TYPES = {
117
+ avatars: Field::ActiveStorage.with_options(
118
+ destroy_url: proc do |namespace, resource, attachment|
119
+ [:custom_user_avatar_destroy, { attachment_id: attachment.id }]
120
+ end
121
+ ),
122
+ # ...
95
123
  end
124
+ # ...
125
+ end
96
126
  ```
97
127
 
128
+ To disable this feature, set `destroy_url` to `nil`.
129
+
98
130
  ## Options
99
131
 
100
132
  Various options can be passed to `Administrate::Field::ActiveStorage#with_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.1"
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>
@@ -24,11 +24,6 @@ controlled via a boolean local variable.
24
24
  Maximum size of the ActiveStorage preview.
25
25
  %>
26
26
 
27
- <%
28
- # By default we don't allow attachment removal
29
- removable = local_assigns.fetch(:removable, false)
30
- %>
31
-
32
27
  <div>
33
28
  <%= render partial: 'fields/active_storage/preview', locals: local_assigns %>
34
29
  </div>
@@ -37,7 +32,10 @@ controlled via a boolean local variable.
37
32
  <%= link_to 'Download', field.blob_url(attachment), title: attachment.filename %>
38
33
  </div>
39
34
 
40
- <% if removable %>
41
- <%= link_to 'Remove', field.destroy_path(field, attachment), method: :delete, class: 'remove-attachment-link' %>
35
+ <% if field.destroy_url.present? %>
36
+ <% destroy_url = field.destroy_url.call(namespace, field.data.record, attachment) %>
37
+ <div>
38
+ <%= link_to 'Remove', destroy_url, method: :delete, class: 'remove-attachment-link' %>
39
+ </div>
42
40
  <hr>
43
41
  <% end %>
@@ -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) { attachments.present? && attachments.count != 1 }
24
20
  end
25
21
 
26
22
  def show_display_preview?
@@ -32,17 +28,22 @@ 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) do
40
+ proc do |namespace, record, attachment|
41
+ options = [attachment.name, namespace, record]
42
+ options << { attachment_id: attachment.id } if many?
43
+ options
44
+ end
45
+ end
46
+ end
46
47
 
47
48
  # currently we are using Rails.application.routes.url_helpers
48
49
  # without including the namespace because it runs into an
@@ -66,22 +67,17 @@ module Administrate
66
67
  Rails.application.routes.url_helpers.rails_blob_path(attachment, disposition: :attachment, only_path: true)
67
68
  end
68
69
 
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
70
  def can_add_attachment?
77
- many? || attachments.empty?
71
+ many? || attachments.blank?
78
72
  end
79
73
 
80
74
  def attached?
81
75
  data.present? && data.attached?
82
76
  end
83
77
 
84
- delegate :attachments, to: :data
78
+ def attachments
79
+ data.attachments if attached?
80
+ end
85
81
  end
86
82
  end
87
83
  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.1
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-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: administrate