Module: Federails::HasUuid

Extended by:
ActiveSupport::Concern
Included in:
Activity, Actor, Following
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

Instance Method Details

#to_paramString



39
40
41
# File 'app/models/concerns/federails/has_uuid.rb', line 39

def to_param
  uuid
end

#uuidString



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