From 748b21db8ff850378b20a69ccd4215c06abea03e Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 17 Dec 2015 14:28:19 +0100 Subject: Add thread_m/cattr_accessor/reader/writer suite of methods for declaring class and module variables that live per-thread --- activesupport/CHANGELOG.md | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'activesupport/CHANGELOG.md') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 07f7fae5d5..386ef821a7 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,52 @@ +* Add thread_m/cattr_accessor/reader/writer suite of methods for declaring class and module variables that live per-thread. + This makes it easy to declare per-thread globals that are encapsulated. Note: This is a sharp edge. A wild proliferation + of globals is A Bad Thing. But like other sharp tools, when it's right, it's right. + + Here's an example of a simple event tracking system where the object being tracked needs not pass a creator that it + doesn't need itself along: + + module Current + thread_mattr_accessor :account + thread_mattr_accessor :user + + def self.reset() self.account = self.user = nil end + end + + class ApplicationController < ActiveController::Base + before_action :set_current + after_action { Current.reset } + + private + def set_current + Current.account = Account.find(params[:account_id]) + Current.person = Current.account.people.find(params[:person_id]) + end + end + + class MessagesController < ApplicationController + def create + @message = Message.create!(message_params) + end + end + + class Message < ApplicationRecord + has_many :events + after_create :track_created + + private + def track_created + events.create! origin: self, action: :create + end + end + + class Event < ApplicationRecord + belongs_to :creator, class_name: 'Person' + before_validation { self.creator ||= Current.person } + end + + *DHH* + + * Deprecated `Module#qualified_const_` in favour of the builtin Module#const_ methods. -- cgit v1.2.3 From b708a6050c640217e3431ff5de73fad91576705b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 17 Dec 2015 14:30:21 +0100 Subject: Use consistent references --- activesupport/CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activesupport/CHANGELOG.md') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 386ef821a7..70d671cd2d 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -19,7 +19,7 @@ private def set_current Current.account = Account.find(params[:account_id]) - Current.person = Current.account.people.find(params[:person_id]) + Current.user = Current.account.users.find(params[:user_id]) end end @@ -40,8 +40,8 @@ end class Event < ApplicationRecord - belongs_to :creator, class_name: 'Person' - before_validation { self.creator ||= Current.person } + belongs_to :creator, class_name: 'User' + before_validation { self.creator ||= Current.user } end *DHH* -- cgit v1.2.3