Module: Skr::Concerns::RandomHashCode::ClassMethods
- Defined in:
- lib/skr/concerns/random_hash_code.rb
Overview
### Random Hash Code Concern This adds the #has_random_hash_code class method
Instance Method Summary collapse
-
#has_random_hash_code(field_name: :hash_code, length: 12) ⇒ Object
A random string that identifies an entity, such as a Customer, or Vendor The code is generated by Strings.random for new records It’s useful for generating magic links for access to an entity that cannot be guessed.
Instance Method Details
#has_random_hash_code(field_name: :hash_code, length: 12) ⇒ Object
A random string that identifies an entity, such as a Customer, or Vendor The code is generated by Strings.random for new records It’s useful for generating magic links for access to an entity that cannot be guessed.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/skr/concerns/random_hash_code.rb', line 19 def has_random_hash_code( field_name: :hash_code, length: 12 ) validates field_name, :presence=>{ :message=>"hash code is not set (should be automatically chosen)" } scope :with_hash_code, lambda{ | code | where({ :hash_code=>code }) } before_validation(:on=>:create) do self[ field_name ] = Lanes::Strings.random( length ) if self[ field_name ].blank? end end |