Skip to content

Commit

Permalink
Merge pull request #43 from DFE-Digital/fix-rails6.0-serialization
Browse files Browse the repository at this point in the history
Pass model names as strings when loading batches
  • Loading branch information
duncanjbrown authored Aug 31, 2022
2 parents 0461270 + 9dc176f commit 5ad04af
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/dfe/analytics/load_entities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def run

ids = relation.pluck(:id)

DfE::Analytics::LoadEntityBatch.perform_later(model, ids, batch_number)
DfE::Analytics::LoadEntityBatch.perform_later(model.to_s, ids, batch_number)
end

Rails.logger.info "Enqueued #{batch_number} batches of #{@batch_size} #{@entity_name} for importing to BigQuery"
Expand Down
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 5ad04af

Please sign in to comment.