You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 an ApplicationRecord class that calls audited if: :audit?, from which all other classes inherit, the default class' audit? method returns true, 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 NULL auditable_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 the audited 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:
Hey @danielmorrison, just checking before jumping in..can I create a PR for this?
I can see in the code here that it explicitly mentions don't allow multiple calls, so maybe it is intentional
However I believe updating the options attribute on multiple calls -while logging a warning- would be useful, so if it is okay I can add the change.
Thanks!
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: