Commit af18cddd authored by James Lopez's avatar James Lopez

udpated a few things based on MR feedback. Also added model spec

parent 8619208b
......@@ -270,8 +270,7 @@ class IssuableFinder
if filter_by_no_label?
items = items.without_label
else
items = items.with_label(label_names.flatten)
items = items.with_label(label_names)
if projects
items = items.where(labels: { project_id: projects })
end
......@@ -282,7 +281,7 @@ class IssuableFinder
end
def label_names
params[:label_name].split(',')
params[:label_name].is_a?(String) ? params[:label_name].split(',') : params[:label_name]
end
def current_user_related?
......
......@@ -32,7 +32,7 @@ module MilestonesHelper
end
def milestone_issues_by_label_count(milestone, label, state:)
milestone.issues.with_label([label.title]).send(state).size
milestone.issues.with_label(label.title).send(state).size
end
def milestone_progress_bar(milestone)
......
......@@ -37,7 +37,6 @@ module Issuable
scope :closed, -> { with_state(:closed) }
scope :order_milestone_due_desc, -> { joins(:milestone).reorder('milestones.due_date DESC, milestones.id DESC') }
scope :order_milestone_due_asc, -> { joins(:milestone).reorder('milestones.due_date ASC, milestones.id ASC') }
scope :with_label, ->(title) { joins(:labels).where(labels: { title: title }).group('issues.id').having("count(distinct labels.title) = #{title.count}") }
scope :without_label, -> { joins("LEFT OUTER JOIN label_links ON label_links.target_type = '#{name}' AND label_links.target_id = #{table_name}.id").where(label_links: { id: nil }) }
scope :join_project, -> { joins(:project) }
......@@ -122,6 +121,14 @@ module Issuable
joins(join_clause).group(issuable_table[:id]).reorder("COUNT(notes.id) DESC")
end
def with_label(title)
if title.is_a?(Array) && title.count > 1
joins(:labels).where(labels: { title: title }).group('issues.id').having("count(distinct labels.title) = #{title.count}")
else
joins(:labels).where(labels: { title: title })
end
end
end
def today?
......
......@@ -212,4 +212,28 @@ describe Issue, "Issuable" do
expect(issue.downvotes).to eq(1)
end
end
end
describe ".with_label" do
let(:example_label) { 'test1' }
let(:example_labels) { ['test1', 'test2'] }
it 'finds issue with 1 label' do
setup_labels([example_label])
expect(Issue.with_label(example_label).count).to eq(1)
end
it 'finds issue with 2 labels' do
setup_labels(example_labels)
expect(Issue.with_label(example_labels).to_a.count).to eq(1)
end
def setup_labels(label_names)
labels = label_names.map do |label|
create(:label, project: issue.project, title: label)
end
issue.labels << labels
end
end
end
\ No newline at end of file
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