Sha256: 4db84a03b54fbf7a2a40f50ebf4597ed09faeab24353431cd2f7d0fa68509cc0
Contents?: true
Size: 1.2 KB
Versions: 2
Compression:
Stored size: 1.2 KB
Contents
# frozen_string_literal: true require "active_support" unless defined?(ActiveSupport) module Lite module Uxid module Record module Hashid extend ActiveSupport::Concern included do after_commit :callback_generate_uxid!, if: proc { respond_to?(:uxid) && !uxid? }, on: :create end class_methods do def find_by_uxid(uxid) decoded_id = Lite::Uxid::Hashid.decode(uxid, prefix: new.uxid_prefix) find_by(id: decoded_id) end def find_by_uxid!(uxid) record = find_by_uxid(uxid) return record unless record.nil? raise ActiveRecord::RecordNotFound end end def id_to_uxid return unless respond_to?(:uxid) Lite::Uxid::Hashid.encode(id, prefix: uxid_prefix) end def uxid_to_id return unless respond_to?(:uxid) Lite::Uxid::Hashid.decode(uxid, prefix: uxid_prefix) end def uxid_prefix nil end private def callback_generate_uxid! update_column(:uxid, id_to_uxid) end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
lite-uxid-1.5.2 | lib/lite/uxid/record/hashid.rb |
lite-uxid-1.5.1 | lib/lite/uxid/record/hashid.rb |