diff --git a/spec/email_spec.cr b/spec/email_spec.cr index 1968aaf..622b5a0 100644 --- a/spec/email_spec.cr +++ b/spec/email_spec.cr @@ -1,3 +1,4 @@ +require "json" require "./spec_helper" private class User @@ -84,6 +85,17 @@ private class UndeliverableEmail < Carbon::Email to Carbon::Address.new("to@example.com") end +private class SerializableEmail < BareMinimumEmail + include JSON::Serializable + + MEMORY = IO::Memory.new + + attachment({io: MEMORY, file_name: "file.txt", mime_type: "text/plain"}) + + # This is to check that an attachment cannot be added more than once + attachment({file_name: "file.txt", mime_type: "text/plain", io: MEMORY}) +end + describe Carbon::Email do it "can build a bare minimum email" do email = BareMinimumEmail.new @@ -153,6 +165,13 @@ describe Carbon::Email do email.html_body.should contain "Email body" end + it "can be serialized" do + email = SerializableEmail.from_json("{}") + + email.attachments.size.should eq(1) + email.attachments.first[:io]?.should eq(SerializableEmail::MEMORY) + end + context "deliverable?" do it "is not delivery it is digiorno" do email = UndeliverableEmail.new diff --git a/src/carbon/email.cr b/src/carbon/email.cr index 93a4942..d185359 100644 --- a/src/carbon/email.cr +++ b/src/carbon/email.cr @@ -91,15 +91,17 @@ abstract class Carbon::Email end end - @attachments = [] of Carbon::Attachment - getter attachments + def attachments + [] of Carbon::Attachment + end macro attachment(value) def attachments : Array(Carbon::Attachment) - {% if @type.methods.map(&.name).includes?("attachments".id) %} - previous_def + {% if @type.methods.map(&.name).includes?(:attachments.id) %} + previous_def | [{{ value }}] + {% else %} + super | [{{ value }}] {% end %} - @attachments << {{value}} end end