diff --git a/app/helpers/time_zone_helper.rb b/app/helpers/time_zone_helper.rb index daf99ad9b5e92e8ccfb9b0fa61f46eef6de6a265..00f65b72c8e9a7c9b38baf5bbe0a693907bd5f78 100644 --- a/app/helpers/time_zone_helper.rb +++ b/app/helpers/time_zone_helper.rb @@ -1,7 +1,26 @@ # frozen_string_literal: true 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| { identifier: timezone.tzinfo.identifier, @@ -9,7 +28,7 @@ module TimeZoneHelper abbr: timezone.tzinfo.strftime('%Z'), offset: timezone.now.utc_offset, formatted_offset: timezone.now.formatted_offset - } + }.slice(*attrs) end end end diff --git a/ee/app/helpers/incident_management/oncall_schedule_helper.rb b/ee/app/helpers/incident_management/oncall_schedule_helper.rb index ec4ab46f7c8ddb7d9c0ac6b0357c653c470314a0..15ca48d681f93dbaa1b6ccce30a63d3ac002bdf2 100644 --- a/ee/app/helpers/incident_management/oncall_schedule_helper.rb +++ b/ee/app/helpers/incident_management/oncall_schedule_helper.rb @@ -6,7 +6,7 @@ module IncidentManagement { 'project-path' => project.full_path, '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 diff --git a/ee/spec/helpers/incident_management/oncall_schedule_helper_spec.rb b/ee/spec/helpers/incident_management/oncall_schedule_helper_spec.rb index 8382bfeaa4fdf3a478cb282aabba600cb32dbc2c..f1071072fd8f9b6ddccda0a3234be1f23daae425 100644 --- a/ee/spec/helpers/incident_management/oncall_schedule_helper_spec.rb +++ b/ee/spec/helpers/incident_management/oncall_schedule_helper_spec.rb @@ -12,7 +12,7 @@ RSpec.describe IncidentManagement::OncallScheduleHelper do is_expected.to eq( 'project-path' => project.full_path, '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 diff --git a/spec/helpers/time_zone_helper_spec.rb b/spec/helpers/time_zone_helper_spec.rb index 7e7eb3684747419284ff845b545c01a624b6d914..391e9bd38ed1f22ab7bb79b22e001fa2e71dafc3 100644 --- a/spec/helpers/time_zone_helper_spec.rb +++ b/spec/helpers/time_zone_helper_spec.rb @@ -4,32 +4,68 @@ require 'spec_helper' RSpec.describe TimeZoneHelper, :aggregate_failures do describe '#timezone_data' do - subject(:timezone_data) { helper.timezone_data } - - 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}" + context 'with short format' do + subject(:timezone_data) { helper.timezone_data } + + 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, + :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, + 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 - it 'formats for display' do - tz = ActiveSupport::TimeZone.all[0] + context 'with unknown format' do + subject(:timezone_data) { helper.timezone_data(format: :unknown) } - 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 - ) + it 'raises an exception' do + expect { timezone_data }.to raise_error ArgumentError, 'Invalid format :unknown. Valid formats are :short, :full.' + end end end end