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

98 lines
1.9 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(authorized_only: true)
@authorized_only = authorized_only
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, :authorized_only
2016-09-19 20:16:16 +00:00
2016-09-20 04:08:04 +00:00
def label_ids
2016-09-19 20:16:16 +00:00
label_ids = []
if project
label_ids << project.group.labels if project.group.present?
label_ids << project.labels
else
label_ids << Label.where(group_id: projects.group_ids)
label_ids << Label.where(project_id: projects.select(:id))
end
label_ids
2016-09-19 20:16:16 +00:00
end
def sort(items)
items.reorder(title: :asc)
end
2016-09-19 20:16:16 +00:00
def with_title(items)
if title
2016-10-24 13:43:13 +00:00
items.where(title: title)
elsif params[:title] || params[:name] # empty input, should match nothing
items.none
else # not filtering
items
end
2016-09-19 20:16:16 +00:00
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 projects_ids
params[:project_ids].presence
end
2016-09-19 20:16:16 +00:00
def title
params[:title].presence || params[:name].presence
2016-09-19 20:16:16 +00:00
end
def project
return @project if defined?(@project)
if project_id
@project = find_project
else
@project = nil
end
@project
end
def find_project
if authorized_only
available_projects.find_by(id: project_id)
else
Project.find_by(id: project_id)
end
end
2016-09-19 20:16:16 +00:00
def projects
return @projects if defined?(@projects)
@projects = authorized_only ? available_projects : Project.all
@projects = @projects.in_namespace(group_id) if group_id
@projects = @projects.where(id: projects_ids) if projects_ids
@projects = @projects.reorder(nil)
2016-09-19 20:16:16 +00:00
@projects
end
def available_projects
@available_projects ||= ProjectsFinder.new.execute(current_user)
end
2016-09-19 20:16:16 +00:00
end