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

Fix lazy load rails #1237

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).

### Fixed

- None
- [#1237](https://github.com/paper-trail-gem/paper_trail/pull/1237)
Fixed Rails lazy load ActiveRecord

### Dependencies

Expand Down
39 changes: 7 additions & 32 deletions lib/paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,13 @@
# can revisit this decision.
require "active_support/all"

# AR is required for, eg. has_paper_trail.rb, so we could put this `require` in
# all of those files, but it seems easier to troubleshoot if we just make sure
# AR is loaded here before loading *any* of PT. See discussion of
# performance/simplicity tradeoff for activesupport above.
require "active_record"

require "request_store"
require "paper_trail/cleaner"
require "paper_trail/compatibility"
require "paper_trail/config"
require "paper_trail/has_paper_trail"
require "paper_trail/record_history"
require "paper_trail/reifier"
require "paper_trail/request"
require "paper_trail/version_concern"
require "paper_trail/version_number"
require "paper_trail/serializers/json"
require "paper_trail/serializers/yaml"

# An ActiveRecord extension that tracks changes to your models, for auditing or
# versioning.
Expand Down Expand Up @@ -126,27 +115,13 @@ def version
end
end

# We use the `on_load` "hook" instead of `ActiveRecord::Base.include` because we
# don't want to cause all of AR to be autloaded yet. See
# https://guides.rubyonrails.org/engines.html#what-are-on-load-hooks-questionmark
# to learn more about `on_load`.
ActiveSupport.on_load(:active_record) do
include PaperTrail::Model
end

# Require frameworks
if defined?(::Rails)
# Rails module is sometimes defined by gems like rails-html-sanitizer
# so we check for presence of Rails.application.
if defined?(::Rails.application)
require "paper_trail/frameworks/rails"
else
::Kernel.warn(::PaperTrail::E_RAILS_NOT_LOADED)
end
if defined?(Rails::Railtie)
require "paper_trail/railtie"
else
require "paper_trail/frameworks/active_record"
end

if defined?(::ActiveRecord)
require "active_record"
::PaperTrail::Compatibility.check_activerecord(::ActiveRecord.gem_version)
require "paper_trail/has_paper_trail"
require "paper_trail/reifier"
require "paper_trail/frameworks/active_record/models/paper_trail/version"
ActiveRecord.include PaperTrail::Model
end
5 changes: 0 additions & 5 deletions lib/paper_trail/frameworks/active_record.rb

This file was deleted.

6 changes: 0 additions & 6 deletions lib/paper_trail/frameworks/rails/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,3 @@ def set_paper_trail_controller_info
end
end
end

if defined?(::ActionController)
::ActiveSupport.on_load(:action_controller) do
include ::PaperTrail::Rails::Controller
end
end
24 changes: 24 additions & 0 deletions lib/paper_trail/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require "paper_trail/frameworks/rails/engine"

module PaperTrail
# Represents code to load within Rails framework
# @api private
class Railtie < ::Rails::Railtie
initializer "paper_trail" do
ActiveSupport.on_load(:action_controller) do
require "paper_trail/frameworks/rails/controller"
include PaperTrail::Rails::Controller
end

ActiveSupport.on_load(:active_record) do
::PaperTrail::Compatibility.check_activerecord(::ActiveRecord.gem_version)
require "paper_trail/has_paper_trail"
require "paper_trail/reifier"
require "paper_trail/frameworks/active_record/models/paper_trail/version"
include PaperTrail::Model
end
end
end
end