Skip to content

Commit 0b9f616

Browse files
committed
#14 allow user to specify his time zone
https://stackoverflow.com/a/12326821/11905229
1 parent ff25648 commit 0b9f616

File tree

6 files changed

+26
-2
lines changed

6 files changed

+26
-2
lines changed

app/controllers/accounts/profiles_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def set_user
2525
end
2626

2727
def user_params
28-
params.require(:user).permit(:email, :full_name)
28+
params.require(:user).permit(:email, :full_name, :time_zone)
2929
end
3030

3131
def after_update_url

app/controllers/application_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
class ApplicationController < ActionController::Base
44
before_action :prepare_meta_tags, if: -> { request.format.html? }
5+
before_action :set_time_zone
56

67
include StoreLocation
78

@@ -56,4 +57,9 @@ def #{s}!(redirect_url: nil)
5657
end
5758
RUBY_EVAL
5859
end
60+
61+
private
62+
def set_time_zone
63+
Time.zone = current_user.time_zone if current_user and current_user.time_zone.present?
64+
end
5965
end

app/views/accounts/profiles/show.html.erb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
<p class="text-muted">
1010
<%= t(".section.email.description") %>
1111
</p>
12+
<h5>
13+
<%= t(".section.time_zone.title") %>
14+
</h5>
15+
<p class="text-muted">
16+
<%= t(".section.time_zone.description") %>
17+
</p>
1218
</div>
1319
<div class="col-lg-8">
1420
<%= form_with(model: @user, url: account_profile_path, html: {method: :put}, local: true) do |f| %>
@@ -17,6 +23,11 @@
1723
<%= f.email_field :email, id: "user_email", autofocus: true, required: "required", class: "form-control", class_for_error: "is-invalid" %>
1824
<%= f.error_message :email, class: "invalid-feedback" %>
1925
</div>
26+
<div class="form-group text-left">
27+
<%= f.label :time_zone %>
28+
<%= f.select :time_zone, ActiveSupport::TimeZone::MAPPING.sort, {}, id: "user_time_zone", autofocus: true, required: "required", class: "form-control", class_for_error: "is-invalid" %>
29+
<%= f.error_message :time_zone, class: "invalid-feedback" %>
30+
</div>
2031

2132
<%= f.submit t(".submit"), class: "btn btn-block btn-primary" %>
2233
<% end %>

config/locales/views/accounts/profiles/en.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ en:
99
email:
1010
title: "Email"
1111
description: "After a successful email update, you should use new email on next log in."
12+
time_zone:
13+
title: "Time zone"
14+
description: "After a successful time zone update, the time will be shown in correct time zone"

config/locales/views/accounts/profiles/zh-CN.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ zh-CN:
99
email:
1010
title: "邮箱"
1111
description: "这些信息会影响到下一次登录,并且不能和其他用户的重复"
12+
time_zone:
13+
title: "时区"
14+
description: "这个选项会影响显示的时间的时区"

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2019_03_02_174954) do
13+
ActiveRecord::Schema.define(version: 2019_08_09_022738) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -43,6 +43,7 @@
4343
t.integer "invitations_count", default: 0
4444
t.datetime "created_at", precision: 6, null: false
4545
t.datetime "updated_at", precision: 6, null: false
46+
t.string "time_zone"
4647
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
4748
t.index ["email"], name: "index_users_on_email", unique: true
4849
t.index ["invitation_token"], name: "index_users_on_invitation_token", unique: true

0 commit comments

Comments
 (0)