Module: Glib::EnumHelper

Included in:
JsonUi::ViewBuilder::Fields::TimeZone
Defined in:
app/helpers/glib/enum_helper.rb

Instance Method Summary collapse

Instance Method Details

#glib_enum_options(clazz, enum_field, keys: nil, hint_suffix: false, hint_subtitle: false) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'app/helpers/glib/enum_helper.rb', line 3

def glib_enum_options(clazz, enum_field, keys: nil, hint_suffix: false, hint_subtitle: false)
  enum_name = enum_field.to_s
  keys ||= clazz.defined_enums[enum_name].keys
  keys.map do |i|
    text = clazz.glib_enum_humanize(enum_field, i, hint_suffix: hint_suffix)
    option = { text: text, value: i }
    if hint_subtitle && (hint = clazz.glib_enum_hint(enum_field, i))
      option[:subtitle] = hint
    end
    option
  end
end

#glib_time_zone_optionsObject



17
18
19
20
21
22
23
24
25
26
27
# File 'app/helpers/glib/enum_helper.rb', line 17

def glib_time_zone_options
  # For some reason, some offsets differ on some machines causing test failures.
  # TZInfo::Timezone.default_dst = true
  return [] if Rails.env.test?

  ActiveSupport::TimeZone.all.group_by { |tz| tz.tzinfo.identifier }.map do |identifier, timezones|
   utc_offset = "#{timezones.first.utc_offset >= 0 ? '+' : ''}#{timezones.first.utc_offset / 60 / 60}h"
   locations = timezones.map(&:name).join(', ')
   { value: identifier, text: "#{utc_offset} #{locations}" }
 end
end