Skip to content

Commit 90fa85e

Browse files
Enable setting Model.global_id_column
This allows overriding the column used to define GIDs
1 parent fd53d6d commit 90fa85e

File tree

7 files changed

+51
-5
lines changed

7 files changed

+51
-5
lines changed

lib/global_id/global_id.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class << self
1111
def create(model, options = {})
1212
if app = options.fetch(:app) { GlobalID.app }
1313
params = options.except(:app, :verifier, :for)
14+
params[:global_id_column] ||= model.global_id_column
1415
new URI::GID.create(app, model, params), options
1516
else
1617
raise ArgumentError, 'An app is required to create a GlobalID. ' \

lib/global_id/identification.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ class GlobalID
2626
# GlobalID::Locator.locate person_gid
2727
# # => #<Person:0x007fae94bf6298 @id="1">
2828
module Identification
29+
def self.included(base)
30+
base.extend(ClassMethods)
31+
end
32+
33+
module ClassMethods
34+
def global_id_column(column_name = nil)
35+
@global_id_column ||= column_name
36+
end
37+
end
38+
39+
def global_id_column
40+
self.class.global_id_column || self.class.try(:primary_key) || :id
41+
end
2942

3043
# Returns the Global ID of the model.
3144
#

lib/global_id/uri/gid.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,11 @@ def parse(uri)
7272
#
7373
# URI::GID.create('bcx', Person.find(5), database: 'superhumans')
7474
def create(app, model, params = nil)
75-
model_id_method = if params.is_a?(Hash) && (model_id_key = params.delete(:model_id_key))
76-
model_id_key
75+
global_id_column = params&.delete(:global_id_column)
76+
model_id_method = if model.id.is_a?(Array)
77+
:id
78+
else
79+
global_id_column
7780
end || :id
7881

7982
build app: app, model_name: model.class.name, model_id: model.send(model_id_method), params: params

test/cases/global_id_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
4545
@person_namespaced_gid = GlobalID.create(Person::Child.new(4))
4646
@person_model_gid = GlobalID.create(PersonModel.new(id: 1))
4747
@cpk_model_gid = GlobalID.create(CompositePrimaryKeyModel.new(id: ["tenant-key-value", "id-value"]))
48+
@ckm_model = ConfigurableKeyModel.new(id: "id-value", external_id: "external-id-value")
49+
@ckm_model_gid = GlobalID.create(@ckm_model)
4850
end
4951

5052
test 'find' do
@@ -53,13 +55,15 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
5355
assert_equal Person::Child.find(@person_namespaced_gid.model_id), @person_namespaced_gid.find
5456
assert_equal PersonModel.find(@person_model_gid.model_id), @person_model_gid.find
5557
assert_equal CompositePrimaryKeyModel.find(@cpk_model_gid.model_id), @cpk_model_gid.find
58+
assert_equal ConfigurableKeyModel.find(@ckm_model_gid.model_id), @ckm_model_gid.find
5659
end
5760

5861
test 'find with class' do
5962
assert_equal Person.find(@person_gid.model_id), @person_gid.find(only: Person)
6063
assert_equal Person.find(@person_uuid_gid.model_id), @person_uuid_gid.find(only: Person)
6164
assert_equal PersonModel.find(@person_model_gid.model_id), @person_model_gid.find(only: PersonModel)
6265
assert_equal CompositePrimaryKeyModel.find(@cpk_model_gid.model_id), @cpk_model_gid.find(only: CompositePrimaryKeyModel)
66+
assert_equal ConfigurableKeyModel.find(@ckm_model_gid.model_id), @ckm_model_gid.find(only: ConfigurableKeyModel)
6367
end
6468

6569
test 'find with class no match' do
@@ -68,6 +72,7 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
6872
assert_nil @person_namespaced_gid.find(only: String)
6973
assert_nil @person_model_gid.find(only: Float)
7074
assert_nil @cpk_model_gid.find(only: Hash)
75+
assert_nil @ckm_model_gid.find(only: Hash)
7176
end
7277

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

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

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

184191
assert_equal 'gid://bcx/CompositePrimaryKeyModel/tenant-key-value/id-value', @cpk_model_gid.as_json
185192
assert_equal '"gid://bcx/CompositePrimaryKeyModel/tenant-key-value/id-value"', @cpk_model_gid.to_json
193+
194+
assert_equal 'gid://bcx/ConfigurableKeyModel/external-id-value', @ckm_model_gid.as_json
195+
assert_equal '"gid://bcx/ConfigurableKeyModel/external-id-value"', @ckm_model_gid.to_json
186196
end
187197

188198
test 'model id' do
@@ -191,6 +201,7 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
191201
assert_equal '4', @person_namespaced_gid.model_id
192202
assert_equal '1', @person_model_gid.model_id
193203
assert_equal ['tenant-key-value', 'id-value'], @cpk_model_gid.model_id
204+
assert_equal 'external-id-value', @ckm_model_gid.model_id
194205
end
195206

196207
test 'model name' do
@@ -199,6 +210,7 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
199210
assert_equal 'Person::Child', @person_namespaced_gid.model_name
200211
assert_equal 'PersonModel', @person_model_gid.model_name
201212
assert_equal 'CompositePrimaryKeyModel', @cpk_model_gid.model_name
213+
assert_equal 'ConfigurableKeyModel', @ckm_model_gid.model_name
202214
end
203215

204216
test 'model class' do
@@ -207,6 +219,7 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
207219
assert_equal Person::Child, @person_namespaced_gid.model_class
208220
assert_equal PersonModel, @person_model_gid.model_class
209221
assert_equal CompositePrimaryKeyModel, @cpk_model_gid.model_class
222+
assert_equal ConfigurableKeyModel, @ckm_model_gid.model_class
210223
assert_raise ArgumentError do
211224
GlobalID.find 'gid://bcx/SignedGlobalID/5'
212225
end

test/cases/uri_gid_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class URI::GIDTest < ActiveSupport::TestCase
4646

4747
test 'create from a configurable key model' do
4848
model = ConfigurableKeyModel.new(id: 'id-value', external_id: 'external-id-123')
49-
assert_equal @ckm_gid_string, URI::GID.create('bcx', model, model_id_key: :external_id).to_s
49+
assert_equal @ckm_gid_string, URI::GID.create('bcx', model, global_id_column: :external_id).to_s
5050
end
5151

5252
test 'build' do

test/models/configurable_key_model.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,19 @@ class ConfigurableKeyModel
66

77
attr_accessor :id, :external_id
88

9-
def self.primary_key
10-
:id
9+
global_id_column :external_id
10+
11+
class << self
12+
def primary_key
13+
:id
14+
end
15+
16+
def find(external_id)
17+
new external_id: external_id, id: "id-value"
18+
end
19+
end
20+
21+
def ==(other)
22+
external_id == other.try(:external_id)
1123
end
1224
end

test/models/person.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class PersonUuid < Person
4141
def self.primary_key
4242
:uuid
4343
end
44+
45+
def uuid
46+
id
47+
end
4448
end
4549

4650
class Person::Scoped < Person

0 commit comments

Comments
 (0)