Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add per user time zone support #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/accounts/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def set_user
end

def user_params
params.require(:user).permit(:email, :full_name)
params.require(:user).permit(:email, :full_name, :time_zone)
end

def after_update_url
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

include StoreLocation

Expand Down Expand Up @@ -56,4 +57,9 @@ def #{s}!(redirect_url: nil)
end
RUBY_EVAL
end

private
def set_time_zone
Time.zone = current_user.time_zone if current_user&.time_zone&.present?
end
end
11 changes: 11 additions & 0 deletions app/views/accounts/profiles/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
<p class="text-muted">
<%= t(".section.email.description") %>
</p>
<h5>
<%= t(".section.time_zone.title") %>
</h5>
<p class="text-muted">
<%= t(".section.time_zone.description") %>
</p>
</div>
<div class="col-lg-8">
<%= form_with(model: @user, url: account_profile_path, html: {method: :put}, local: true) do |f| %>
Expand All @@ -17,6 +23,11 @@
<%= f.email_field :email, id: "user_email", autofocus: true, required: "required", class: "form-control", class_for_error: "is-invalid" %>
<%= f.error_message :email, class: "invalid-feedback" %>
</div>
<div class="form-group text-left">
<%= f.label :time_zone %>
<%= f.select :time_zone, ActiveSupport::TimeZone::MAPPING.sort, {}, id: "user_time_zone", autofocus: true, required: "required", class: "form-control", class_for_error: "is-invalid" %>
<%= f.error_message :time_zone, class: "invalid-feedback" %>
</div>

<%= f.submit t(".submit"), class: "btn btn-block btn-primary" %>
<% end %>
Expand Down
3 changes: 3 additions & 0 deletions config/locales/views/accounts/profiles/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ en:
email:
title: "Email"
description: "After a successful email update, you should use new email on next log in."
time_zone:
title: "Time zone"
description: "After a successful time zone update, the time will be shown in correct time zone"
3 changes: 3 additions & 0 deletions config/locales/views/accounts/profiles/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ zh-CN:
email:
title: "邮箱"
description: "这些信息会影响到下一次登录,并且不能和其他用户的重复"
time_zone:
title: "时区"
description: "这个选项会影响显示的时间的时区"
5 changes: 5 additions & 0 deletions db/migrate/20190809022738_add_user_time_zone.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUserTimeZone < ActiveRecord::Migration[6.0]
def change
add_column :users, :time_zone, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_03_02_174954) do
ActiveRecord::Schema.define(version: 2019_08_09_022738) do

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