ActiveSupport::CurrentAttributes provides a thread-isolated attributes singleton

简介: ActiveSupport::CurrentAttributes provides a thread-isolated attributes singleton

使用场景:需要 persist 一些全局数据,如 request 中内容,user 之类

Abstract super class that provides a thread-isolated attributes singleton.

Primary use case is keeping all the per-request attributes easily 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 :set_current_authenticated_user
  end
  private
    def set_current_authenticated_user
      Current.user = User.find(cookies.signed[:user_id])
    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.

相关文章
|
数据采集 监控 数据可视化
Scrapy可视化管理管理工具总结
Scrapy可视化管理管理工具总结
1665 0
Scrapy可视化管理管理工具总结
WPF使用DataGridComboBoxColumn完成绑定
 在使用DataGrid的时候,有时候需要使某些列为ComboBox,这时自然想到使用DataGridComboBoxColumn,但是如果使用的是ItemsSource数据绑定后台的对象,就会发现,这根本就不能用。
2435 0
|
Java C++
C++的学习之路:21、继承(2)
C++的学习之路:21、继承(2)
126 0
|
存储 数据采集 大数据
Flink实时湖仓,为汽车行业数字化加速!
本文由阿里云计算平台产品专家李鲁兵(云觉)分享,聚焦汽车行业大数据应用。内容涵盖市场趋势、典型大数据架构、产品市场地位及能力解读,以及典型客户案例。文章详细介绍了新能源汽车市场的快速增长、大数据架构分析、实时湖仓方案的优势,以及Flink和Paimon在车联网中的应用案例。
732 8
Flink实时湖仓,为汽车行业数字化加速!
|
Kubernetes Cloud Native Docker
云原生之旅:从容器到微服务的架构演变
【8月更文挑战第29天】在数字化时代的浪潮下,云原生技术以其灵活性、可扩展性和弹性管理成为企业数字化转型的关键。本文将通过浅显易懂的语言和生动的比喻,带领读者了解云原生的基本概念,探索容器化技术的奥秘,并深入微服务架构的世界。我们将一起见证代码如何转化为现实中的服务,实现快速迭代和高效部署。无论你是初学者还是有经验的开发者,这篇文章都会为你打开一扇通往云原生世界的大门。
|
10月前
|
安全 数据安全/隐私保护
DzzOffice:太完美啦,开源免费Word、Exce、PPT,多人同时协作,最主要还有免费的网盘,将这个项目集成到你的产品里面,项目立刻拥有整套offce解决方案
嗨,大家好,我是小华同学。DzzOffice是一个免费开源的企业协同办公平台,适合中小型企业及团队使用,功能涵盖网盘、文档、表格、演示文稿等,支持企业微信和钉钉移动办公,保障数据私有部署安全。 关注我们,获取更多优质开源项目和高效工作学习方法。
1703 5
|
机器学习/深度学习 Python
删除指定文件夹重复的文件
这是一个Python脚本,用于删除指定文件夹(包括子目录)中的重复图片文件,基于文件的MD5值。程序依赖`NStudyPy`库,可通过`pip install -U NStudyPy`安装。核心函数`delete_repeat_file`接收路径和是否递归参数,调用未展示的`get_repeat_file`函数获取重复文件并删除。
217 1
|
机器学习/深度学习 算法 数据可视化
统计建模——模型——python为例
统计建模——模型——python为例
837 0
|
JavaScript 前端开发
vue学习第六章(条件显示)
欢迎来到我的博客!我是瑞雨溪,一名自学前端两年半的大一学生,擅长JavaScript与Vue,正向全栈进发。本篇介绍v-if、v-else、v-elseif及v-show的使用方法,附带实例演示。希望我的分享能帮到你,欢迎关注,持续更新中!🎉🎉🎉
113 1
|
Shell Linux
linux中的sh脚本语法
这些是Shell脚本的基本语法要点。Shell脚本还支持许多其他功能,如管道、重定向、通配符等,用于进行更复杂的操作。
481 0