跳至内容 跳至搜索

当前属性

抽象父类,提供了一个线程隔离的属性单例,该单例会在每次请求之前和之后自动重置。这允许您轻松地将每个请求的属性提供给整个系统。

以下一个完整的类比示例演示了如何使用 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
包含的模块

Attributes

[W] attributes

类公共方法

after_reset(*methods, &block)

别名: resets

attribute(*names, default: NOT_SET)

声明一个或多个将同时拥有类访问器和实例访问器方法的属性。

选项

  • :default - 属性的默认值。如果值为 proc 或 lambda,则在构造实例时会调用它。否则,值会通过 dup 进行复制。默认值在属性重置时重新赋值。

# File activesupport/lib/active_support/current_attributes.rb, line 115
def attribute(*names, default: NOT_SET)
  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

  Delegation.generate(singleton_class, names, to: :instance, nilable: false, signature: "")
  Delegation.generate(singleton_class, names.map { |n| "#{n}=" }, to: :instance, nilable: false, signature: "value")

  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

  self.defaults = defaults.merge(names.index_with { default })
end

before_reset(*methods, &block)

在实例上调用 reset 之前调用此回调。用于重置依赖于当前值的外部协作者。

# File activesupport/lib/active_support/current_attributes.rb, line 145
def before_reset(*methods, &block)
  set_callback :reset, :before, *methods, &block
end

instance()

在此线程中返回此类别的单例实例。如果不存在,则创建一个。

# File activesupport/lib/active_support/current_attributes.rb, line 103
def instance
  current_instances[current_instances_key] ||= new
end

new()

# File activesupport/lib/active_support/current_attributes.rb, line 205
def initialize
  @attributes = resolve_defaults
end

resets(*methods, &block)

在实例上调用 reset 后调用此回调。用于重置外部协作者,如 Time.zone

也别名化为: after_reset
# File activesupport/lib/active_support/current_attributes.rb, line 150
def resets(*methods, &block)
  set_callback :reset, :after, *methods, &block
end

实例公共方法

attributes()

# File activesupport/lib/active_support/current_attributes.rb, line 209
def attributes
  @attributes.dup
end

reset()

重置所有属性。当作为每个请求的单例使用时,应在操作之前和之后调用。

# File activesupport/lib/active_support/current_attributes.rb, line 228
def reset
  run_callbacks :reset do
    self.attributes = resolve_defaults
  end
end

set(attributes, &block)

在块内公开一个或多个属性。块结束后会返回旧值。示例演示了在请求周期外设置 Current 属性的常见用法

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
# File activesupport/lib/active_support/current_attributes.rb, line 223
def set(attributes, &block)
  with(**attributes, &block)
end