Skip to content

Commit 8cab346

Browse files
committed
New task: list attachments
1 parent cad070d commit 8cab346

4 files changed

Lines changed: 72 additions & 3 deletions

File tree

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# frozen_string_literal: true
22

3-
# desc 'ActiveStorageDB sync'
4-
# task :active_storage_db do
5-
# end
3+
namespace :asdb do
4+
desc 'ActiveStorageDB: list attachments'
5+
task ls: [:environment] do |_t, _args|
6+
::ActiveStorage::Blob.order(:filename).pluck(:byte_size, :created_at, :filename).each do |size, dt, filename|
7+
size_k = (size / 1024).to_s.rjust(7)
8+
date = dt.strftime('%Y-%m-%d %H:%M')
9+
puts "#{size_k}K #{date} #{filename}"
10+
end
11+
end
12+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
FactoryBot.define do
4+
factory :active_storage_blob, class: 'ActiveStorage::Blob' do
5+
sequence(:filename) { |n| "file_#{n}" }
6+
byte_size { 1234 }
7+
checksum { 'some_checksum' }
8+
created_at { Time.now }
9+
end
10+
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
shared_context 'rake context' do
4+
Rails.application.load_tasks
5+
6+
def execute_task(task, args = nil)
7+
with_captured_stdout do
8+
Rake::Task[task].execute(args)
9+
end
10+
end
11+
12+
def with_captured_stderr
13+
original_stderr = $stderr
14+
$stderr = StringIO.new
15+
yield
16+
$stderr.string
17+
ensure
18+
$stderr = original_stderr
19+
end
20+
21+
def with_captured_stdout
22+
original_stdout = $stdout
23+
$stdout = StringIO.new
24+
yield
25+
$stdout.string
26+
ensure
27+
$stdout = original_stdout
28+
end
29+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe 'active_storage_db_tasks' do
4+
include_context 'rake context'
5+
6+
describe 'asdb:ls' do
7+
subject(:task) { execute_task('asdb:ls') }
8+
9+
let(:file1) { create(:active_storage_blob, filename: 'file1', created_at: Time.now - 1.hour) }
10+
let(:file2) { create(:active_storage_blob, filename: 'file2', created_at: Time.now - 5.hour) }
11+
let(:file3) { create(:active_storage_blob, filename: 'file3', created_at: Time.now - 3.hour) }
12+
13+
before do
14+
[file1, file2, file3]
15+
end
16+
17+
it { is_expected.to match /file1.+file2.+file3/m }
18+
19+
it 'prints the list of 3 files' do
20+
expect(task.split("\n").size).to be 3
21+
end
22+
end
23+
end

0 commit comments

Comments
 (0)