Read about our upcoming Code of Conduct on this issue

This instance was upgraded to Heptapod 0.28.0 today

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 ...@@ -128,8 +128,7 @@ def labels
@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 @@ def by_label(items) ...@@ -277,7 +276,7 @@ def by_label(items)
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 @@ def execute ...@@ -16,8 +16,16 @@ def execute
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 @@ def project_id ...@@ -37,18 +45,38 @@ def project_id
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 @@ def cached_count ...@@ -388,6 +388,10 @@ def cached_count
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 @@ def find_label(project_ref, label_id, label_name) ...@@ -39,13 +39,7 @@ def find_label(project_ref, label_id, label_name)
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 @@ def same_project?(object) ...@@ -91,6 +85,10 @@ def same_project?(object)
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