Sha256: e0a2f52d0779be94b19704853d2e905ba7c1490275245b0517f074242c14f4d8

Contents?: true

Size: 741 Bytes

Versions: 3

Compression:

Stored size: 741 Bytes

Contents

# frozen_string_literal: true

require 'active_record'
require 'active_support'

module Lite
  module Uxid
    module Record
      module Nanoid

        extend ActiveSupport::Concern

        included do
          before_create :callback_generate_uxid!, if: proc { respond_to?(:uxid) }
        end

        class_methods do
          def find_by_uxid(uxid)
            find_by(uxid: uxid)
          end

          def find_by_uxid!(uxid)
            record = find_by_uxid(uxid)
            return record unless record.nil?

            raise ActiveRecord::RecordNotFound
          end
        end

        private

        def callback_generate_uxid!
          self.uxid = Lite::Uxid::Nanoid.encode
        end

      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lite-uxid-1.0.5 lib/lite/uxid/record/nanoid.rb
lite-uxid-1.0.4 lib/lite/uxid/record/nanoid.rb
lite-uxid-1.0.3 lib/lite/uxid/record/nanoid.rb