Module: Fedipub::HasUuid

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

Instance Method Details

#to_paramString

Returns The UUID.

Returns:

  • (String)

    The UUID



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

def to_param
  uuid
end

#uuidString

Returns:

  • (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