module GlobalID::Identification
def to_gid_param(options = {})
model = Person.new id: 1
Returns the Global ID parameter of the model.
def to_gid_param(options = {}) to_global_id(options).to_param end
def to_global_id(options = {})
global_id.modal_id # => "1"
global_id.modal_class # => Person
global_id = model.to_global_id
model = Person.new id: 1
Returns the Global ID of the model.
def to_global_id(options = {}) GlobalID.create(self, options) end
def to_sgid_param(options = {})
model = Person.new id: 1
Returns the Signed Global ID parameter.
def to_sgid_param(options = {}) to_signed_global_id(options).to_param end
def to_signed_global_id(options = {})
GlobalID::Locator.locate_signed(signup_person_sgid.to_s, for: 'signup_form')
# => #
form's SGID on the login page. For example.
Signed Global ID is for. In this way evildoers can't reuse a sign-up
You can even bump the security up some more by explaining what purpose a
==== Purpose
Note that an explicit `:expires_at` takes precedence over a relative `:expires_in`.
# => nil
GlobalID::Locator.locate_signed explicit_expiring_sgid.to_s
# 1 hour later...
# => #
explicit_expiring_sgid = SecretAgentMessage.find(5).to_sgid(expires_at: Time.now.advance(hours: 1))
It's also possible to pass a specific expiry time
# => #
GlobalID::Locator.locate_signed never_expiring_sgid
# Any time later...
# => #
never_expiring_sgid = Document.find(5).to_sgid(expires_in: nil)
SGID that will not expire,
You need to explicitly pass `expires_in: nil` to generate a permanent
In Rails, an auto-expiry of 1 month is set by default.
# => nil
GlobalID::Locator.locate_signed(expiring_sgid.to_s, for: 'sharing')
# More than 2 hours later...
# => #
GlobalID::Locator.locate_signed(expiring_sgid.to_s, for: 'sharing')
# Within 2 hours...
# => #
expiring_sgid = Document.find(5).to_sgid(expires_in: 2.hours, for: 'sharing')
share link.
there's a resource people shouldn't have indefinite access to, like a
Signed Global IDs can expire some time in the future. This is useful if
==== Expiration
signed_global_id.to_param # => "BAh7CEkiCGdpZAY6BkVUSSIiZ2..."
signed_global_id.modal_id # => "1"
signed_global_id.modal_class # => Person
signed_global_id = model.to_signed_global_id
model = Person.new id: 1
Signed Global IDs ensure that the data hasn't been tampered with.
Returns the Signed Global ID of the model.
def to_signed_global_id(options = {}) SignedGlobalID.create(self, options) end