Skip to content

Commit

Permalink
Replace RequestStore with ActiveSupport::CurrentAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
the-spectator committed Apr 30, 2023
1 parent 379aeed commit f8416b1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion audited.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |gem|
gem.required_ruby_version = ">= 2.3.0"

gem.add_dependency "activerecord", ">= 5.0", "< 7.1"
gem.add_dependency "request_store", "~> 1.2"
gem.add_dependency "activesupport", ">= 5.0", "< 7.1"

gem.add_development_dependency "appraisal"
gem.add_development_dependency "rails", ">= 5.0", "< 7.1"
Expand Down
4 changes: 2 additions & 2 deletions lib/audited.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require "active_record"
require "request_store"

module Audited
class << self
Expand All @@ -25,7 +24,7 @@ def audit_class
deprecate audit_model: "use Audited.audit_class instead of Audited.audit_model. This method will be removed."

def store
RequestStore.store[:audited_store] ||= {}
Audited::RequestStore.audited_store ||= {}
end

def config
Expand All @@ -41,6 +40,7 @@ def config
end

require "audited/auditor"
require "audited/request_store"

ActiveSupport.on_load :active_record do
require "audited/audit"
Expand Down
9 changes: 9 additions & 0 deletions lib/audited/request_store.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require "active_support"

module Audited
class RequestStore < ActiveSupport::CurrentAttributes
attribute :audited_store
end
end
6 changes: 5 additions & 1 deletion spec/audited_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
describe Audited do
describe "#store" do
describe "maintains state of store" do
let(:current_user) { RequestStore.store[:audited_store] }
let(:current_user) { Audited::RequestStore.audited_store }
before { Audited.store[:current_user] = current_user }

it "checks store is not nil" do
expect(Audited.store[:current_user]).to eq(current_user)
end

it "when executed with Fibers" do
Fiber.new { expect(Audited.store[:current_user]).to eq(current_user) }.resume
end
end
end
end

0 comments on commit f8416b1

Please sign in to comment.