Module: WithUid::ClassMethods
- Defined in:
- lib/with_uid.rb
Instance Method Summary collapse
-
#by_uid(uid) ⇒ ActiveRecord::Base?
Find record by uid.
-
#by_uid!(uid) ⇒ ActiveRecord::Base
Find record by uid.
-
#generate_uid(**options, &block) ⇒ void
Generate uid for given model.
-
#humanize_uid_with(attr, **options) ⇒ void
Generate uid using record’s attribute.
Instance Method Details
#by_uid(uid) ⇒ ActiveRecord::Base?
Find record by uid
36 37 38 |
# File 'lib/with_uid.rb', line 36 def by_uid(uid) find_by(uid: uid) end |
#by_uid!(uid) ⇒ ActiveRecord::Base
Find record by uid
26 27 28 |
# File 'lib/with_uid.rb', line 26 def by_uid!(uid) find_by!(uid: uid) end |
#generate_uid(**options, &block) ⇒ void
This method returns an undefined value.
Generate uid for given model
If no block given it will generate uid according RFC 4122. You are free to customize this uid with :prefix
.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/with_uid.rb', line 71 def generate_uid(**, &block) before_validation do if block_given? generate_uid(**, &block) else generate_uid(**.merge(suffix: '')) do SecureRandom.uuid end end end end |