gitlab-org--gitlab-foss/app/finders/labels_finder.rb

55 lines
1.2 KiB
Ruby
Raw Normal View History

2016-09-20 04:08:04 +00:00
class LabelsFinder < UnionFinder
2016-09-19 20:16:16 +00:00
def initialize(current_user, params = {})
@current_user = current_user
@params = params
end
def execute
2016-09-20 04:08:04 +00:00
items = find_union(label_ids, Label)
2016-09-19 20:16:16 +00:00
items = with_title(items)
sort(items)
end
private
attr_reader :current_user, :params
2016-09-20 04:08:04 +00:00
def label_ids
2016-09-19 20:16:16 +00:00
label_ids = []
label_ids << Label.where(group_id: projects.joins(:namespace).where(namespaces: { type: 'Group' }).select(:namespace_id)).select(:id)
2016-09-20 04:08:04 +00:00
label_ids << Label.where(project_id: projects.select(:id)).select(:id)
2016-09-19 20:16:16 +00:00
end
def sort(items)
items.reorder(title: :asc, type: :desc)
end
2016-09-19 20:16:16 +00:00
def with_title(items)
items = items.where(title: title) if title.present?
items
end
def group_id
params[:group_id].presence
2016-09-19 20:16:16 +00:00
end
def project_id
params[:project_id].presence
end
def title
params[:title].presence
end
def projects
return @projects if defined?(@projects)
@projects = ProjectsFinder.new.execute(current_user)
@projects = @projects.joins(:namespace).where(namespaces: { id: group_id, type: 'Group' }) if group_id
@projects = @projects.where(id: project_id) if project_id
@projects = @projects.reorder(nil)
2016-09-19 20:16:16 +00:00
@projects
end
end