Class: ActiveAdmin::Favorites::Favorite::MacroCatalog

Inherits:
Object
  • Object
show all
Defined in:
app/models/active_admin/favorites/favorite/macro_catalog.rb

Defined Under Namespace

Classes: Template

Constant Summary collapse

CATEGORIES =
%i[time_range fiscal].freeze

Class Method Summary collapse

Class Method Details

.allObject



11
12
13
# File 'app/models/active_admin/favorites/favorite/macro_catalog.rb', line 11

def self.all
  @all ||= build_templates.index_by(&:key)
end

.attribute_label(attribute) ⇒ Object



48
49
50
# File 'app/models/active_admin/favorites/favorite/macro_catalog.rb', line 48

def self.attribute_label(attribute)
  attribute.to_s.tr("_", " ")
end

.describe(macro) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/active_admin/favorites/favorite/macro_catalog.rb', line 33

def self.describe(macro)
  case macro["type"]
  when "template"
    attribute = macro["attribute"]
    label = I18n.t("active_admin.favorites.macro_templates.#{macro["name"]}", default: macro["name"].humanize)
    "#{attribute_label(attribute)}: #{label}"
  when "relative_time_range"
    "#{attribute_label(macro["attribute"])}: #{I18n.t("active_admin.favorites.macro_templates.#{macro["preset"]}", default: macro["preset"].humanize)}"
  when "relative_time_bound"
    "#{attribute_label(macro["attribute"])} #{macro["predicate"]} (#{macro["unit"]} #{macro["offset"]})"
  else
    macro["type"].to_s.humanize
  end
end

.fetch(key) ⇒ Object



15
16
17
# File 'app/models/active_admin/favorites/favorite/macro_catalog.rb', line 15

def self.fetch(key)
  all.fetch(key.to_s) { raise KeyError, "Unknown macro template: #{key}" }
end

.format_date(value) ⇒ Object



105
106
107
# File 'app/models/active_admin/favorites/favorite/macro_catalog.rb', line 105

def self.format_date(value)
  value.to_date.strftime("%Y-%m-%d")
end

.options_for_select(category: nil) ⇒ Object



23
24
25
26
27
# File 'app/models/active_admin/favorites/favorite/macro_catalog.rb', line 23

def self.options_for_select(category: nil)
  templates = all.values
  templates = templates.select { |template| template.category == category.to_sym } if category.present?
  templates.map { |template| [I18n.t("active_admin.favorites.macro_templates.#{template.key}"), template.key] }
end

.resolve(template_key, attribute:, at: Time.zone.now) ⇒ Object



29
30
31
# File 'app/models/active_admin/favorites/favorite/macro_catalog.rb', line 29

def self.resolve(template_key, attribute:, at: Time.zone.now)
  fetch(template_key).resolver.call(attribute:, at: at.in_time_zone)
end

.template_keysObject



19
20
21
# File 'app/models/active_admin/favorites/favorite/macro_catalog.rb', line 19

def self.template_keys
  all.keys
end