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

Support GlobalID #176

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions activerecord-bitemporal.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "activerecord", ">= 6.1"

spec.add_development_dependency "bundler"
spec.add_development_dependency "globalid"
spec.add_development_dependency "rake", "~> 13.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "pg"
Expand Down
2 changes: 2 additions & 0 deletions lib/activerecord-bitemporal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require "activerecord-bitemporal/version"
require "activerecord-bitemporal/visualizer"
require "activerecord-bitemporal/callbacks"
require "activerecord-bitemporal/global_id"

module ActiveRecord::Bitemporal
DEFAULT_VALID_FROM = Time.utc(1900, 12, 31).in_time_zone.freeze
Expand Down Expand Up @@ -130,6 +131,7 @@ def bitemporalize(
include InstanceMethods
include ActiveRecord::Bitemporal::Scope
include ActiveRecord::Bitemporal::Callbacks
prepend ActiveRecord::Bitemporal::GlobalID if defined?(GlobalID)

if enable_merge_with_except_bitemporal_default_scope
relation_delegate_class(ActiveRecord::Relation).prepend ActiveRecord::Bitemporal::Relation::MergeWithExceptBitemporalDefaultScope
Expand Down
23 changes: 23 additions & 0 deletions lib/activerecord-bitemporal/global_id.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

begin
require "globalid"
rescue LoadError
return
end

module ActiveRecord
module Bitemporal
module GlobalID
include ::GlobalID::Identification

def to_global_id(options = {})
super(options.merge(app: "bitemporal"))
end
alias to_gid to_global_id
end
end
end

# BiTemporal Data Model requires default scope, so `UnscopedLocator` cannot be used.
GlobalID::Locator.use "bitemporal", GlobalID::Locator::BaseLocator.new
75 changes: 75 additions & 0 deletions spec/activerecord-bitemporal/global_id_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# frozen_string_literal: true

require "spec_helper"

GlobalID.app = "test-app"

RSpec.describe "GlobalID" do
describe "#to_globalid" do
context "when BTDM" do
let(:company) { Company.create!(name: "Company1").tap { _1.update!(name: "Company2") } }

it "app name is bitemporal" do
expect(company.to_global_id).to eq GlobalID.new("gid://bitemporal/Company/#{company.bitemporal_id}")
end
end

context "when non BTDM" do
let(:non_btdm_class) do
Class.new(CompanyWithoutBitemporal) do
include GlobalID::Identification

def self.name
"CompanyWithoutBitemporalGID"
end
end
end
let(:company) { non_btdm_class.create!(name: "Company1") }

it "app name is default" do
expect(company.to_global_id).to eq GlobalID.new("gid://test-app/CompanyWithoutBitemporalGID/#{company.id}")
end
end
end

describe "#to_gid" do
context "when BTDM" do
let(:company) { Company.create!(name: "Company1").tap { _1.update!(name: "Company2") } }

it "app name is bitemporal" do
expect(company.to_gid).to eq GlobalID.new("gid://bitemporal/Company/#{company.bitemporal_id}")
end
end

context "when non BTDM" do
let(:non_btdm_class) do
Class.new(CompanyWithoutBitemporal) do
include GlobalID::Identification

def self.name
"CompanyWithoutBitemporalGID"
end
end
end
let(:company) { non_btdm_class.create!(name: "Company1") }

it "app name is default" do
expect(company.to_gid).to eq GlobalID.new("gid://test-app/CompanyWithoutBitemporalGID/#{company.id}")
end
end
end

describe "GlobalID::Locator.locate" do
let(:company) do
Company.create!(name: "Company1").tap do |m|
m.update!(name: "Company2")
m.update!(name: "Company3")
end
end
let(:gid) { company.to_global_id }

it "can find current record" do
expect(GlobalID::Locator.locate(gid).name).to eq "Company3"
end
end
end