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.

Examples:

Automatic integration (via Railtie)

# This module is automatically included in ActionController::Base
class PostsController < ApplicationController
  # Whodunit automatically tracks current_user for all actions
  def create
    @post = Post.create(params[:post])
    # @post.creator_id is automatically set to current_user.id
  end
end

Manual user setting

class PostsController < ApplicationController
  def admin_action
    with_whodunit_user(User.admin) do
      # Actions here will be tracked as performed by admin user
      Post.create(title: "Admin Post")
    end
  end
end

Skipping whodunit for specific actions

class PostsController < ApplicationController
  skip_whodunit_for :index, :show
  # or
  whodunit_only_for :create, :update, :destroy
end

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#current_whodunit_userObject?

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.

Examples:

Custom user detection

def current_whodunit_user
  # Custom logic for finding user
  session[:admin_user] || super
end

Returns:

  • (Object, nil)

    the current user object or nil if none found

Since:

  • 0.1.0



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_uservoid

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.

Since:

  • 0.1.0



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.

Examples:

Manual usage

set_whodunit_user(User.find(123))
set_whodunit_user(nil)  # Uses current_whodunit_user detection

Parameters:

  • user (Object, nil) (defaults to: nil)

    the user object or user ID to set If nil, attempts to detect user via current_whodunit_user

Since:

  • 0.1.0



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.

Returns:

  • (Boolean)

    true if a user is available

Since:

  • 0.1.0



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.

Examples:

with_whodunit_user(admin_user) do
  Post.create(title: "Admin post")
end

Parameters:

  • user (Object)

    the user object to set temporarily

Yields:

  • the block to execute with the temporary user

Returns:

  • (Object)

    the return value of the block

Since:

  • 0.1.0



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.

Examples:

without_whodunit_user do
  Post.bulk_import(data)  # No creator_id will be set
end

Yields:

  • the block to execute without user tracking

Returns:

  • (Object)

    the return value of the block

Since:

  • 0.1.0



150
151
152
# File 'lib/whodunit/controller_methods.rb', line 150

def without_whodunit_user(&)
  with_whodunit_user(nil, &)
end