Skip to content

Commit

Permalink
Support CarrierWave 2.0 multiple file upload's keep, append and reord…
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya committed Jul 7, 2019
1 parent 043e187 commit fb093e0
Show file tree
Hide file tree
Showing 23 changed files with 337 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ group :development, :test do
end

group :test do
gem 'carrierwave', '>= 0.8'
gem 'carrierwave', ['>= 2.0.0.rc', '< 3']
gem 'coveralls'
gem 'database_cleaner', ['>= 1.2', '!= 1.4.0', '!= 1.5.0']
gem 'dragonfly', '~> 1.0'
Expand Down
3 changes: 2 additions & 1 deletion app/assets/javascripts/rails_admin/ra.widgets.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ $(document).on 'rails_admin.dom_ready', (e, content) ->
# multiple-fileupload

content.find('[data-multiple-fileupload]').each ->
$(this).closest('.controls').find('.btn-remove-image').on 'click', ->
$(this).closest('.controls').find('.btn-remove-image').on('click', ->
$(this).siblings('[type=checkbox]').click()
$(this).parent('.toggle').toggle('slow')
$(this).toggleClass('btn-danger btn-info')
false
).end().sortable(items: '.sortables')

# multiple-fileupload-preview

Expand Down
4 changes: 4 additions & 0 deletions app/assets/stylesheets/rails_admin/ra.widgets.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ iframe.wysihtml5-sandbox, .wysihtml5-editor{
max-height: 200px;
max-width: 300px;
}

.controls.ui-sortable .sortables{
cursor: move;
}
10 changes: 6 additions & 4 deletions app/views/rails_admin/main/_form_multiple_file_upload.html.haml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
- field.attachments.each_with_index do |attachment, i|
.toggle
.toggle{class: field.reorderable? ? 'sortables' : ''}
= attachment.pretty_value
- if field.delete_method
- if field.delete_method || field.keep_method
%a.btn.btn-info.btn-remove-image{href: '#', :'data-toggle' => 'button', role: 'button'}
%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)
- if field.keep_method
= form.check_box(field.keep_method, {multiple:true, checked: true, style: 'display:none;'}, attachment.keep_key, nil)
- elsif field.delete_method
= 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 }))

Expand Down
2 changes: 1 addition & 1 deletion gemfiles/cancan.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ group :development, :test do
end

group :test do
gem "carrierwave", ">= 0.8"
gem "carrierwave", [">= 2.0.0.rc", "< 3"]
gem "coveralls"
gem "database_cleaner", [">= 1.2", "!= 1.4.0", "!= 1.5.0"]
gem "dragonfly", "~> 1.0"
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_5.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ group :development, :test do
end

group :test do
gem "carrierwave", ">= 0.8"
gem "carrierwave", [">= 2.0.0.rc", "< 3"]
gem "coveralls"
gem "database_cleaner", [">= 1.2", "!= 1.4.0", "!= 1.5.0"]
gem "dragonfly", "~> 1.0"
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_5.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ group :development, :test do
end

group :test do
gem "carrierwave", ">= 0.8"
gem "carrierwave", [">= 2.0.0.rc", "< 3"]
gem "coveralls"
gem "database_cleaner", [">= 1.2", "!= 1.4.0", "!= 1.5.0"]
gem "dragonfly", "~> 1.0"
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_5.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ group :development, :test do
end

group :test do
gem "carrierwave", ">= 0.8"
gem "carrierwave", [">= 2.0.0.rc", "< 3"]
gem "coveralls"
gem "database_cleaner", [">= 1.2", "!= 1.4.0", "!= 1.5.0"]
gem "dragonfly", "~> 1.0"
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_6.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ group :development, :test do
end

group :test do
gem "carrierwave", ">= 0.8"
gem "carrierwave", [">= 2.0.0.rc", "< 3"]
gem "coveralls"
gem "database_cleaner", [">= 1.2", "!= 1.4.0", "!= 1.5.0"]
gem "dragonfly", "~> 1.0"
Expand Down
14 changes: 13 additions & 1 deletion lib/rails_admin/config/fields/types/multiple_carrierwave.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class CarrierwaveAttachment < RailsAdmin::Config::Fields::Types::MultipleFileUpl
@thumb_method ||= ((versions = value.versions.keys).detect { |k| k.in?([:thumb, :thumbnail, 'thumb', 'thumbnail']) } || versions.first.to_s)
end

