class CurrentAttributes
Ruby on Rails 8.0.4
Since v5.2.8.1Current Attributes
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 :account, :user attribute :request_id, :user_agent, :ip_address resets { Time.zone = nil } def user=(user) super self.account = user.account 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.account.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.
Inherits from
Includes
Constants
Attributes
Methods (defined here)
- # reset
- # set
- self. after_reset
- self. attribute
- self. before_reset
- self. instance
- self. new
- self. resets
Private methods
(7)
Implementation detail — not part of the public API.
- # resolve_defaults
- self. current_instances
- self. current_instances_key
- self. generated_attribute_methods
- self. method_added
- self. method_missing
- self. respond_to_missing?
Methods (inherited)
From ActiveSupport::Callbacks (1)
From Object (17)
- # acts_like?
- # blank?
- # deep_dup
- # duplicable?
- # html_safe?
- # in?
- # instance_values
- # instance_variable_names
- # presence
- # presence_in
- # present?
- # to_param
- # to_query
- # try
- # try!
- # with
- # with_options
From ActiveSupport::Concern (3)
- # class_methods
- # included
- # prepended
From ActiveSupport::DescendantsTracker (3)
- # descendants
- self. descendants
- self. subclasses
From ActiveSupport::NumericWithFormat (2)
- # to_formatted_s
- # to_fs