diff --git a/lib/audited/sweeper.rb b/lib/audited/sweeper.rb index 24ec1de6..2d58a120 100644 --- a/lib/audited/sweeper.rb +++ b/lib/audited/sweeper.rb @@ -20,7 +20,16 @@ def around(controller) end def current_user - lambda { controller.send(Audited.current_user_method) if controller.respond_to?(Audited.current_user_method, true) } + lambda { + method = Audited.current_user_method + if controller.respond_to?(method, true) + controller.send(method) + else + warn "[audited] #{controller.class}##{method} is not defined. " \ + "Audit records will have a nil user." + nil + end + } end def remote_ip diff --git a/spec/audited/sweeper_spec.rb b/spec/audited/sweeper_spec.rb index 799790bf..f497a49c 100644 --- a/spec/audited/sweeper_spec.rb +++ b/spec/audited/sweeper_spec.rb @@ -47,12 +47,14 @@ def populate_user expect(controller.company.audits.last.user).to eq(user) end - it "does not audit when method is not found" do + it "warns and sets nil user when method is not found" do controller.send(:current_user=, user) Audited.current_user_method = :nope expect { - post :create - }.to change(Audited::Audit, :count) + expect { + post :create + }.to change(Audited::Audit, :count) + }.to output(/\[audited\].*nope.*not defined/).to_stderr expect(controller.company.audits.last.user).to eq(nil) end