Reuse LabelsFinder on Banzai::Filter::LabelReferenceFilter

parent 723e5767
...@@ -128,8 +128,7 @@ class IssuableFinder ...@@ -128,8 +128,7 @@ class IssuableFinder
@labels = Label.where(title: label_names) @labels = Label.where(title: label_names)
if projects if projects
label_ids = LabelsFinder.new(current_user, project_id: projects).execute.select(:id) @labels = LabelsFinder.new(current_user, project_ids: projects, title: label_names).execute
@labels = @labels.where(labels: { id: label_ids })
end end
else else
@labels = Label.none @labels = Label.none
...@@ -277,7 +276,7 @@ class IssuableFinder ...@@ -277,7 +276,7 @@ class IssuableFinder
items = items.with_label(label_names, params[:sort]) items = items.with_label(label_names, params[:sort])
if projects 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 }) items = items.where(labels: { id: label_ids })
end end
end end
......
...@@ -16,8 +16,16 @@ class LabelsFinder < UnionFinder ...@@ -16,8 +16,16 @@ class LabelsFinder < UnionFinder
def label_ids def label_ids
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 end
def sort(items) def sort(items)
...@@ -37,18 +45,38 @@ class LabelsFinder < UnionFinder ...@@ -37,18 +45,38 @@ class LabelsFinder < UnionFinder
params[:project_id].presence params[:project_id].presence
end end
def project_ids
params[:project_ids].presence
end
def title def title
params[:title].presence params[:title].presence
end 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 def projects
return @projects if defined?(@projects) return @projects if defined?(@projects)
@projects = ProjectsFinder.new.execute(current_user) @projects = available_projects
@projects = @projects.joins(:namespace).where(namespaces: { id: group_id, type: 'Group' }) if group_id @projects = @projects.in_namespace(group_id) if group_id
@projects = @projects.where(id: project_id) if project_id @projects = @projects.where(id: project_ids) if project_ids
@projects = @projects.reorder(nil) @projects = @projects.reorder(nil)
@projects @projects
end end
def available_projects
@available_projects ||= ProjectsFinder.new.execute(current_user)
end
end end
...@@ -388,6 +388,10 @@ class Project < ActiveRecord::Base ...@@ -388,6 +388,10 @@ class Project < ActiveRecord::Base
Project.count Project.count
end end
end end
def group_ids
joins(:namespace).where(namespaces: { type: 'Group' }).pluck(:namespace_id)
end
end end
def lfs_enabled? def lfs_enabled?
......
...@@ -39,13 +39,7 @@ module Banzai ...@@ -39,13 +39,7 @@ module Banzai
end end
def find_labels(project) def find_labels(project)
label_ids = [] LabelsFinder.new(user, project_id: project.id).execute
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})")
end end
# Parameters to pass to `Label.find_by` based on the given arguments # Parameters to pass to `Label.find_by` based on the given arguments
...@@ -91,6 +85,10 @@ module Banzai ...@@ -91,6 +85,10 @@ module Banzai
object.is_a?(ProjectLabel) && object.project == project object.is_a?(ProjectLabel) && object.project == project
end end
def user
context[:current_user] || context[:author]
end
def project def project
context[:project] context[:project]
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment