From e4627b8832a2d667b86817990d27cc563c279096 Mon Sep 17 00:00:00 2001 From: Max Melentiev Date: Mon, 14 Aug 2017 17:37:12 +0300 Subject: [PATCH] Remove automatic Sweeper install Load hook runs twice in rails 5, so callbacks will also run twice. And there is no way to disable sweeper for particular controllers. --- README.md | 10 +++++++++- lib/audited/sweeper.rb | 9 --------- spec/audited/sweeper_spec.rb | 1 + spec/spec_helper.rb | 1 - 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 7905c46e6..9525bcd7e 100644 --- a/README.md +++ b/README.md @@ -143,16 +143,24 @@ end ### Current User Tracking -If you're using Audited in a Rails application, all audited changes made within a request will automatically be attributed to the current user. By default, Audited uses the `current_user` method in your controller. +If you're using Audited in a Rails application, add around callback for actions you want to track changes in. +All audited changes made within a request will automatically be attributed to the current user. By default, Audited uses the `current_user` method in your controller. ```ruby class PostsController < ApplicationController + around_action Audited::Sweeper.new + def create current_user # => # @post = Post.create(params[:post]) @post.audits.last.user # => # end end + +# or enable this callback for all controllers: +class ApplicationController < ActionController::Base + around_action Audited::Sweeper.new +end ``` To use a method other than `current_user`, put the following in an initializer: diff --git a/lib/audited/sweeper.rb b/lib/audited/sweeper.rb index aca68e36f..47009e15c 100644 --- a/lib/audited/sweeper.rb +++ b/lib/audited/sweeper.rb @@ -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 diff --git a/spec/audited/sweeper_spec.rb b/spec/audited/sweeper_spec.rb index 07093fb40..97fce41bf 100644 --- a/spec/audited/sweeper_spec.rb +++ b/spec/audited/sweeper_spec.rb @@ -1,6 +1,7 @@ require "spec_helper" class AuditsController < ActionController::Base + around_action Audited::Sweeper.new before_action :populate_user attr_reader :company diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f115f2bea..df1040547 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,7 +8,6 @@ require 'audited' require 'audited_spec_helpers' require 'support/active_record/models' -load "audited/sweeper.rb" # force to reload sweeper SPEC_ROOT = Pathname.new(File.expand_path('../', __FILE__))