register_instance_option :keep_key do
value.cache_name || value.identifier
end

register_instance_option :delete_key do
value.file.filename
end
Expand All @@ -27,7 +31,15 @@ def resource_url(thumb = false)
end

register_instance_option :cache_method do
"#{name}_cache"
"#{name}_cache" unless ::CarrierWave::VERSION >= '2'
end

register_instance_option :keep_method do
name if ::CarrierWave::VERSION >= '2'
end

register_instance_option :reorderable? do
::CarrierWave::VERSION >= '2'
end

register_instance_option :delete_method do
Expand Down
12 changes: 12 additions & 0 deletions lib/rails_admin/config/fields/types/multiple_file_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def initialize(value)
nil
end

register_instance_option :keep_key do
nil
end

register_instance_option :delete_key do
nil
end
Expand Down Expand Up @@ -67,6 +71,14 @@ def initialize(*args)
nil
end

register_instance_option :keep_method do
nil
end

register_instance_option :reorderable? do
false
end

register_instance_option :export_value do
attachments.map(&:resource_url).map(&:to_s).join(',')
end
Expand Down
8 changes: 1 addition & 7 deletions spec/controllers/rails_admin/main_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,7 @@ def get(action, params)
it 'allows for delete method with Carrierwave' do
RailsAdmin.config FieldTest do
field :carrierwave_asset
field :carrierwave_assets do
delete_method :delete_carrierwave_assets
end
field :carrierwave_assets
field :dragonfly_asset
field :paperclip_asset do
delete_method :delete_paperclip_asset
Expand All @@ -348,8 +346,6 @@ def get(action, params)
'carrierwave_asset_cache' => 'test',
'remove_carrierwave_asset' => 'test',
'carrierwave_assets' => 'test',
'carrierwave_assets_cache' => 'test',
'delete_carrierwave_assets' => 'test',
'dragonfly_asset' => 'test',
'remove_dragonfly_asset' => 'test',
'retained_dragonfly_asset' => 'test',
Expand All @@ -367,8 +363,6 @@ def get(action, params)
'remove_carrierwave_asset' => 'test',
'carrierwave_asset_cache' => 'test',
'carrierwave_assets' => 'test',
'carrierwave_assets_cache' => 'test',
'delete_carrierwave_assets' => 'test',
'dragonfly_asset' => 'test',
'remove_dragonfly_asset' => 'test',
'retained_dragonfly_asset' => 'test',
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy_app/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ group :mongoid do
gem 'shrine-mongoid'
end

gem 'carrierwave', '>= 0.8'
gem 'carrierwave', '>= 2.0.0.rc', '< 3.0'
gem 'devise', '>= 3.2'
gem 'dragonfly', '~> 1.0'
gem 'mini_magick', '>= 3.4'
Expand Down
18 changes: 0 additions & 18 deletions spec/dummy_app/app/active_record/field_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,6 @@ class FieldTest < ActiveRecord::Base
mount_uploader :carrierwave_asset, CarrierwaveUploader
mount_uploaders :carrierwave_assets, CarrierwaveUploader
serialize :carrierwave_assets, JSON
attr_accessor :delete_carrierwave_assets
after_validation do
uploaders = carrierwave_assets.delete_if do |uploader|
if Array(delete_carrierwave_assets).include?(uploader.file.filename)
uploader.remove!
true
end
end
write_attribute(:carrierwave_assets, uploaders.map { |uploader| uploader.file.filename })
end
def carrierwave_assets=(files)
appended = files.map do |file|
uploader = _mounter(:carrierwave_assets).blank_uploader
uploader.cache! file
uploader
end
super(carrierwave_assets + appended)
end

attachment :refile_asset if defined?(Refile)

Expand Down
Binary file added spec/fixtures/test.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spec/fixtures/test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spec/fixtures/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions spec/integration/fields/multiple_carrierwave_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'spec_helper'

