Sha256: e60cbbfee2b09591394c966e65ba942dfbc09b7b95a25c562a9477b2cefb679c

Contents?: true

Size: 792 Bytes

Versions: 1

Compression:

Stored size: 792 Bytes

Contents

module Uuidable
  # ActiveRecord mixin
  module ActiveRecord
    extend ActiveSupport::Concern

    class UuidChangeError < Exception; end

    # ClassMethods
    module ClassMethods
      def uuidable
        after_initialize { self.uuid = Uuidable.generate_uuid if uuid.blank? }
        validates :uuid, presence: true, uniqueness: true

        define_method :to_param do
          uuid
        end

        define_method :uuid= do |val|
          raise UuidChangeError, 'Uuid changing is bad idea!' unless new_record? || uuid.blank? || uuid == val

          super(val)
        end
      end
    end

    def short_uuid
      UUIDTools::UUID.parse(uuid).hexdigest
    end
  end
end

ActiveSupport.on_load(:active_record) do
  ActiveRecord::Base.send(:include, Uuidable::ActiveRecord)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
uuidable-0.0.2 lib/uuidable/active_record.rb