Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/global_id/identification.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "active_support/core_ext/class/attribute"

class GlobalID
# Mix `GlobalID::Identification` into any model with a `#find(id)` class
# method. Support is automatically included in Active Record.
Expand Down Expand Up @@ -26,6 +28,9 @@ class GlobalID
# GlobalID::Locator.locate person_gid
# # => #<Person:0x007fae94bf6298 @id="1">
module Identification
def self.included(base)
base.class_attribute :global_id_column, default: :id, instance_writer: false
end

# Returns the Global ID of the model.
#
Expand Down
6 changes: 5 additions & 1 deletion lib/global_id/locator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ def locate(gid, options = {})
model_class = gid.model_class
model_class = model_class.includes(options[:includes]) if options[:includes]

model_class.find gid.model_id
if model_class.global_id_column == :id
model_class.find gid.model_id
else
model_class.find_by model_class.global_id_column => gid.model_id
end
Comment on lines +165 to +169
Copy link
Contributor Author

@tylerwillingham tylerwillingham Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💭 Simplifying on find_by (or find_by!) for ActiveRecord users feels nice, but I stopped short of doing that because I recall seeing that globalid doesn't want to make ORM assumptions

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It already does with the includes? I think we should be using find_by! as well to match the find.

Also, should locate_many be updated as well?

Copy link
Contributor Author

@tylerwillingham tylerwillingham Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rafaelfranca good feedback 👍

I'm looking at implementing #locate_many and I'm a little stumped. Here's my hang-up:
The current implementation uses .find with an array of IDs for the default path. That's supported in ActiveRecord for configurable primary keys and composite primary keys, but what I'm missing is mimicking where!(model.global_id_column => array_of_ids) behavior.

I could take a page out of ActiveRecord's book and implement something similar to the totals checked in the private API of find_some... but I'm trying to be mindful of the risk of overloading BaseLocator

I'm going to take some time and come back to this over the next few days.

end

def locate_many(gids, options = {})
Expand Down
2 changes: 1 addition & 1 deletion lib/global_id/uri/gid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def parse(uri)
#
# URI::GID.create('bcx', Person.find(5), database: 'superhumans')
def create(app, model, params = nil)
build app: app, model_name: model.class.name, model_id: model.id, params: params
build app: app, model_name: model.class.name, model_id: model.public_send(model.global_id_column), params: params
end

# Create a new URI::GID from components with argument check.
Expand Down
13 changes: 13 additions & 0 deletions test/cases/global_id_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
@person_namespaced_gid = GlobalID.create(Person::Child.new(4))
@person_model_gid = GlobalID.create(PersonModel.new(id: 1))
@cpk_model_gid = GlobalID.create(CompositePrimaryKeyModel.new(id: ["tenant-key-value", "id-value"]))
@ckm_model = ConfigurableKeyModel.new(id: "id-value", external_id: "external-id-value")
@ckm_model_gid = GlobalID.create(@ckm_model)
end

test 'find' do
Expand All @@ -53,13 +55,15 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
assert_equal Person::Child.find(@person_namespaced_gid.model_id), @person_namespaced_gid.find
assert_equal PersonModel.find(@person_model_gid.model_id), @person_model_gid.find
assert_equal CompositePrimaryKeyModel.find(@cpk_model_gid.model_id), @cpk_model_gid.find
assert_equal ConfigurableKeyModel.find_by(external_id: @ckm_model_gid.model_id), @ckm_model_gid.find
end

test 'find with class' do
assert_equal Person.find(@person_gid.model_id), @person_gid.find(only: Person)
assert_equal Person.find(@person_uuid_gid.model_id), @person_uuid_gid.find(only: Person)
assert_equal PersonModel.find(@person_model_gid.model_id), @person_model_gid.find(only: PersonModel)
assert_equal CompositePrimaryKeyModel.find(@cpk_model_gid.model_id), @cpk_model_gid.find(only: CompositePrimaryKeyModel)
assert_equal ConfigurableKeyModel.find_by(external_id: @ckm_model_gid.model_id), @ckm_model_gid.find(only: ConfigurableKeyModel)
end

test 'find with class no match' do
Expand All @@ -68,6 +72,7 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
assert_nil @person_namespaced_gid.find(only: String)
assert_nil @person_model_gid.find(only: Float)
assert_nil @cpk_model_gid.find(only: Hash)
assert_nil @ckm_model_gid.find(only: Hash)
end

test 'find with subclass' do
Expand Down Expand Up @@ -140,6 +145,7 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
assert_equal 'gid://bcx/Person::Child/4', @person_namespaced_gid.to_s
assert_equal 'gid://bcx/PersonModel/1', @person_model_gid.to_s
assert_equal 'gid://bcx/CompositePrimaryKeyModel/tenant-key-value/id-value', @cpk_model_gid.to_s
assert_equal 'gid://bcx/ConfigurableKeyModel/external-id-value', @ckm_model_gid.to_s
end

test 'as param' do
Expand All @@ -166,6 +172,7 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
assert_equal URI('gid://bcx/Person::Child/4'), @person_namespaced_gid.uri
assert_equal URI('gid://bcx/PersonModel/1'), @person_model_gid.uri
assert_equal URI('gid://bcx/CompositePrimaryKeyModel/tenant-key-value/id-value'), @cpk_model_gid.uri
assert_equal URI('gid://bcx/ConfigurableKeyModel/external-id-value'), @ckm_model_gid.uri
end

