Saturday, 4 July 2015

User TimeZones

#1 .ApplicationController
class ApplicationController < ActionController::Base
around_filter :set_time_zone
def set_time_zone(&block)
time_zone = current_user.shop.try(:timezone) || 'UTC'
Time.use_zone(time_zone, &block)
end
end
#2. Generate migration
class AddTimezoneToShops < ActiveRecord::Migration
def change
add_column :shops, :timezone, :string, :default => 'Europe/Athens'
end
end
#3. Set a form
<div class="form-group">
<%= f.label :timezone, :class => "col-lg-2 control-label" %>
<div class="col-lg-10">
<%= f.input :timezone, :collection => Timezone::Zone.names, :label => false ,:include_blank => false,
:class => "form-control" %>
</div>
</div>
view raw gistfile1.rb hosted with ❤ by GitHub