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

controller metadata methods (user_for_paper_trail and info_for_paper_trail) should be evaluated only when paper_trail_enabled_for_controller is true. #182

Merged
merged 1 commit into from Nov 16, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/paper_trail/controller.rb
Expand Up @@ -2,9 +2,9 @@ module PaperTrail
module Controller

def self.included(base)
base.before_filter :set_paper_trail_enabled_for_controller
base.before_filter :set_paper_trail_whodunnit
base.before_filter :set_paper_trail_controller_info
base.before_filter :set_paper_trail_enabled_for_controller
end

protected
Expand Down Expand Up @@ -57,7 +57,7 @@ def set_paper_trail_enabled_for_controller

# Tells PaperTrail who is responsible for any changes that occur.
def set_paper_trail_whodunnit
::PaperTrail.whodunnit = user_for_paper_trail
::PaperTrail.whodunnit = user_for_paper_trail if paper_trail_enabled_for_controller
end

# DEPRECATED: please use `set_paper_trail_whodunnit` instead.
Expand All @@ -69,7 +69,7 @@ def set_whodunnit
# Tells PaperTrail any information from the controller you want
# to store alongside any changes that occur.
def set_paper_trail_controller_info
::PaperTrail.controller_info = info_for_paper_trail
::PaperTrail.controller_info = info_for_paper_trail if paper_trail_enabled_for_controller
end

end
Expand Down
19 changes: 19 additions & 0 deletions test/functional/controller_test.rb
Expand Up @@ -68,4 +68,23 @@ class ControllerTest < ActionController::TestCase
assert_equal '127.0.0.1', versions_for_widget.last.ip
assert_equal 'Rails Testing', versions_for_widget.last.user_agent
end

test "controller metadata methods should get evaluated if paper trail is enabled for controller" do
@request.env['HTTP_USER_AGENT'] = 'User-Agent'
post :create, :widget => { :name => 'Flugel' }
assert PaperTrail.enabled_for_controller?
assert_equal 153, PaperTrail.whodunnit
assert PaperTrail.controller_info.present?
assert PaperTrail.controller_info.keys.include?(:ip)
assert PaperTrail.controller_info.keys.include?(:user_agent)
end

test "controller metadata methods should not get evaluated if paper trail is disabled for controller" do
@request.env['HTTP_USER_AGENT'] = 'Disable User-Agent'
post :create, :widget => { :name => 'Flugel' }
assert_equal 0, assigns(:widget).versions.length
assert !PaperTrail.enabled_for_controller?
assert PaperTrail.whodunnit.nil?
assert PaperTrail.controller_info.nil?
end
end