Commit 4dcb5783 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Change optionally_search to always require 2 args

We explicitly define the optional keyword on the model classes
because in some environments, passing an empty Hash as the last
argument isn't treated as keyword arguments
parent 438e0332
......@@ -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