Module: Whodunit::ControllerMethods
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/whodunit/controller_methods.rb
Overview
Controller integration for automatic user setting.
This module provides seamless integration with Rails controllers by automatically setting and resetting the current user context for auditing purposes. It includes methods for manual user management and supports various user detection patterns.
Instance Method Summary collapse
-
#current_whodunit_user ⇒ Object?
Detect the current user for auditing.
-
#reset_whodunit_user ⇒ void
Reset the current user context.
-
#set_whodunit_user(user = nil) ⇒ void
Set the current user for whodunit tracking.
-
#whodunit_user_available? ⇒ Boolean
Check if a user is available for stamping.
-
#with_whodunit_user(user) { ... } ⇒ Object
Temporarily set a different user for a block of code.
-
#without_whodunit_user { ... } ⇒ Object
Temporarily disable user tracking for a block of code.
Instance Method Details
#current_whodunit_user ⇒ Object?
Detect the current user for auditing.
This method tries multiple strategies to find the current user: 1. current_user method (most common Rails pattern) 2. @current_user instance variable 3. Other common authentication method names
Override this method in your controller to customize user detection.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/whodunit/controller_methods.rb', line 91 def current_whodunit_user return current_user if respond_to?(:current_user) && current_user return @current_user if defined?(@current_user) && @current_user # Try other common patterns user_methods = %i[ current_user current_account current_admin signed_in_user authenticated_user ] user_methods.each do |method| return send(method) if respond_to?(method, true) && send(method) end nil end |
#reset_whodunit_user ⇒ void
This method returns an undefined value.
Reset the current user context.
This method is automatically called as an after_action in controllers to ensure proper cleanup between requests.
72 73 74 |
# File 'lib/whodunit/controller_methods.rb', line 72 def reset_whodunit_user Whodunit::Current.reset end |
#set_whodunit_user(user = nil) ⇒ void
This method returns an undefined value.
Set the current user for whodunit tracking.
This method is automatically called as a before_action in controllers. Can also be called manually to set a specific user context.
61 62 63 64 |
# File 'lib/whodunit/controller_methods.rb', line 61 def set_whodunit_user(user = nil) user ||= current_whodunit_user Whodunit::Current.user = user end |
#whodunit_user_available? ⇒ Boolean
Check if a user is available for stamping.
Used internally by callbacks to determine if user tracking should occur.
113 114 115 |
# File 'lib/whodunit/controller_methods.rb', line 113 def whodunit_user_available? current_whodunit_user.present? end |
#with_whodunit_user(user) { ... } ⇒ Object
Temporarily set a different user for a block of code.
Useful for admin actions or background jobs that need to run as a specific user. Automatically restores the previous user context.
129 130 131 132 133 134 135 136 137 |
# File 'lib/whodunit/controller_methods.rb', line 129 def with_whodunit_user(user) previous_user = Whodunit::Current.user begin Whodunit::Current.user = user yield ensure Whodunit::Current.user = previous_user end end |
#without_whodunit_user { ... } ⇒ Object
Temporarily disable user tracking for a block of code.
Useful for system operations or bulk imports where user tracking is not desired.
150 151 152 |
# File 'lib/whodunit/controller_methods.rb', line 150 def without_whodunit_user(&) with_whodunit_user(nil, &) end |