1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #38260 from kamipo/fix_kwargs_warning_for_activejob

Fix keyword arguments warnings in Active Job
This commit is contained in:
Ryuta Kamizono 2020-01-20 09:31:36 +09:00 committed by GitHub
commit 80e72c5eb7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 66 additions and 5 deletions

View file

@ -619,6 +619,7 @@ module ActionMailer
super
end
end
ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)
def respond_to_missing?(method, include_all = false)
action_methods.include?(method.to_s) || super

View file

@ -23,6 +23,7 @@ module ActionMailer
@processed_mailer = nil
@mail_message = nil
end
ruby2_keywords(:initialize) if respond_to?(:ruby2_keywords, true)
# Method calls are delegated to the Mail::Message that's ready to deliver.
def __getobj__ #:nodoc:
@ -143,10 +144,16 @@ module ActionMailer
def arguments_for(delivery_job, delivery_method)
if delivery_job <= MailDeliveryJob
[@mailer_class.name, @action.to_s, delivery_method.to_s, args: @args]
ruby2_keywords_arguments \
@mailer_class.name, @action.to_s, delivery_method.to_s, args: @args
else
[@mailer_class.name, @action.to_s, delivery_method.to_s, *@args]
end
end
def ruby2_keywords_arguments(*args)
args
end
ruby2_keywords(:ruby2_keywords_arguments) if respond_to?(:ruby2_keywords, true)
end
end

View file

@ -132,6 +132,7 @@ module ActionMailer
super(mailer_class, action, *args)
@params = params
end
ruby2_keywords(:initialize) if respond_to?(:ruby2_keywords, true)
private
def processed_mailer
@ -161,7 +162,8 @@ module ActionMailer
def arguments_for(delivery_job, delivery_method)
if delivery_job <= MailDeliveryJob
[@mailer_class.name, @action.to_s, delivery_method.to_s, params: @params, args: @args]
ruby2_keywords_arguments \
@mailer_class.name, @action.to_s, delivery_method.to_s, params: @params, args: @args
else
[@mailer_class.name, @action.to_s, delivery_method.to_s, @params, *@args]
end

View file

@ -22,6 +22,10 @@ class DelayedMailer < ActionMailer::Base
mail(from: "test-sender@test.com", to: "test-receiver@test.com", subject: "Test Subject", body: "Test Body")
end
def test_kwargs(argument:)
mail(from: "test-sender@test.com", to: "test-receiver@test.com", subject: "Test Subject", body: "Test Body")
end
def test_raise(klass_name)
raise klass_name.constantize, "boom"
end

View file

@ -164,4 +164,11 @@ class MessageDeliveryTest < ActiveSupport::TestCase
assert_equal DelayedMailer, DelayedMailer.last_rescue_from_instance
assert_equal "Error while trying to deserialize arguments: boom, missing find", DelayedMailer.last_error.message
end
test "allows for keyword arguments" do
assert_performed_with(job: ActionMailer::MailDeliveryJob, args: ["DelayedMailer", "test_kwargs", "deliver_now", args: [argument: 1]]) do
message = DelayedMailer.test_kwargs(argument: 1)
message.deliver_later
end
end
end

View file

@ -52,6 +52,8 @@ module ActiveJob
# :nodoc:
SYMBOL_KEYS_KEY = "_aj_symbol_keys"
# :nodoc:
RUBY2_KEYWORDS_KEY = "_aj_ruby2_keywords"
# :nodoc:
WITH_INDIFFERENT_ACCESS_KEY = "_aj_hash_with_indifferent_access"
# :nodoc:
OBJECT_SERIALIZER_KEY = "_aj_serialized"
@ -60,10 +62,39 @@ module ActiveJob
RESERVED_KEYS = [
GLOBALID_KEY, GLOBALID_KEY.to_sym,
SYMBOL_KEYS_KEY, SYMBOL_KEYS_KEY.to_sym,
RUBY2_KEYWORDS_KEY, RUBY2_KEYWORDS_KEY.to_sym,
OBJECT_SERIALIZER_KEY, OBJECT_SERIALIZER_KEY.to_sym,
WITH_INDIFFERENT_ACCESS_KEY, WITH_INDIFFERENT_ACCESS_KEY.to_sym,
]
private_constant :PERMITTED_TYPES, :RESERVED_KEYS, :GLOBALID_KEY, :SYMBOL_KEYS_KEY, :WITH_INDIFFERENT_ACCESS_KEY
private_constant :PERMITTED_TYPES, :RESERVED_KEYS, :GLOBALID_KEY,
:SYMBOL_KEYS_KEY, :RUBY2_KEYWORDS_KEY, :WITH_INDIFFERENT_ACCESS_KEY
unless Hash.respond_to?(:ruby2_keywords_hash?) && Hash.respond_to?(:ruby2_keywords_hash)
using Module.new {
refine Hash do
class << Hash
if RUBY_VERSION >= "2.7"
def ruby2_keywords_hash?(hash)
!new(*[hash]).default.equal?(hash)
end
else
def ruby2_keywords_hash?(hash)
false
end
end
def ruby2_keywords_hash(hash)
_ruby2_keywords_hash(**hash)
end
private def _ruby2_keywords_hash(*args)
args.last
end
ruby2_keywords(:_ruby2_keywords_hash) if respond_to?(:ruby2_keywords, true)
end
end
}
end
def serialize_argument(argument)
case argument
@ -76,9 +107,10 @@ module ActiveJob
when ActiveSupport::HashWithIndifferentAccess
serialize_indifferent_hash(argument)
when Hash
symbol_keys = argument.each_key.grep(Symbol).map(&:to_s)
symbol_keys = argument.each_key.grep(Symbol).map!(&:to_s)
result = serialize_hash(argument)
result[SYMBOL_KEYS_KEY] = symbol_keys
result[RUBY2_KEYWORDS_KEY] = true if Hash.ruby2_keywords_hash?(argument)
result
when -> (arg) { arg.respond_to?(:permitted?) }
serialize_indifferent_hash(argument.to_h)
@ -132,6 +164,10 @@ module ActiveJob
result = result.with_indifferent_access
elsif symbol_keys = result.delete(SYMBOL_KEYS_KEY)
result = transform_symbol_keys(result, symbol_keys)
if result.delete(RUBY2_KEYWORDS_KEY)
result = Hash.ruby2_keywords_hash(result)
end
end
result
end

View file

@ -86,6 +86,7 @@ module ActiveJob
@executions = 0
@exception_executions = {}
end
ruby2_keywords(:initialize) if respond_to?(:ruby2_keywords, true)
# Returns a hash with the job data that can safely be passed to the
# queuing adapter.

View file

@ -21,11 +21,13 @@ module ActiveJob
def perform_later(*args)
job_or_instantiate(*args).enqueue
end
ruby2_keywords(:perform_later) if respond_to?(:ruby2_keywords, true)
private
def job_or_instantiate(*args) # :doc:
args.first.is_a?(self) ? args.first : new(*args)
end
ruby2_keywords(:job_or_instantiate) if respond_to?(:ruby2_keywords, true)
end
# Enqueues the job to be performed by the queue adapter.

View file

@ -17,6 +17,7 @@ module ActiveJob
def perform_now(*args)
job_or_instantiate(*args).perform_now
end
ruby2_keywords(:perform_now) if respond_to?(:ruby2_keywords, true)
def execute(job_data) #:nodoc:
ActiveJob::Callbacks.run_callbacks(:execute) do

View file

@ -170,7 +170,7 @@ class ArgumentSerializationTest < ActiveSupport::TestCase
end
test "allows for keyword arguments" do
KwargsJob.perform_later(argument: 2)
KwargsJob.perform_now(argument: 2)
assert_equal "Job with argument: 2", JobBuffer.last_value
end