Skip to content

Commit

Permalink
Reuse LabelsFinder on Banzai::Filter::LabelReferenceFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
dbalexandre committed Oct 19, 2016
1 parent 1e5ea6e commit d3b76e8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
18 changes: 14 additions & 4 deletions app/finders/labels_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ def initialize(current_user, params = {})
@params = params
end

def execute
def execute(authorized_only: true)
@authorized_only = authorized_only

items = find_union(label_ids, Label)
items = with_title(items)
sort(items)
end

private

attr_reader :current_user, :params
attr_reader :current_user, :params, :authorized_only

def label_ids
label_ids = []
Expand Down Expand Up @@ -57,18 +59,26 @@ def project
return @project if defined?(@project)

if project_id
@project = available_projects.find(project_id) rescue nil
@project = find_project
else
@project = nil
end

@project
end

def find_project
if authorized_only
available_projects.find_by(id: project_id)
else
Project.find_by(id: project_id)
end
end

def projects
return @projects if defined?(@projects)

@projects = available_projects
@projects = authorized_only ? available_projects : Project.all
@projects = @projects.in_namespace(group_id) if group_id
@projects = @projects.where(id: projects_ids) if projects_ids
@projects = @projects.reorder(nil)
Expand Down
8 changes: 1 addition & 7 deletions lib/banzai/filter/label_reference_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Label.where("labels.id IN (#{union.to_sql})")
LabelsFinder.new(nil, project_id: project.id).execute(authorized_only: false)
end

# Parameters to pass to `Label.find_by` based on the given arguments
Expand Down

0 comments on commit d3b76e8

Please sign in to comment.