Improve readability and add specs for label filtering

This commit is contained in:
Stan Hu 2016-10-24 23:04:38 -07:00
parent ce256c28f2
commit 02f835c105
2 changed files with 21 additions and 10 deletions

View File

@ -35,13 +35,10 @@ class LabelsFinder < UnionFinder
end
def with_title(items)
if title
items.where(title: title)
elsif params[:title] || params[:name] # empty input, should match nothing
items.none
else # not filtering
items
end
return items if title.nil?
return items.none if title.blank?
items.where(title: title)
end
def group_id
@ -57,7 +54,7 @@ class LabelsFinder < UnionFinder
end
def title
params[:title].presence || params[:name].presence
params[:title] || params[:name]
end
def project

View File

@ -38,6 +38,14 @@ describe LabelsFinder do
expect(finder.execute).to eq [group_label_2, group_label_3, project_label_1, group_label_1, project_label_2, project_label_4]
end
it 'returns labels available if nil title is supplied' do
group_2.add_developer(user)
# params[:title] will return `nil` regardless whether it is specified
finder = described_class.new(user, title: nil)
expect(finder.execute).to eq [group_label_2, group_label_3, project_label_1, group_label_1, project_label_2, project_label_4]
end
end
context 'filtering by group_id' do
@ -71,13 +79,19 @@ describe LabelsFinder do
expect(finder.execute).to eq [group_label_2]
end
it 'returns no labels if empty titles are supplied' do
it 'returns no labels if empty title is supplied' do
finder = described_class.new(user, title: [])
expect(finder.execute).to be_empty
end
it 'returns no labels if empty names are supplied' do
it 'returns no labels if blank title is supplied' do
finder = described_class.new(user, title: '')
expect(finder.execute).to be_empty
end
it 'returns no labels if empty name is supplied' do
finder = described_class.new(user, name: [])
expect(finder.execute).to be_empty