Check if audits is loaded before executing database queries
This should be a no-op change that allows for performance improvements by preloading the audits association. By checking if the audits association is already loaded, we can avoid N+1 queries by operating on the already loaded audits in ruby instead of going to the database again to check if a version exists, etc.
This code should be functionally identical to the existing code, but allows for something like:
@posts = Post.all.include(:audits)
@posts_at_v1 = @posts.map do |post|
post.revision(1)
end
To avoid repeated database access.
Fixes #719
Tests are failing, but I like the idea.
@danielmorrison thanks, will fix test failures
@danielmorrison specs are now passing except for code coverage (see the test run on our fork here: https://github.com/apsislabs/audited/actions/runs/10538941999?pr=1). I believe this is because of the addition of the new loaded? branches being untested.
I'm happy to either:
- Update coverage exceptions so the tests pass
- Attempt to add some specs to cover these branches
@danielmorrison just thought I'd poke in here and see if you had an answer for how you wanted me to handle the new untested lines?