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

Fix loading of fixtures #70

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
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