From 22caddaa246fd4e8de4be8b422c31fc8e96357dc Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Thu, 19 Dec 2024 03:10:39 -0500 Subject: [PATCH] Update user role position range limit (#33349) --- app/models/user_role.rb | 4 ++-- spec/models/user_role_spec.rb | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/app/models/user_role.rb b/app/models/user_role.rb index f3edbf811e..f9c4c14c4b 100644 --- a/app/models/user_role.rb +++ b/app/models/user_role.rb @@ -41,7 +41,7 @@ class UserRole < ApplicationRecord EVERYONE_ROLE_ID = -99 NOBODY_POSITION = -1 - POSITION_LIMIT = 2**31 + POSITION_LIMIT = (2**31) - 1 module Flags NONE = 0 @@ -91,7 +91,7 @@ class UserRole < ApplicationRecord validates :name, presence: true, unless: :everyone? validates :color, format: { with: /\A#?(?:[A-F0-9]{3}){1,2}\z/i }, unless: -> { color.blank? } - validates :position, numericality: { greater_than_or_equal_to: -POSITION_LIMIT, less_than_or_equal_to: POSITION_LIMIT } + validates :position, numericality: { in: (-POSITION_LIMIT..POSITION_LIMIT) } validate :validate_permissions_elevation validate :validate_position_elevation diff --git a/spec/models/user_role_spec.rb b/spec/models/user_role_spec.rb index 8fc09a2963..48768719a1 100644 --- a/spec/models/user_role_spec.rb +++ b/spec/models/user_role_spec.rb @@ -21,11 +21,9 @@ RSpec.describe UserRole do describe 'position' do subject { Fabricate.build :user_role } - let(:excess) { 2**32 } - let(:limit) { 2**31 } + let(:limit) { described_class::POSITION_LIMIT } - it { is_expected.to_not allow_values(-excess, excess).for(:position) } - it { is_expected.to allow_values(-limit, limit).for(:position) } + it { is_expected.to validate_numericality_of(:position).is_in(-limit..limit) } end describe 'color' do