Module: Hippo::Concerns::RandomIdentifer::ClassMethods
- Defined in:
- lib/hippo/concerns/random_identifier.rb
Overview
### Random Identifier Concern This adds the #has_random_hash_code class method
Instance Method Summary collapse
-
#has_random_identifier(field_name: :identifier, length: 12) ⇒ Object
A random string that identifies an model The code is generated by Strings.random for new records It’s useful for generating links for anonymous access to an entity that cannot be guessed.
Instance Method Details
#has_random_identifier(field_name: :identifier, length: 12) ⇒ Object
A random string that identifies an model The code is generated by Strings.random for new records It’s useful for generating links for anonymous access to an entity that cannot be guessed.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/hippo/concerns/random_identifier.rb', line 18 def has_random_identifier(field_name: :identifier, length: 12) validates field_name, presence: { message: "random identifier is not set (should be automatically chosen)" } before_validation(on: :create) do 5.times do if self[field_name].blank? code = Hippo::Strings.random(length) self[field_name] = code if self.class.where("#{field_name}" => code).none? end break if self[field_name].present? end end end |