Module: SolidWebUi::Cache::ApplicationHelper

Defined in:
app/helpers/solid_web_ui/cache/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#cache_expires_at(expiry) ⇒ Object

Absolute expiry of a cache entry (see EntriesController#entry_expiry).



24
25
26
27
28
29
30
31
32
# File 'app/helpers/solid_web_ui/cache/application_helper.rb', line 24

def cache_expires_at(expiry)
  case expiry
  when Numeric then short_time(Time.at(expiry))
  when :never  then "Never"
  else ""
  end
rescue StandardError
  "" # a corrupt/out-of-range epoch shouldn't break the list
end

#cache_nav(active) ⇒ Object



5
6
7
8
9
10
# File 'app/helpers/solid_web_ui/cache/application_helper.rb', line 5

def cache_nav(active)
  [
    { label: "Dashboard", href: root_path, active: active == :dashboard },
    { label: "Entries", href: entries_path, active: active == :entries }
  ]
end

#cache_time_to_expire(expiry) ⇒ Object

Remaining time until a cache entry expires, e.g. “about 2 hours” / “expired”.



35
36
37
38
39
40
41
42
# File 'app/helpers/solid_web_ui/cache/application_helper.rb', line 35

def cache_time_to_expire(expiry)
  return "" unless expiry.is_a?(Numeric)

  remaining = expiry - Time.now.to_f
  return "expired" if remaining <= 0

  distance_of_time_in_words(remaining)
end

#readable_key(key, length: 80) ⇒ Object

Cache keys are stored as binary; present them as readable, truncated text.



13
14
15
# File 'app/helpers/solid_web_ui/cache/application_helper.rb', line 13

def readable_key(key, length: 80)
  truncate(key.to_s.dup.force_encoding("UTF-8").scrub("?"), length: length)
end

#readable_value(value, length: 2000) ⇒ Object

Cache values are binary (often a Marshal/compressed blob). Present a scrubbed, truncated preview so the show page never dumps megabytes of unreadable bytes.



19
20
21
# File 'app/helpers/solid_web_ui/cache/application_helper.rb', line 19

def readable_value(value, length: 2000)
  truncate(value.to_s.dup.force_encoding("UTF-8").scrub("?"), length: length)
end

#short_time(time) ⇒ Object



44
45
46
47
48
# File 'app/helpers/solid_web_ui/cache/application_helper.rb', line 44

def short_time(time)
  return "" if time.nil?

  time.in_time_zone(SolidWebUi::Cache.config.time_zone).strftime("%Y-%m-%d %H:%M:%S")
end