From ef1a8d1457485b8076f04f826f0abf91f440e879 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Thu, 16 Mar 2023 17:44:10 +0100 Subject: [PATCH] Ensure each wizard has the right user --- lib/custom_wizard/submission.rb | 5 +++-- .../submission_serializer_spec.rb | 20 +++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/custom_wizard/submission.rb b/lib/custom_wizard/submission.rb index edd0e1c9..38cad982 100644 --- a/lib/custom_wizard/submission.rb +++ b/lib/custom_wizard/submission.rb @@ -134,8 +134,9 @@ class CustomWizard::Submission submission_user = list_user || User.find_by(id: record.key.to_i) submission_data.each do |data| - wizard.user = submission_user if submission_user.present? - result.submissions.push(new(wizard, data)) + _wizard = wizard.clone + _wizard.user = submission_user if submission_user.present? + result.submissions.push(new(_wizard, data)) end end end diff --git a/spec/serializers/custom_wizard/submission_serializer_spec.rb b/spec/serializers/custom_wizard/submission_serializer_spec.rb index 7853f768..2fa000e8 100644 --- a/spec/serializers/custom_wizard/submission_serializer_spec.rb +++ b/spec/serializers/custom_wizard/submission_serializer_spec.rb @@ -14,11 +14,12 @@ describe CustomWizard::SubmissionSerializer do before do CustomWizard::Template.save(template_json, skip_jobs: true) + wizard = CustomWizard::Wizard.create(template_json["id"], user1) - CustomWizard::Submission.new(wizard, - step_1_field_1: "I am user submission", - submitted_at: Time.now.iso8601 - ).save + CustomWizard::Submission.new(wizard, step_1_field_1: "I am user1 submission", submitted_at: Time.now.iso8601).save + + wizard = CustomWizard::Wizard.create(template_json["id"], user2) + CustomWizard::Submission.new(wizard, step_1_field_1: "I am user2 submission", submitted_at: Time.now.iso8601).save end it 'should return submission attributes' do @@ -30,12 +31,11 @@ describe CustomWizard::SubmissionSerializer do each_serializer: described_class ).as_json - expect(json_array.length).to eq(1) + expect(json_array.length).to eq(2) expect(json_array[0][:id].present?).to eq(true) - expect(json_array[0][:user]).to eq( - BasicUserSerializer.new(user1, root: false).as_json - ) expect(json_array[0][:submitted_at].present?).to eq(true) + expect(json_array[0][:user]).to eq(BasicUserSerializer.new(user2, root: false).as_json) + expect(json_array[1][:user]).to eq(BasicUserSerializer.new(user1, root: false).as_json) end it "should return field values, types and labels" do @@ -47,10 +47,10 @@ describe CustomWizard::SubmissionSerializer do each_serializer: described_class ).as_json - expect(json_array.length).to eq(1) + expect(json_array.length).to eq(2) expect(json_array[0][:fields].as_json).to eq({ "step_1_field_1": { - "value": "I am user submission", + "value": "I am user2 submission", "type": "text", "label": "Text" }