test 'as JSON' do
Expand All @@ -183,6 +190,9 @@ class GlobalIDCreationTest < ActiveSupport::TestCase

assert_equal 'gid://bcx/CompositePrimaryKeyModel/tenant-key-value/id-value', @cpk_model_gid.as_json
assert_equal '"gid://bcx/CompositePrimaryKeyModel/tenant-key-value/id-value"', @cpk_model_gid.to_json

assert_equal 'gid://bcx/ConfigurableKeyModel/external-id-value', @ckm_model_gid.as_json
assert_equal '"gid://bcx/ConfigurableKeyModel/external-id-value"', @ckm_model_gid.to_json
end

test 'model id' do
Expand All @@ -191,6 +201,7 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
assert_equal '4', @person_namespaced_gid.model_id
assert_equal '1', @person_model_gid.model_id
assert_equal ['tenant-key-value', 'id-value'], @cpk_model_gid.model_id
assert_equal 'external-id-value', @ckm_model_gid.model_id
end

test 'model name' do
Expand All @@ -199,6 +210,7 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
assert_equal 'Person::Child', @person_namespaced_gid.model_name
assert_equal 'PersonModel', @person_model_gid.model_name
assert_equal 'CompositePrimaryKeyModel', @cpk_model_gid.model_name
assert_equal 'ConfigurableKeyModel', @ckm_model_gid.model_name
end

test 'model class' do
Expand All @@ -207,6 +219,7 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
assert_equal Person::Child, @person_namespaced_gid.model_class
assert_equal PersonModel, @person_model_gid.model_class
assert_equal CompositePrimaryKeyModel, @cpk_model_gid.model_class
assert_equal ConfigurableKeyModel, @ckm_model_gid.model_class
assert_raise ArgumentError do
GlobalID.find 'gid://bcx/SignedGlobalID/5'
end
Expand Down
25 changes: 25 additions & 0 deletions test/cases/uri_gid_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ class URI::GIDTest < ActiveSupport::TestCase
@gid = URI::GID.parse(@gid_string)
@cpk_gid_string = 'gid://bcx/CompositePrimaryKeyModel/tenant-key-value/id-value'
@cpk_gid = URI::GID.parse(@cpk_gid_string)
@ckm_gid_string = 'gid://bcx/ConfigurableKeyModel/external-id-123'
@ckm_gid = URI::GID.parse(@ckm_gid_string)
end

test 'parsed' do
assert_equal @gid.app, 'bcx'
assert_equal @gid.model_name, 'Person'
assert_equal @gid.model_id, '5'
assert_equal ["tenant-key-value", "id-value"], @cpk_gid.model_id
assert_equal @ckm_gid.app, 'bcx'
assert_equal @ckm_gid.model_name, 'ConfigurableKeyModel'
assert_equal @ckm_gid.model_id, 'external-id-123'
end

test 'parsed for non existing model class' do
Expand All @@ -39,6 +44,11 @@ class URI::GIDTest < ActiveSupport::TestCase
assert_equal @cpk_gid_string, URI::GID.create('bcx', model).to_s
end

test 'create from a configurable key model' do
model = ConfigurableKeyModel.new(id: 'id-value', external_id: 'external-id-123')
assert_equal @ckm_gid_string, URI::GID.create('bcx', model).to_s
end

test 'build' do
array = URI::GID.build(['bcx', 'Person', '5', nil])
assert array
Expand All @@ -65,6 +75,21 @@ class URI::GIDTest < ActiveSupport::TestCase
assert_equal("gid://bcx/CompositePrimaryKeyModel/tenant-key-value/id-value", array.to_s)
end

# NOTE: I'm not sure if this test is valuable, it's pretty duplicative with the standard
# test path, but with a different value passed in for `model_id:`
test 'build with a configurable key model' do
array = URI::GID.build(['bcx', 'ConfigurableKeyModel', 'external-id-123', nil])
gid = URI::GID.build(
app: 'bcx',
model_name: 'ConfigurableKeyModel',
model_id: 'external-id-123',
params: nil
)

assert_equal array, gid
assert_equal 'gid://bcx/ConfigurableKeyModel/external-id-123', array.to_s
end

test 'build with wrong ordered array creates a wrong ordered gid' do
assert_not_equal @gid_string, URI::GID.build(['Person', '5', 'bcx', nil]).to_s
end
Expand Down
1 change: 1 addition & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'models/person'
require 'models/person_model'
require 'models/composite_primary_key_model'
require 'models/configurable_key_model'

require 'json'

Expand Down
28 changes: 28 additions & 0 deletions test/models/configurable_key_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'active_model'

class ConfigurableKeyModel
include ActiveModel::Model
include GlobalID::Identification

attr_accessor :id, :external_id

self.global_id_column = :external_id

class << self
def primary_key
:id
end

def find(_id)
raise ".find is not supported for ConfigurableKeyModel"
end

def find_by(external_id:)
new external_id: external_id, id: "id-value"
end
end

def ==(other)
external_id == other.try(:external_id)
end
end
Loading