Skip to content

Commit

Permalink
♻️ Model#global_id_method instead of Model#global_id_column
Browse files Browse the repository at this point in the history
`Model.primary_key` is actually irrelevant to this feature, the only
thing that matters is the model method.

For composite keys, or primary keys not named "id", we still get the
important value from `Model#id`, the only time we don't is when we need
to use a non-PK column.
  • Loading branch information
tylerwillingham committed Aug 23, 2024
1 parent 6cd2b82 commit 314bc0d
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/global_id/identification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def global_id_column(column_name = nil)
end
end

def global_id_column
self.class.global_id_column || self.class.try(:primary_key) || :id
def global_id_method
self.class.global_id_column || :id
end

# Returns the Global ID of the model.
Expand Down
4 changes: 1 addition & 3 deletions lib/global_id/uri/gid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ def parse(uri)
#
# URI::GID.create('bcx', Person.find(5), database: 'superhumans')
def create(app, model, params = nil)
model_id_method = model.id.is_a?(Array) ? :id : model.global_id_column

build app: app, model_name: model.class.name, model_id: model.send(model_id_method), 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
4 changes: 0 additions & 4 deletions test/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ class PersonUuid < Person
def self.primary_key
:uuid
end

def uuid
id
end
end

class Person::Scoped < Person
Expand Down

0 comments on commit 314bc0d

Please sign in to comment.