Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails time_zone_select to use "official" time zone values

My view (haml):

= f.time_zone_select :time_zone, nil, {}, class: 'form-control'

Renders to:

...
<option value="Ljubljana">(GMT+01:00) Ljubljana</option>
<option value="Madrid">(GMT+01:00) Madrid</option>
<option value="Paris">(GMT+01:00) Paris</option>
<option value="Prague">(GMT+01:00) Prague</option>
...

When creating a record I want to set the :time_zone attribute to the current time zone of the user. There are several ways to do this but after researching I found either the gem timezone_local or the JS library jstz most common ones. Either way, all those open source libraries seem to use a different pattern then rails does, e.g the Paris timezone is defined as:

Europe/Paris

However, the TimeZone object in Rails uses

Paris

this leads to inconsistencies when I want to create a record with the current time zone of the user as it won't match. I need rails to use the same patterns as jstz or vice versa but I don't want to gsub strings here or similiar. What is the best approach here?

like image 275
DonMB Avatar asked Oct 30 '22 15:10

DonMB


1 Answers

You can use Draper to modify all and name (the list and value methods used by time_zone_select):

class TimeZoneDecorator < Draper::Decorator
  decorates ActiveSupport::TimeZone
  delegate_all

  def self.all
    TimeZoneDecorator.decorate_collection(ActiveSupport::TimeZone.all)
  end

  def self.us_zones
    TimeZoneDecorator.decorate_collection(ActiveSupport::TimeZone.us_zones)
  end

  # other custom collection methods

  def name
    tzinfo.name # "Europe/Paris" instead of "Paris"
  end
end

And then use the model option in your tag:

= f.time_zone_select :timezone, TimeZoneDecorator.us_zones, {include_blank: "Set from Lat/Lng", model: TimeZoneDecorator}, {class: 'form-control'}
like image 182
Jerph Avatar answered Nov 15 '22 04:11

Jerph