Skip to content

Commit 974c54a

Browse files
committed
Fix MultipleActiveStorage field deleting attachments in Rails 7.0
Fixes #3520
1 parent 7cae91a commit 974c54a

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

lib/rails_admin/config/fields/types/multiple_active_storage.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class ActiveStorageAttachment < RailsAdmin::Config::Fields::Types::MultipleFileU
1414
{resize_to_limit: [100, 100]}
1515
end
1616

17+
register_instance_option :keep_value do
18+
value.signed_id
19+
end
20+
1721
register_instance_option :delete_value do
1822
value.id
1923
end
@@ -43,6 +47,10 @@ def resource_url(thumb = false)
4347
ActiveStorageAttachment
4448
end
4549

50+
register_instance_option :keep_method do
51+
method_name if ::ActiveStorage.replace_on_assign_to_many
52+
end
53+
4654
register_instance_option :delete_method do
4755
"remove_#{name}" if bindings[:object].respond_to?("remove_#{name}")
4856
end

spec/dummy_app/config/application.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ class Application < Rails::Application
3838
config.i18n.load_path += Dir[Rails.root.join('app', 'locales', '*.{rb,yml}').to_s]
3939
config.active_record.time_zone_aware_types = %i[datetime time] if CI_ORM == :active_record
4040
config.active_storage.service = :local if defined?(ActiveStorage)
41+
config.active_storage.replace_on_assign_to_many = false if defined?(ActiveStorage) && ActiveStorage.version < Gem::Version.create('6.1')
4142
end
4243
end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe 'MultipleActiveStorage field', type: :request, active_record: true do
6+
subject { page }
7+
before do
8+
RailsAdmin.config FieldTest do
9+
edit do
10+
field :active_storage_assets
11+
end
12+
end
13+
# To suppress 'SQLite3::BusyException: database is locked' exception
14+
@original = page.driver.browser.url_blacklist # rubocop:disable Naming/InclusiveLanguage
15+
page.driver.browser.url_blacklist = ['/rails/active_storage/'] # rubocop:disable Naming/InclusiveLanguage
16+
end
17+
after { page.driver.browser.url_blacklist = @original } # rubocop:disable Naming/InclusiveLanguage
18+
19+
it 'supports uploading multiple files', js: true do
20+
visit new_path(model_name: 'field_test')
21+
attach_file 'Active storage assets', [file_path('test.jpg'), file_path('test.png')]
22+
click_button 'Save'
23+
is_expected.to have_content 'Field test successfully created'
24+
expect(FieldTest.first.active_storage_assets.map { |image| image.filename.to_s }).to match_array ['test.jpg', 'test.png']
25+
end
26+
27+
context 'when working with existing files' do
28+
let(:field_test) { FactoryBot.create(:field_test, active_storage_assets: ['test.jpg', 'test.png'].map { |img| {io: File.open(file_path(img)), filename: img} }) }
29+
30+
it 'supports appending a file', js: true do
31+
visit edit_path(model_name: 'field_test', id: field_test.id)
32+
attach_file 'Active storage assets', [file_path('test.gif')]
33+
click_button 'Save'
34+
is_expected.to have_content 'Field test successfully updated'
35+
field_test.reload
36+
expect(field_test.active_storage_assets.map { |image| image.filename.to_s }).to eq ['test.jpg', 'test.png', 'test.gif']
37+
end
38+
39+
it 'supports deleting a file', js: true do
40+
visit edit_path(model_name: 'field_test', id: field_test.id)
41+
click_link "Delete 'Active storage assets' #1"
42+
click_button 'Save'
43+
is_expected.to have_content 'Field test successfully updated'
44+
field_test.reload
45+
expect(field_test.active_storage_assets.map { |image| image.filename.to_s }).to eq ['test.png']
46+
end
47+
end
48+
end

0 commit comments

Comments
 (0)