Skip to content

Commit b102e7b

Browse files
committed
fix handling ActiveStorage::Attached::Many
1 parent 2d6b7d1 commit b102e7b

File tree

3 files changed

+25
-29
lines changed

3 files changed

+25
-29
lines changed

lib/ruby_llm/active_record/acts_as_legacy.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +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
305+
when ActiveStorage::Attached::One, ActiveStorage::Attachment
306306
attachment.blob
307-
when ActiveStorage::Attached::Many
308-
attachment.blobs
309307
when Hash
310308
attachment.values.map { |v| prepare_for_active_storage(v) }
311309
else

lib/ruby_llm/active_record/chat_methods.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,8 @@ def prepare_for_active_storage(attachments)
307307
case attachment
308308
when ActionDispatch::Http::UploadedFile, ActiveStorage::Blob
309309
attachment
310-
when ActiveStorage::Attached::One
310+
when ActiveStorage::Attached::One, ActiveStorage::Attachment
311311
attachment.blob
312-
when ActiveStorage::Attached::Many
313-
attachment.blobs
314312
when Hash
315313
attachment.values.map { |v| prepare_for_active_storage(v) }
316314
else

spec/ruby_llm/active_record/acts_as_attachment_spec.rb

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,29 +65,29 @@ def uploaded_file(path, type)
6565
expect(response.content).to be_present
6666
end
6767

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
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
9191
end
9292

9393
describe 'attachment types' do

0 commit comments

Comments
 (0)