module FriendlyUUID::Class

Public Instance Methods

compact(uuid) click to toggle source
# File lib/friendly_uuid.rb, line 43
def compact(uuid)
  (0..uuid.length).each do |length|
    candidate = uuid[0..length]
    return candidate if self.expand(candidate) == uuid
  end
  raise ValueError("Cannot find expansion for UUID #{uuid}")
end
expand(short_uuids) click to toggle source
# File lib/friendly_uuid.rb, line 27
def expand(short_uuids)
  # If a single ID passed as a string
  return self.expand_to_record(short_uuids).id if short_uuids.class == String

  # If a single ID passed as a non-nested array
  if short_uuids[0].class != Array && short_uuids.length == 1
    return self.expand_to_record(short_uuids.join).id
  end

  short_uuids.flatten!

  short_uuids.map do |uuid|
    self.expand_to_record(uuid).id
  end
end
expand_to_record(short_uuid) click to toggle source
# File lib/friendly_uuid.rb, line 51
def expand_to_record(short_uuid)
  raise ActiveRecord::RecordNotFound unless short_uuid
  record = self.possible_expansions(short_uuid).first
  raise ActiveRecord::RecordNotFound unless record
  record
end
find(*ids) click to toggle source
Calls superclass method
# File lib/friendly_uuid.rb, line 23
def find(*ids)
  super(self.expand(ids))
end
find_by(arg, *args) click to toggle source
Calls superclass method
# File lib/friendly_uuid.rb, line 62
def find_by(arg, *args)
  if arg.respond_to?(:[])
    arg[:id] = self.expand(arg[:id]) if arg[:id]
    arg["id"] = self.expand(arg["id"]) if arg["id"]
  end

  super
end
possible_expansions(short_uuid) click to toggle source
# File lib/friendly_uuid.rb, line 58
def possible_expansions(short_uuid)
  self.where(substr_query, short_uuid.length, short_uuid).order(created_at: :asc)
end
substr_query() click to toggle source
# File lib/friendly_uuid.rb, line 71
def substr_query
  case self.connection_config[:adapter]
  when "mysql2"
    raise ValueError("Sorry, FriendlyUUID does not support MySQL")
  when "postgresql"
    "LEFT(#{self.table_name}.id::text, ?) = ?"
  when "sqlite3"
    "SUBSTR(#{self.table_name}.id, 0, ?) = ?"
  else
    raise ValueError("Unknown database type; FriendlyUUID cannot support it")
  end
end