summaryrefslogtreecommitdiff
path: root/app/finders/packages/packages_finder.rb
blob: 840cbbf7b9d01a7bc8dec398dde74a4bcc469f94 (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
# frozen_string_literal: true

module Packages
  class PackagesFinder
    include ::Packages::FinderHelper

    def initialize(project, params = {})
      @project = project
      @params = params

      params[:order_by] ||= 'created_at'
      params[:sort] ||= 'asc'
    end

    def execute
      packages = project.packages
                        .including_build_info
                        .including_project_route
                        .including_tags
                        .processed
      packages = filter_with_version(packages)
      packages = filter_by_package_type(packages)
      packages = filter_by_package_name(packages)
      packages = filter_by_status(packages)
      order_packages(packages)
    end

    private

    attr_reader :params, :project

    def order_packages(packages)
      packages.sort_by_attribute("#{params[:order_by]}_#{params[:sort]}")
    end
  end
end