Reuse LabelsFinder on Banzai::Filter::LabelReferenceFilter

This commit is contained in:
Douglas Barbosa Alexandre 2016-09-29 15:50:14 -03:00
parent 723e576782
commit 3db2261005
4 changed files with 44 additions and 15 deletions

View file

@ -128,8 +128,7 @@ class IssuableFinder
@labels = Label.where(title: label_names)
if projects
label_ids = LabelsFinder.new(current_user, project_id: projects).execute.select(:id)
@labels = @labels.where(labels: { id: label_ids })
@labels = LabelsFinder.new(current_user, project_ids: projects, title: label_names).execute
end
else
@labels = Label.none
@ -277,7 +276,7 @@ class IssuableFinder
items = items.with_label(label_names, params[:sort])
if projects
label_ids = LabelsFinder.new(current_user, project_id: projects).execute.select(:id)
label_ids = LabelsFinder.new(current_user, project_ids: projects).execute.select(:id)
items = items.where(labels: { id: label_ids })
end
end

View file

@ -16,8 +16,16 @@ class LabelsFinder < UnionFinder
def label_ids
label_ids = []
label_ids << Label.where(group_id: projects.joins(:namespace).where(namespaces: { type: 'Group' }).select(:namespace_id)).select(:id)
label_ids << Label.where(project_id: projects.select(:id)).select(:id)
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
end
def sort(items)
@ -37,18 +45,38 @@ class LabelsFinder < UnionFinder
params[:project_id].presence
end
def project_ids
params[:project_ids].presence
end
def title
params[:title].presence
end
def project
return @project if defined?(@project)
if project_id
@project = available_projects.find(project_id) rescue nil
else
@project = nil
end
@project
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 = available_projects
@projects = @projects.in_namespace(group_id) if group_id
@projects = @projects.where(id: project_ids) if project_ids
@projects = @projects.reorder(nil)
@projects
end
def available_projects
@available_projects ||= ProjectsFinder.new.execute(current_user)
end
end

View file

@ -388,6 +388,10 @@ class Project < ActiveRecord::Base
Project.count
end
end
def group_ids
joins(:namespace).where(namespaces: { type: 'Group' }).pluck(:namespace_id)
end
end
def lfs_enabled?

View file

@ -39,13 +39,7 @@ module Banzai
end
def find_labels(project)
label_ids = []
label_ids << project.group.labels.select(:id) if project.group.present?
label_ids << project.labels.select(:id)
union = Gitlab::SQL::Union.new(label_ids)
object_class.where("labels.id IN (#{union.to_sql})")
LabelsFinder.new(user, project_id: project.id).execute
end
# Parameters to pass to `Label.find_by` based on the given arguments
@ -91,6 +85,10 @@ module Banzai
object.is_a?(ProjectLabel) && object.project == project
end
def user
context[:current_user] || context[:author]
end
def project
context[:project]
end