当前属性
提供线程隔离属性单例的抽象超类,该单例在每次请求之前和之后会自动重置。这使您可以轻松地将所有每请求属性提供给整个系统。
以下完整的应用程序示例演示了如何使用 Current 类来简化对全局每请求属性的访问,而无需将它们深度传递到各个地方。
# 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
注意:过度使用像 Current 这样的全局单例很容易,从而导致模型变得混乱。Current 应该只用于少数顶级全局变量,例如帐户、用户和请求详细信息。Current 中的属性应该被所有请求上的大多数操作使用。如果您开始在其中放置特定于控制器的属性,您将制造混乱。
- A
- B
- I
- N
- R
- S
属性
[RW] | attributes |
类公共方法
attribute(*names) 链接
声明一个或多个属性,这些属性将被赋予类和实例访问器方法。
源代码:显示 | 在 GitHub 上
# File activesupport/lib/active_support/current_attributes.rb, line 104 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
before_reset(*methods, &block) 链接
在 reset
被实例调用之前调用此回调。用于重置依赖于当前值的外部协作者。
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/current_attributes.rb, line 146 def before_reset(*methods, &block) set_callback :reset, :before, *methods, &block end
instance() 链接
返回此类在当前线程中的单例实例。如果不存在,则创建一个。
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/current_attributes.rb, line 99 def instance current_instances[current_instances_key] ||= new end
new() 链接
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/current_attributes.rb, line 197 def initialize @attributes = {} end
resets(*methods, &block) 链接
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/current_attributes.rb, line 151 def resets(*methods, &block) set_callback :reset, :after, *methods, &block end
实例公共方法
reset() 链接
重置所有属性。在作为每个请求的单例使用时,应在操作之前和之后调用。
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/current_attributes.rb, line 220 def reset run_callbacks :reset do self.attributes = {} end end
set(set_attributes) 链接
在块中公开一个或多个属性。块结束后的旧值将被返回。示例演示了在请求周期之外设置当前属性的常见用法
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 211 def set(set_attributes) old_attributes = compute_attributes(set_attributes.keys) assign_attributes(set_attributes) yield ensure assign_attributes(old_attributes) end