globalid 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of globalid might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/lib/global_id/locator.rb +17 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0900d9c61b92f834447117f95eb42e288bc4c295
|
4
|
+
data.tar.gz: a16f774ad3ff54c240b65575316d88186f7de201
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c011c4f6d933622e912095363f7443291fff84e51cedd885cf75d3d30c64bd11ddc9a23d9be58428f24fe2672793aff9252ad8ddfce7999ff47d6083b779e04b
|
7
|
+
data.tar.gz: 939192c118d11e7e2975e4d20042b4fec032a5bb8970917b608b079062da32ae2f0b7188d0cd029f6012b5a22fb6d1010e92d62e7109d7deaea8521a43411dcb
|
data/lib/global_id/locator.rb
CHANGED
@@ -22,8 +22,8 @@ class GlobalID
|
|
22
22
|
# The GlobalIDs are located using Model.find(array_of_ids), so the models must respond to
|
23
23
|
# that finder signature.
|
24
24
|
#
|
25
|
-
# This approach will efficiently call only one #find
|
26
|
-
# the results to match the order in which the gids were passed.
|
25
|
+
# This approach will efficiently call only one #find (or #where(id: id), when using ignore_missing)
|
26
|
+
# per model class, but still interpolate the results to match the order in which the gids were passed.
|
27
27
|
#
|
28
28
|
# Options:
|
29
29
|
# * <tt>:only</tt> - A class, module or Array of classes and/or modules that are
|
@@ -31,11 +31,17 @@ class GlobalID
|
|
31
31
|
# classes to those classes or their subclasses. Passing one or more modules in limits
|
32
32
|
# instances of returned classes to those including that module. If no classes or
|
33
33
|
# modules match, +nil+ is returned.
|
34
|
+
# * <tt>:ignore_missing</tt> - By default, locate_many will call #find on the model to locate the
|
35
|
+
# ids extracted from the GIDs. In Active Record (and other data stores following the same pattern),
|
36
|
+
# #find will raise an exception if a named ID can't be found. When you set this option to true,
|
37
|
+
# we will use #where(id: ids) instead, which does not raise on missing records.
|
34
38
|
def locate_many(gids, options = {})
|
35
39
|
if (allowed_gids = parse_allowed(gids, options[:only])).any?
|
36
40
|
models_and_ids = allowed_gids.collect { |gid| [ gid.model_name.constantize, gid.model_id ] }
|
37
41
|
ids_by_model = models_and_ids.group_by(&:first)
|
38
|
-
loaded_by_model = Hash[ids_by_model.map { |model, ids|
|
42
|
+
loaded_by_model = Hash[ids_by_model.map { |model, ids|
|
43
|
+
[ model, find_records(model, ids.map(&:last), ignore_missing: options[:ignore_missing]).index_by { |record| record.id.to_s } ]
|
44
|
+
}]
|
39
45
|
|
40
46
|
models_and_ids.collect { |(model, id)| loaded_by_model[model][id] }.compact
|
41
47
|
else
|
@@ -116,6 +122,14 @@ class GlobalID
|
|
116
122
|
def normalize_app(app)
|
117
123
|
app.to_s.downcase
|
118
124
|
end
|
125
|
+
|
126
|
+
def find_records(model_class, ids, ignore_missing:)
|
127
|
+
if ignore_missing
|
128
|
+
model_class.where(id: ids)
|
129
|
+
else
|
130
|
+
model_class.find(ids)
|
131
|
+
end
|
132
|
+
end
|
119
133
|
end
|
120
134
|
|
121
135
|
private
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: globalid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Heinemeier Hansson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-04-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|