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 +4 -4
- data/.gitignore +1 -0
- data/.ruby-version +1 -1
- data/README.md +76 -20
- data/administrate-field-active_storage.gemspec +1 -1
- data/app/views/fields/active_storage/_form.html.erb +1 -1
- data/app/views/fields/active_storage/_index.html.erb +1 -1
- data/app/views/fields/active_storage/_item.html.erb +11 -12
- data/app/views/fields/active_storage/_preview.html.erb +1 -1
- data/example-project/app/controllers/admin/users_controller.rb +6 -0
- data/example-project/app/controllers/users_controller.rb +4 -4
- data/example-project/app/dashboards/user_dashboard.rb +3 -1
- data/example-project/config/routes.rb +3 -2
- data/lib/administrate/field/active_storage.rb +9 -19
- 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: eeae1cb2ea6a2ed7534c917f87c06d73ff22abbb03e299af2253f65abc7f1490
|
4
|
+
data.tar.gz: e1b8b00ec8cf58cf084fd916e0afee627110ea991d1fe31a27b617ef2a8919df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aeec55b49785338dfecebeb33cce2a7da199f3fe0e9c5cb56c9684a069bb0cbd555818d26384c10160a46a4dd63473f759ea255a8a7f6e88d9a8590e1bb3a393
|
7
|
+
data.tar.gz: c8950a969fb5503ab53a7cda2a59badd286f0ac4d1166ff0d7e8b42d4b279ad1c0c65ee97ecc28cdaf5bde1cad52d087d10bd4aab5a811fe20b3d7bcf04808d8
|
data/.gitignore
CHANGED
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.6.
|
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
|
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 <
|
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
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
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
|
-
|
89
|
-
|
90
|
-
```
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
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.
|
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
|
24
|
+
<%= render partial: 'fields/active_storage/items', locals: { field: field } %>
|
25
25
|
<% end %>
|
26
26
|
|
27
27
|
<div>
|
@@ -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
|
-
|
29
|
-
|
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
|
33
|
+
<%= link_to attachment.filename, field.blob_url(attachment), title: attachment.filename %>
|
38
34
|
</div>
|
39
35
|
|
40
|
-
<% if
|
41
|
-
|
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:
|
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
|
3
|
-
|
4
|
-
|
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(
|
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
|
-
|
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
|
-
|
44
|
-
|
45
|
-
|
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.
|
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
|
-
|
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.
|
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-
|
11
|
+
date: 2020-06-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: administrate
|