Skip to content

Commit e48e0d1

Browse files
committed
fix handling activestorage attachments
1 parent 46bc870 commit e48e0d1

11 files changed

+305
-20
lines changed

lib/ruby_llm/active_record/acts_as_legacy.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,8 @@ def prepare_for_active_storage(attachments)
302302
case attachment
303303
when ActionDispatch::Http::UploadedFile, ActiveStorage::Blob
304304
attachment
305-
when ActiveStorage::Attached::One, ActiveStorage::Attached::Many
306-
attachment.blobs
305+
when ActiveStorage::Attachment
306+
attachment.blob
307307
when Hash
308308
attachment.values.map { |v| prepare_for_active_storage(v) }
309309
else

lib/ruby_llm/active_record/chat_methods.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,8 @@ def prepare_for_active_storage(attachments)
315315
case attachment
316316
when ActionDispatch::Http::UploadedFile, ActiveStorage::Blob
317317
attachment
318-
when ActiveStorage::Attached::One, ActiveStorage::Attached::Many
319-
attachment.blobs
318+
when ActiveStorage::Attachment
319+
attachment.blob
320320
when Hash
321321
attachment.values.map { |v| prepare_for_active_storage(v) }
322322
else

spec/dummy/app/models/document.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
class Document < ApplicationRecord
4+
has_one_attached :file
5+
has_many_attached :files
6+
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+
class CreateDocuments < ActiveRecord::Migration[7.0]
4+
def change
5+
create_table :documents do |t|
6+
t.string :title
7+
t.timestamps
8+
end
9+
end
10+
end

spec/dummy/db/schema.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@
7474
t.index ['tool_call_id'], name: 'index_document_messages_on_tool_call_id'
7575
end
7676

77+
create_table 'documents', force: :cascade do |t|
78+
t.string 'title'
79+
t.datetime 'created_at', null: false
80+
t.datetime 'updated_at', null: false
81+
end
82+
7783
create_table 'messages', force: :cascade do |t|
7884
t.integer 'chat_id'
7985
t.string 'role'

spec/fixtures/vcr_cassettes/activerecord_actsas_attachment_handling_handles_activestorage_attached_one_in_ask_method.yml

Lines changed: 123 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/vcr_cassettes/activerecord_actsas_attachment_handling_handles_multiple_attachments_in_ask_method.yml

Lines changed: 123 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/ruby_llm/active_record/acts_as_attachment_spec.rb

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,45 @@ def uploaded_file(path, type)
4747
expect(llm_message.content.attachments.first.mime_type).to eq('image/png')
4848
end
4949

50-
it 'handles multiple attachments' do
50+
it 'handles ActiveStorage::Attached::One in ask method' do
5151
chat = Chat.create!(model: model)
5252

53-
image_upload = uploaded_file(image_path, 'image/png')
54-
pdf_upload = uploaded_file(pdf_path, 'application/pdf')
53+
document = Document.create!(title: 'Test Document')
54+
document.file.attach(
55+
io: File.open(image_path),
56+
filename: 'ruby.png',
57+
content_type: 'image/png'
58+
)
5559

56-
response = chat.ask('Analyze these', with: [image_upload, pdf_upload])
60+
response = chat.ask('What do you see?', with: document.file)
5761

5862
user_message = chat.messages.find_by(role: 'user')
59-
expect(user_message.attachments.count).to eq(2)
63+
expect(user_message.attachments.count).to eq(1)
64+
expect(user_message.attachments.first.filename.to_s).to eq('ruby.png')
6065
expect(response.content).to be_present
6166
end
6267

63-
it 'handles attachments in ask method' do
68+
it 'handles ActiveStorage::Attached::Many in ask method' do
6469
chat = Chat.create!(model: model)
6570

66-
image_upload = uploaded_file(image_path, 'image/png')
71+
document = Document.create!(title: 'Test Document')
72+
document.files.attach(
73+
io: File.open(image_path),
74+
filename: 'ruby.png',
75+
content_type: 'image/png'
76+
)
77+
document.files.attach(
78+
io: File.open(pdf_path),
79+
filename: 'sample.pdf',
80+
content_type: 'application/pdf'
81+
)
6782

68-
response = chat.ask('What do you see?', with: image_upload)
83+
response = chat.ask('Analyze these', with: document.files)
6984

7085
user_message = chat.messages.find_by(role: 'user')
71-
expect(user_message.attachments.count).to eq(1)
86+
expect(user_message.attachments.count).to eq(2)
87+
filenames = user_message.attachments.map { |a| a.filename.to_s }.sort
88+
expect(filenames).to eq(['ruby.png', 'sample.pdf'])
7289
expect(response.content).to be_present
7390
end
7491
end

0 commit comments

Comments
 (0)