Module: WithUid

Extended by:
ActiveSupport::Concern
Defined in:
lib/with_uid.rb,
lib/with_uid/version.rb,
lib/with_uid/uid_generator.rb,
lib/with_uid/uid_validator.rb

Overview

Allow to generate nice uids for ActiveRecord objects

Defined Under Namespace

Modules: ClassMethods Classes: UidGenerator, UidValidator

Constant Summary collapse

VERSION =
'0.0.1'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.format(str) ⇒ Object



119
120
121
# File 'lib/with_uid.rb', line 119

def self.format(str)
  I18n.transliterate(str.to_s).parameterize
end

Instance Method Details

#generate_uid(**options, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



124
125
126
127
128
129
130
131
# File 'lib/with_uid.rb', line 124

def generate_uid(**options, &block)
  fail LocalJumpError, 'no block given' unless block_given?
  return if uid.present?

  self.uid = uid_generator(**options, &block).detect do |uid|
    !self.class.exists?(uid: uid)
  end
end

#skip_uid_uniqueness?Boolean

If you are not interested in uid uniqeness redefine this method in your model

Examples:

class User < ActiveRecord::Base
  humanize_uid_with(:name, prefix: 'user_')

  def skip_uid_uniqueness?
    true
  end
end

Returns:

  • (Boolean)


115
116
117
# File 'lib/with_uid.rb', line 115

def skip_uid_uniqueness?
  false
end

#uid_generator(**options, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



134
135
136
# File 'lib/with_uid.rb', line 134

def uid_generator(**options, &block)
  UidGenerator.new(self, **options, &block)
end