Skip to content

Commit aa92189

Browse files
committed
Add rubocop-obsession
1 parent a52c6e9 commit aa92189

File tree

10 files changed

+70
-59
lines changed

10 files changed

+70
-59
lines changed

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require:
77
- rubocop-rspec
88
- rubocop-rspec_rails
99
- rubocop-factory_bot
10+
- rubocop-obsession
1011

1112
AllCops:
1213
NewCops: enable
@@ -27,6 +28,9 @@ AllCops:
2728
FactoryBot/ExcessiveCreateList:
2829
Enabled: false
2930

31+
Obsession/Graphql/MutationName:
32+
Enabled: false
33+
3034
RSpec/ExampleLength:
3135
Enabled: false
3236

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ group :development do
5656
gem 'rubocop-rspec', require: false
5757
gem 'rubocop-rspec_rails', require: false
5858
gem 'rubocop-factory_bot', require: false
59+
gem 'rubocop-obsession', require: false
5960
gem 'bundler-audit', require: false
6061
gem 'brakeman', require: false
6162
gem 'lefthook', require: false

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,9 @@ GEM
349349
parser (>= 3.3.1.0)
350350
rubocop-factory_bot (2.26.1)
351351
rubocop (~> 1.61)
352+
rubocop-obsession (0.1.5)
353+
activesupport
354+
rubocop (~> 1.41)
352355
rubocop-performance (1.22.1)
353356
rubocop (>= 1.48.1, < 2.0)
354357
rubocop-ast (>= 1.31.1, < 2.0)
@@ -456,6 +459,7 @@ DEPENDENCIES
456459
redis
457460
rspec-rails
458461
rubocop-factory_bot
462+
rubocop-obsession
459463
rubocop-performance
460464
rubocop-rails
461465
rubocop-rspec

app/channels/application_cable/connection.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ def jwt_payload
2222
@jwt_payload ||= jwt_token&.first
2323
end
2424

25-
def auth_token
26-
@auth_token ||= begin
27-
auth_token = request.headers['Authorization']
28-
auth_token || cookies.signed['jwt_token']
29-
end
30-
end
31-
3225
def jwt_token
3326
@jwt_token ||=
3427
if auth_token
3528
jwt_token = auth_token.remove('Bearer ')
3629
JwtToken.decode(jwt_token)
3730
end
3831
end
32+
33+
def auth_token
34+
@auth_token ||= begin
35+
auth_token = request.headers['Authorization']
36+
auth_token || cookies.signed['jwt_token']
37+
end
38+
end
3939
end
4040
end

app/controllers/graphql_controller.rb

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,22 @@ def execute
2222

2323
private
2424

25-
def current_user
26-
@current_user ||=
25+
def update_session
26+
current_session&.mark_visit!
27+
end
28+
29+
def current_session
30+
@current_session ||=
2731
if jwt_payload
28-
uuid = jwt_payload.dig('user', 'uuid')
29-
User.find_by(uuid: uuid)
32+
jti = jwt_payload['jti']
33+
Session.find_by(jwt_id: jti)
3034
end
3135
end
3236

3337
def jwt_payload
3438
@jwt_payload ||= jwt_token&.first
3539
end
3640

37-
def auth_token
38-
@auth_token ||= begin
39-
auth_token = request.headers['Authorization']
40-
auth_token || cookies.signed['jwt_token']
41-
end
42-
end
43-
4441
def jwt_token
4542
@jwt_token ||=
4643
if auth_token
@@ -49,25 +46,11 @@ def jwt_token
4946
end
5047
end
5148

52-
def current_session
53-
@current_session ||=
54-
if jwt_payload
55-
jti = jwt_payload['jti']
56-
Session.find_by(jwt_id: jti)
57-
end
58-
end
59-
60-
def update_session
61-
current_session&.mark_visit!
62-
end
63-
64-
def invalid_jwt
65-
render json: {
66-
errors: [{
67-
code: 401,
68-
message: 'Invalid JWT Token',
69-
}],
70-
}
49+
def auth_token
50+
@auth_token ||= begin
51+
auth_token = request.headers['Authorization']
52+
auth_token || cookies.signed['jwt_token']
53+
end
7154
end
7255

