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

Follow Rails autoloading conventions #532

Merged

Conversation

duncanjbrown
Copy link
Contributor

Depends on #530, because it won't pass specs without.

Thank you for this excellent gem, which we're using happily in production.

I encountered a conflict between audited and another gem which I believe is caused by the way audited hooks into Rails core classes. A minimal app displaying the behaviour is here: https://github.com/duncanjbrown/audited-bug. The behaviour is outlined on a rubyonrails-talk thread here: https://discuss.rubyonrails.org/t/understanding-a-conflict-between-two-activesupport-on-load-calls/74836.

Where an on_load reference is required to a non-ActiveSupport class (such as ActionController::API), wrapping the initialization code in a proper railtie prevents the weirdness I see in the above app. As far as I can tell this is the correct way to inject this code and it doesn't seem to break audited, which is why I'm submitting this PR.

While investigating this I learned that the current guidance (https://edgeguides.rubyonrails.org/engines.html#avoid-loading-rails-frameworks) it is best practice to extend core classes by hooking into ActiveSupport.on_load hooks, so this PR includes a change to the way audited does that, too.

@luke-hawk
Copy link

This will also fix an issue I encountered when using audited with devise-jwt. Thanks for investigating and fixing @duncanjbrown !

@danielmorrison danielmorrison merged commit 10068ef into collectiveidea:master May 29, 2021
mvastola pushed a commit to dailypay/audited that referenced this pull request Jul 27, 2021
While PR collectiveidea#532 worked to prevent `audited` from
initializing certain rails components too early, an issue remains
wherein the gem entrypoint, `lib/audited.rb`, immediately requires
`lib/audited/audit.rb`.

Therein `Audited::Audit` subclasses, `ActiveRecord::Base` and thereby
causes it to load too early in the Rails boot process.

This change merely moves the `require` of `audited/audit` into the `AR`
`on_load` block.
danielmorrison added a commit that referenced this pull request Sep 16, 2021
Resolve lingering issue from #532
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

Successfully merging this pull request may close these issues.

3 participants