Skip to content

Commit 639d584

Browse files
committed
Introduce rubocop-thread_safety and fix issues
1 parent aa92189 commit 639d584

File tree

5 files changed

+14
-6
lines changed

5 files changed

+14
-6
lines changed

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require:
88
- rubocop-rspec_rails
99
- rubocop-factory_bot
1010
- rubocop-obsession
11+
- rubocop-thread_safety
1112

1213
AllCops:
1314
NewCops: enable

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ group :development do
5757
gem 'rubocop-rspec_rails', require: false
5858
gem 'rubocop-factory_bot', require: false
5959
gem 'rubocop-obsession', require: false
60+
gem 'rubocop-thread_safety', require: false
6061
gem 'bundler-audit', require: false
6162
gem 'brakeman', require: false
6263
gem 'lefthook', require: false

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ GEM
367367
rubocop-rspec (~> 3, >= 3.0.1)
368368
rubocop-shopify (2.15.1)
369369
rubocop (~> 1.51)
370+
rubocop-thread_safety (0.5.1)
371+
rubocop (>= 0.90.0)
370372
ruby-progressbar (1.13.0)
371373
ruby_parser (3.21.1)
372374
racc (~> 1.5)
@@ -465,6 +467,7 @@ DEPENDENCIES
465467
rubocop-rspec
466468
rubocop-rspec_rails
467469
rubocop-shopify
470+
rubocop-thread_safety
468471
selenium-webdriver
469472
simplecov
470473
solid_queue

app/graphql/concerns/object_helpers.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ module ObjectHelpers
55

66
class_methods do
77
def model
8-
@model ||= name.demodulize.constantize
8+
Thread.current["#{name}.model"] ||=
9+
name.demodulize.constantize
910
end
1011

1112
def association(association, ...)

app/graphql/objects/base.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@ class Base < GraphQL::Schema::Object
77
include ObjectHelpers
88

99
class << self
10-
def policy(policy)
11-
@policy = policy
10+
def policy(policy = nil)
11+
Thread.current["#{name}.policy"] ||= policy
1212
end
1313

1414
def authorized?(obj, ctx)
1515
super && pundit_authorized?(obj, ctx)
1616
end
1717

1818
def pundit_authorized?(obj, ctx)
19-
return true unless @policy
19+
return true unless policy
2020

2121
user = ctx[:current_user]
22-
policy = Pundit.policy(user, obj)
23-
policy.send(@policy)
22+
23+
Pundit
24+
.policy(user, obj)
25+
.send(policy)
2426
end
2527
end
2628
end

0 commit comments

Comments
 (0)