Module: PrefixedIds

Defined in:
lib/prefixed_ids.rb,
lib/prefixed_ids/engine.rb,
lib/prefixed_ids/version.rb,
lib/prefixed_ids/prefix_id.rb

Defined Under Namespace

Modules: Attribute, Finder, Rails, ToParam Classes: Engine, Error, PrefixId

Constant Summary collapse

VERSION =
"1.2.2"

Class Method Summary collapse

Class Method Details

.find(prefix_id) ⇒ Object


17
18
19
20
21
22
# File 'lib/prefixed_ids.rb', line 17

def self.find(prefix_id)
  prefix, _ = split_id(prefix_id)
  models.fetch(prefix).find_by_prefix_id(prefix_id)
rescue KeyError
  raise Error, "Unable to find model with prefix `#{prefix}`. Available prefixes are: #{models.keys.join(", ")}"
end

.split_id(prefix_id, delimiter = PrefixedIds.delimiter) ⇒ Object

Splits a prefixed ID into its prefix and ID


25
26
27
28
# File 'lib/prefixed_ids.rb', line 25

def self.split_id(prefix_id, delimiter = PrefixedIds.delimiter)
  prefix, _, id = prefix_id.to_s.rpartition(delimiter)
  [prefix, id]
end