globalid 1.4.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +62 -2
- data/lib/global_id/fixture_set.rb +3 -0
- data/lib/global_id/global_id.rb +2 -1
- data/lib/global_id/identification.rb +46 -8
- data/lib/global_id/locator.rb +4 -4
- data/lib/global_id/railtie.rb +2 -0
- data/lib/global_id/uri/gid.rb +16 -1
- data/lib/global_id/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 804cac003e6d65c3f64578d921fd60f6913eb101b3646959edca3a2488631f70
|
|
4
|
+
data.tar.gz: ba0bace915ba29d39512e7fe6dd194c08480f0a2ba9c99ef938d22d9db1829ba
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9e4fe6a1b7b45c0dfac5f296515d207df090f04ac5c44f2fb883cb7544113ed4cc5dbdf027a32b9ecf6b5cc4c7ec0c33c137a0be4e34587e4aa590fcbf3a8632
|
|
7
|
+
data.tar.gz: 0b9f96a48be439e71bdc6680a116cb72a31688f99ae74b5e94f5497e4e8abcd91f647d76444d62c29a99eb8d85a32ccf63c60ef154deeed136b85224441bdda4
|
data/README.md
CHANGED
|
@@ -20,8 +20,9 @@ IDs, we have a universal identifier that works for objects of both classes.
|
|
|
20
20
|
|
|
21
21
|
## Usage
|
|
22
22
|
|
|
23
|
-
Mix `GlobalID::Identification` into any model with a
|
|
24
|
-
|
|
23
|
+
Mix `GlobalID::Identification` into any model with a `.find(id)` class method that returns
|
|
24
|
+
an instance of the model, and a `.where(id:)` class method that returns an enumerable of
|
|
25
|
+
instance(s). Support is automatically included in Active Record.
|
|
25
26
|
|
|
26
27
|
```ruby
|
|
27
28
|
person_gid = Person.find(1).to_global_id
|
|
@@ -273,6 +274,65 @@ This allows you to work with Global IDs that reference models that don't exist i
|
|
|
273
274
|
|
|
274
275
|
**Note**: For backward compatibility, if a custom locator doesn't implement `model_class`, GlobalID will fall back to the default behavior (`constantize`) but will emit a deprecation warning. To avoid this, inherit from `GlobalID::Locator::BaseLocator` or `GlobalID::Locator::UnscopedLocator`.
|
|
275
276
|
|
|
277
|
+
### Naming models by `polymorphic_name`
|
|
278
|
+
|
|
279
|
+
Active Record names a model in polymorphic associations by `polymorphic_name`,
|
|
280
|
+
which the model can override to keep its stored name across a rename:
|
|
281
|
+
|
|
282
|
+
```ruby
|
|
283
|
+
class Person < ApplicationRecord
|
|
284
|
+
# Renamed from Human
|
|
285
|
+
def self.polymorphic_name
|
|
286
|
+
"Human"
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
Set `config.global_id.use_polymorphic_name` to name models in Global IDs the
|
|
292
|
+
same way, so a rename keeps their Global IDs stable too:
|
|
293
|
+
|
|
294
|
+
```ruby
|
|
295
|
+
# config/application.rb
|
|
296
|
+
config.global_id.use_polymorphic_name = true
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
```ruby
|
|
300
|
+
Person.find(1).to_global_id.to_s
|
|
301
|
+
# => "gid://app/Human/1"
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
Outside Rails, set it like so:
|
|
305
|
+
|
|
306
|
+
```ruby
|
|
307
|
+
GlobalID.use_polymorphic_name = true
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
Only models whose `polymorphic_name` differs from their class name get
|
|
311
|
+
different Global IDs. A single table inheritance subclass keeps its class name,
|
|
312
|
+
since its `polymorphic_name` is its base class's. With
|
|
313
|
+
`store_full_class_name = false`, Global IDs carry the short name too, so the
|
|
314
|
+
app's locator has to resolve it. Fixture Global IDs
|
|
315
|
+
(`global_id("people", :david)` in a fixture file) follow the same rule.
|
|
316
|
+
|
|
317
|
+
Resolving the old name is the app's job. Map it in a custom locator's
|
|
318
|
+
`model_class`:
|
|
319
|
+
|
|
320
|
+
```ruby
|
|
321
|
+
class PersonLocator < GlobalID::Locator::UnscopedLocator
|
|
322
|
+
def model_class(gid)
|
|
323
|
+
gid.model_name == "Human" ? Person : super
|
|
324
|
+
end
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
GlobalID::Locator.use :app, PersonLocator.new
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
or keep a constant alias for the old name:
|
|
331
|
+
|
|
332
|
+
```ruby
|
|
333
|
+
Human = Person
|
|
334
|
+
```
|
|
335
|
+
|
|
276
336
|
### Custom Default Locator
|
|
277
337
|
|
|
278
338
|
A custom default locator can be set for an app by calling `GlobalID::Locator.default_locator=` and providing a default locator to use for that app.
|
|
@@ -14,6 +14,9 @@ class GlobalID
|
|
|
14
14
|
def create_global_id(fixture_set_name, label, klass:, column_type: :integer, **options)
|
|
15
15
|
identifier = identify(label, column_type)
|
|
16
16
|
model_name = default_fixture_model_name(fixture_set_name)
|
|
17
|
+
if GlobalID.use_polymorphic_name && (model_class = model_name.safe_constantize)
|
|
18
|
+
model_name = URI::GID.model_name_for(model_class)
|
|
19
|
+
end
|
|
17
20
|
uri = URI::GID.build([GlobalID.app, model_name, identifier, {}])
|
|
18
21
|
klass.new(uri, **options)
|
|
19
22
|
end
|
data/lib/global_id/global_id.rb
CHANGED
|
@@ -8,10 +8,11 @@ require 'global_id/uri/gid'
|
|
|
8
8
|
class GlobalID
|
|
9
9
|
class << self
|
|
10
10
|
attr_reader :app
|
|
11
|
+
attr_accessor :use_polymorphic_name
|
|
11
12
|
|
|
12
13
|
def create(model, options = {})
|
|
13
14
|
if app = options.fetch(:app) { GlobalID.app }
|
|
14
|
-
params = options.except(:app, :verifier, :for)
|
|
15
|
+
params = options.except(:app, :verifier, :for, :expires_in, :expires_at)
|
|
15
16
|
new URI::GID.create(app, model, params), options
|
|
16
17
|
else
|
|
17
18
|
raise ArgumentError, 'An app is required to create a GlobalID. ' \
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
|
+
require 'active_support/concern'
|
|
3
|
+
require 'active_support/core_ext/class/attribute'
|
|
4
|
+
|
|
2
5
|
class GlobalID
|
|
3
6
|
# Mix `GlobalID::Identification` into any model with a `#find(id)` class
|
|
4
7
|
# method. Support is automatically included in Active Record.
|
|
@@ -27,6 +30,23 @@ class GlobalID
|
|
|
27
30
|
# GlobalID::Locator.locate person_gid
|
|
28
31
|
# # => #<Person:0x007fae94bf6298 @id="1">
|
|
29
32
|
module Identification
|
|
33
|
+
extend ActiveSupport::Concern
|
|
34
|
+
|
|
35
|
+
included do
|
|
36
|
+
class_attribute :_global_id_app, instance_accessor: false
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
class_methods do
|
|
40
|
+
# Returns a class-specific app to use when creating Global IDs.
|
|
41
|
+
def global_id_app
|
|
42
|
+
_global_id_app
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Sets a class-specific app to use when creating Global IDs.
|
|
46
|
+
def global_id_app=(app)
|
|
47
|
+
self._global_id_app = URI::GID.validate_app(app)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
30
50
|
|
|
31
51
|
# Returns the Global ID of the model.
|
|
32
52
|
#
|
|
@@ -36,16 +56,20 @@ class GlobalID
|
|
|
36
56
|
# global_id.model_id # => "1"
|
|
37
57
|
# global_id.to_param # => "Z2lkOi8vYm9yZGZvbGlvL1BlcnNvbi8x"
|
|
38
58
|
def to_global_id(options = {})
|
|
39
|
-
GlobalID.create(self, options)
|
|
59
|
+
GlobalID.create(self, global_id_options(options))
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Returns the Global ID of the model. Shortcut for `#to_global_id`.
|
|
63
|
+
def to_gid(...)
|
|
64
|
+
to_global_id(...)
|
|
40
65
|
end
|
|
41
|
-
alias to_gid to_global_id
|
|
42
66
|
|
|
43
67
|
# Returns the Global ID parameter of the model.
|
|
44
68
|
#
|
|
45
69
|
# model = Person.new id: 1
|
|
46
70
|
# model.to_gid_param # => ""Z2lkOi8vYm9yZGZvbGlvL1BlcnNvbi8x"
|
|
47
|
-
def to_gid_param(
|
|
48
|
-
to_global_id(
|
|
71
|
+
def to_gid_param(...)
|
|
72
|
+
to_global_id(...).to_param
|
|
49
73
|
end
|
|
50
74
|
|
|
51
75
|
# Returns the Signed Global ID of the model.
|
|
@@ -106,16 +130,30 @@ class GlobalID
|
|
|
106
130
|
# GlobalID::Locator.locate_signed(signup_person_sgid.to_s, for: 'signup_form')
|
|
107
131
|
# => #<Person:0x007fae94bf6298 @id="1">
|
|
108
132
|
def to_signed_global_id(options = {})
|
|
109
|
-
SignedGlobalID.create(self, options)
|
|
133
|
+
SignedGlobalID.create(self, global_id_options(options))
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Returns the Signed Global ID of the model. Shortcut for `#to_signed_global_id`.
|
|
137
|
+
def to_sgid(...)
|
|
138
|
+
to_signed_global_id(...)
|
|
110
139
|
end
|
|
111
|
-
alias to_sgid to_signed_global_id
|
|
112
140
|
|
|
113
141
|
# Returns the Signed Global ID parameter.
|
|
114
142
|
#
|
|
115
143
|
# model = Person.new id: 1
|
|
116
144
|
# model.to_sgid_param # => "BAh7CEkiCGdpZAY6BkVUSSIiZ2..."
|
|
117
|
-
def to_sgid_param(
|
|
118
|
-
to_signed_global_id(
|
|
145
|
+
def to_sgid_param(...)
|
|
146
|
+
to_signed_global_id(...).to_param
|
|
119
147
|
end
|
|
148
|
+
|
|
149
|
+
private
|
|
150
|
+
def global_id_options(options)
|
|
151
|
+
return options if options.key?(:app)
|
|
152
|
+
|
|
153
|
+
app = self.class.global_id_app
|
|
154
|
+
return options unless app
|
|
155
|
+
|
|
156
|
+
options.merge(app: app)
|
|
157
|
+
end
|
|
120
158
|
end
|
|
121
159
|
end
|
data/lib/global_id/locator.rb
CHANGED
|
@@ -224,7 +224,7 @@ class GlobalID
|
|
|
224
224
|
ids_by_model[gid.model_class] << gid.model_id
|
|
225
225
|
end
|
|
226
226
|
|
|
227
|
-
|
|
227
|
+
records_by_model_and_id = {}
|
|
228
228
|
|
|
229
229
|
ids_by_model.each do |model, ids|
|
|
230
230
|
records = find_records(model, ids, ignore_missing: options[:ignore_missing], includes: options[:includes])
|
|
@@ -233,17 +233,17 @@ class GlobalID
|
|
|
233
233
|
record.id.is_a?(Array) ? record.id.map(&:to_s) : record.id.to_s
|
|
234
234
|
end
|
|
235
235
|
|
|
236
|
-
|
|
236
|
+
records_by_model_and_id[model] = records_by_id
|
|
237
237
|
end
|
|
238
238
|
|
|
239
|
-
gids.filter_map { |gid|
|
|
239
|
+
gids.filter_map { |gid| records_by_model_and_id.dig(gid.model_class, gid.model_id) }
|
|
240
240
|
end
|
|
241
241
|
|
|
242
242
|
private
|
|
243
243
|
def find_records(model_class, ids, options)
|
|
244
244
|
model_class = model_class.includes(options[:includes]) if options[:includes]
|
|
245
245
|
|
|
246
|
-
if options[:ignore_missing]
|
|
246
|
+
if options[:ignore_missing] && model_class.respond_to?(:where)
|
|
247
247
|
model_class.where(primary_key(model_class) => ids)
|
|
248
248
|
else
|
|
249
249
|
model_class.find(ids)
|
data/lib/global_id/railtie.rb
CHANGED
|
@@ -16,10 +16,12 @@ class GlobalID
|
|
|
16
16
|
default_app_name = app.railtie_name.remove('_application').dasherize
|
|
17
17
|
|
|
18
18
|
GlobalID.app = app.config.global_id.app ||= default_app_name
|
|
19
|
+
GlobalID.use_polymorphic_name = app.config.global_id.fetch(:use_polymorphic_name, false)
|
|
19
20
|
SignedGlobalID.expires_in = app.config.global_id.fetch(:expires_in, default_expires_in)
|
|
20
21
|
|
|
21
22
|
config.after_initialize do
|
|
22
23
|
GlobalID.app = app.config.global_id.app ||= default_app_name
|
|
24
|
+
GlobalID.use_polymorphic_name = app.config.global_id.fetch(:use_polymorphic_name, false)
|
|
23
25
|
SignedGlobalID.expires_in = app.config.global_id.fetch(:expires_in, default_expires_in)
|
|
24
26
|
|
|
25
27
|
app.config.global_id.verifier ||= begin
|
data/lib/global_id/uri/gid.rb
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
require 'uri/generic'
|
|
3
|
+
require 'cgi/escape'
|
|
3
4
|
require 'active_support/core_ext/module/aliasing'
|
|
4
5
|
require 'active_support/core_ext/object/blank'
|
|
5
6
|
require 'active_support/core_ext/hash/indifferent_access'
|
|
@@ -74,7 +75,16 @@ module URI
|
|
|
74
75
|
#
|
|
75
76
|
# URI::GID.create('bcx', Person.find(5), database: 'superhumans')
|
|
76
77
|
def create(app, model, params = nil)
|
|
77
|
-
build app: app, model_name: model.class
|
|
78
|
+
build app: app, model_name: model_name_for(model.class), model_id: model.id, params: params
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Subclasses keep their class name, since their +polymorphic_name+ is the base class's.
|
|
82
|
+
def model_name_for(model_class) # :nodoc:
|
|
83
|
+
if GlobalID.use_polymorphic_name && model_class.respond_to?(:polymorphic_name) && base_class?(model_class)
|
|
84
|
+
model_class.polymorphic_name
|
|
85
|
+
else
|
|
86
|
+
model_class.name
|
|
87
|
+
end
|
|
78
88
|
end
|
|
79
89
|
|
|
80
90
|
# Create a new URI::GID from components with argument check.
|
|
@@ -101,6 +111,11 @@ module URI
|
|
|
101
111
|
|
|
102
112
|
super parts
|
|
103
113
|
end
|
|
114
|
+
|
|
115
|
+
private
|
|
116
|
+
def base_class?(model_class)
|
|
117
|
+
!model_class.respond_to?(:base_class) || model_class.base_class == model_class
|
|
118
|
+
end
|
|
104
119
|
end
|
|
105
120
|
|
|
106
121
|
def to_s
|
data/lib/global_id/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: globalid
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- David Heinemeier Hansson
|
|
@@ -75,9 +75,9 @@ licenses:
|
|
|
75
75
|
- MIT
|
|
76
76
|
metadata:
|
|
77
77
|
bug_tracker_uri: https://github.com/rails/globalid/issues
|
|
78
|
-
changelog_uri: https://github.com/rails/globalid/releases/tag/v1.
|
|
78
|
+
changelog_uri: https://github.com/rails/globalid/releases/tag/v1.5.0
|
|
79
79
|
mailing_list_uri: https://discuss.rubyonrails.org/c/rubyonrails-talk
|
|
80
|
-
source_code_uri: https://github.com/rails/globalid/tree/v1.
|
|
80
|
+
source_code_uri: https://github.com/rails/globalid/tree/v1.5.0
|
|
81
81
|
rubygems_mfa_required: 'true'
|
|
82
82
|
rdoc_options: []
|
|
83
83
|
require_paths:
|
|
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
93
93
|
- !ruby/object:Gem::Version
|
|
94
94
|
version: '0'
|
|
95
95
|
requirements: []
|
|
96
|
-
rubygems_version: 4.0.
|
|
96
|
+
rubygems_version: 4.0.20
|
|
97
97
|
specification_version: 4
|
|
98
98
|
summary: 'Refer to any model with a URI: gid://app/class/id'
|
|
99
99
|
test_files: []
|