From cbf8f1fa678504aee081eb4ab81325aac3fe6d55 Mon Sep 17 00:00:00 2001 From: jasder Date: Wed, 27 Oct 2021 17:19:39 +0800 Subject: [PATCH] FIX code review --- app/controllers/accounts_controller.rb | 2 +- app/controllers/organizations/organizations_controller.rb | 2 +- app/forms/base_form.rb | 2 +- app/forms/register/check_columns_form.rb | 2 +- app/models/reversed_keyword.rb | 4 ++++ 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index dcffea5ca..d165d9370 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -9,7 +9,7 @@ class AccountsController < ApplicationController # 其他平台同步注册的用户 def remote_register username = params[:username]&.gsub(/\s+/, "") - tip_exception("无法使用以下关键词:#{username},请重新命名") if ReversedKeyword.is_reversed(username).present? + tip_exception("无法使用以下关键词:#{username},请重新命名") if ReversedKeyword.check_exists?(username) email = params[:email]&.gsub(/\s+/, "") password = params[:password] platform = (params[:platform] || 'forge')&.gsub(/\s+/, "") diff --git a/app/controllers/organizations/organizations_controller.rb b/app/controllers/organizations/organizations_controller.rb index 218bc872d..2dbc9ffd3 100644 --- a/app/controllers/organizations/organizations_controller.rb +++ b/app/controllers/organizations/organizations_controller.rb @@ -26,7 +26,7 @@ class Organizations::OrganizationsController < Organizations::BaseController def create ActiveRecord::Base.transaction do - tip_exception("无法使用以下关键词:#{organization_params[:name]},请重新命名") if ReversedKeyword.is_reversed(organization_params[:name]).present? + tip_exception("无法使用以下关键词:#{organization_params[:name]},请重新命名") if ReversedKeyword.check_exists?(organization_params[:name]) Organizations::CreateForm.new(organization_params).validate! @organization = Organizations::CreateService.call(current_user, organization_params) Util.write_file(@image, avatar_path(@organization)) if params[:image].present? diff --git a/app/forms/base_form.rb b/app/forms/base_form.rb index 71eaee174..8f4f9d2ca 100644 --- a/app/forms/base_form.rb +++ b/app/forms/base_form.rb @@ -23,7 +23,7 @@ class BaseForm end def check_reversed_keyword(repository_name) - raise "项目标识已被占用." if ReversedKeyword.is_reversed(repository_name).exists? + raise "项目标识已被占用." if ReversedKeyword.check_exists?(repository_name) end end diff --git a/app/forms/register/check_columns_form.rb b/app/forms/register/check_columns_form.rb index 24283b810..5fdb82fa0 100644 --- a/app/forms/register/check_columns_form.rb +++ b/app/forms/register/check_columns_form.rb @@ -20,7 +20,7 @@ module Register def check_login raise("登录名格式有误") unless strip_value =~ CustomRegexp::LOGIN - login_exist = Owner.exists?(login: strip_value) || ReversedKeyword.exists?(identifier: strip_value) + login_exist = Owner.exists?(login: strip_value) || ReversedKeyword.check_exists?(strip_value) raise('登录名已被使用') if login_exist end diff --git a/app/models/reversed_keyword.rb b/app/models/reversed_keyword.rb index cd8027fbd..10ad62fa0 100644 --- a/app/models/reversed_keyword.rb +++ b/app/models/reversed_keyword.rb @@ -18,6 +18,10 @@ class ReversedKeyword < ApplicationRecord before_validation :set_identifier + def self.check_exists?(identifier) + self.is_reversed(identifier).exists? + end + private def set_identifier