Commit 3db22610 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre
Browse files

Reuse LabelsFinder on Banzai::Filter::LabelReferenceFilter

parent 3629198fcf9d
......@@ -128,8 +128,7 @@ def labels
@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 @@ def by_label(items)
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
......
......@@ -16,8 +16,16 @@ def execute
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 @@ def project_id
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
......@@ -388,6 +388,10 @@ def cached_count
Project.count
end
end
def group_ids
joins(:namespace).where(namespaces: { type: 'Group' }).pluck(:namespace_id)
end
end
def lfs_enabled?
......
......@@ -39,13 +39,7 @@ def find_label(project_ref, label_id, label_name)
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 @@ def same_project?(object)
object.is_a?(ProjectLabel) && object.project == project
end
def user
context[:current_user] || context[:author]
end
def project
context[:project]
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