Skip to content

Commit

Permalink
Add decode_prefix_id to PrefixedIds type
Browse files Browse the repository at this point in the history
  • Loading branch information
namolnad authored Nov 15, 2024
1 parent 5c51967 commit a980089
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/prefixed_ids.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ def self.find(prefix_id)
raise Error, "Unable to find model with prefix `#{prefix}`. Available prefixes are: #{models.keys.join(", ")}"
end

# Returns a decoded ID from a prefix_id
def self.decode_prefix_id(prefix_id)
return prefix_id if prefix_id.nil? || prefix_id.is_a?(Integer)
return prefix_id unless models.keys.any? { |key| prefix_id.to_s.start_with?("#{key}#{delimiter}") }

model_name, _ = split_id(prefix_id)

models.fetch(model_name).decode_prefix_id(prefix_id)
end

# Splits a prefixed ID into its prefix and ID
def self.split_id(prefix_id, delimiter = PrefixedIds.delimiter)
prefix, _, id = prefix_id.to_s.rpartition(delimiter)
Expand Down

0 comments on commit a980089

Please sign in to comment.