class ActiveSupport::CurrentAttributes
sticking controller-specific attributes in there, you’re going to create a mess.
The attributes stuck in Current should be used by more or less all actions on all requests. If you start
Current should only be used for a few, top-level globals, like account, user, and request details.
A word of caution: It’s easy to overdo a global singleton like Current and tangle your model as a result.
end
end
self.ip_address = Current.ip_address
self.user_agent = Current.user_agent
self.request_id = Current.request_id
before_create do
class Event < ApplicationRecord
end
after_create { |message| Event.create(record: message) }
belongs_to :creator, default: -> { Current.user }
class Message < ApplicationRecord
end
end
Current.account.messages.create(message_params)
def create
class MessagesController < ApplicationController
end
include SetCurrentRequestDetails
include Authentication
class ApplicationController < ActionController::Base
end
end
end
Current.ip_address = request.ip
Current.user_agent = request.user_agent
Current.request_id = request.uuid
before_action do
included do
extend ActiveSupport::Concern
module SetCurrentRequestDetails
# app/controllers/concerns/set_current_request_details.rb
end
end
end
redirect_to new_session_url
else
Current.user = authenticated_user
if authenticated_user = User.find_by(id: cookies.encrypted)
def authenticate
private
end
before_action :authenticate
included do
extend ActiveSupport::Concern
module Authentication
# app/controllers/concerns/authentication.rb
end
end
Time.zone = user.time_zone
self.account = user.account
super
def user=(user)
resets { Time.zone = nil }
attribute :request_id, :user_agent, :ip_address
attribute :account, :user
class Current < ActiveSupport::CurrentAttributes
# app/models/current.rb
around everywhere:
facilitate easy access to the global, per-request attributes without passing them deeply
The following full app-like example demonstrates how to use a Current class to
available to the whole system.
before and after each request. This allows you to keep all the per-request attributes easily
Abstract super class that provides a thread-isolated attributes singleton, which resets automatically
= Current Attributes
def assign_attributes(new_attributes)
def assign_attributes(new_attributes) new_attributes.each { |key, value| public_send("#{key}=", value) } end
def attribute(*names)
def attribute(*names) invalid_attribute_names = names.map(&:to_sym) & INVALID_ATTRIBUTE_NAMES if invalid_attribute_names.any? raise ArgumentError, "Restricted attribute names: #{invalid_attribute_names.join(", ")}" end ActiveSupport::CodeGenerator.batch(generated_attribute_methods, __FILE__, __LINE__) do |owner| names.each do |name| owner.define_cached_method(name, namespace: :current_attributes) do |batch| batch << "def #{name}" << "attributes[:#{name}]" << "end" end owner.define_cached_method("#{name}=", namespace: :current_attributes) do |batch| batch << "def #{name}=(value)" << "attributes[:#{name}] = value" << "end" end end end ActiveSupport::CodeGenerator.batch(singleton_class, __FILE__, __LINE__) do |owner| names.each do |name| owner.define_cached_method(name, namespace: :current_attributes_delegation) do |batch| batch << "def #{name}" << "instance.#{name}" << "end" end owner.define_cached_method("#{name}=", namespace: :current_attributes_delegation) do |batch| batch << "def #{name}=(value)" << "instance.#{name} = value" << "end" end end end end
def before_reset(*methods, &block)
def before_reset(*methods, &block) set_callback :reset, :before, *methods, &block end
def clear_all # :nodoc:
def clear_all # :nodoc: reset_all current_instances.clear end
def compute_attributes(keys)
def compute_attributes(keys) keys.index_with { |key| public_send(key) } end
def current_instances
def current_instances IsolatedExecutionState[:current_attributes_instances] ||= {} end
def current_instances_key
def current_instances_key @current_instances_key ||= name.to_sym end
def generated_attribute_methods
def generated_attribute_methods @generated_attribute_methods ||= Module.new.tap { |mod| include mod } end
def initialize
def initialize @attributes = {} end
def instance
def instance current_instances[current_instances_key] ||= new end
def method_missing(name, *args, &block)
def method_missing(name, *args, &block) # Caches the method definition as a singleton method of the receiver. # # By letting #delegate handle it, we avoid an enclosure that'll capture args. singleton_class.delegate name, to: :instance send(name, *args, &block) end
def reset
def reset run_callbacks :reset do self.attributes = {} end end
def reset_all # :nodoc:
def reset_all # :nodoc: current_instances.each_value(&:reset) end
def resets(*methods, &block)
def resets(*methods, &block) set_callback :reset, :after, *methods, &block end
def respond_to_missing?(name, _)
def respond_to_missing?(name, _) super || instance.respond_to?(name) end
def set(set_attributes)
end
end
Chat::Publisher.publish(attributes: attributes, room_number: room_number)
Current.set(person: creator) do
def perform(attributes, room_number, creator)
class Chat::PublicationJob < ApplicationJob
Example demonstrating the common use of needing to set Current attributes outside the request-cycle:
Expose one or more attributes within a block. Old values are returned after the block concludes.
def set(set_attributes) old_attributes = compute_attributes(set_attributes.keys) assign_attributes(set_attributes) yield ensure assign_attributes(old_attributes) end