2016-09-20 00:08:04 -04:00
|
|
|
class LabelsFinder < UnionFinder
|
2016-09-19 16:16:16 -04:00
|
|
|
def initialize(current_user, params = {})
|
|
|
|
@current_user = current_user
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
2016-10-17 15:30:49 -04:00
|
|
|
def execute(authorized_only: true)
|
|
|
|
@authorized_only = authorized_only
|
|
|
|
|
2016-09-20 00:08:04 -04:00
|
|
|
items = find_union(label_ids, Label)
|
2016-09-19 16:16:16 -04:00
|
|
|
items = with_title(items)
|
|
|
|
sort(items)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-10-17 15:30:49 -04:00
|
|
|
attr_reader :current_user, :params, :authorized_only
|
2016-09-19 16:16:16 -04:00
|
|
|
|
2016-09-20 00:08:04 -04:00
|
|
|
def label_ids
|
2016-09-19 16:16:16 -04:00
|
|
|
label_ids = []
|
2016-09-29 14:50:14 -04:00
|
|
|
|
|
|
|
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 16:16:16 -04:00
|
|
|
end
|
|
|
|
|
2016-09-20 10:55:31 -04:00
|
|
|
def sort(items)
|
2016-10-19 09:59:31 -04:00
|
|
|
items.reorder(title: :asc)
|
2016-09-20 10:55:31 -04:00
|
|
|
end
|
|
|
|
|
2016-09-19 16:16:16 -04:00
|
|
|
def with_title(items)
|
2016-10-23 19:46:21 -04:00
|
|
|
# Match no labels if an empty title is supplied to avoid matching all
|
|
|
|
# labels (e.g. when an issue is moved)
|
2016-10-24 09:25:18 -04:00
|
|
|
return items.none if raw_title && raw_title.empty?
|
2016-10-23 19:46:21 -04:00
|
|
|
|
2016-10-24 09:25:18 -04:00
|
|
|
if title
|
|
|
|
items = items.where(title: title)
|
|
|
|
else
|
|
|
|
items
|
|
|
|
end
|
2016-09-19 16:16:16 -04:00
|
|
|
end
|
|
|
|
|
2016-09-20 10:55:31 -04:00
|
|
|
def group_id
|
|
|
|
params[:group_id].presence
|
2016-09-19 16:16:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def project_id
|
|
|
|
params[:project_id].presence
|
|
|
|
end
|
|
|
|
|
2016-10-17 14:55:46 -04:00
|
|
|
def projects_ids
|
2016-09-29 14:50:14 -04:00
|
|
|
params[:project_ids].presence
|
|
|
|
end
|
|
|
|
|
2016-09-19 16:16:16 -04:00
|
|
|
def title
|
2016-10-18 02:30:43 -04:00
|
|
|
params[:title].presence || params[:name].presence
|
2016-09-19 16:16:16 -04:00
|
|
|
end
|
|
|
|
|
2016-10-24 09:25:18 -04:00
|
|
|
def raw_title
|
|
|
|
params[:title] || params[:name]
|
|
|
|
end
|
|
|
|
|
2016-09-29 14:50:14 -04:00
|
|
|
def project
|
|
|
|
return @project if defined?(@project)
|
|
|
|
|
|
|
|
if project_id
|
2016-10-17 15:30:49 -04:00
|
|
|
@project = find_project
|
2016-09-29 14:50:14 -04:00
|
|
|
else
|
|
|
|
@project = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
@project
|
|
|
|
end
|
|
|
|
|
2016-10-17 15:30:49 -04:00
|
|
|
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 16:16:16 -04:00
|
|
|
def projects
|
|
|
|
return @projects if defined?(@projects)
|
|
|
|
|
2016-10-17 15:30:49 -04:00
|
|
|
@projects = authorized_only ? available_projects : Project.all
|
2016-09-29 14:50:14 -04:00
|
|
|
@projects = @projects.in_namespace(group_id) if group_id
|
2016-10-17 14:55:46 -04:00
|
|
|
@projects = @projects.where(id: projects_ids) if projects_ids
|
2016-09-20 10:55:31 -04:00
|
|
|
@projects = @projects.reorder(nil)
|
2016-09-19 16:16:16 -04:00
|
|
|
|
|
|
|
@projects
|
|
|
|
end
|
2016-09-29 14:50:14 -04:00
|
|
|
|
|
|
|
def available_projects
|
|
|
|
@available_projects ||= ProjectsFinder.new.execute(current_user)
|
|
|
|
end
|
2016-09-19 16:16:16 -04:00
|
|
|
end
|