123456789_123456789_123456789_123456789_123456789_

Class: ActiveSupport::CurrentAttributes

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
self, Callbacks
Inherits: Object
Defined in: activesupport/lib/active_support/current_attributes.rb

Overview

Abstract super class that provides a thread-isolated attributes singleton, which resets automatically before and after each request. This allows you to keep all the per-request attributes easily available to the whole system.

The following full app-like example demonstrates how to use a Current class to facilitate easy access to the global, per-request attributes without passing them deeply around everywhere:

# app/models/current.rb
class Current < ActiveSupport::CurrentAttributes
  attribute :, :user
  attribute :request_id, :user_agent, :ip_address

  resets { Time.zone = nil }

  def user=(user)
    super
    self. = user.
    Time.zone    = user.time_zone
  end
end

# app/controllers/concerns/authentication.rb
module Authentication
  extend ActiveSupport::Concern

  included do
    before_action :authenticate
  end

  private
    def authenticate
      if authenticated_user = User.find_by(id: cookies.encrypted[:user_id])
        Current.user = authenticated_user
      else
        redirect_to new_session_url
      end
    end
end

# app/controllers/concerns/set_current_request_details.rb
module SetCurrentRequestDetails
  extend ActiveSupport::Concern

  included do
    before_action do
      Current.request_id = request.uuid
      Current.user_agent = request.user_agent
      Current.ip_address = request.ip
    end
  end
end

class ApplicationController < ActionController::Base
  include Authentication
  include SetCurrentRequestDetails
end

class MessagesController < ApplicationController
  def create
    Current..messages.create(message_params)
  end
end

class Message < ApplicationRecord
  belongs_to :creator, default: -> { Current.user }
  after_create { |message| Event.create(record: message) }
end

class Event < ApplicationRecord
  before_create do
    self.request_id = Current.request_id
    self.user_agent = Current.user_agent
    self.ip_address = Current.ip_address
  end
end

A word of caution: It’s easy to overdo a global singleton like Current and tangle your model as a result. Current should only be used for a few, top-level globals, like account, user, and request details. The attributes stuck in Current should be used by more or less all actions on all requests. If you start sticking controller-specific attributes in there, you’re going to create a mess.

Constant Summary

Callbacks - Included

CALLBACK_FILTER_TYPES

Callbacks - Attributes & Methods

Class Method Summary

DescendantsTracker - self

clear, descendants, direct_descendants,
store_inherited

This is the only method that is not thread safe, but is only ever called during the eager loading phase.

subclasses

Instance Attribute Summary

Instance Method Summary

Callbacks - Included

#run_callbacks

Runs the callbacks for the given event.

Constructor Details

.newCurrentAttributes

[ GitHub ]

  
# File 'activesupport/lib/active_support/current_attributes.rb', line 172

def initialize
  @attributes = {}
end

Class Attribute Details

.__callbacks (rw)

[ GitHub ]

  
# File 'activesupport/lib/active_support/callbacks.rb', line 67

class_attribute :__callbacks, instance_writer: false, default: {}

.__callbacks?Boolean (rw)

[ GitHub ]

  
# File 'activesupport/lib/active_support/callbacks.rb', line 67

class_attribute :__callbacks, instance_writer: false, default: {}

Class Method Details

.after_reset(&block)

Alias for .resets.

[ GitHub ]

  
# File 'activesupport/lib/active_support/current_attributes.rb', line 133

alias_method :after_reset, :resets

.attribute(*names)

Declares one or more attributes that will be given both class and instance accessor methods.

[ GitHub ]

  
# File 'activesupport/lib/active_support/current_attributes.rb', line 100

def attribute(*names)
  generated_attribute_methods.module_eval do
    names.each do |name|
      define_method(name) do
        attributes[name.to_sym]
      end

      define_method("#{name}=") do |attribute|
        attributes[name.to_sym] = attribute
      end
    end
  end

  names.each do |name|
    define_singleton_method(name) do
      instance.public_send(name)
    end

    define_singleton_method("#{name}=") do |attribute|
      instance.public_send("#{name}=", attribute)
    end
  end
end

.before_reset(&block)

Calls this block before #reset is called on the instance. Used for resetting external collaborators that depend on current values.

[ GitHub ]

  
# File 'activesupport/lib/active_support/current_attributes.rb', line 125

def before_reset(&block)
  set_callback :reset, :before, &block
end

.instance

Returns singleton instance for this class in this thread. If none exists, one is created.

[ GitHub ]

  
# File 'activesupport/lib/active_support/current_attributes.rb', line 95

def instance
  current_instances[current_instances_key] ||= new
end

.resets(&block) Also known as: .after_reset

Calls this block after #reset is called on the instance. Used for resetting external collaborators, like Time.zone.

[ GitHub ]

  
# File 'activesupport/lib/active_support/current_attributes.rb', line 130

def resets(&block)
  set_callback :reset, :after, &block
end

Instance Attribute Details

#__callbacks (readonly)

[ GitHub ]

  
# File 'activesupport/lib/active_support/callbacks.rb', line 67

class_attribute :__callbacks, instance_writer: false, default: {}

#__callbacks?Boolean (readonly)

[ GitHub ]

  
# File 'activesupport/lib/active_support/callbacks.rb', line 67

class_attribute :__callbacks, instance_writer: false, default: {}

#attributes (rw)

[ GitHub ]

  
# File 'activesupport/lib/active_support/current_attributes.rb', line 170

attr_accessor :attributes

#reset (readonly)

Reset all attributes. Should be called before and after actions, when used as a per-request singleton.

[ GitHub ]

  
# File 'activesupport/lib/active_support/current_attributes.rb', line 195

delegate :set, :reset, to: :instance

#set(set_attributes) (readonly)

Expose one or more attributes within a block. Old values are returned after the block concludes. Example demonstrating the common use of needing to set Current attributes outside the request-cycle:

class Chat::PublicationJob < ApplicationJob
  def perform(attributes, room_number, creator)
    Current.set(person: creator) do
      Chat::Publisher.publish(attributes: attributes, room_number: room_number)
    end
  end
end
[ GitHub ]

  
# File 'activesupport/lib/active_support/current_attributes.rb', line 186

delegate :set, :reset, to: :instance