Sitelet https://github.com/aasm/aasm/issues/810
Skip to content

AASM appears to unreliably interfere with Papertrail #810

Description

@rangerscience

Describe the bug

This test is failing sometimes. Maybe 50% of the time?

RSpec.describe Model, type: :model do
  let(:model) { create(:model) }

  it 'has paper trial' do
    model.first_event!('updated_by_test')
    model.second_event!('updated_by_test')

    expect(model.versions.count).to eq(3)
  end
end

Papertrail should have 3 version; the nil -> create, and the two from the events. When it works, Papertrail has the expected values. When it doesn't, papertrail has zero versions.

To Reproduce
(Note: Code is sanitized from our codebase. It's possible there's something else in our codebase that's messing with things)

Model:

class Model< ApplicationRecord
    include AASM
    has_paper_trail
    
    after_commit do
      puts "PT shows #{self.versions.count} versions"
    end 
    
    aasm column: :state do
      state :first_state, initial: true
      state :second_state
      state :third_state
    end
    
    before_all_events do |attribution|
      self.paper_trail_event = aasm.current_event
      self.updated_by = attribution
    end
   
    event :first_event do
      transitions from: :first_state, to: :second_state
    end

    event :second_event do
      transitions from: :second_state, to: :third_state
    end
  end
end

On success, console shows:

% rspec spec/models/model_spec.rb:7
Model
PT shows 1 versions
PT shows 2 versions
PT shows 3 versions
  has paper trial

Finished in 0.06665 seconds (files took 2.22 seconds to load)
1 example, 0 failures

On failure, console shows:

% rspec spec/models/model_spec.rb:7
Model
PT shows 0 versions
PT shows 0 versions
PT shows 0 versions
  has paper trial (FAILED - 1)

Failures:

  1) Model has paper trial
     Failure/Error: expect(transfer.versions.count).to eq(3)
     
       expected: 3
            got: 0
     
       (compared using ==)
     # ./spec/models/model_spec.rb:12:in `block (2 levels) in <top (required)>'

Finished in 0.0655 seconds (files took 2.19 seconds to load)
1 example, 1 failure

Expected behavior
Papertrail should reliably record versions when AASM events cause database updates.

Additional context
We have another model in our codebase that uses PT and does not use AASM, and it appears to function fine with PT (reran a test checking that on creation there's one PT version ~15 times, all passed)

Versions:
ruby '3.0.4'
aasm (5.2.0)
rails (6.0.4.7)
paper_trail (12.3.0)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions