From 3389c41b5899c1da479bfc08e84588184e09902d Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Fri, 1 Mar 2024 06:05:24 -0500 Subject: [PATCH] Move `nobody` position in `UserRole` magic number to constant (#29465) --- app/models/user_role.rb | 5 +++-- spec/models/user_role_spec.rb | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/models/user_role.rb b/app/models/user_role.rb index 9115d91c24..23cc28b9b7 100644 --- a/app/models/user_role.rb +++ b/app/models/user_role.rb @@ -39,6 +39,7 @@ class UserRole < ApplicationRecord }.freeze EVERYONE_ROLE_ID = -99 + NOBODY_POSITION = -1 module Flags NONE = 0 @@ -104,7 +105,7 @@ class UserRole < ApplicationRecord has_many :users, inverse_of: :role, foreign_key: 'role_id', dependent: :nullify def self.nobody - @nobody ||= UserRole.new(permissions: Flags::NONE, position: -1) + @nobody ||= UserRole.new(permissions: Flags::NONE, position: NOBODY_POSITION) end def self.everyone @@ -173,7 +174,7 @@ class UserRole < ApplicationRecord end def set_position - self.position = -1 if everyone? + self.position = NOBODY_POSITION if everyone? end def validate_own_role_edition diff --git a/spec/models/user_role_spec.rb b/spec/models/user_role_spec.rb index 9dd04a8172..96d12263ae 100644 --- a/spec/models/user_role_spec.rb +++ b/spec/models/user_role_spec.rb @@ -139,7 +139,7 @@ RSpec.describe UserRole do end it 'has negative position' do - expect(subject.position).to eq(-1) + expect(subject.position).to eq(described_class::NOBODY_POSITION) end end @@ -159,7 +159,7 @@ RSpec.describe UserRole do end it 'has negative position' do - expect(subject.position).to eq(-1) + expect(subject.position).to eq(described_class::NOBODY_POSITION) end end