Skip to content

Commit

Permalink
Merge pull request #19 from EvgeniyShelest/inherit_defaults
Browse files Browse the repository at this point in the history
Inherit defaults
  • Loading branch information
demoore committed Apr 21, 2015
2 parents bc48e69 + 9541f13 commit 2686e69
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/sendwithus_ruby_action_mailer/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tt>SendWithUsMailer::Base</tt>. There should not be
# any reason to call this directly.
Expand Down
24 changes: 23 additions & 1 deletion test/lib/sendwithus_ruby_action_mailer/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[email protected]'
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'
Expand All @@ -63,6 +72,19 @@ def send_an_email
mail = MailerWithDefault.send_an_email
mail.to[:address].must_equal '[email protected]'
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
Expand Down

0 comments on commit 2686e69

Please sign in to comment.