Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AASM appears to unreliably interfere with Papertrail #810

Open
rangerscience opened this issue Feb 6, 2023 · 2 comments
Open

AASM appears to unreliably interfere with Papertrail #810

rangerscience opened this issue Feb 6, 2023 · 2 comments

Comments

@rangerscience
Copy link

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)

@rangerscience
Copy link
Author

Update:
Explicitly adding the PT update callback a second time, at the bottom of the model:

  paper_trail.on_update
end

causes there to be (1,3,5) versions (consistent with two update callbacks being triggered) or all zero, as in the above. So it seems like something is blocking Papertrail's callbacks from running at all.

@rangerscience
Copy link
Author

Update:
I tried explicitly telling PT to record a version in an AASM callback:

      ensure_on_all_events do
        self.paper_trail.save_with_version
      end

This causes the same behavior as my last comment - either an extra PT record on all events, or still zero PT records.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant