Skip to content

Commit

Permalink
Fix #68
Browse files Browse the repository at this point in the history
  • Loading branch information
bquorning committed Mar 9, 2024
1 parent 94d1e33 commit 66e142f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/ulid/rails.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "active_record"
require "active_record/fixtures"
require "active_support/concern"
require "active_model/type"
require "ulid"
Expand Down Expand Up @@ -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
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 66e142f

Please sign in to comment.