Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 10 additions & 1 deletion lib/audited/sweeper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions spec/audited/sweeper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down