Welcome to mirror list, hosted at ThFree Co, Russian Federation.

resource.rb « catalog « ci « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f947c5158cf5a03caf738c5dd78938603266a04f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true

module Ci
  module Catalog
    # This class represents a CI/CD Catalog resource.
    # A Catalog resource is normally associated to a project.
    # This model connects to the `main` database because of its
    # dependency on the Project model and its need to join with that table
    # in order to generate the CI/CD catalog.
    class Resource < ::ApplicationRecord
      include Gitlab::SQL::Pattern

      self.table_name = 'catalog_resources'

      belongs_to :project
      has_many :components, class_name: 'Ci::Catalog::Resources::Component', foreign_key: :catalog_resource_id,
        inverse_of: :catalog_resource
      has_many :versions, class_name: 'Ci::Catalog::Resources::Version', foreign_key: :catalog_resource_id,
        inverse_of: :catalog_resource

      scope :for_projects, ->(project_ids) { where(project_id: project_ids) }
      scope :search, ->(query) { fuzzy_search(query, [:name, :description], use_minimum_char_limit: false) }

      scope :order_by_created_at_desc, -> { reorder(created_at: :desc) }
      scope :order_by_created_at_asc, -> { reorder(created_at: :asc) }
      scope :order_by_name_desc, -> { reorder(arel_table[:name].desc.nulls_last) }
      scope :order_by_name_asc, -> { reorder(arel_table[:name].asc.nulls_last) }
      scope :order_by_latest_released_at_desc, -> { reorder(arel_table[:latest_released_at].desc.nulls_last) }
      scope :order_by_latest_released_at_asc, -> { reorder(arel_table[:latest_released_at].asc.nulls_last) }

      delegate :avatar_path, :star_count, :forks_count, to: :project

      enum state: { draft: 0, published: 1 }

      before_create :sync_with_project

      def unpublish!
        update!(state: :draft)
      end

      def publish!
        update!(state: :published)
      end

      def sync_with_project!
        sync_with_project
        save!
      end

      private

      # These columns are denormalized from the `projects` table. We first sync these
      # columns when the catalog resource record is created. Then any updates to the
      # `projects` columns will be synced to the `catalog_resources` table by a worker
      # (to be implemented in https://gitlab.com/gitlab-org/gitlab/-/issues/429376.)
      def sync_with_project
        self.name = project.name
        self.description = project.description
        self.visibility_level = project.visibility_level
      end
    end
  end
end

Ci::Catalog::Resource.prepend_mod_with('Ci::Catalog::Resource')