Module: OpenLoam::Dictionaries

Defined in:
lib/open_loam/dictionaries.rb

Overview

Read API for per-tenant managed lookup lists (OpenLoam::Dictionary / OpenLoam::DictionaryEntry). Everything is tenant-scoped automatically and memoized per request in OpenLoam::Current.dictionary_cache (keyed with the tenant id, so an as_tenant switch mid-request can't read another tenant's list) — the same simple prototype cache as OpenLoam::Configs.

OpenLoam::Dictionaries.entries("damage_severity")        # active, ordered
OpenLoam::Dictionaries.default("damage_severity")        # the default entry
OpenLoam::Dictionaries.label_for("damage_severity", "critical")  # => "Critical"

Class Method Summary collapse

Class Method Details

.clear_cache ⇒ Object



40
41
42
# File 'lib/open_loam/dictionaries.rb', line 40

def clear_cache
  OpenLoam::Current.dictionary_cache = {}
end

.default(key) ⇒ Object

The default entry (the first flagged one), or nil.



27
28
29
# File 'lib/open_loam/dictionaries.rb', line 27

def default(key)
  entries(key).find(&:is_default?)
end

.entries(key) ⇒ Object

Active entries, ordered by position. Empty for an unknown key.



19
20
21
22
23
24
# File 'lib/open_loam/dictionaries.rb', line 19

def entries(key)
  dictionary = get(key)
  return [] unless dictionary

  memo([:entries, key.to_s]) { dictionary.entries.active.ordered.to_a }
end

.get(key) ⇒ Object

The dictionary for the current tenant, or nil.



14
15
16
# File 'lib/open_loam/dictionaries.rb', line 14

def get(key)
  memo([:dict, key.to_s]) { OpenLoam::Dictionary.find_by(key: key.to_s) }
end

.label_for(key, value) ⇒ Object

The label for a stored value — or the raw value itself when the key or value is unknown, so an old/foreign value still renders as something.



33
34
35
36
37
38
# File 'lib/open_loam/dictionaries.rb', line 33

def label_for(key, value)
  return value if value.blank?

  entry = entries(key).find { |e| e.value == value.to_s }
  entry ? entry.label : value
end