Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maily Hooks don't accept kwargs #58

Open
stefanomarch opened this issue Oct 30, 2024 · 1 comment
Open

Maily Hooks don't accept kwargs #58

stefanomarch opened this issue Oct 30, 2024 · 1 comment

Comments

@stefanomarch
Copy link

stefanomarch commented Oct 30, 2024

Allow Hooks to receive keyword arguments. Currently it seems to fail ❌

@stefanomarch stefanomarch changed the title Maily Hook doesn t accept key value arguments Maily Hook doesn t accept kw_args arguments Oct 30, 2024
Repository owner deleted a comment from stefanomarch Oct 30, 2024
@markets markets changed the title Maily Hook doesn t accept kw_args arguments Maily Hooks don't accept kw_args Oct 30, 2024
@markets markets changed the title Maily Hooks don't accept kw_args Maily Hooks don't accept kwargs Oct 30, 2024
@markets
Copy link
Owner

markets commented Oct 30, 2024

Thanks for reporting @stefanomarch! Please feel free to explore a solution 🤝

All the relevant code of handling arguments is here:

maily/lib/maily/email.rb

Lines 46 to 102 in a68c231

def required_arguments
parameters.select { |param| param.first == :req }.map(&:last)
end
def optional_arguments
parameters.select { |param| param.first == :opt }.map(&:last)
end
def validate_arguments
from = required_arguments.size
to = from + optional_arguments.size
passed_by_hook = arguments && arguments.size || 0
if passed_by_hook < from
[false, "#{name} email requires at least #{from} arguments, passed #{passed_by_hook}"]
elsif passed_by_hook > to
[false, "#{name} email requires at the most #{to} arguments, passed #{passed_by_hook}"]
else
[true, nil]
end
end
def register_hook(*args)
if args.last.is_a?(Hash)
self.description = args.last.delete(:description)
self.with_params = args.last.delete(:with_params)
self.version = Maily::Email.formatted_version(args.last.delete(:version))
if tpl_path = args.last.delete(:template_path)
self.template_path = tpl_path
end
if tpl_name = args.last.delete(:template_name)
self.template_name = tpl_name
end
args.pop
end
self.arguments = args
end
def call
*args = arguments && arguments.map { |arg| arg.respond_to?(:call) ? arg.call : arg }
message = if args == [nil]
parameterized_mailer_klass.public_send(name)
else
parameterized_mailer_klass.public_send(name, *args)
end
ActionMailer::Base.preview_interceptors.each do |interceptor|
interceptor.previewing_email(message)
end
message
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants