Module: Uniqable

Defined in:
lib/uniqable.rb,
lib/uniqable/version.rb

Overview

Generates uniq, random token for ActiveRecord model’s fields

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
'0.4.1'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
# File 'lib/uniqable.rb', line 8

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#uniqable_uid(field) ⇒ Object

Generate and set random and uniq field @TODO: split into 2 actions generate and set



68
69
70
71
72
73
74
75
76
# File 'lib/uniqable.rb', line 68

def uniqable_uid(field)
  loop do
    uniq_code = Nanoid.generate(size: 16)
    if uniq_code =~ /\D+/
      send("#{field}=", uniq_code)
      break unless self.class.where(field => uniq_code).exists?
    end
  end
end