globalid 1.2.1 → 1.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 18495b4f7910329fd0a65dcfa294742d0cdbb129a4c7f6971c4f9c62e9ea890d
4
- data.tar.gz: 5b6a25aabd0d04d9a4a2d4bb4d5152d68a67758fad96deabc011a4a169f2a055
3
+ metadata.gz: e7f6fd8b8b0b07aad73739e2004b5c4229e48dfa64960dcfa243f6c1b6276b44
4
+ data.tar.gz: a1cc24c9968a2236d2974f4ecac4d3fbb8513e4ac74d18685185afa684fad41d
5
5
  SHA512:
6
- metadata.gz: fb525d60050154b885197a89aaf2b1f2f577d0574b83160a3c0cbc2899d67e8995cf23c2de852c30baaa1ef8cc594cad39187e3808b350195c95ead32359957c
7
- data.tar.gz: 5dda5c09cffcc6e60da06931f3be5ba144db99f6ed0b9d7dc085633e0bac642f1c9d9ac7ba686dd59a6b9be64bb275ee8e78b39fae6e372b69c8131709db6f0f
6
+ metadata.gz: 68bd4cc42217eb59cff8b1f73c989e9f17ddce332f7d65f533e5b2fa6e429a10328d9c5b02f165e778cd83c02afa095103984910c098e75c9dcab788a4c97024
7
+ data.tar.gz: 652bc775cb1da110f6eca011d5a6ef3d907f79ddbfda755206d84eddca06c2153b734befb5b5dc6ea09ed911b87c72fcae6f9bfb5554da1b4616d60d22a298fb
data/README.md CHANGED
@@ -57,7 +57,7 @@ GlobalID::Locator.locate_signed person_sgid
57
57
 
58
58
  **Expiration**
59
59
 
60
- Signed Global IDs can expire some time in the future. This is useful if there's a resource
60
+ Signed Global IDs can expire sometime in the future. This is useful if there's a resource
61
61
  people shouldn't have indefinite access to, like a share link.
62
62
 
63
63
  ```ruby
@@ -73,8 +73,8 @@ GlobalID::Locator.locate_signed(expiring_sgid.to_s, for: 'sharing')
73
73
  # => nil
74
74
  ```
75
75
 
76
- **In Rails, an auto-expiry of 1 month is set by default.** You can alter that deal
77
- in an initializer with:
76
+ **In Rails, an auto-expiry of 1 month is set by default.** You can alter that
77
+ default in an initializer with:
78
78
 
79
79
  ```ruby
80
80
  # config/initializers/global_id.rb
@@ -87,7 +87,7 @@ You can assign a default SGID lifetime like so:
87
87
  SignedGlobalID.expires_in = 1.month
88
88
  ```
89
89
 
90
- This way any generated SGID will use that relative expiry.
90
+ This way, any generated SGID will use that relative expiry.
91
91
 
92
92
  It's worth noting that _expiring SGIDs are not idempotent_ because they encode the current timestamp; repeated calls to `to_sgid` will produce different results. For example, in Rails
93
93
 
@@ -165,19 +165,19 @@ Note the order is maintained in the returned results.
165
165
 
166
166
  Either `GlobalID::Locator.locate` or `GlobalID::Locator.locate_many` supports a hash of options as second parameter. The supported options are:
167
167
 
