Skip to content

Commit

Permalink
Fix #68
Browse files Browse the repository at this point in the history
  • Loading branch information
bquorning committed Mar 8, 2024
1 parent 8a4b20f commit 52ac4ac
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/ulid/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,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
10 changes: 10 additions & 0 deletions lib/ulid/rails/patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions lib/ulid/rails/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 52ac4ac

Please sign in to comment.