7356
def schema_options
@@ -100,6 +83,23 @@ def ensure_hash(ambiguous_param)
10083
end
10184
end
10285

86+
def current_user
87+
@current_user ||=
88+
if jwt_payload
89+
uuid = jwt_payload.dig('user', 'uuid')
90+
User.find_by(uuid: uuid)
91+
end
92+
end
93+
94+
def invalid_jwt
95+
render json: {
96+
errors: [{
97+
code: 401,
98+
message: 'Invalid JWT Token',
99+
}],
100+
}
101+
end
102+
103103
def handle_error_in_development(err)
104104
logger.error(err.message)
105105
logger.error(err.backtrace.join("\n"))

app/graphql/loaders/association.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ def validate
4141
raise ArgumentError, "No association #{@association_name} on #{@model}"
4242
end
4343

44-
def preload_association(records)
45-
::ActiveRecord::Associations::Preloader.new(records: records, associations: @association_name).call
44+
def association_loaded?(record)
45+
record.association(@association_name).loaded?
4646
end
4747

4848
def read_association(record)
4949
record.public_send(@association_name)
5050
end
5151

52-
def association_loaded?(record)
53-
record.association(@association_name).loaded?
52+
def preload_association(records)
53+
::ActiveRecord::Associations::Preloader.new(records: records, associations: @association_name).call
5454
end
5555
end
5656
end

app/models/session.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ class Session < ApplicationRecord
2727

2828
belongs_to :user
2929

30-
before_validation :populate_jwt_id
31-
before_validation :populate_expiration
32-
3330
validates :jwt_id, presence: true
3431
validates :expires_at, presence: true
3532

33+
before_validation :populate_jwt_id
34+
before_validation :populate_expiration
35+
3636
def mark_visit!
3737
# rubocop:disable Rails/SkipsModelValidations
3838
update_column(:last_access_at, Time.zone.now)

app/models/user.rb

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ class User < ApplicationRecord
4040
has_many :sessions, dependent: :destroy
4141
has_many :posts, dependent: :destroy
4242

43-
before_validation :process_otp_action
44-
4543
validates :email, presence: true, email: true, uniqueness: true
4644
validates :name, presence: true
4745
validates :password, format: { with: PASSWORD_FORMAT }, allow_blank: true
4846

47+
before_validation :process_otp_action
48+
4949
def save_password_reset_token
5050
generate_unique_token(:password_reset_token)
5151
self.password_reset_sent_at = Time.zone.now
@@ -60,17 +60,6 @@ def otp_action=(input)
6060
@otp_action = input.to_h
6161
end
6262

63-
def process_otp_action
64-
# otp_action = nil OR {} OR {enable:, code:}
65-
return if @otp_action.blank?
66-
67-
if @otp_action[:enable]
68-
enable_otp
69-
else
70-
disable_otp
71-
end
72-
end
73-
7463
def enable_otp
7564
code = @otp_action[:code]
7665
if authenticate_otp(code, drift: 60)
@@ -89,4 +78,17 @@ def disable_otp
8978
errors.add(:otp_code)
9079
end
9180
end
81+
82+
private
83+
84+
def process_otp_action
85+
# otp_action = nil OR {} OR {enable:, code:}
86+
return if @otp_action.blank?
87+
88+
if @otp_action[:enable]
89+
enable_otp
90+
else
91+
disable_otp
92+
end
93+
end
9294
end

db/migrate/20201205004037_create_sessions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class CreateSessions < ActiveRecord::Migration[6.0]
44
def change
55
create_table :sessions do |t|
6-
t.references :user, null: false, foreign_key: true
6+
t.belongs_to :user, null: false, foreign_key: true
77
t.string :jwt_id, null: false
88

99
t.timestamps

db/migrate/20201205020514_add_user_id_to_posts.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
class AddUserIdToPosts < ActiveRecord::Migration[6.0]
44
def change
5-
add_reference :posts, :user, foreign_key: true
5+
add_belongs_to :posts, :user, foreign_key: true
66
end
77
end

0 commit comments

Comments
 (0)