-
Notifications
You must be signed in to change notification settings - Fork 670
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
Make audited
class method idempotent
#731
Comments
Hey @danielmorrison, just checking before jumping in..can I create a PR for this? |
I don't think it was intentional, but probably what was easy at the time. I'd love to see a PR for it. |
Done, here is the PR 🙏 |
This is an issue that's caused major headaches for me in many of my projects, because
audited
can only be called once. Calling it a second time does not function as intended, and worse, it doesn't error out if it's been called prior.The use case is simple--I want all my models to be audited unless I opt out of the process. Whether its my careless mistake or a team member that's unaware of it, it's quite easy to miss calling
audited
when you create a new model. To solve this problem, I've created anApplicationRecord
class that callsaudited if: :audit?
, from which all other classes inherit, the default class'audit?
method returnstrue
, and I can opt out on a per-model basis. Cool.Problem 1
Options can't be amended for models that need to use them. If I want to use
only:
/except:
/associated_with:
, these options are totally ignored on the second invocation. So now I have several years' worth of audit history all clogged up with ephemeral attributes that I didn't want to trigger an audit record.Problem 2
self.primary_key = :name
is only respected if called beforeaudited
is called. So now I have several years' worth of audit history with NULLauditable_id
attributes, and have no idea what records they actually belong to.Solution
Make the
audited
call idempotent so we can call it more than once. The options from the latest invocation should be respected, or at the very least, trigger an error to warn of unintended consequences. Many of the calls in theaudited
class method are already idempotent, but I would like the options to be respected or merged so we can call this on an abstract model, and call it again on the concrete model if needed.The text was updated successfully, but these errors were encountered: