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
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) 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
def before_reset(&block)
def before_reset(&block) set_callback :reset, :before, &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.collect { |key| [ key, public_send(key) ] }.to_h end
def current_instances
def current_instances Thread.current[:current_attributes_instances] ||= {} 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[name] ||= 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(&block)
def resets(&block) set_callback :reset, :after, &block 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