Skip to content

Commit ea03fac

Browse files
author
Mattia Roccoberton
committed
DB service: add compose method for Rails 7 only
1 parent 78af5b4 commit ea03fac

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ RSpec/MultipleExpectations:
3636

3737
RSpec/MultipleMemoizedHelpers:
3838
# Default is 5
39-
Max: 10
39+
Max: 12
4040

4141
RSpec/NestedGroups:
4242
# Default is 3

lib/active_storage/service/db_service_rails70.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
module ActiveStorage
44
module DBServiceRails70
5+
def compose(source_keys, destination_key, **)
6+
buffer = nil
7+
source_keys.each do |source_key|
8+
data = ::ActiveStorageDB::File.find_by!(ref: source_key).data
9+
if buffer
10+
buffer << data
11+
else
12+
buffer = data
13+
end
14+
end
15+
::ActiveStorageDB::File.create!(ref: destination_key, data: buffer) if buffer
16+
end
17+
518
private
619

720
def current_host

spec/service/active_storage/service/db_service_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@
4848
end
4949
end
5050

51+
if Rails::VERSION::MAJOR >= 7
52+
describe '.compose' do
53+
subject(:compose) { service.compose(%w[key1 key2 key3], 'dest_key') }
54+
55+
let!(:db_file1) { create(:active_storage_db_file, ref: 'key1', data: 'first file') }
56+
let!(:db_file2) { create(:active_storage_db_file, ref: 'key2', data: 'second file') }
57+
let!(:db_file3) { create(:active_storage_db_file, ref: 'key3', data: 'third file') }
58+
59+
it 'composes the source files' do
60+
expect { compose }.to change { ::ActiveStorageDB::File.where(ref: 'dest_key').count }.by(1)
61+
expect(compose).to be_kind_of ::ActiveStorageDB::File
62+
expect(compose.data).to eq [db_file1.data, db_file2.data, db_file3.data].join
63+
end
64+
end
65+
end
66+
5167
describe '.delete' do
5268
subject(:delete) { service.delete(key) }
5369

0 commit comments

Comments
 (0)