Skip to content
Merged
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
3 changes: 2 additions & 1 deletion lib/global_id/global_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class << self

def create(model, options = {})
if app = options.fetch(:app) { GlobalID.app }
new URI::GID.create(app, model), options
params = options.except(:app, :verifier, :for)
new URI::GID.create(app, model, params), options
else
raise ArgumentError, 'An app is required to create a GlobalID. ' \
'Pass the :app option or set the default GlobalID.app.'
Expand Down
8 changes: 4 additions & 4 deletions lib/global_id/identification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ class GlobalID
module Identification
extend ActiveSupport::Concern

def to_global_id
@global_id ||= GlobalID.create(self)
def to_global_id(options = {})
@global_id ||= GlobalID.create(self, options)
end
alias to_gid to_global_id

def to_gid_param
to_global_id.to_param
def to_gid_param(options = {})
to_global_id(options).to_param
end

def to_signed_global_id(options = {})
Expand Down
5 changes: 4 additions & 1 deletion lib/global_id/uri/gid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ def build(args)
parts = Util.make_components_hash(self, args)
parts[:host] = parts[:app]
parts[:path] = "/#{parts[:model_name]}/#{CGI.escape(parts[:model_id].to_s)}"
parts[:query] = URI.encode_www_form(parts[:params]) if parts[:params]

if parts[:params] && !parts[:params].empty?
parts[:query] = URI.encode_www_form(parts[:params])
end

super parts
end
Expand Down
7 changes: 6 additions & 1 deletion test/cases/global_id_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,12 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
end

class GlobalIDCustomParamsTest < ActiveSupport::TestCase
test 'custom params' do
test 'create custom params' do
gid = GlobalID.create(Person.new(5), hello: 'world')
assert_equal 'world', gid.params[:hello]
end

test 'parse custom params' do
gid = GlobalID.parse 'gid://bcx/Person/5?hello=world'
assert_equal 'world', gid.params[:hello]
end
Expand Down
10 changes: 10 additions & 0 deletions test/cases/global_identification_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ class GlobalIdentificationTest < ActiveSupport::TestCase
assert_equal GlobalID.create(@model), @model.to_gid
end

test 'creates a Global ID with custom params' do
assert_equal GlobalID.create(@model, some: 'param'), @model.to_global_id(some: 'param')
assert_equal GlobalID.create(@model, some: 'param'), @model.to_gid(some: 'param')
end

test 'creates a signed Global ID from self' do
assert_equal SignedGlobalID.create(@model), @model.to_signed_global_id
assert_equal SignedGlobalID.create(@model), @model.to_sgid
Expand All @@ -19,4 +24,9 @@ class GlobalIdentificationTest < ActiveSupport::TestCase
assert_equal SignedGlobalID.create(@model, for: 'login'), @model.to_signed_global_id(for: 'login')
assert_equal SignedGlobalID.create(@model, for: 'login'), @model.to_sgid(for: 'login')
end

test 'creates a signed Global ID with custom params' do
assert_equal SignedGlobalID.create(@model, some: 'param'), @model.to_signed_global_id(some: 'param')
assert_equal SignedGlobalID.create(@model, some: 'param'), @model.to_sgid(some: 'param')
end
end
12 changes: 12 additions & 0 deletions test/cases/signed_global_id_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,15 @@ def with_expiration_in(expires_in)
SignedGlobalID.expires_in = old_expires
end
end

class SignedGlobalIDCustomParamsTest < ActiveSupport::TestCase
test 'create custom params' do
sgid = SignedGlobalID.create(Person.new(5), hello: 'world')
assert_equal 'world', sgid.params[:hello]
end

test 'parse custom params' do
sgid = SignedGlobalID.parse('eyJnaWQiOiJnaWQ6Ly9iY3gvUGVyc29uLzU/aGVsbG89d29ybGQiLCJwdXJwb3NlIjoiZGVmYXVsdCIsImV4cGlyZXNfYXQiOm51bGx9--7c042f09483dec470fa1088b76d9fd946eb30ffa')
assert_equal 'world', sgid.params[:hello]
end
end