Skip to content

Commit

Permalink
Merge pull request #673 from the-spectator/migrate_to_current_attributes
Browse files Browse the repository at this point in the history
Replace RequestStore with `ActiveSupport::CurrentAttributes`
  • Loading branch information
danielmorrison authored Nov 6, 2023
2 parents 9a1bed7 + 626a97b commit 7e881fb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
6 changes: 3 additions & 3 deletions audited.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ Gem::Specification.new do |gem|

gem.required_ruby_version = ">= 2.3.0"

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

gem.add_development_dependency "appraisal"
gem.add_development_dependency "rails", ">= 5.0", "< 7.1"
gem.add_development_dependency "rails", ">= 5.0", "< 7.7"
gem.add_development_dependency "rspec-rails"
gem.add_development_dependency "standard"
gem.add_development_dependency "single_cov"
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 @@ -26,7 +25,7 @@ def audit_class
deprecator: ActiveSupport::Deprecation.new('6.0.0', 'Audited')

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

def config
Expand All @@ -42,6 +41,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 7e881fb

Please sign in to comment.