Sha256: 437f35e55f2d4a2b49da461b6670856a4f38608a27b741085a206b9fde08b683
Contents?: true
Size: 1.89 KB
Versions: 1
Compression:
Stored size: 1.89 KB
Contents
module UUIDTools class UUID # monkey-patch Friendly::UUID to serialize UUIDs to MySQL def quoted_id s = raw.unpack("H*")[0] "x'#{s}'" end end end module Arel module Visitors class MySQL < Arel::Visitors::ToSql def visit_UUIDTools_UUID(o) o.quoted_id end end end end module ActiveUUID class UUIDSerializer def load(binary) case binary when UUIDTools::UUID then binary when nil then nil else UUIDTools::UUID.parse_raw(binary) end end def dump(uuid) uuid ? uuid.raw : nil end end module UUID extend ActiveSupport::Concern included do before_create :generate_uuid_if_needed set_primary_key "id" serialize :id, ActiveUUID::UUIDSerializer.new def generate_uuid_if_needed generate_uuid unless self.id end def generate_uuid if nka = self.class.natural_key_attributes # TODO if all the attributes return nil you might want to warn about this chained = nka.collect{|a| self.send(a).to_s}.join("-") self.id = UUIDTools::UUID.sha1_create(UUIDTools::UUID_OID_NAMESPACE, chained) else self.id = UUIDTools::UUID.timestamp_create end end end module ClassMethods def natural_key_attributes @_activeuuid_natural_key_attributes end def natural_key(*attributes) @_activeuuid_natural_key_attributes = attributes end def uuids(*attributes) attributes.each do |attribute| serialize "#{attribute_id}".intern, ActiveUUID::UUIDSerializer.new #class_eval <<-eos # # def #{@association_name} # # @_#{@association_name} ||= self.class.associations[:#{@association_name}].new_proxy(self) # # end #eos end end end module InstanceMethods end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
activeuuid-0.0.4 | lib/activeuuid/uuid.rb |