Module: Federails::HasUuid
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/federails/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:
“‘rb # Example migration add_column :my_table, :uuid, :text, default: nil, index: { unique: true } “`
Usage:
“‘rb class MyModel < ApplicationRecord
include Federails::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
39 40 41 |
# File 'app/models/concerns/federails/has_uuid.rb', line 39 def to_param uuid end |
#uuid ⇒ String
44 45 46 47 48 49 50 51 |
# File 'app/models/concerns/federails/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 |