Commit ebb8fcdd authored by Peter Leitzen's avatar Peter Leitzen

Merge branch '290295-make-timezone-helper-configurable' into 'master'

Allow to customize keys for timezone_data helper

See merge request gitlab-org/gitlab!49113
parents 838ca714 2c459fb3
# frozen_string_literal: true # frozen_string_literal: true
module TimeZoneHelper module TimeZoneHelper
def timezone_data TIME_ZONE_FORMAT_ATTRS = {
short: %i[identifier name offset],
full: %i[identifier name abbr offset formatted_offset]
}.freeze
private_constant :TIME_ZONE_FORMAT_ATTRS
# format:
# * :full - all available fields
# * :short (default)
#
# Example:
# timezone_data # :short by default
# timezone_data(format: :full)
#
def timezone_data(format: :short)
attrs = TIME_ZONE_FORMAT_ATTRS.fetch(format) do
valid_formats = TIME_ZONE_FORMAT_ATTRS.keys.map { |k| ":#{k}"}.join(", ")
raise ArgumentError.new("Invalid format :#{format}. Valid formats are #{valid_formats}.")
end
ActiveSupport::TimeZone.all.map do |timezone| ActiveSupport::TimeZone.all.map do |timezone|
{ {
identifier: timezone.tzinfo.identifier, identifier: timezone.tzinfo.identifier,
...@@ -9,7 +28,7 @@ module TimeZoneHelper ...@@ -9,7 +28,7 @@ module TimeZoneHelper
abbr: timezone.tzinfo.strftime('%Z'), abbr: timezone.tzinfo.strftime('%Z'),
offset: timezone.now.utc_offset, offset: timezone.now.utc_offset,
formatted_offset: timezone.now.formatted_offset formatted_offset: timezone.now.formatted_offset
} }.slice(*attrs)
end end
end end
end end
...@@ -6,7 +6,7 @@ module IncidentManagement ...@@ -6,7 +6,7 @@ module IncidentManagement
{ {
'project-path' => project.full_path, 'project-path' => project.full_path,
'empty-oncall-schedules-svg-path' => image_path('illustrations/empty-state/empty-on-call.svg'), 'empty-oncall-schedules-svg-path' => image_path('illustrations/empty-state/empty-on-call.svg'),
'timezones' => timezone_data.to_json 'timezones' => timezone_data(format: :full).to_json
} }
end end
end end
......
...@@ -12,7 +12,7 @@ RSpec.describe IncidentManagement::OncallScheduleHelper do ...@@ -12,7 +12,7 @@ RSpec.describe IncidentManagement::OncallScheduleHelper do
is_expected.to eq( is_expected.to eq(
'project-path' => project.full_path, 'project-path' => project.full_path,
'empty-oncall-schedules-svg-path' => helper.image_path('illustrations/empty-state/empty-on-call.svg'), 'empty-oncall-schedules-svg-path' => helper.image_path('illustrations/empty-state/empty-on-call.svg'),
'timezones' => helper.timezone_data.to_json 'timezones' => helper.timezone_data(format: :full).to_json
) )
end end
end end
......
...@@ -4,32 +4,68 @@ require 'spec_helper' ...@@ -4,32 +4,68 @@ require 'spec_helper'
RSpec.describe TimeZoneHelper, :aggregate_failures do RSpec.describe TimeZoneHelper, :aggregate_failures do
describe '#timezone_data' do describe '#timezone_data' do
subject(:timezone_data) { helper.timezone_data } context 'with short format' do
subject(:timezone_data) { helper.timezone_data }
it 'matches schema' do
expect(timezone_data).not_to be_empty it 'matches schema' do
expect(timezone_data).not_to be_empty
timezone_data.each_with_index do |timezone_hash, i|
expect(timezone_hash.keys).to contain_exactly( timezone_data.each_with_index do |timezone_hash, i|
:identifier, expect(timezone_hash.keys).to contain_exactly(
:name, :identifier,
:abbr, :name,
:offset, :offset
:formatted_offset ), "Failed at index #{i}"
), "Failed at index #{i}" end
end
it 'formats for display' do
tz = ActiveSupport::TimeZone.all[0]
expect(timezone_data[0]).to eq(
identifier: tz.tzinfo.identifier,
name: tz.name,
offset: tz.now.utc_offset
)
end
end
context 'with full format' do
subject(:timezone_data) { helper.timezone_data(format: :full) }
it 'matches schema' do
expect(timezone_data).not_to be_empty
timezone_data.each_with_index do |timezone_hash, i|
expect(timezone_hash.keys).to contain_exactly(
:identifier,
:name,
:abbr,
:offset,
:formatted_offset
), "Failed at index #{i}"
end
end
it 'formats for display' do
tz = ActiveSupport::TimeZone.all[0]
expect(timezone_data[0]).to eq(
identifier: tz.tzinfo.identifier,
name: tz.name,
abbr: tz.tzinfo.strftime('%Z'),
offset: tz.now.utc_offset,
formatted_offset: tz.now.formatted_offset
)
end end
end end
it 'formats for display' do context 'with unknown format' do
tz = ActiveSupport::TimeZone.all[0] subject(:timezone_data) { helper.timezone_data(format: :unknown) }
expect(timezone_data[0]).to eq( it 'raises an exception' do
identifier: tz.tzinfo.identifier, expect { timezone_data }.to raise_error ArgumentError, 'Invalid format :unknown. Valid formats are :short, :full.'
name: tz.name, end
abbr: tz.tzinfo.strftime('%Z'),
offset: tz.now.utc_offset,
formatted_offset: tz.now.formatted_offset
)
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment