Fix name extraction for module queue adapters

Before this change, setting a module as queue adapter would result in
`queue_adapter_name` being `"module"`. This PR fixes the name extraction logic
to handle module or class queue adapters.
This commit is contained in:
Sander Verdonschot 2023-04-19 18:51:49 -04:00
parent 02c1b7ac48
commit 6b4acb57e2
No known key found for this signature in database
GPG Key ID: 447E1882AA7DF0CD
2 changed files with 21 additions and 1 deletions

@ -43,7 +43,8 @@ def queue_adapter=(name_or_adapter)
assign_adapter(name_or_adapter.to_s, queue_adapter)
else
if queue_adapter?(name_or_adapter)
adapter_name = "#{name_or_adapter.class.name.demodulize.remove('Adapter').underscore}"
adapter_class = name_or_adapter.is_a?(Module) ? name_or_adapter : name_or_adapter.class
adapter_name = "#{adapter_class.name.demodulize.remove('Adapter').underscore}"
assign_adapter(adapter_name, name_or_adapter)
else
raise ArgumentError

@ -50,4 +50,23 @@ class QueueAdapterTest < ActiveJob::TestCase
assert_not_nil child_job_three.queue_adapter
end
test "should extract a reasonable name from a class instance" do
child_job = Class.new(ActiveJob::Base)
child_job.queue_adapter = ActiveJob::QueueAdapters::StubOneAdapter.new
assert_equal "stub_one", child_job.queue_adapter_name
end
module StubThreeAdapter
class << self
def enqueue(*); end
def enqueue_at(*); end
end
end
test "should extract a reasonable name from a class or module" do
child_job = Class.new(ActiveJob::Base)
child_job.queue_adapter = StubThreeAdapter
assert_equal "stub_three", child_job.queue_adapter_name
end
end