Module: Fedipub::HasUuid
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/fedipub/has_uuid.rb
Overview
Model concern providing UUIDs as model parameter (instead of IDs).
A required, uuid field is required on the model's table for this concern to work:
# Example migration
add_column :my_table, :uuid, :text, default: nil, index: { unique: true }
Usage:
class MyModel < ApplicationRecord
include Fedipub::HasUuid
end
# And now:
instance = MyModel.find_param 'aaaa_bbbb_cccc_dddd_....'
instance.to_param
# => 'aaaa_bbbb_cccc_dddd_....'
It can be added on existing tables without data migration as the uuid accessor will generate the value when missing.
Instance Method Summary collapse
-
#to_param ⇒ String
The UUID.
- #uuid ⇒ String
Instance Method Details
#to_param ⇒ String
Returns The UUID.
39 40 41 |
# File 'app/models/concerns/fedipub/has_uuid.rb', line 39 def to_param uuid end |
#uuid ⇒ String
44 45 46 47 48 49 50 51 |
# File 'app/models/concerns/fedipub/has_uuid.rb', line 44 def uuid # Override UUID accessor to provide lazy initialization of UUIDs for old data if self[:uuid].blank? generate_uuid save! end self[:uuid] end |