Module: RailsBase::MailerKwargInject::ClassMethods

Defined in:
app/mailers/rails_base/mailer_kwarg_inject.rb

Instance Method Summary collapse

Instance Method Details

#inject_safety_net!Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/mailers/rails_base/mailer_kwarg_inject.rb', line 9

def inject_safety_net!
  action_methods.each do |method_name|
    alias_method_name = "__rails_base__kwarg_inject_#{method_name}__"
    self.alias_method(alias_method_name, method_name)

    self.define_method(method_name) do |*args, **kwargs, &block|
      parameter_name_order = method(alias_method_name).parameters.map(&:second)
      begin
        self.send(alias_method_name, *args, **kwargs, &block)
      rescue ArgumentError => e
        if Hash === args[0]
          new_arg_list = parameter_name_order.map { args[0][_1] }
          self.send(alias_method_name, *new_arg_list, &block)
          ActiveSupport::Deprecation.warn("Method Signature of `#{self.class}.#{method_name}` will change from KWargs to ARGs. Please modify your code.")
        else
          raise
        end
      end
    end
  end
end