From 9541f135d725eec518d83a5fbf08abda501137d8 Mon Sep 17 00:00:00 2001 From: Evgeniy Shelest Date: Fri, 17 Apr 2015 14:31:58 +0300 Subject: [PATCH] Inherit defaults --- lib/sendwithus_ruby_action_mailer/base.rb | 5 ++++ .../integration_test.rb | 24 ++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/sendwithus_ruby_action_mailer/base.rb b/lib/sendwithus_ruby_action_mailer/base.rb index 54fa40c..86761e5 100644 --- a/lib/sendwithus_ruby_action_mailer/base.rb +++ b/lib/sendwithus_ruby_action_mailer/base.rb @@ -60,6 +60,11 @@ def default(value = nil) @defaults = defaults.merge(value) end + # Inherit defaults from ancestor + def inherited(heir) + heir.__send__(:default, defaults) + end + # Return the mailer methods that are defined in any instance # of SendWithUsMailer::Base. There should not be # any reason to call this directly. diff --git a/test/lib/sendwithus_ruby_action_mailer/integration_test.rb b/test/lib/sendwithus_ruby_action_mailer/integration_test.rb index 0c7a89f..647da58 100644 --- a/test/lib/sendwithus_ruby_action_mailer/integration_test.rb +++ b/test/lib/sendwithus_ruby_action_mailer/integration_test.rb @@ -47,13 +47,22 @@ def example_email describe "using default to set a parameter" do class MailerWithDefault < SendWithUsMailer::Base - default email_id: 'def-4bBEddKbhKBu5xsU2p58KX' + default email_id: 'def-4bBEddKbhKBu5xsU2p58KX', + from_address: 'from@mailer_with_default.com', + reply_to: 'reply_to@mailer_with_default.com' def send_an_email mail recipient_address: 'def-dave@example.com' end end + class InheritDefaultsMailer < MailerWithDefault + end + + class ReplacedDefaultsMailer < MailerWithDefault + default from_address: 'from@replaced_defaults_mailer.com' + end + it "sets the email_id" do mail = MailerWithDefault.send_an_email mail.email_id.must_equal 'def-4bBEddKbhKBu5xsU2p58KX' @@ -63,6 +72,19 @@ def send_an_email mail = MailerWithDefault.send_an_email mail.to[:address].must_equal 'def-dave@example.com' end + + it "inherits defaults from ancestor" do + InheritDefaultsMailer.defaults[:from_address].must_equal 'from@mailer_with_default.com' + InheritDefaultsMailer.defaults[:reply_to].must_equal 'reply_to@mailer_with_default.com' + end + + it "replaces ancestor defaults by own" do + ReplacedDefaultsMailer.defaults[:from_address].must_equal 'from@replaced_defaults_mailer.com' + end + + it "uses ancestors defaults that has not been replaced by own" do + ReplacedDefaultsMailer.defaults[:reply_to].must_equal 'reply_to@mailer_with_default.com' + end end describe "all parameters can be set by default" do