Skip to content

Commit

Permalink
Pass model names as strings when loading batches
Browse files Browse the repository at this point in the history
ActiveJob < 6.1 cannot handle classes in params

"Allow Class and Module instances to be serialized." appeared in
Rails changelog at https://github.com/rails/rails/releases/tag/v6.1.0.rc1
  • Loading branch information
duncanjbrown committed Aug 30, 2022
1 parent 72e7e7e commit 4dc410a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/dfe/analytics/load_entity_batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module DfE
module Analytics
class LoadEntityBatch < AnalyticsJob
def perform(model_class, ids, batch_number)
# Support string args for Rails < 6.1
model_class = model_class.constantize if model_class.respond_to?(:constantize)

events = model_class.where(id: ids).map do |record|
DfE::Analytics::Event.new
.with_type('import_entity')
Expand Down
37 changes: 37 additions & 0 deletions spec/dfe/analytics/load_entity_batch_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
RSpec.describe DfE::Analytics::LoadEntityBatch do
include ActiveJob::TestHelper

with_model :Candidate do
table do |t|
t.string :email_address
end
end

before do
allow(DfE::Analytics::SendEvents).to receive(:perform_later)
end

around do |ex|
perform_enqueued_jobs do
ex.run
end
end

it 'accepts its first arg as a String to support Rails < 6.1' do
# Rails 6.1rc1 added support for deserializing Class and Module params
c = Candidate.create(email_address: 'foo@example.com')

described_class.perform_later("Candidate", [c.id], 1)

expect(DfE::Analytics::SendEvents).to have_received(:perform_later).once
end

it 'accepts its first arg as a Class' do
# backwards compatability with existing enqueued jobs
c = Candidate.create(email_address: 'foo@example.com')

described_class.perform_later(Candidate, [c.id], 1)

expect(DfE::Analytics::SendEvents).to have_received(:perform_later).once
end
end

0 comments on commit 4dc410a

Please sign in to comment.