debian-mirror-gitlab/app/finders/releases_finder.rb

52 lines
1.2 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
class ReleasesFinder
2020-10-24 23:57:45 +05:30
include Gitlab::Utils::StrongMemoize
2020-05-24 23:13:21 +05:30
2020-10-24 23:57:45 +05:30
attr_reader :parent, :current_user, :params
def initialize(parent, current_user = nil, params = {})
@parent = parent
2019-02-15 15:39:39 +05:30
@current_user = current_user
2020-05-24 23:13:21 +05:30
@params = params
2021-01-03 14:25:43 +05:30
params[:order_by] ||= 'released_at'
params[:sort] ||= 'desc'
2019-02-15 15:39:39 +05:30
end
2019-12-26 22:10:19 +05:30
def execute(preload: true)
2020-10-24 23:57:45 +05:30
return Release.none if projects.empty?
2019-02-15 15:39:39 +05:30
2020-10-24 23:57:45 +05:30
releases = get_releases
2020-05-24 23:13:21 +05:30
releases = by_tag(releases)
2019-12-26 22:10:19 +05:30
releases = releases.preloaded if preload
2021-04-29 21:17:54 +05:30
order_releases(releases)
2019-02-15 15:39:39 +05:30
end
2020-05-24 23:13:21 +05:30
private
2020-10-24 23:57:45 +05:30
def get_releases
Release.where(project_id: projects).where.not(tag: nil) # rubocop: disable CodeReuse/ActiveRecord
end
def projects
strong_memoize(:projects) do
if parent.is_a?(Project)
Ability.allowed?(current_user, :read_release, parent) ? [parent] : []
end
end
end
2020-05-24 23:13:21 +05:30
# rubocop: disable CodeReuse/ActiveRecord
def by_tag(releases)
return releases unless params[:tag].present?
releases.where(tag: params[:tag])
end
# rubocop: enable CodeReuse/ActiveRecord
2021-01-03 14:25:43 +05:30
def order_releases(releases)
releases.sort_by_attribute("#{params[:order_by]}_#{params[:sort]}")
end
2019-02-15 15:39:39 +05:30
end