diff --git a/lib/mail/notify.rb b/lib/mail/notify.rb index 480116d..53125d9 100644 --- a/lib/mail/notify.rb +++ b/lib/mail/notify.rb @@ -5,7 +5,6 @@ require "mail/notify/version" require "mail/notify/railtie" if defined? Rails require "mail/notify/delivery_method" -require "mail/notify/personalisation" require "mail/notify/mailer" require "mail/notify/message" require "mail/notify/mail_notify_previews_controller" diff --git a/lib/mail/notify/mailer.rb b/lib/mail/notify/mailer.rb index f3c12c8..64f0c57 100644 --- a/lib/mail/notify/mailer.rb +++ b/lib/mail/notify/mailer.rb @@ -91,7 +91,7 @@ def view_mail(template_id, options) # allows blank personalisation options def blank_allowed(value) - value.presence || Personalisation::BLANK + value.to_s end end end diff --git a/lib/mail/notify/personalisation.rb b/lib/mail/notify/personalisation.rb deleted file mode 100644 index 4aeb2e7..0000000 --- a/lib/mail/notify/personalisation.rb +++ /dev/null @@ -1,30 +0,0 @@ -# frozen_string_literal: true - -module Mail - module Notify - class Personalisation - BLANK = Object.new - - def initialize(mail) - @body = mail.body.raw_source - @subject = mail.subject - @personalisation = mail[:personalisation]&.unparsed_value || {} - end - - def to_h - merged_options - .reject { |_k, v| v.blank? } - .transform_values { |value| (value == BLANK) ? "" : value } - end - - private - - def merged_options - { - body: @body, - subject: @subject - }.merge(@personalisation) - end - end - end -end diff --git a/spec/mail/notify/mailer_spec.rb b/spec/mail/notify/mailer_spec.rb index 3e3bd98..63ab48a 100644 --- a/spec/mail/notify/mailer_spec.rb +++ b/spec/mail/notify/mailer_spec.rb @@ -211,9 +211,15 @@ end end - context "when given a blank value" do + context "when given an empty string" do it "returns explicitly blank personalisation" do - expect(TestMailer.new.blank_allowed("")).to be Mail::Notify::Personalisation::BLANK + expect(TestMailer.new.blank_allowed("")).to eq "" + end + end + + context "when given nil" do + it "returns an empty string" do + expect(TestMailer.new.blank_allowed(nil)).to eq "" end end end diff --git a/spec/mail/notify/personalisation_spec.rb b/spec/mail/notify/personalisation_spec.rb deleted file mode 100644 index 36fa524..0000000 --- a/spec/mail/notify/personalisation_spec.rb +++ /dev/null @@ -1,64 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -RSpec.describe Mail::Notify::Personalisation do - let(:personalisation) { described_class.new(mail) } - - context "without any personalisation" do - let(:mail) { Mail.new(body: "foo", subject: "bar") } - - it "returns the body and subject as a hash" do - expect(personalisation.to_h).to eq( - body: "foo", - subject: "bar" - ) - end - end - - context "with some personalisations" do - let(:mail) do - Mail.new(body: "foo", subject: "bar", personalisation: { - foo: "bar" - }) - end - - it "merges everything" do - expect(personalisation.to_h).to eq( - :body => "foo", - :subject => "bar", - "foo" => "bar" - ) - end - end - - context "with blank subject and body" do - let(:mail) do - Mail.new(body: "", subject: "", personalisation: { - foo: "bar" - }) - end - - it "removes the subject and body" do - expect(personalisation.to_h).to eq( - "foo" => "bar" - ) - end - end - - context "with explicitly blank personalisation" do - let(:mail) { Mail.new(personalisation: {foo: Mail::Notify::Personalisation::BLANK}) } - - it "replaces it with a blank string" do - expect(personalisation.to_h).to eq("foo" => "") - end - end - - context "with implicitly blank personalisation" do - let(:mail) { Mail.new(personalisation: {foo: [nil, "", false].sample}) } - - it "removes it from the hash" do - expect(personalisation.to_h).to be_empty - end - end -end