Skip to content

Commit 2d6b7d1

Browse files
committed
fix handling activestorage attachments
1 parent 10b31b3 commit 2d6b7d1

11 files changed

+315
-26
lines changed

lib/ruby_llm/active_record/acts_as_legacy.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,9 @@ 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
305+
when ActiveStorage::Attached::One
306+
attachment.blob
307+
when ActiveStorage::Attached::Many
306308
attachment.blobs
307309
when Hash
308310
attachment.values.map { |v| prepare_for_active_storage(v) }

lib/ruby_llm/active_record/chat_methods.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,9 @@ def prepare_for_active_storage(attachments)
307307
case attachment
308308
when ActionDispatch::Http::UploadedFile, ActiveStorage::Blob
309309
attachment
310-
when ActiveStorage::Attached::One, ActiveStorage::Attached::Many
310+
when ActiveStorage::Attached::One
311+
attachment.blob
312+
when ActiveStorage::Attached::Many
311313
attachment.blobs
312314
when Hash
313315
attachment.values.map { |v| prepare_for_active_storage(v) }

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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#
1313
# It's strongly recommended that you check this file into your version control system.
1414

15-
ActiveRecord::Schema[7.1].define(version: 20_250_602_134_116) do
15+
ActiveRecord::Schema[8.0].define(version: 20_251_002_152_808) do
1616
create_table 'active_storage_attachments', force: :cascade do |t|
1717
t.string 'name', null: false
1818
t.string 'record_type', null: false
@@ -49,6 +49,12 @@
4949
t.index ['model_id'], name: 'index_chats_on_model_id'
5050
end
5151

52+
create_table 'documents', force: :cascade do |t|
53+
t.string 'title'
54+
t.datetime 'created_at', null: false
55+
t.datetime 'updated_at', null: false
56+
end
57+
5258
create_table 'messages', force: :cascade do |t|
5359
t.integer 'chat_id'
5460
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: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,47 @@ 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')
55-
56-
response = chat.ask('Analyze these', with: [image_upload, pdf_upload])
57-
58-
user_message = chat.messages.find_by(role: 'user')
59-
expect(user_message.attachments.count).to eq(2)
60-
expect(response.content).to be_present
61-
end
62-
63-
it 'handles attachments in ask method' do
64-
chat = Chat.create!(model: model)
65-
66-
image_upload = uploaded_file(image_path, 'image/png')
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+
)
6759

68-
response = chat.ask('What do you see?', with: image_upload)
60+
response = chat.ask('What do you see?', with: document.file)
6961

7062
user_message = chat.messages.find_by(role: 'user')
7163
expect(user_message.attachments.count).to eq(1)
64+
expect(user_message.attachments.first.filename.to_s).to eq('ruby.png')
7265
expect(response.content).to be_present
7366
end
67+
68+
# it 'handles ActiveStorage::Attached::Many in ask method' do
69+
# chat = Chat.create!(model: model)
70+
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+
# )
82+
83+
# response = chat.ask('Analyze these', with: document.files)
84+
85+
# user_message = chat.messages.find_by(role: 'user')
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'])
89+
# expect(response.content).to be_present
90+
# end
7491
end
7592

7693
describe 'attachment types' do

0 commit comments

Comments
 (0)