Skip to content

Commit

Permalink
Use a railtie for Audited::Sweeper's ActiveSupport.on_load hook
Browse files Browse the repository at this point in the history
This prevents loading ActionController::API before Rails itself has
loaded it.
  • Loading branch information
duncanjbrown committed May 12, 2020
1 parent 4ad3a9c commit 0a33e4f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/audited.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ def config
::ActiveRecord::Base.send :include, Audited::Auditor

require 'audited/sweeper'
require 'audited/railtie'
14 changes: 14 additions & 0 deletions lib/audited/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Audited
class Railtie < Rails::Railtie
initializer "audited.sweeper" do
ActiveSupport.on_load(:action_controller) do
if defined?(ActionController::Base)
ActionController::Base.around_action Audited::Sweeper.new
end
if defined?(ActionController::API)
ActionController::API.around_action Audited::Sweeper.new
end
end
end
end
end
9 changes: 0 additions & 9 deletions lib/audited/sweeper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,3 @@ def controller=(value)
end
end
end

ActiveSupport.on_load(:action_controller) do
if defined?(ActionController::Base)
ActionController::Base.around_action Audited::Sweeper.new
end
if defined?(ActionController::API)
ActionController::API.around_action Audited::Sweeper.new
end
end
1 change: 1 addition & 0 deletions spec/audited/sweeper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def populate_user; end
render_views

before do
Audited::Railtie.initializers.each(&:run)
Audited.current_user_method = :current_user
end

Expand Down

0 comments on commit 0a33e4f

Please sign in to comment.