@@ -8,14 +8,27 @@ class << self
8
8
# Takes either a GlobalID or a string that can be turned into a GlobalID
9
9
#
10
10
# Options:
11
+ # * <tt>:includes</tt> - A Symbol, Array, Hash or combination of them.
12
+ # The same structure you would pass into a +includes+ method of Active Record.
13
+ # If present, locate will load all the relationships specified here.
14
+ # See https://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations.
11
15
# * <tt>:only</tt> - A class, module or Array of classes and/or modules that are
12
16
# allowed to be located. Passing one or more classes limits instances of returned
13
17
# classes to those classes or their subclasses. Passing one or more modules in limits
14
18
# instances of returned classes to those including that module. If no classes or
15
19
# modules match, +nil+ is returned.
16
20
def locate ( gid , options = { } )
17
- if gid = GlobalID . parse ( gid )
18
- locator_for ( gid ) . locate gid if find_allowed? ( gid . model_class , options [ :only ] )
21
+ gid = GlobalID . parse ( gid )
22
+
23
+ return unless gid && find_allowed? ( gid . model_class , options [ :only ] )
24
+
25
+ locator = locator_for ( gid )
26
+
27
+ if locator . method ( :locate ) . arity == 1
28
+ GlobalID . deprecator . warn "It seems your locator is defining the `locate` method only with one argument. Please make sure your locator is receiving the options argument as well, like `locate(gid, options = {})`."
29
+ locator . locate ( gid )
30
+ else
31
+ locator . locate ( gid , options . except ( :only ) )
19
32
end
20
33
end
21
34
@@ -30,6 +43,11 @@ def locate(gid, options = {})
30
43
# per model class, but still interpolate the results to match the order in which the gids were passed.
31
44
#
32
45
# Options:
46
+ # * <tt>:includes</tt> - A Symbol, Array, Hash or combination of them
47
+ # The same structure you would pass into a includes method of Active Record.
48
+ # @see https://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations
49
+ # If present, locate_many will load all the relationships specified here.
50
+ # Note: It only works if all the gids models have that relationships.
33
51
# * <tt>:only</tt> - A class, module or Array of classes and/or modules that are
34
52
# allowed to be located. Passing one or more classes limits instances of returned
35
53
# classes to those classes or their subclasses. Passing one or more modules in limits
@@ -51,6 +69,10 @@ def locate_many(gids, options = {})
51
69
# Takes either a SignedGlobalID or a string that can be turned into a SignedGlobalID
52
70
#
53
71
# Options:
72
+ # * <tt>:includes</tt> - A Symbol, Array, Hash or combination of them
73
+ # The same structure you would pass into a includes method of Active Record.
74
+ # @see https://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations
75
+ # If present, locate_signed will load all the relationships specified here.
54
76
# * <tt>:only</tt> - A class, module or Array of classes and/or modules that are
55
77
# allowed to be located. Passing one or more classes limits instances of returned
56
78
# classes to those classes or their subclasses. Passing one or more modules in limits
@@ -68,6 +90,11 @@ def locate_signed(sgid, options = {})
68
90
# the results to match the order in which the gids were passed.
69
91
#
70
92
# Options:
93
+ # * <tt>:includes</tt> - A Symbol, Array, Hash or combination of them
94
+ # The same structure you would pass into a includes method of Active Record.
95
+ # @see https://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations
96
+ # If present, locate_many_signed will load all the relationships specified here.
97
+ # Note: It only works if all the gids models have that relationships.
71
98
# * <tt>:only</tt> - A class, module or Array of classes and/or modules that are
72
99
# allowed to be located. Passing one or more classes limits instances of returned
73
100
# classes to those classes or their subclasses. Passing one or more modules in limits
@@ -84,7 +111,7 @@ def locate_many_signed(sgids, options = {})
84
111
#
85
112
# Using a block:
86
113
#
87
- # GlobalID::Locator.use :foo do |gid|
114
+ # GlobalID::Locator.use :foo do |gid, options |
88
115
# FooRemote.const_get(gid.model_name).find(gid.model_id)
89
116
# end
90
117
#
@@ -93,7 +120,7 @@ def locate_many_signed(sgids, options = {})
93
120
# GlobalID::Locator.use :bar, BarLocator.new
94
121
#
95
122
# class BarLocator
96
- # def locate(gid)
123
+ # def locate(gid, options = {} )
97
124
# @search_client.search name: gid.model_name, id: gid.model_id
98
125
# end
99
126
# end
@@ -127,9 +154,12 @@ def normalize_app(app)
127
154
@locators = { }
128
155
129
156
class BaseLocator
130
- def locate ( gid )
157
+ def locate ( gid , options = { } )
131
158
return unless model_id_is_valid? ( gid )
132
- gid . model_class . find gid . model_id
159
+ model_class = gid . model_class
160
+ model_class = model_class . includes ( options [ :includes ] ) if options [ :includes ]
161
+
162
+ model_class . find gid . model_id
133
163
end
134
164
135
165
def locate_many ( gids , options = { } )
@@ -143,7 +173,7 @@ def locate_many(gids, options = {})
143
173
records_by_model_name_and_id = { }
144
174
145
175
ids_by_model . each do |model , ids |
146
- records = find_records ( model , ids , ignore_missing : options [ :ignore_missing ] )
176
+ records = find_records ( model , ids , ignore_missing : options [ :ignore_missing ] , includes : options [ :includes ] )
147
177
148
178
records_by_id = records . index_by do |record |
149
179
record . id . is_a? ( Array ) ? record . id . map ( &:to_s ) : record . id . to_s
@@ -157,6 +187,8 @@ def locate_many(gids, options = {})
157
187
158
188
private
159
189
def find_records ( model_class , ids , options )
190
+ model_class = model_class . includes ( options [ :includes ] ) if options [ :includes ]
191
+
160
192
if options [ :ignore_missing ]
161
193
model_class . where ( model_class . primary_key => ids )
162
194
else
@@ -170,7 +202,7 @@ def model_id_is_valid?(gid)
170
202
end
171
203
172
204
class UnscopedLocator < BaseLocator
173
- def locate ( gid )
205
+ def locate ( gid , options = { } )
174
206
unscoped ( gid . model_class ) { super }
175
207
end
176
208
@@ -194,12 +226,12 @@ def initialize(block)
194
226
@locator = block
195
227
end
196
228
197
- def locate ( gid )
198
- @locator . call ( gid )
229
+ def locate ( gid , options = { } )
230
+ @locator . call ( gid , options )
199
231
end
200
232
201
233
def locate_many ( gids , options = { } )
202
- gids . map { |gid | locate ( gid ) }
234
+ gids . map { |gid | locate ( gid , options ) }
203
235
end
204
236
end
205
237
end
0 commit comments