describe 'MultipleCarrierwave field', type: :request, active_record: true do
subject { page }
before do
RailsAdmin.config FieldTest do
edit do
field :carrierwave_assets
end
end
end

it 'supports uploading multiple files' do
visit new_path(model_name: 'field_test')
attach_file "Carrierwave assets", [file_path('test.jpg'), file_path('test.png')]
click_button 'Save'
expect(FieldTest.first.carrierwave_assets.map { |image| File.basename(image.url) }).to match_array ['test.jpg', 'test.png']
end

context 'when working with existing files' do
let(:field_test) { FactoryBot.create(:field_test, carrierwave_assets: ['test.jpg', 'test.png'].map { |img| File.open(file_path(img)) }) }

it 'supports appending a file', js: true do
visit edit_path(model_name: 'field_test', id: field_test.id)
attach_file "Carrierwave assets", [file_path('test.gif')]
click_button 'Save'
field_test.reload
expect(field_test.carrierwave_assets.map { |image| File.basename(image.url) }).to eq ['test.jpg', 'test.png', 'test.gif']
end

it 'supports deleting a file', js: true do
visit edit_path(model_name: 'field_test', id: field_test.id)
click_link 'Delete carrierwave assets #1'
click_button 'Save'
field_test.reload
expect(field_test.carrierwave_assets.map { |image| File.basename(image.url) }).to eq ['test.png']
end

it 'supports reordering files', js: true do
visit edit_path(model_name: 'field_test', id: field_test.id)
page.execute_script File.read(File.expand_path('../../../support/jquery.simulate.drag-sortable.js', __FILE__))
page.execute_script %{$(".ui-sortable-handle:first-child").simulateDragSortable({ move: 1});}
click_button 'Save'
field_test.reload
expect(field_test.carrierwave_assets.map { |image| File.basename(image.url) }).to eq ['test.png', 'test.jpg']
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
RailsAdmin.config('FieldTest').fields.detect do |f|
f.name == :carrierwave_assets
end.with(
object: FieldTest.new(carrierwave_assets: ['dummy.txt']),
object: FieldTest.new(carrierwave_assets: [File.open(file_path('test.jpg'))]),
view: ApplicationController.new.view_context,
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
it 'includes delete_method and cache_method' do
RailsAdmin.config do |config|
config.model FieldTest do
field :carrierwave_assets, :multiple_carrierwave do
delete_method :delete_carrierwave_assets
end
field :carrierwave_assets, :multiple_carrierwave
field :active_storage_assets, :multiple_active_storage do
delete_method :remove_active_storage_assets
end if defined?(ActiveStorage)
end
end
expect(RailsAdmin.config(FieldTest).field(:carrierwave_assets).allowed_methods.collect(&:to_s)).to eq %w(carrierwave_assets carrierwave_assets_cache delete_carrierwave_assets)
expect(RailsAdmin.config(FieldTest).field(:active_storage_assets).allowed_methods.collect(&:to_s)).to eq %w(active_storage_assets remove_active_storage_assets) if defined?(ActiveStorage)
expect(RailsAdmin.config(FieldTest).field(:carrierwave_assets).with(object: FieldTest.new).allowed_methods.collect(&:to_s)).to eq %w(carrierwave_assets)
expect(RailsAdmin.config(FieldTest).field(:active_storage_assets).with(object: FieldTest.new).allowed_methods.collect(&:to_s)).to eq %w(active_storage_assets remove_active_storage_assets) if defined?(ActiveStorage)
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
require 'policies'
require 'database_cleaner'
require "orm/#{CI_ORM}"
require 'support/fakeio'

Dir[File.expand_path('../shared_examples/**/*.rb', __FILE__)].each { |f| require f }
Dir[File.expand_path('../support/**/*.rb', __FILE__),
File.expand_path('../shared_examples/**/*.rb', __FILE__)].each { |f| require f }

ActionMailer::Base.delivery_method = :test
ActionMailer::Base.perform_deliveries = true
Expand Down
3 changes: 3 additions & 0 deletions spec/support/fixtures.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def file_path(*paths)
File.expand_path(File.join(File.dirname(__FILE__), '../fixtures', *paths))
end
Loading

0 comments on commit fb093e0

Please sign in to comment.