diff --git a/app/controllers/admin/timeline_controller.rb b/app/controllers/admin/timeline_controller.rb index 846a2d55..64ae526c 100644 --- a/app/controllers/admin/timeline_controller.rb +++ b/app/controllers/admin/timeline_controller.rb @@ -135,7 +135,7 @@ def show # For Stimulus: provide initial selected users with details @initial_selected_user_objects = User.where(id: @selected_user_ids) - .select(:id, :custom_name, :slack_username, :github_username, :slack_avatar_url, :github_avatar_url) + .select(:id, :username, :slack_username, :github_username, :slack_avatar_url, :github_avatar_url) .map { |u| { id: u.id, display_name: "#{u.display_name}", avatar_url: u.avatar_url } } .sort_by { |u_obj| @selected_user_ids.index(u_obj[:id]) || Float::INFINITY } # Preserve order @@ -185,10 +185,10 @@ def search_users avatar_url: user_id_match.avatar_url } ] else - users = User.where("LOWER(custom_name) LIKE :query OR LOWER(slack_username) LIKE :query OR CAST(id AS TEXT) LIKE :query OR EXISTS (SELECT 1 FROM email_addresses WHERE email_addresses.user_id = users.id AND LOWER(email_addresses.email) LIKE :query)", query: "%#{query_term}%") - .order(Arel.sql("CASE WHEN LOWER(custom_name) = #{ActiveRecord::Base.connection.quote(query_term)} THEN 0 ELSE 1 END, username ASC")) # Prioritize exact match + users = User.where("LOWER(username) LIKE :query OR LOWER(slack_username) LIKE :query OR CAST(id AS TEXT) LIKE :query OR EXISTS (SELECT 1 FROM email_addresses WHERE email_addresses.user_id = users.id AND LOWER(email_addresses.email) LIKE :query)", query: "%#{query_term}%") + .order(Arel.sql("CASE WHEN LOWER(username) = #{ActiveRecord::Base.connection.quote(query_term)} THEN 0 ELSE 1 END, username ASC")) # Prioritize exact match .limit(20) - .select(:id, :custom_name, :slack_username, :github_username, :slack_avatar_url, :github_avatar_url) + .select(:id, :username, :slack_username, :github_username, :slack_avatar_url, :github_avatar_url) results = users.map do |user| { @@ -218,7 +218,7 @@ def leaderboard_users all_ids_to_fetch.unshift(current_user.id).uniq! users_data = User.where(id: all_ids_to_fetch) - .select(:id, :custom_name, :slack_username, :github_username, :slack_avatar_url, :github_avatar_url) + .select(:id, :username, :slack_username, :github_username, :slack_avatar_url, :github_avatar_url) .index_by(&:id) final_user_objects = [] diff --git a/app/controllers/admin/trust_level_audit_logs_controller.rb b/app/controllers/admin/trust_level_audit_logs_controller.rb index 60959f55..a9257983 100644 --- a/app/controllers/admin/trust_level_audit_logs_controller.rb +++ b/app/controllers/admin/trust_level_audit_logs_controller.rb @@ -25,7 +25,7 @@ def index if params[:user_search].present? search_term = params[:user_search].strip user_ids = User.joins(:email_addresses) - .where("LOWER(users.custom_name) LIKE ? OR LOWER(users.slack_username) LIKE ? OR LOWER(users.github_username) LIKE ? OR LOWER(email_addresses.email) LIKE ? OR CAST(users.id AS TEXT) LIKE ?", + .where("LOWER(users.username) LIKE ? OR LOWER(users.slack_username) LIKE ? OR LOWER(users.github_username) LIKE ? OR LOWER(email_addresses.email) LIKE ? OR CAST(users.id AS TEXT) LIKE ?", "%#{search_term.downcase}%", "%#{search_term.downcase}%", "%#{search_term.downcase}%", "%#{search_term.downcase}%", "%#{search_term}%") .pluck(:id) @audit_logs = @audit_logs.where(user_id: user_ids) @@ -35,7 +35,7 @@ def index if params[:admin_search].present? search_term = params[:admin_search].strip admin_ids = User.joins(:email_addresses) - .where("LOWER(users.custom_name) LIKE ? OR LOWER(users.slack_username) LIKE ? OR LOWER(users.github_username) LIKE ? OR LOWER(email_addresses.email) LIKE ? OR CAST(users.id AS TEXT) LIKE ?", + .where("LOWER(users.username) LIKE ? OR LOWER(users.slack_username) LIKE ? OR LOWER(users.github_username) LIKE ? OR LOWER(email_addresses.email) LIKE ? OR CAST(users.id AS TEXT) LIKE ?", "%#{search_term.downcase}%", "%#{search_term.downcase}%", "%#{search_term.downcase}%", "%#{search_term.downcase}%", "%#{search_term}%") .pluck(:id) @audit_logs = @audit_logs.where(changed_by_id: admin_ids) diff --git a/app/controllers/api/admin/v1/admin_controller.rb b/app/controllers/api/admin/v1/admin_controller.rb index 42afaf27..14f9da24 100644 --- a/app/controllers/api/admin/v1/admin_controller.rb +++ b/app/controllers/api/admin/v1/admin_controller.rb @@ -15,7 +15,7 @@ def check }, creator: { id: creator.id, - username: creator.custom_name, + username: creator.username, display_name: creator.display_name, admin_level: creator.admin_level } @@ -75,7 +75,7 @@ def user_info render json: { user: { id: user.id, - username: user.custom_name, + username: user.username, display_name: user.display_name, slack_uid: user.slack_uid, slack_username: user.slack_username, diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 6a450b6d..df3d0d9a 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -153,7 +153,7 @@ def user_params :timezone, :allow_public_stats_lookup, :default_timezone_leaderboard, - :custom_name, + :username, ) end end diff --git a/app/models/user.rb b/app/models/user.rb index 94514547..ab28023e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,23 +2,23 @@ class User < ApplicationRecord include TimezoneRegions include PublicActivity::Model - CUSTOM_NAME_MAX_LENGTH = 21 # going over 21 overflows the navbar + USERNAME_MAX_LENGTH = 21 # going over 21 overflows the navbar has_paper_trail after_create :create_signup_activity - before_validation :normalize_custom_name + before_validation :normalize_username encrypts :slack_access_token, :github_access_token validates :slack_uid, uniqueness: true, allow_nil: true validates :github_uid, uniqueness: { conditions: -> { where.not(github_access_token: nil) } }, allow_nil: true validates :timezone, inclusion: { in: TZInfo::Timezone.all_identifiers }, allow_nil: false validates :country_code, inclusion: { in: ISO3166::Country.codes }, allow_nil: true - validates :custom_name, - length: { maximum: CUSTOM_NAME_MAX_LENGTH }, + validates :username, + length: { maximum: USERNAME_MAX_LENGTH }, format: { with: /\A[A-Za-z0-9_-]+\z/, message: "may only include letters, numbers, '-', and '_'" }, allow_nil: true - validate :custom_name_must_be_visible + validate :username_must_be_visible attribute :allow_public_stats_lookup, :boolean, default: true attribute :default_timezone_leaderboard, :boolean, default: true @@ -468,7 +468,7 @@ def avatar_url end def display_name - name = custom_name || slack_username || github_username + name = username || slack_username || github_username return name if name.present? # "zach@hackclub.com" -> "zach (email sign-up)" @@ -517,9 +517,9 @@ def create_signup_activity create_activity :first_signup, owner: self end - def normalize_custom_name - original = custom_name - @custom_name_cleared_for_invisible = false + def normalize_username + original = username + @username_cleared_for_invisible = false return if original.nil? @@ -527,16 +527,16 @@ def normalize_custom_name stripped = cleaned.strip if stripped.empty? - self.custom_name = nil - @custom_name_cleared_for_invisible = original.length.positive? + self.username = nil + @username_cleared_for_invisible = original.length.positive? else - self.custom_name = stripped + self.username = stripped end end - def custom_name_must_be_visible - if instance_variable_defined?(:@custom_name_cleared_for_invisible) && @custom_name_cleared_for_invisible - errors.add(:custom_name, "must include visible characters") + def username_must_be_visible + if instance_variable_defined?(:@username_cleared_for_invisible) && @username_cleared_for_invisible + errors.add(:username, "must include visible characters") end end end diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index 73b0ae44..039d8f19 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -73,24 +73,24 @@
<%= @user.errors[:custom_name].to_sentence %>
+ maxlength: User::USERNAME_MAX_LENGTH %> + <% if @user.errors[:username].present? %> +<%= @user.errors[:username].to_sentence %>
<% end %>- Choose a name to use in Hackatime. This will take priority over Slack or GitHub names when possible. Letters, numbers, "-" and "_" only, max <%= User::CUSTOM_NAME_MAX_LENGTH %> chars. + Choose a name to use in Hackatime. This will take priority over Slack or GitHub names when possible. Letters, numbers, "-" and "_" only, max <%= User::USERNAME_MAX_LENGTH %> chars.
<%= f.submit "Save Settings", class: "w-full px-4 py-2 bg-primary text-white font-medium rounded transition-colors duration-200 cursor-pointer" %> <% end %> diff --git a/db/migrate/20251021211039_rename_custom_username.rb b/db/migrate/20251021211039_rename_custom_username.rb new file mode 100644 index 00000000..2ad02cd8 --- /dev/null +++ b/db/migrate/20251021211039_rename_custom_username.rb @@ -0,0 +1,5 @@ +class RenameCustomUsername < ActiveRecord::Migration[8.0] + def change + rename_column :users, :custom_name, :username + end +end diff --git a/db/schema.rb b/db/schema.rb index 135c13a0..2a7bd550 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2025_10_21_202329) do +ActiveRecord::Schema[8.0].define(version: 2025_10_21_211039) do create_schema "pganalyze" # These are extensions that must be enabled in order to support this database @@ -562,7 +562,7 @@ t.boolean "allow_public_stats_lookup", default: true, null: false t.boolean "default_timezone_leaderboard", default: true, null: false t.integer "admin_level", default: 0, null: false - t.string "custom_name" + t.string "username" t.index ["github_uid", "github_access_token"], name: "index_users_on_github_uid_and_access_token" t.index ["github_uid"], name: "index_users_on_github_uid" t.index ["slack_uid"], name: "index_users_on_slack_uid", unique: true diff --git a/db/seeds.rb b/db/seeds.rb index a821f48c..5318f9f4 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -14,7 +14,7 @@ if Rails.env.development? # Creating test user test_user = User.find_or_create_by(slack_uid: 'TEST123456') do |user| - user.custom_name = 'testuser' + user.username = 'testuser' user.slack_username = 'testuser' # Before you had user.is_admin = true, does not work, changed it to that, looks like it works but idk how to use the admin pages so pls check this, i just guess coded this, the cmd to seed the db works without errors