Skip to content

Commit

Permalink
Version: 0.9.12
Browse files Browse the repository at this point in the history
  • Loading branch information
pnegri committed May 19, 2023
1 parent 26dcaed commit 9df30fc
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
uuid_attribute (0.9.11)
uuid_attribute (0.9.12)
activemodel (>= 5.2)
activerecord (>= 5.2)

Expand Down
1 change: 1 addition & 0 deletions lib/uuid_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "active_model/type"
require "rails"
require_relative "uuid_attribute/version"
require_relative "uuid_attribute/type"
require_relative "uuid_attribute/uuid"
require_relative "uuid_attribute/utils"

Expand Down
95 changes: 94 additions & 1 deletion lib/uuid_attribute/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,99 @@
# frozen_string_literal: true

require "rails/railtie"
require "active_record/associations/association_scope"
require "active_record/fixture_set/table_row"

module ActiveRecord
module Associations
class AssociationScope # :nodoc:
def last_chain_scope(scope, reflection, owner)
primary_key = reflection.join_primary_key
foreign_key = reflection.join_foreign_key

table = reflection.aliased_table

prevalue = owner[foreign_key]
if owner.class.attribute_types[foreign_key].class.name == "UuidAttribute::Type"
prevalue = UuidAttribute::Utils.raw_bytes(UuidAttribute::Utils.normalize(UuidAttribute::Utils.parse(prevalue)))
end

value = transform_value(prevalue)
scope = apply_scope(scope, table, primary_key, value)

if reflection.type
polymorphic_type = transform_value(owner.class.polymorphic_name)
scope = apply_scope(scope, table, reflection.type, polymorphic_type)
end

scope
end
end
end
end

module ActiveStorage
class FixtureSet
def self.blob(filename:, **attributes)
generated_uuid = Digest::UUID.uuid_v5(Digest::UUID::OID_NAMESPACE, filename)
raw_uuid = UuidAttribute::Utils.raw_bytes(
UuidAttribute::Utils.normalize(UuidAttribute::Utils.parse(generated_uuid))
)
new.prepare Blob.new(filename: filename, id: raw_uuid, key: generated_uuid), **attributes
end
end
end

module ActiveRecord
class FixtureSet
class << self
def identify(label, column_type = :integer)
if column_type == :uuid
generated_uuid = Digest::UUID.uuid_v5(Digest::UUID::OID_NAMESPACE, label.to_s)
UuidAttribute::Utils.raw_bytes(UuidAttribute::Utils.normalize(UuidAttribute::Utils.parse(generated_uuid)))
else
Zlib.crc32(label.to_s) % MAX_ID
end
end
end

class TableRow
private

alias org_fill_row_model_attributes fill_row_model_attributes
def fill_row_model_attributes
org_fill_row_model_attributes
return unless model_class

resolve_string_uuids
end

def resolve_string_uuids
@row.each do |key, value|
next if model_class.to_s.include?("ActiveStorage") && key == "record_id"

transform_uuid(key, value)
end
end

def transform_uuid(key, value)
if test_uuid36?(key, value)
@row[key] = [value&.gsub("-", "")&.upcase].pack("H*")
elsif test_uuid22?(key, value)
@row[key] = [UuidAttribute::Utils.unshort(value)&.gsub("-", "")&.upcase].pack("H*")
end
end

def test_uuid36?(key, value)
(key == "id" || key.ends_with?("_id")) && value.instance_of?(String) && value.length == 36
end

def test_uuid22?(key, value)
(key == "id" || key.ends_with?("_id")) && value.instance_of?(String) && value.length == 22
end
end
end
end

module UuidAttribute
# Rails Initializer
Expand Down Expand Up @@ -57,7 +150,7 @@ def configure_binary_ids

default = nil
default = -> { SecureRandom.uuid } if att.eql? "id"
model.attribute att, ::UuidAttribute::UUID.new, default: default
model.attribute att, ::UuidAttribute::Type.new, default: default
end
end
rescue ActiveRecord::NoDatabaseError
Expand Down
43 changes: 43 additions & 0 deletions lib/uuid_attribute/type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

module UuidAttribute
# UUID Attribute
class Type < ActiveModel::Type::Binary
def type
:uuid
end

def serialize(value)
#puts "SERIALIZE"
return if value.blank?
return value if value.is_a?(ActiveRecord::Type::Binary::Data)

binary_data = value
binary_data = Utils.raw_bytes(Utils.normalize(Utils.parse(value))) if value.is_a?(String)
# binary_data = value.raw if value.is_a?(UUID)

ActiveRecord::Type::Binary::Data.new(
binary_data
)
end

def deserialize(value)
# puts "DESERIALIZE"
return nil if value.nil?
value = value.to_s if value.is_a?(ActiveModel::Type::Binary::Data)

# return UUID.new(Utils.normalize(Utils.parse(value)))
return Utils.shorten(Utils.normalize(Utils.parse(value)))
# ActiveRecord::Type::Binary::Data.new(Utils.raw_bytes(Utils.normalize(Utils.parse(value))))
end

def cast(value)
# puts "CAST"
return nil if value.nil?
# return value if value.is_a?(UUID)

value = value.to_s if value.is_a?(ActiveModel::Type::Binary::Data)
return Utils.shorten(Utils.normalize(Utils.parse(value)))
# super
end
end
end
1 change: 1 addition & 0 deletions lib/uuid_attribute/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def raw_bytes(uuid_string)
end

def parse(str)
return format_uuid(str.hex) if str.is_a?(UUID)
return nil if str.length.zero?
return str if str.length == 36

Expand Down
26 changes: 10 additions & 16 deletions lib/uuid_attribute/uuid.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
# frozen_string_literal: true

module UuidAttribute
# UUID Attribute
class UUID < ActiveModel::Type::Binary
def type
:uuid
class UUID
def initialize(value)
@hex = Utils.normalize(Utils.parse(value))
@shorten = Utils.shorten(@hex)
end

def serialize(value)
return if value.blank?

ActiveRecord::Type::Binary::Data.new(
Utils.raw_bytes(Utils.normalize(Utils.parse(value)))
)
def to_s
@shorten
end

def deserialize(value)
return nil if value.nil?

Utils.shorten(Utils.parse(value.to_s))
def hex
@hex
end

def cast(value)
deserialize(value)
def raw
UuidAttribute::Utils.raw_bytes(@hex)
end
end
end
2 changes: 1 addition & 1 deletion lib/uuid_attribute/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module UuidAttribute
VERSION = "0.9.11"
VERSION = "0.9.12"
end

0 comments on commit 9df30fc

Please sign in to comment.