blob: 51bd85016a5c6fbd026151cb8b620e8090457cfc (
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
|
# frozen_string_literal: true
module Ci
module Catalog
class Listing
# This class is the SSoT to displaying the list of resources in the CI/CD Catalog.
# This model is not directly backed by a table and joins catalog resources
# with projects to return relevant data.
MIN_SEARCH_LENGTH = 3
def initialize(current_user)
@current_user = current_user
end
def resources(namespace: nil, sort: nil, search: nil)
relation = all_resources
relation = by_namespace(relation, namespace)
relation = by_search(relation, search)
case sort.to_s
when 'name_desc' then relation.order_by_name_desc
when 'name_asc' then relation.order_by_name_asc
when 'latest_released_at_desc' then relation.order_by_latest_released_at_desc
when 'latest_released_at_asc' then relation.order_by_latest_released_at_asc
when 'created_at_asc' then relation.order_by_created_at_asc
else
relation.order_by_created_at_desc
end
end
private
attr_reader :current_user
def all_resources
Ci::Catalog::Resource.joins(:project).includes(:project)
.merge(Project.public_or_visible_to_user(current_user))
end
def by_namespace(relation, namespace)
return relation unless namespace
raise ArgumentError, 'Namespace is not a root namespace' unless namespace.root?
relation.merge(Project.in_namespace(namespace.self_and_descendant_ids))
end
def by_search(relation, search)
return relation unless search
return relation.none if search.length < MIN_SEARCH_LENGTH
relation.search(search)
end
end
end
end
|