-
Notifications
You must be signed in to change notification settings - Fork 899
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Postgres Range Serializer support
- Loading branch information
1 parent
7923128
commit a4a6e7b
Showing
8 changed files
with
205 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
lib/paper_trail/type_serializers/postgres_range_serializer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
module PaperTrail | ||
module TypeSerializers | ||
# Provides an alternative method of serialization | ||
# and deserialization of PostgreSQL range columns. | ||
class PostgresRangeSerializer | ||
# @see https://github.com/rails/rails/blob/main/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L147-L152 | ||
RANGE_TYPES = %i[ | ||
daterange | ||
numrange | ||
tsrange | ||
tstzrange | ||
int4range | ||
int8range | ||
].freeze | ||
|
||
def self.range_type?(type) | ||
RANGE_TYPES.include?(type) | ||
end | ||
|
||
def initialize(active_record_serializer) | ||
@active_record_serializer = active_record_serializer | ||
end | ||
|
||
def serialize(range) | ||
range | ||
end | ||
|
||
def deserialize(range) | ||
range.is_a?(String) ? deserialize_with_ar(range) : range | ||
end | ||
|
||
private | ||
|
||
def deserialize_with_ar(string) | ||
return nil if string.blank? | ||
|
||
delimiter = string[/\.{2,3}/] | ||
range_start, range_end = string.split(delimiter) | ||
|
||
range_start = @active_record_serializer.subtype.cast(range_start) | ||
range_end = @active_record_serializer.subtype.cast(range_end) | ||
|
||
Range.new(range_start, range_end, exclude_end: delimiter == "...") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
spec/paper_trail/type_serializers/postgres_range_serializer_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
module PaperTrail | ||
module TypeSerializers | ||
::RSpec.describe PostgresRangeSerializer do | ||
if ENV["DB"] == "postgres" | ||
let(:active_record_serializer) { | ||
ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Range.new(subtype) | ||
} | ||
let(:serializer) { described_class.new(active_record_serializer) } | ||
|
||
describe ".deserialize" do | ||
let(:range_string) { range_ruby.to_s } | ||
|
||
context "with daterange" do | ||
let(:subtype) { ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Date.new } | ||
let(:range_ruby) { Date.new(2024, 1, 1)..Date.new(2024, 1, 31) } | ||
|
||
it "deserializes to Ruby" do | ||
expect(serializer.deserialize(range_string)).to eq(range_ruby) | ||
end | ||
|
||
context "with exclude_end" do | ||
let(:range_ruby) { Date.new(2024, 1, 1)...Date.new(2024, 1, 31) } | ||
|
||
it "deserializes to Ruby" do | ||
expect(serializer.deserialize(range_string)).to eq(range_ruby) | ||
end | ||
end | ||
end | ||
|
||
context "with numrange" do | ||
let(:subtype) { ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Decimal.new } | ||
let(:range_ruby) { 1.5..3.5 } | ||
|
||
it "deserializes to Ruby" do | ||
expect(serializer.deserialize(range_string)).to eq(range_ruby) | ||
end | ||
end | ||
|
||
context "with tsrange" do | ||
let(:subtype) { ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Timestamp.new } | ||
let(:range_ruby) { 1.day.ago..1.day.from_now } | ||
|
||
it "deserializes to Ruby" do | ||
expect(serializer.deserialize(range_string)).to eq(range_ruby) | ||
end | ||
end | ||
|
||
context "with tstzrange" do | ||
let(:subtype) { | ||
ActiveRecord::ConnectionAdapters::PostgreSQL::OID::TimestampWithTimeZone.new | ||
} | ||
let(:range_ruby) { Date.new(2021, 1, 1)..Date.new(2021, 1, 31) } | ||
|
||
it "deserializes to Ruby" do | ||
expect(serializer.deserialize(range_string)).to eq(range_ruby) | ||
end | ||
end | ||
|
||
context "with int4range" do | ||
let(:subtype) { ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer.new } | ||
let(:range_ruby) { 1..10 } | ||
|
||
it "deserializes to Ruby" do | ||
expect(serializer.deserialize(range_string)).to eq(range_ruby) | ||
end | ||
end | ||
|
||
context "with int8range" do | ||
let(:subtype) { ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer.new } | ||
let(:range_ruby) { 2_200_000_000..2_500_000_000 } | ||
|
||
it "deserializes to Ruby" do | ||
expect(serializer.deserialize(range_string)).to eq(range_ruby) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |