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