Skip to content

Commit

Permalink
Support passing keyword arguments to mailer plugin mail/sendmail clas…
Browse files Browse the repository at this point in the history
…s methods
  • Loading branch information
jeremyevans committed Jan 31, 2025
1 parent b55d171 commit 7c51b9b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
=== master

* Support passing keyword arguments to mailer plugin mail/sendmail class methods (jeremyevans)

* Improve performance when using :assume_fixed_locals render plugin option, when using compiled methods and freezing the app (jeremyevans)

* Add part plugin, with a simpler (and better performing when using :assume_fixed_locals) method for rendering with locals (jeremyevans)
Expand Down
6 changes: 6 additions & 0 deletions lib/roda/plugins/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,19 @@ def mail(path, *args)
mail
end
end
# :nocov:
ruby2_keywords(:mail) if respond_to?(:ruby2_keywords, true)
# :nocov:

# Calls +mail+ with given arguments and immediately sends the resulting mail.
def sendmail(*args)
if m = mail(*args)
m.deliver
end
end
# :nocov:
ruby2_keywords(:sendmail) if respond_to?(:ruby2_keywords, true)
# :nocov:
end

module RequestMethods
Expand Down
17 changes: 17 additions & 0 deletions spec/plugin/mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ def deliveries
app.sendmail('/bar', 1, 2).body.must_be :==, '["bar", 1, 2]'
end

it "supports keywords arguments to mail/sendmail methods, yielding them to the route blocks" do
instance_eval(<<-'RUBY', __FILE__, __LINE__ + 1)
app(:mailer) do |r|
instance_exec(&setup_email)
r.mail "foo" do |*args, **kw|
"foo#{args.inspect}#{kw.to_a.inspect}"
end
r.mail :d do |*args, **kw|
[args, kw.to_a].inspect
end
end
RUBY

app.mail('/foo', 1, a: 2).body.must_be :==, 'foo[1][[:a, 2]]'
app.sendmail('/bar', 1, a: 2).body.must_be :==, '[["bar", 1], [[:a, 2]]]'
end if RUBY_VERSION >= "2"

it "supports no_mail! method for skipping mailing" do
app(:mailer) do |r|
instance_exec(&setup_email)
Expand Down

0 comments on commit 7c51b9b

Please sign in to comment.