debian-mirror-gitlab/app/models/projects/topic.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

67 lines
2.6 KiB
Ruby
Raw Normal View History

2021-11-11 11:23:49 +05:30
# frozen_string_literal: true
2021-11-18 22:05:49 +05:30
require 'carrierwave/orm/activerecord'
2021-11-11 11:23:49 +05:30
module Projects
class Topic < ApplicationRecord
2021-11-18 22:05:49 +05:30
include Avatarable
include Gitlab::SQL::Pattern
2022-05-07 20:08:51 +05:30
validates :name, presence: true, length: { maximum: 255 }
validates :name, uniqueness: { case_sensitive: false }, if: :name_changed?
2022-07-16 23:28:13 +05:30
validates :title, presence: true, length: { maximum: 255 }, on: :create
2021-11-18 22:05:49 +05:30
validates :description, length: { maximum: 1024 }
2021-11-11 11:23:49 +05:30
has_many :project_topics, class_name: 'Projects::ProjectTopic'
has_many :projects, through: :project_topics
2021-11-18 22:05:49 +05:30
2022-08-27 11:52:29 +05:30
scope :without_assigned_projects, -> { where(total_projects_count: 0) }
2022-05-07 20:08:51 +05:30
scope :order_by_non_private_projects_count, -> { order(non_private_projects_count: :desc).order(id: :asc) }
2021-11-18 22:05:49 +05:30
scope :reorder_by_similarity, -> (search) do
order_expression = Gitlab::Database::SimilarityScore.build_expression(search: search, rules: [
{ column: arel_table['name'] }
])
2022-05-07 20:08:51 +05:30
reorder(order_expression.desc, arel_table['non_private_projects_count'].desc, arel_table['id'])
2021-11-18 22:05:49 +05:30
end
2022-07-16 23:28:13 +05:30
def title_or_name
title || name
end
2021-11-18 22:05:49 +05:30
class << self
2022-06-21 17:19:12 +05:30
def find_by_name_case_insensitive(name)
find_by('LOWER(name) = ?', name.downcase)
end
2021-11-18 22:05:49 +05:30
def search(query)
2022-07-16 23:28:13 +05:30
fuzzy_search(query, [:name, :title])
2021-11-18 22:05:49 +05:30
end
2022-04-04 11:22:00 +05:30
def update_non_private_projects_counter(ids_before, ids_after, project_visibility_level_before, project_visibility_level_after)
project_visibility_level_before ||= project_visibility_level_after
topics_to_decrement = []
topics_to_increment = []
topic_ids_removed = ids_before - ids_after
topic_ids_retained = ids_before & ids_after
topic_ids_added = ids_after - ids_before
if project_visibility_level_before > Gitlab::VisibilityLevel::PRIVATE
topics_to_decrement += topic_ids_removed
topics_to_decrement += topic_ids_retained if project_visibility_level_after == Gitlab::VisibilityLevel::PRIVATE
end
if project_visibility_level_after > Gitlab::VisibilityLevel::PRIVATE
topics_to_increment += topic_ids_added
topics_to_increment += topic_ids_retained if project_visibility_level_before == Gitlab::VisibilityLevel::PRIVATE
end
where(id: topics_to_increment).update_counters(non_private_projects_count: 1) unless topics_to_increment.empty?
where(id: topics_to_decrement).where('non_private_projects_count > 0').update_counters(non_private_projects_count: -1) unless topics_to_decrement.empty?
end
2021-11-18 22:05:49 +05:30
end
2021-11-11 11:23:49 +05:30
end
end
2021-12-11 22:18:48 +05:30
::Projects::Topic.prepend_mod_with('Projects::Topic')