Module: Mailscope::ApplicationHelper

Defined in:
app/helpers/mailscope/application_helper.rb

Constant Summary collapse

ASSET_ROOT =
Mailscope::Engine.root.join('app', 'assets', 'mailscope')

Instance Method Summary collapse

Instance Method Details

#mailscope_addresses(addresses, limit: 2) ⇒ Object



108
109
110
111
112
113
114
# File 'app/helpers/mailscope/application_helper.rb', line 108

def mailscope_addresses(addresses, limit: 2)
  return tag.span('', class: 'ms-muted') if addresses.empty?

  shown = addresses.first(limit).map(&:short).join(', ')
  rest  = addresses.size - limit
  rest.positive? ? "#{shown} +#{rest}" : shown
end

#mailscope_asset_path(file) ⇒ Object



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

def mailscope_asset_path(file)
  asset_path(digest: mailscope_assets_digest, file: file)
end

#mailscope_assets_digestObject

Digest of the asset bundle, so Cache-Control: immutable is safe and a gem upgrade (or a local edit while developing the gem) busts the cache.



11
12
13
14
15
16
17
# File 'app/helpers/mailscope/application_helper.rb', line 11

def mailscope_assets_digest
  if Rails.env.development? || ENV['MAILSCOPE_DEV']
    return Digest::MD5.hexdigest(Dir[ASSET_ROOT.join('*')].map { |f| "#{f}#{File.mtime(f).to_i}" }.join)[0, 12]
  end

  @mailscope_assets_digest ||= Digest::MD5.hexdigest(Mailscope::VERSION)[0, 12]
end

#mailscope_avatar(source, size: :md) ⇒ Object

Accepts a Mailbox, a Message::Address or a plain string. Initials come from the display name when there is one, so "Equipe Impulso" beats "nao-responda@...".



95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/helpers/mailscope/application_helper.rb', line 95

def mailscope_avatar(source, size: :md)
  mailbox = case source
            when Mailbox then source
            when Message::Address then Mailbox.new(address: source.address.to_s, name: source.name, count: 0)
            else Mailbox.new(address: source.to_s, name: nil, count: 0)
            end

  tag.span(mailbox.initials,
           class: "ms-avatar ms-avatar--#{size}",
           style: "--avatar-hue: #{mailbox.hue}",
           aria: { hidden: true })
end

#mailscope_bytes(size) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'app/helpers/mailscope/application_helper.rb', line 80

def mailscope_bytes(size)
  size = size.to_i
  units = %w[B KB MB GB]
  index = 0
  value = size.to_f
  while value >= 1024 && index < units.size - 1
    value /= 1024
    index += 1
  end
  format(index.zero? ? '%d %s' : '%.1f %s', value, units[index])
end

#mailscope_datetime(time) ⇒ Object



76
77
78
# File 'app/helpers/mailscope/application_helper.rb', line 76

def mailscope_datetime(time)
  time.getlocal.strftime(ms_t('time.datetime'))
end

#mailscope_filter_url(overrides = {}) ⇒ Object



122
123
124
125
# File 'app/helpers/mailscope/application_helper.rb', line 122

def mailscope_filter_url(overrides = {})
  base = { mailbox: params[:mailbox], q: params[:q], only: params[:only] }
  root_path(base.merge(overrides).compact_blank)
end

#mailscope_icon(name, size: 16) ⇒ Object



116
117
118
119
120
# File 'app/helpers/mailscope/application_helper.rb', line 116

def mailscope_icon(name, size: 16)
  tag.svg(tag.use(nil, href: "##{name}"),
          class: "ms-icon ms-icon--#{name}", width: size, height: size,
          viewBox: '0 0 24 24', fill: 'none', aria: { hidden: true })
end

#mailscope_js_stringsObject

Strings the front-end needs. Serialised once into the page so the JS bundle stays a static, cacheable asset with no locale baked in.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/helpers/mailscope/application_helper.rb', line 50

def mailscope_js_strings
  {
    confirmClearTitle: ms_t('confirm.clear_title'),
    confirmClearBodyOne: ms_t('confirm.clear_body', count: 1),
    confirmClearBody: ms_t('confirm.clear_body', count: 2).sub('2', '%{count}'),
    confirmClearAccept: ms_t('confirm.clear_accept'),
    deleted: ms_t('toasts.deleted'),
    deleteFailed: ms_t('toasts.delete_failed'),
    clearedOne: ms_t('toasts.cleared', count: 1),
    clearedMany: ms_t('toasts.cleared', count: 2).sub('2', '%{count}'),
    clearFailed: ms_t('toasts.clear_failed'),
    newMail: ms_t('toasts.new_mail'),
    open: ms_t('toasts.open'),
    listFailed: ms_t('toasts.list_failed'),
    openFailed: ms_t('toasts.open_failed'),
    sourceError: ms_t('message.source_error'),
    emptyTitle: ms_t('empty.inbox_title'),
    theme: ms_t('toasts.theme', name: '%{name}'),
    themeSystem: ms_t('toasts.theme_system'),
    themeLight: ms_t('toasts.theme_light'),
    themeDark: ms_t('toasts.theme_dark'),
    timeNow: ms_t('time.now'),
    timeMinutes: ms_t('time.minutes', count: 0).sub('0', '%{count}')
  }
end

#mailscope_short_time(time) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/helpers/mailscope/application_helper.rb', line 35

def mailscope_short_time(time)
  now = Time.now
  seconds = (now - time).to_i

  case seconds
  when ...60      then ms_t('time.now')
  when ...3600    then ms_t('time.minutes', count: seconds / 60)
  when ...86_400  then time.strftime('%H:%M')
  when ...604_800 then ms_t('time.weekdays')[time.wday]
  else time.strftime('%d/%m')
  end
end

#mailscope_time_tag(time) ⇒ Object



28
29
30
31
32
33
# File 'app/helpers/mailscope/application_helper.rb', line 28

def mailscope_time_tag(time)
  tag.time(mailscope_short_time(time),
           datetime: time.iso8601,
           title: mailscope_datetime(time),
           data: { relative: time.iso8601 })
end

#ms_t(key, **options) ⇒ Object

Shorthand for the engine's translation namespace.



24
25
26
# File 'app/helpers/mailscope/application_helper.rb', line 24

def ms_t(key, **options)
  t("mailscope.#{key}", **options)
end