Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support multiple file upload for ActiveStorage and CarrierWave
- Loading branch information
Showing
20 changed files
with
552 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
app/views/rails_admin/main/_form_multiple_file_upload.html.haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
- field.attachments.each_with_index do |attachment, i| | ||
.toggle | ||
= attachment.pretty_value | ||
- if field.delete_method | ||
%a.btn.btn-info.btn-remove-image{href: '#', :'data-toggle' => 'button', role: 'button', onclick: "$(this).siblings('[type=checkbox]').click(); $(this).parent('.toggle').toggle('slow'); jQuery(this).toggleClass('btn-danger btn-info'); return false;"} | ||
%i.icon-white.icon-trash | ||
= I18n.t('admin.actions.delete.menu').capitalize + " #{field.label.downcase} ##{i + 1}" | ||
|
||
= form.check_box(field.delete_method, {multiple:true, style: 'display:none;'}, attachment.delete_key, nil) | ||
|
||
= form.file_field(field.name, field.html_attributes.reverse_merge({ data: { :"multiple-fileupload" => true }, multiple: true })) | ||
|
||
- if field.cache_method | ||
= form.hidden_field(field.cache_method) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
lib/rails_admin/config/fields/types/multiple_active_storage.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require 'rails_admin/config/fields/types/multiple_file_upload' | ||
|
||
module RailsAdmin | ||
module Config | ||
module Fields | ||
module Types | ||
class MultipleActiveStorage < RailsAdmin::Config::Fields::Types::MultipleFileUpload | ||
RailsAdmin::Config::Fields::Types.register(self) | ||
|
||
class ActiveStorageAttachment < RailsAdmin::Config::Fields::Types::MultipleFileUpload::AbstractAttachment | ||
register_instance_option :thumb_method do | ||
{resize: '100x100>'} | ||
end | ||
|
||
register_instance_option :delete_key do | ||
value.id | ||
end | ||
|
||
register_instance_option :image? do | ||
if value | ||
value.filename.to_s.split('.').last =~ /jpg|jpeg|png|gif|svg/i | ||
end | ||
end | ||
|
||
def resource_url(thumb = false) | ||
return nil unless value | ||
if thumb && value.variable? | ||
variant = value.variant(thumb_method) | ||
Rails.application.routes.url_helpers.rails_blob_representation_path( | ||
variant.blob.signed_id, variant.variation.key, variant.blob.filename, only_path: true | ||
) | ||
else | ||
Rails.application.routes.url_helpers.rails_blob_path(value, only_path: true) | ||
end | ||
end | ||
end | ||
|
||
register_instance_option :attachment_class do | ||
ActiveStorageAttachment | ||
end | ||
|
||
register_instance_option :delete_method do | ||
"remove_#{name}" if bindings[:object].respond_to?("remove_#{name}") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
44 changes: 44 additions & 0 deletions
44
lib/rails_admin/config/fields/types/multiple_carrierwave.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require 'rails_admin/config/fields/types/multiple_file_upload' | ||
|
||
module RailsAdmin | ||
module Config | ||
module Fields | ||
module Types | ||
class MultipleCarrierwave < RailsAdmin::Config::Fields::Types::MultipleFileUpload | ||
RailsAdmin::Config::Fields::Types.register(self) | ||
|
||
class CarrierwaveAttachment < RailsAdmin::Config::Fields::Types::MultipleFileUpload::AbstractAttachment | ||
register_instance_option :thumb_method do | ||
@thumb_method ||= ((versions = value.versions.keys).detect { |k| k.in?([:thumb, :thumbnail, 'thumb', 'thumbnail']) } || versions.first.to_s) | ||
end | ||
|
||
register_instance_option :delete_key do | ||
value.file.identifier | ||
end | ||
|
||
def resource_url(thumb = false) | ||
return nil unless value | ||
thumb.present? ? value.send(thumb).url : value.url | ||
end | ||
end | ||
|
||
register_instance_option :attachment_class do | ||
CarrierwaveAttachment | ||
end | ||
|
||
register_instance_option :cache_method do | ||
"#{name}_cache" | ||
end | ||
|
||
register_instance_option :delete_method do | ||
"delete_#{name}" if bindings[:object].respond_to?("delete_#{name}") | ||
end | ||
|
||
def value | ||
bindings[:object].send(name) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
106 changes: 106 additions & 0 deletions
106
lib/rails_admin/config/fields/types/multiple_file_upload.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
module RailsAdmin | ||
module Config | ||
module Fields | ||
module Types | ||
class MultipleFileUpload < RailsAdmin::Config::Fields::Base | ||
RailsAdmin::Config::Fields::Types.register(self) | ||
|
||
class AbstractAttachment | ||
include RailsAdmin::Config::Proxyable | ||
include RailsAdmin::Config::Configurable | ||
|
||
attr_reader :value | ||
|
||
def initialize(value) | ||
@value = value | ||
end | ||
|
||
register_instance_option :thumb_method do | ||
nil | ||
end | ||
|
||
register_instance_option :delete_key do | ||
nil | ||
end | ||
|
||
register_instance_option :export_value do | ||
resource_url.to_s | ||
end | ||
|
||
register_instance_option :pretty_value do | ||
if value.presence | ||
v = bindings[:view] | ||
url = resource_url | ||
if image | ||
thumb_url = resource_url(thumb_method) | ||
image_html = v.image_tag(thumb_url, class: 'img-thumbnail') | ||
url != thumb_url ? v.link_to(image_html, url, target: '_blank') : image_html | ||
else | ||
v.link_to(value, url, target: '_blank') | ||
end | ||
end | ||
end | ||
|
||
register_instance_option :image? do | ||
(url = resource_url.to_s) && url.split('.').last =~ /jpg|jpeg|png|gif|svg/i | ||
end | ||
|
||
def resource_url | ||
raise('not implemented') | ||
end | ||
end | ||
|
||
def initialize(*args) | ||
super | ||
@attachment_configurations = [] | ||
end | ||
|
||
register_instance_option :attachment_class do | ||
AbstractAttachment | ||
end | ||
|
||
register_instance_option :partial do | ||
:form_multiple_file_upload | ||
end | ||
|
||
register_instance_option :cache_method do | ||
nil | ||
end | ||
|
||
register_instance_option :delete_method do | ||
nil | ||
end | ||
|
||
register_instance_option :allowed_methods do | ||
[method_name, cache_method, delete_method].compact | ||
end | ||
|
||
register_instance_option :html_attributes do | ||
{ | ||
required: required? && !value.present?, | ||
} | ||
end | ||
|
||
def attachment(&block) | ||
@attachment_configurations << block | ||
end | ||
|
||
def attachments | ||
Array(value).map do |attached| | ||
attachment = attachment_class.new(attached).with(bindings) | ||
@attachment_configurations.each do |config| | ||
attachment.instance_eval(&config) | ||
end | ||
attachment | ||
end | ||
end | ||
|
||
# virtual class | ||
def virtual? | ||
true | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.