Commit 69b6fd12 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch '212273-fix-arguments-to-search-scope' into 'master'

Change classes that include OptionallySearch to explicitly receive 2 arguments

Closes #212273

See merge request gitlab-org/gitlab!27879
parents 0a9b6817 4dcb5783
...@@ -4,7 +4,7 @@ module OptionallySearch ...@@ -4,7 +4,7 @@ module OptionallySearch
extend ActiveSupport::Concern extend ActiveSupport::Concern
class_methods do class_methods do
def search(*) def search(query, **options)
raise( raise(
NotImplementedError, NotImplementedError,
'Your model must implement the "search" class method' 'Your model must implement the "search" class method'
......
...@@ -138,7 +138,7 @@ class Label < ApplicationRecord ...@@ -138,7 +138,7 @@ class Label < ApplicationRecord
# query - The search query as a String. # query - The search query as a String.
# #
# Returns an ActiveRecord::Relation. # Returns an ActiveRecord::Relation.
def self.search(query) def self.search(query, **options)
fuzzy_search(query, [:title, :description]) fuzzy_search(query, [:title, :description])
end end
......
...@@ -511,7 +511,7 @@ class User < ApplicationRecord ...@@ -511,7 +511,7 @@ class User < ApplicationRecord
# query - The search query as a String # query - The search query as a String
# #
# Returns an ActiveRecord::Relation. # Returns an ActiveRecord::Relation.
def search(query) def search(query, **options)
query = query&.delete_prefix('@') query = query&.delete_prefix('@')
return none if query.blank? return none if query.blank?
......
...@@ -3,28 +3,39 @@ ...@@ -3,28 +3,39 @@
require 'spec_helper' require 'spec_helper'
describe OptionallySearch do describe OptionallySearch do
describe '.search' do
let(:model) do let(:model) do
Class.new(ActiveRecord::Base) do Class.new do
self.table_name = 'users'
include OptionallySearch include OptionallySearch
end end
end end
describe '.search' do
it 'raises NotImplementedError' do it 'raises NotImplementedError' do
expect { model.search('foo') }.to raise_error(NotImplementedError) expect { model.search('foo') }.to raise_error(NotImplementedError)
end end
end end
describe '.optionally_search' do describe '.optionally_search' do
let(:model) do
Class.new(ActiveRecord::Base) do
self.table_name = 'users'
include OptionallySearch
def self.search(query, **options)
[query, options]
end
end
end
context 'when a query is given' do context 'when a query is given' do
it 'delegates to the search method' do it 'delegates to the search method' do
expect(model) expect(model)
.to receive(:search) .to receive(:search)
.with('foo', {}) .with('foo', {})
.and_call_original
model.optionally_search('foo') expect(model.optionally_search('foo')).to eq(['foo', {}])
end end
end end
...@@ -33,8 +44,9 @@ describe OptionallySearch do ...@@ -33,8 +44,9 @@ describe OptionallySearch do
expect(model) expect(model)
.to receive(:search) .to receive(:search)
.with('foo', some_option: true) .with('foo', some_option: true)
.and_call_original
model.optionally_search('foo', some_option: true) expect(model.optionally_search('foo', some_option: true)).to eq(['foo', { some_option: true }])
end end
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