Skip to content

Commit 156c150

Browse files
committed
Resolving comments in PR
- Deprecate locate without options - Calling includes when options[:includes] is passed - Adjusting test for the deprecation - Documenting the new locate method structure with options
1 parent 1a95eed commit 156c150

File tree

3 files changed

+8
-12
lines changed

3 files changed

+8
-12
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ A custom locator can either be a block or a class.
191191
Using a block:
192192

193193
```ruby
194-
GlobalID::Locator.use :foo do |gid|
194+
GlobalID::Locator.use :foo do |gid, options|
195195
FooRemote.const_get(gid.model_name).find(gid.model_id)
196196
end
197197
```
@@ -201,7 +201,7 @@ Using a class:
201201
```ruby
202202
GlobalID::Locator.use :bar, BarLocator.new
203203
class BarLocator
204-
def locate(gid)
204+
def locate(gid, options = {})
205205
@search_client.search name: gid.model_name, id: gid.model_id
206206
end
207207
end

lib/global_id/locator.rb

+5-9
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ def locate(gid, options = {})
2020
gid = GlobalID.parse(gid)
2121
return unless gid && find_allowed?(gid.model_class, options[:only])
2222

23-
if locator_for(gid).method(:locate).arity == 1
24-
locator_for(gid).locate(gid)
25-
else
26-
locator_for(gid).locate(gid, options.except(:only))
27-
end
23+
locator_for(gid).locate(gid, options.except(:only))
2824
end
2925

3026
# Takes an array of GlobalIDs or strings that can be turned into a GlobalIDs.
@@ -106,7 +102,7 @@ def locate_many_signed(sgids, options = {})
106102
#
107103
# Using a block:
108104
#
109-
# GlobalID::Locator.use :foo do |gid|
105+
# GlobalID::Locator.use :foo do |gid, options|
110106
# FooRemote.const_get(gid.model_name).find(gid.model_id)
111107
# end
112108
#
@@ -115,7 +111,7 @@ def locate_many_signed(sgids, options = {})
115111
# GlobalID::Locator.use :bar, BarLocator.new
116112
#
117113
# class BarLocator
118-
# def locate(gid)
114+
# def locate(gid, options = {})
119115
# @search_client.search name: gid.model_name, id: gid.model_id
120116
# end
121117
# end
@@ -151,7 +147,7 @@ def normalize_app(app)
151147
class BaseLocator
152148
def locate(gid, options = {})
153149
model_class = gid.model_class
154-
model_class = model_class.includes(options[:includes]) if model_class.respond_to?(:includes)
150+
model_class = model_class.includes(options[:includes]) if options[:includes]
155151

156152
model_class.find gid.model_id
157153
end
@@ -168,7 +164,7 @@ def locate_many(gids, options = {})
168164

169165
private
170166
def find_records(model_class, ids, options)
171-
model_class = model_class.includes(options[:includes]) if model_class.respond_to?(:includes)
167+
model_class = model_class.includes(options[:includes]) if options[:includes]
172168

173169
if options[:ignore_missing]
174170
model_class.where(id: ids)

test/cases/global_locator_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ class GlobalLocatorTest < ActiveSupport::TestCase
217217

218218
test 'use locator with class' do
219219
class BarLocator
220-
def locate(gid); :bar; end
220+
def locate(gid, options = {}); :bar; end
221221
def locate_many(gids, options = {}); gids.map(&:model_id); end
222222
end
223223

0 commit comments

Comments
 (0)