Commit 580fa6be authored by Mark Fletcher's avatar Mark Fletcher

Yield no results for blank searches on User name, username and email

Given no search term, the `search` and `search_with_secondary_emails` methods will yield an empty result set
parent 062f5b71
...@@ -318,6 +318,8 @@ class User < ActiveRecord::Base ...@@ -318,6 +318,8 @@ class User < ActiveRecord::Base
# #
# Returns an ActiveRecord::Relation. # Returns an ActiveRecord::Relation.
def search(query) def search(query)
return none if query.blank?
query = query.downcase query = query.downcase
order = <<~SQL order = <<~SQL
...@@ -341,6 +343,8 @@ class User < ActiveRecord::Base ...@@ -341,6 +343,8 @@ class User < ActiveRecord::Base
# This method uses ILIKE on PostgreSQL and LIKE on MySQL. # This method uses ILIKE on PostgreSQL and LIKE on MySQL.
def search_with_secondary_emails(query) def search_with_secondary_emails(query)
return none if query.blank?
query = query.downcase query = query.downcase
email_table = Email.arel_table email_table = Email.arel_table
......
...@@ -966,6 +966,14 @@ describe User do ...@@ -966,6 +966,14 @@ describe User do
expect(described_class.search(user3.username.upcase)).to eq([user3]) expect(described_class.search(user3.username.upcase)).to eq([user3])
end end
end end
it 'returns no matches for an empty string' do
expect(described_class.search('')).to be_empty
end
it 'returns no matches for nil' do
expect(described_class.search(nil)).to be_empty
end
end end
describe '.search_with_secondary_emails' do describe '.search_with_secondary_emails' do
...@@ -1020,6 +1028,14 @@ describe User do ...@@ -1020,6 +1028,14 @@ describe User do
it 'does not return users with a matching part of secondary email' do it 'does not return users with a matching part of secondary email' do
expect(search_with_secondary_emails(email.email[1..4])).not_to include([email.user]) expect(search_with_secondary_emails(email.email[1..4])).not_to include([email.user])
end end
it 'returns no matches for an empty string' do
expect(search_with_secondary_emails('')).to be_empty
end
it 'returns no matches for nil' do
expect(search_with_secondary_emails(nil)).to be_empty
end
end end
describe '.find_by_ssh_key_id' do describe '.find_by_ssh_key_id' do
......
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