diff --git a/lib/mail/attachments_list.rb b/lib/mail/attachments_list.rb index 42e51c7fc..39d759210 100644 --- a/lib/mail/attachments_list.rb +++ b/lib/mail/attachments_list.rb @@ -106,5 +106,11 @@ def set_mime_type(filename) @mime_type && @mime_type.content_type end + # Recursively calls #[]= to merge a hash of attachments + def merge!(hash) + hash.each do |key, value| + self[key] = value + end + end end end diff --git a/spec/mail/attachments_list_spec.rb b/spec/mail/attachments_list_spec.rb index 04c6d59a2..fe08cd396 100644 --- a/spec/mail/attachments_list_spec.rb +++ b/spec/mail/attachments_list_spec.rb @@ -142,6 +142,20 @@ def check_decoded(actual, expected) expect(mail.attachments[3].filename).to eq 'test.zip' end + it "should allow you to merge a hash of attachments" do + mail = Mail.new + attachments = { + 'test.pdf' => read_raw_fixture('attachments', 'test.pdf'), + 'test.gif' => read_raw_fixture('attachments', 'test.gif'), + 'test.jpg' => read_raw_fixture('attachments', 'test.jpg'), + 'test.zip' => read_raw_fixture('attachments', 'test.zip') + } + mail.attachments.merge!(attachments) + expect(mail.attachments['test.pdf']).not_to be_nil + expect(mail.attachments['test.gif']).not_to be_nil + expect(mail.attachments['test.jpg']).not_to be_nil + expect(mail.attachments['test.zip']).not_to be_nil + end end describe "inline attachments" do