rails PaperTrail gem causing duplicate version entries starting with version 6

314 Views Asked by At

Here is a piece of rspec code defining a risk_action :

  let(:risk_action)           {
    risk_action = risk.risk_actions.new({name: 'action 1', user: user, action_status_id: created.id, due_date: '2018-02-10', owner_email: user.email, org: org})
    risk_action.org = org
    risk_action.assigned_to = user
    risk_action.save!
    risk_action
  }

Both in rails 4 & 5, this behaves as I expect (it creates and persists the risk_action without error). Later in the rspec, I have a test case which runs a controller method and eventually calls this risk_action. I can confirm nothing has manipulated the object in the meantime (largely because my test case which is checking it via papertrail is working correctly) :

it "correctly assigns the Paper_Trail whodunnit type", versioning: true, focus: true  do
  session = RiskActionSession.create({risk_action: risk_action})
  expect(session).to be_valid
  expect(session.status).to eq('CREATED')

  jwt = session.generate_jwt
  #request.env["HTTP_AUTHORIZATION"] = "JWT #{jwt}"    # <-- DOES NOT WORK
  get :update_action, id: session.id, risk_action: {action_status_id: closed.id}, token: jwt

  expect(response.status).to eq(200)
  # there should be 2 versions [create, update]
  versions = risk_action.versions
  expect(versions.length).to eq(2)
  # 1 update event
  ver = versions.where(event: "update")
  expect(ver.length).to eq(1)
  #whodunnit_type should be set to 'User'
  expect(ver[0].whodunnit_type).to eq("User")
end

end #describe "update_action [PUT]" do

Then, after retrieving the risk_action and calling versions on it, I would expect to see a single create entry. Instead, I see two, precise duplicated except for the id (of the version object) :

[#<PaperTrail::Version:0x0000000006b1a930
  id: 24328,
  item_type: "RiskAction",
  item_id: 44074,
  event: "create",
  whodunnit: "543076",
  object: nil,
  created_at: Mon, 15 Aug 2022 16:14:45 UTC +00:00,
  object_changes:
   "---\nname:\n- \n- action 1\nuser_id:\n- \n- 543076\naction_status_id:\n- \n- 66607\ndue_date:\n- \n- '2018-02-10 00:00:00'\nowner_email:\n- ''\n- [email protected]\norg_id:\n- \n- 642273\nassigned_to_id:\n- \n- 543076\nassigned_to_type:\n- \n- User\nid:\n- \n- 44074\n",
  whodunnit_type: nil,
  comments: nil,
  transaction_id: 24322>,
 #<PaperTrail::Version:0x0000000006b1a430
  id: 24329,
  item_type: "RiskAction",
  item_id: 44074,
  event: "create",
  whodunnit: "543076",
  object: nil,
  created_at: Mon, 15 Aug 2022 16:14:45 UTC +00:00,
  object_changes:
   "---\nname:\n- \n- action 1\nuser_id:\n- \n- 543076\naction_status_id:\n- \n- 66607\ndue_date:\n- \n- '2018-02-10 00:00:00'\nowner_email:\n- ''\n- [email protected]\norg_id:\n- \n- 642273\nassigned_to_id:\n- \n- 543076\nassigned_to_type:\n- \n- User\nid:\n- \n- 44074\n",
  whodunnit_type: nil,
  comments: nil,
  transaction_id: 24322>]

This behaviour starts occurring with the papertrail gem version 6 on rails 4.2.10. The behaviour continues into rails 5.2.8 and papertrail gem versions 9 thru 11, the latter of which is where I'm trying to end up.

I am not sure why the behaviour begins here, there is nothing in the changelog (at least, that's obvious to me) why this would be the case. Any help is appreciated -- thank you.

0

There are 0 best solutions below