Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for non-PK columns #185

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
13 changes: 13 additions & 0 deletions lib/global_id/identification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ class GlobalID
# GlobalID::Locator.locate person_gid
# # => #<Person:0x007fae94bf6298 @id="1">
module Identification
def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods
def global_id_column(column_name = nil)
@global_id_column ||= column_name
end
end

def global_id_method
self.class.global_id_column || :id
end

# Returns the Global ID of the model.
#
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 @@ -70,7 +70,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.send(model.global_id_method), 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(@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(@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 @@ -7,6 +7,7 @@
require 'models/person'
require 'models/person_model'
require 'models/composite_primary_key_model'
require 'models/configurable_key_model'

require 'json'

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

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

attr_accessor :id, :external_id

global_id_column :external_id

class << self
def primary_key
:id
end

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

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