2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2018-10-20 01:22:02 -04:00
|
|
|
require "active_support/callbacks"
|
2020-03-04 20:24:14 -05:00
|
|
|
require "active_support/core_ext/enumerable"
|
2018-10-20 01:22:02 -04:00
|
|
|
|
2017-05-26 14:00:27 -04:00
|
|
|
module ActiveSupport
|
|
|
|
# Abstract super class that provides a thread-isolated attributes singleton, which resets automatically
|
2017-05-27 08:34:53 -04:00
|
|
|
# before and after each request. This allows you to keep all the per-request attributes easily
|
2017-05-26 14:00:27 -04:00
|
|
|
# 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
|
2017-08-07 23:32:03 -04:00
|
|
|
# if authenticated_user = User.find_by(id: cookies.encrypted[:user_id])
|
2017-05-26 14:00:27 -04:00
|
|
|
# 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.
|
|
|
|
class CurrentAttributes
|
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
define_callbacks :reset
|
|
|
|
|
|
|
|
class << self
|
|
|
|
# Returns singleton instance for this class in this thread. If none exists, one is created.
|
|
|
|
def instance
|
2019-08-19 13:05:56 -04:00
|
|
|
current_instances[current_instances_key] ||= new
|
2017-05-26 14:00:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Declares one or more attributes that will be given both class and instance accessor methods.
|
|
|
|
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
|
|
|
|
|
2019-01-27 05:18:40 -05:00
|
|
|
# Calls this block before #reset is called on the instance. Used for resetting external collaborators that depend on current values.
|
|
|
|
def before_reset(&block)
|
|
|
|
set_callback :reset, :before, &block
|
|
|
|
end
|
|
|
|
|
2017-05-26 14:00:27 -04:00
|
|
|
# Calls this block after #reset is called on the instance. Used for resetting external collaborators, like Time.zone.
|
|
|
|
def resets(&block)
|
|
|
|
set_callback :reset, :after, &block
|
|
|
|
end
|
2019-01-27 05:18:40 -05:00
|
|
|
alias_method :after_reset, :resets
|
2017-05-26 14:00:27 -04:00
|
|
|
|
|
|
|
delegate :set, :reset, to: :instance
|
|
|
|
|
|
|
|
def reset_all # :nodoc:
|
2017-05-28 04:12:16 -04:00
|
|
|
current_instances.each_value(&:reset)
|
2017-05-26 14:00:27 -04:00
|
|
|
end
|
|
|
|
|
2017-05-28 04:47:17 -04:00
|
|
|
def clear_all # :nodoc:
|
|
|
|
reset_all
|
|
|
|
current_instances.clear
|
|
|
|
end
|
|
|
|
|
2017-05-26 14:00:27 -04:00
|
|
|
private
|
|
|
|
def generated_attribute_methods
|
|
|
|
@generated_attribute_methods ||= Module.new.tap { |mod| include mod }
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_instances
|
2017-05-28 04:12:16 -04:00
|
|
|
Thread.current[:current_attributes_instances] ||= {}
|
2017-05-26 14:00:27 -04:00
|
|
|
end
|
|
|
|
|
2019-08-19 13:05:56 -04:00
|
|
|
def current_instances_key
|
|
|
|
@current_instances_key ||= name.to_sym
|
|
|
|
end
|
|
|
|
|
2017-05-26 14:00:27 -04:00
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :attributes
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@attributes = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|
|
|
|
def set(set_attributes)
|
|
|
|
old_attributes = compute_attributes(set_attributes.keys)
|
|
|
|
assign_attributes(set_attributes)
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
assign_attributes(old_attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Reset all attributes. Should be called before and after actions, when used as a per-request singleton.
|
|
|
|
def reset
|
|
|
|
run_callbacks :reset do
|
|
|
|
self.attributes = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def assign_attributes(new_attributes)
|
|
|
|
new_attributes.each { |key, value| public_send("#{key}=", value) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def compute_attributes(keys)
|
2020-03-04 20:24:14 -05:00
|
|
|
keys.index_with { |key| public_send(key) }
|
2017-05-26 14:00:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|