168
- * :includes - A Symbol, Array, Hash or combination of them
169
- The same structure you would pass into a `includes` method of Active Record.
170
- See [Active Record eager loading associations](https://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations)
168
+ * `:includes` - A Symbol, Array, Hash or combination of them.
169
+ The same structure you would pass into an `includes` method of Active Record.
170
+ See [Active Record eager loading associations](https://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations).
171
171
  If present, `locate` or `locate_many` will eager load all the relationships specified here.
172
- Note: It only works if all the gids models have that relationships.
173
- * :only - A class, module or Array of classes and/or modules that are
172
+ Note: It only works if all the GIDs Models have those relationships.
173
+ * `:only` - A class, module, or Array of classes and/or modules that are
174
174
  allowed to be located. Passing one or more classes limits instances of returned
175
175
  classes to those classes or their subclasses. Passing one or more modules in limits
176
176
  instances of returned classes to those including that module. If no classes or
177
- modules match, +nil+ is returned.
178
- * :ignore_missing (Only for `locate_many`) - By default, `locate_many` will call `#find` on the model to locate the
177
+ modules match, `nil` is returned.
178
+ * `:ignore_missing` (Only for `locate_many`) - By default, `locate_many` will call `#find` on the model to locate the
179
179
  ids extracted from the GIDs. In Active Record (and other data stores following the same pattern),
180
- `#find` will raise an exception if a named ID can't be found. When you set this option to true,
180
+ `#find` will raise an exception if a named ID can't be found. When you set this option to `true`,
181
181
  we will use `#where(id: ids)` instead, which does not raise on missing records.
182
182
 
183
183
  ### Custom App Locator
@@ -207,9 +207,31 @@ class BarLocator
207
207
  end
208
208
  ```
209
209
 
210
- After defining locators as above, URIs like "gid://foo/Person/1" and "gid://bar/Person/1" will now use the foo block locator and `BarLocator` respectively.
210
+ After defining locators as above, URIs like `gid://foo/Person/1` and `gid://bar/Person/1` will now use the foo block locator and `BarLocator` respectively.
211
211
  Other apps will still keep using the default locator.
212
212
 
213
+ ### Custom Default Locator
214
+
215
+ 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.
216
+
217
+ ```ruby
218
+ class MyCustomLocator < UnscopedLocator
219
+ def locate(gid, options = {})
220
+ ActiveRecord::Base.connected_to(role: :reading) do
221
+ super(gid, options)
222
+ end
223
+ end
224
+
225
+ def locate_many(gids, options = {})
226
+ ActiveRecord::Base.connected_to(role: :reading) do
227
+ super(gids, options)
228
+ end
229
+ end
230
+ end
231
+
232
+ GlobalID::Locator.default_locator = MyCustomLocator.new
233
+ ```
234
+
213
235
  ## Contributing to GlobalID
214
236
 
215
237
  GlobalID is work of many contributors. You're encouraged to submit pull requests, propose
@@ -32,6 +32,10 @@ class GlobalID
32
32
  @app = URI::GID.validate_app(app)
33
33
  end
34
34
 
35
+ def default_locator(default_locator)
36
+ Locator.default_locator = default_locator
37
+ end
38
+
35
39
  private
36
40
  def parse_encoded_gid(gid, options)
37
41
  new(Base64.urlsafe_decode64(gid), options) rescue nil
@@ -31,8 +31,8 @@ class GlobalID
31
31
  #
32
32
  # model = Person.new id: 1
33
33
  # global_id = model.to_global_id
34
- # global_id.modal_class # => Person
35
- # global_id.modal_id # => "1"
34
+ # global_id.model_class # => Person
35
+ # global_id.model_id # => "1"
36
36
  # global_id.to_param # => "Z2lkOi8vYm9yZGZvbGlvL1BlcnNvbi8x"
37
37
  def to_global_id(options = {})
38
38
  GlobalID.create(self, options)
@@ -52,8 +52,8 @@ class GlobalID
52
52
  #
53
53
  # model = Person.new id: 1
54
54
  # signed_global_id = model.to_signed_global_id
55
- # signed_global_id.modal_class # => Person
56
- # signed_global_id.modal_id # => "1"
55
+ # signed_global_id.model_class # => Person
56
+ # signed_global_id.model_id # => "1"
57
57
  # signed_global_id.to_param # => "BAh7CEkiCGdpZAY6BkVUSSIiZ2..."
58
58
  #
59
59
  # ==== Expiration
@@ -5,6 +5,9 @@ class GlobalID
5
5
  class InvalidModelIdError < StandardError; end
6
6
 
7
7
  class << self
8
+ # The default locator used when no app-specific locator is found.
9
+ attr_accessor :default_locator
10
+
8
11
  # Takes either a GlobalID or a string that can be turned into a GlobalID
9
12
  #
10
13
  # Options:
@@ -134,7 +137,7 @@ class GlobalID
134
137
 
135
138
  private
136
139
  def locator_for(gid)
137
- @locators.fetch(normalize_app(gid.app)) { DEFAULT_LOCATOR }
140
+ @locators.fetch(normalize_app(gid.app)) { default_locator }
138
141
  end
139
142
 
140
143
  def find_allowed?(model_class, only = nil)
@@ -223,7 +226,8 @@ class GlobalID
223
226
  end
224
227
  end
225
228
  end
226
- DEFAULT_LOCATOR = UnscopedLocator.new
229
+
230
+ self.default_locator = UnscopedLocator.new
227
231
 
228
232
  class BlockLocator
229
233
  def initialize(block)
@@ -36,6 +36,8 @@ module URI
36
36
  COMPOSITE_MODEL_ID_MAX_SIZE = 20
37
37
  COMPOSITE_MODEL_ID_DELIMITER = "/"
38
38
 
39
+ URI_PARSER = URI::RFC2396_Parser.new # :nodoc:
40
+
39
41
  class << self
40
42
  # Validates +app+'s as URI hostnames containing only alphanumeric characters
41
43
  # and hyphens. An ArgumentError is raised if +app+ is invalid.
@@ -62,7 +64,7 @@ module URI
62
64
  # URI.parse('gid://bcx') # => URI::GID instance
63
65
  # URI::GID.parse('gid://bcx/') # => raises URI::InvalidComponentError
64
66
  def parse(uri)
65
- generic_components = URI.split(uri) << nil << true # nil parser, true arg_check
67
+ generic_components = URI.split(uri) << URI_PARSER << true # RFC2396 parser, true arg_check
66
68
  new(*generic_components)
67
69
  end
68
70
 
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: globalid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2023-09-05 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: activesupport
@@ -60,8 +59,11 @@ homepage: http://www.rubyonrails.org
60
59
  licenses:
61
60
  - MIT
62
61
  metadata:
62
+ bug_tracker_uri: https://github.com/rails/globalid/issues
63
+ changelog_uri: https://github.com/rails/globalid/releases/tag/v1.3.0
64
+ mailing_list_uri: https://discuss.rubyonrails.org/c/rubyonrails-talk
65
+ source_code_uri: https://github.com/rails/globalid/tree/v1.3.0
63
66
  rubygems_mfa_required: 'true'
64
- post_install_message:
65
67
  rdoc_options: []
66
68
  require_paths:
67
69
  - lib
@@ -69,15 +71,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
69
71
  requirements:
70
72
  - - ">="
71
73
  - !ruby/object:Gem::Version
72
- version: 2.5.0
74
+ version: 2.7.0
73
75
  required_rubygems_version: !ruby/object:Gem::Requirement
74
76
  requirements:
75
77
  - - ">="
76
78
  - !ruby/object:Gem::Version
77
79
  version: '0'
78
80
  requirements: []
79
- rubygems_version: 3.4.1
80
- signing_key:
81
+ rubygems_version: 3.6.7
81
82
  specification_version: 4
82
83
  summary: 'Refer to any model with a URI: gid://app/class/id'
83
84
  test_files: []