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 ...@@ -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
let(:model) do describe '.search' do
Class.new(ActiveRecord::Base) do let(:model) do
self.table_name = 'users' Class.new do
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