]> git.openstreetmap.org Git - rails.git/commitdiff
Merge pull request #4485 from tomhughes/drop-lower-index
authorAndy Allan <git@gravitystorm.co.uk>
Thu, 18 Jan 2024 10:49:52 +0000 (10:49 +0000)
committerGitHub <noreply@github.com>
Thu, 18 Jan 2024 10:49:52 +0000 (10:49 +0000)
Drop lowercase index on display names

.github/workflows/lint.yml
.github/workflows/tests.yml
.rubocop_todo.yml
app/models/user.rb
config/locales/en.yml
test/models/user_test.rb

index 0608b699bdfdcc56bb6f3f10087762b3546e3140..9fc1320141d9f50d0ac19a5940bc48fdbe9dd87f 100644 (file)
@@ -50,7 +50,7 @@ jobs:
         rubygems: 3.4.10
         bundler-cache: true
     - name: Cache node modules
-      uses: actions/cache@v3
+      uses: actions/cache@v4
       with:
         path: node_modules
         key: yarn-${{ env.os }}-${{ hashFiles('yarn.lock') }}
index f2fa6763aa55a03af02acbfc6dc0f7fd42a3ca24..bfe456076df761798ec9a1e15e6674691b9f0e06 100644 (file)
@@ -26,7 +26,7 @@ jobs:
         rubygems: 3.4.10
         bundler-cache: true
     - name: Cache node modules
-      uses: actions/cache@v3
+      uses: actions/cache@v4
       with:
         path: node_modules
         key: yarn-ubuntu-${{ matrix.ubuntu }}-${{ hashFiles('yarn.lock') }}
index 8a196f52e0317b45cc56b4c4ca856e4d353efa97..9874aa3793a18cb1e65b2e9f7374ce6eabeeaeee 100644 (file)
@@ -66,7 +66,7 @@ Metrics/BlockNesting:
 # Offense count: 26
 # Configuration parameters: CountComments, CountAsOne.
 Metrics/ClassLength:
-  Max: 299
+  Max: 305
 
 # Offense count: 59
 # Configuration parameters: AllowedMethods, AllowedPatterns.
index 8ed6d4465f8687d2c17434770a56967d1da9228f..7faf748cd8afdb5866af50915e8dcd033a8dd2c2 100644 (file)
@@ -99,6 +99,7 @@ class User < ApplicationRecord
   validates :display_name, :if => proc { |u| u.display_name_changed? },
                            :characters => { :url_safe => true },
                            :whitespace => { :leading => false, :trailing => false }
+  validate :display_name_cannot_be_user_id_with_other_id, :if => proc { |u| u.display_name_changed? }
   validates :email, :presence => true, :confirmation => true, :characters => true
   validates :email, :if => proc { |u| u.email_changed? },
                     :uniqueness => { :case_sensitive => false }
@@ -123,6 +124,12 @@ class User < ApplicationRecord
   before_save :update_tile
   after_save :spam_check
 
+  def display_name_cannot_be_user_id_with_other_id
+    display_name&.match(/^user_(\d+)$/i) do |m|
+      errors.add :display_name, I18n.t("activerecord.errors.messages.display_name_is_user_n") unless m[1].to_i == id
+    end
+  end
+
   def to_param
     display_name
   end
index 653e363a67bcfb6d39927c5df0c99457a4e09192..ca22de9bde23f9c723608dabf6feb4b02ad679c3 100644 (file)
@@ -40,6 +40,7 @@ en:
       messages:
         invalid_email_address: does not appear to be a valid e-mail address
         email_address_not_routable: is not routable
+        display_name_is_user_n: can't be user_n unless n is your user id
       models:
         user_mute:
           attributes:
index d21512f2a1861d317c13664f5ebcbc7ef17d3f39..42949504f633925b8a6473cd203972ab944d9a71 100644 (file)
@@ -94,6 +94,36 @@ class UserTest < ActiveSupport::TestCase
     end
   end
 
+  def test_display_name_user_id_new
+    existing_user = create(:user)
+    user = build(:user)
+
+    user.display_name = "user_#{existing_user.id}"
+    assert_not user.valid?, "user_<id> name is valid for existing user id when it shouldn't be"
+
+    user.display_name = "user_#{existing_user.id + 1}"
+    assert_not user.valid?, "user_<id> name is valid for new user id when it shouldn't be"
+  end
+
+  def test_display_name_user_id_rename
+    existing_user = create(:user)
+    user = create(:user)
+
+    user.display_name = "user_#{existing_user.id}"
+    assert_not user.valid?, "user_<id> name is valid for existing user id when it shouldn't be"
+
+    user.display_name = "user_#{user.id}"
+    assert_predicate user, :valid?, "user_<id> name is invalid for own id, when it should be"
+  end
+
+  def test_display_name_user_id_unchanged_is_valid
+    user = build(:user, :display_name => "user_0")
+    user.save(:validate => false)
+    user.reload
+
+    assert_predicate user, :valid?, "user_0 display_name is invalid but it hasn't been changed"
+  end
+
   def test_friends_with
     alice = create(:user, :active)
     bob = create(:user, :active)