From 2c018d7b2736cedee4e9bd6786301cd1d7496d49 Mon Sep 17 00:00:00 2001 From: Benjamin Quorning Date: Sat, 9 Mar 2024 00:54:48 +0100 Subject: [PATCH] Fix #68 --- lib/ulid/rails.rb | 2 ++ lib/ulid/rails/patch.rb | 10 ++++++++++ lib/ulid/rails/type.rb | 4 ++++ 3 files changed, 16 insertions(+) diff --git a/lib/ulid/rails.rb b/lib/ulid/rails.rb index 32a9fc3..098b2d0 100644 --- a/lib/ulid/rails.rb +++ b/lib/ulid/rails.rb @@ -1,4 +1,5 @@ require "active_record" +require "active_record/fixtures" require "active_support/concern" require "active_model/type" require "ulid" @@ -51,5 +52,6 @@ def ulid_extract_timestamp(ulid_column, timestamp_column = :created_at) ActiveRecord::Type.register(:ulid, ULID::Rails::SqliteType, adapter: :sqlite) ActiveRecord::Type.register(:ulid, ULID::Rails::SqliteType, adapter: :sqlite3) ActiveRecord::ConnectionAdapters::TableDefinition.include(Patch::Migrations) + ActiveRecord::FixtureSet.singleton_class.prepend(Patch::FixtureSet) end end diff --git a/lib/ulid/rails/patch.rb b/lib/ulid/rails/patch.rb index 1223ead..3db9e76 100644 --- a/lib/ulid/rails/patch.rb +++ b/lib/ulid/rails/patch.rb @@ -8,6 +8,16 @@ def virtual_ulid_timestamp(timestamp_column_name, ulid_column_name) as: "FROM_UNIXTIME(CONV(HEX(#{ulid_column_name} >> 80), 16, 10) / 1000.0)" end end + + module FixtureSet + def identify(label, column_type = :integer) + if column_type == :ulid + ULID::Rails::Type.new.serialize(label.to_s) + else + super + end + end + end end end end diff --git a/lib/ulid/rails/type.rb b/lib/ulid/rails/type.rb index 72e57d2..eb88fb7 100644 --- a/lib/ulid/rails/type.rb +++ b/lib/ulid/rails/type.rb @@ -5,6 +5,10 @@ module ULID module Rails class Type < ActiveModel::Type::Binary + def type + :ulid + end + def assert_valid_value(value) raise ArgumentError, "`#{value}` is not a ULID format" unless Data.valid_ulid?(value) end