From 56e9843b3c84914bde6aacbb1516ca824c254622 Mon Sep 17 00:00:00 2001 From: Markus Kahl Date: Tue, 20 Feb 2018 12:41:57 +0000 Subject: [PATCH] user default time zone setting --- app/cells/settings/time_zone_setting_cell.rb | 54 +++++++++++++++++++ .../views/settings/time_zone_setting/show.erb | 3 ++ app/models/user.rb | 6 +++ app/views/settings/_display.html.erb | 9 ++++ app/views/users/_preferences.html.erb | 14 ++--- config/locales/en.yml | 3 ++ config/settings.yml | 2 + spec/models/users/default_timezone_spec.rb | 45 ++++++++++++++++ 8 files changed, 130 insertions(+), 6 deletions(-) create mode 100644 app/cells/settings/time_zone_setting_cell.rb create mode 100644 app/cells/views/settings/time_zone_setting/show.erb create mode 100644 spec/models/users/default_timezone_spec.rb diff --git a/app/cells/settings/time_zone_setting_cell.rb b/app/cells/settings/time_zone_setting_cell.rb new file mode 100644 index 0000000000..b0c5f1ecfd --- /dev/null +++ b/app/cells/settings/time_zone_setting_cell.rb @@ -0,0 +1,54 @@ +module Settings + ## + # A text field to enter numeric values. + class TimeZoneSettingCell < ::RailsCell + include ActionView::Helpers::FormOptionsHelper + include SettingsHelper + + options :form, :title + options container_class: "-wide" + options include_blank: true + + def name # name of setting and tag + model + end + + def render_select + if form.nil? + render_setting_select + else + render_form_select + end + end + + def render_form_select + form.select( + name, + time_zone_entries, + include_blank: include_blank, + container_class: container_class, + title: title + ) + end + + def render_setting_select + setting_select( + name, + time_zone_entries, + include_blank: include_blank, + container_class: container_class, + title: title + ) + end + + def time_zones + ActiveSupport::TimeZone.all + end + + ## + # Returns time zone (label, value) tuples to be used for a select field. + def time_zone_entries + time_zones.map { |tz| [tz.to_s, tz.name ] } + end + end +end diff --git a/app/cells/views/settings/time_zone_setting/show.erb b/app/cells/views/settings/time_zone_setting/show.erb new file mode 100644 index 0000000000..15e215a261 --- /dev/null +++ b/app/cells/views/settings/time_zone_setting/show.erb @@ -0,0 +1,3 @@ +
+ <%= render_select %> +
diff --git a/app/models/user.rb b/app/models/user.rb index 5759d5278d..05c262f9b8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -127,6 +127,8 @@ class User < Principal validate :password_meets_requirements after_save :update_password + after_save :set_default_timezone! + before_create :sanitize_mail_notification_setting before_destroy :delete_associated_private_queries before_destroy :reassign_associated @@ -694,6 +696,10 @@ class User < Principal system_user end + def set_default_timezone! + pref.time_zone = pref.time_zone.presence || Setting.user_default_timezone.presence + end + protected # Login must not be special value 'me' diff --git a/app/views/settings/_display.html.erb b/app/views/settings/_display.html.erb index ecb92ea23a..219eb5433c 100644 --- a/app/views/settings/_display.html.erb +++ b/app/views/settings/_display.html.erb @@ -44,6 +44,15 @@ See docs/COPYRIGHT.rdoc for more details.
<%= setting_select :user_format, @options[:user_format], container_class: '-slim' %>
+ <%= + cell( + Settings::TimeZoneSettingCell, + "user_default_timezone", + container_class: "-slim", + title: I18n.t("tooltip_user_default_timezone") + ) + %> +
<%= setting_text_field :journal_aggregation_time_minutes, unit: t(:label_minute_plural), container_class: '-xslim' %> <%= t(:text_journal_aggregation_time_explanation) %>
diff --git a/app/views/users/_preferences.html.erb b/app/views/users/_preferences.html.erb index 7cba1602af..b4abc3b1c9 100644 --- a/app/views/users/_preferences.html.erb +++ b/app/views/users/_preferences.html.erb @@ -27,12 +27,14 @@ See docs/COPYRIGHT.rdoc for more details. ++#%> <%= fields_for :pref, @user.pref, builder: TabularFormBuilder, lang: current_language do |pref_fields| %> -
- <%= pref_fields.select :time_zone, - ActiveSupport::TimeZone.all.collect {|z| [ z.to_s, z.name ]}, - include_blank: true, - container_class: (defined? input_size) ? "-#{input_size}" : '-wide' %> -
+ <%= + cell( + Settings::TimeZoneSettingCell, + "time_zone", + form: pref_fields, + container_class: (defined? input_size) ? "-#{input_size}" : "-wide" + ) + %>
<%= pref_fields.select :comments_sorting, [[t(:label_chronological_order), 'asc'], diff --git a/config/locales/en.yml b/config/locales/en.yml index 353585f57b..78e28fce9f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2007,6 +2007,7 @@ en: setting_time_format: "Time format" setting_accessibility_mode_for_anonymous: "Enable accessibility mode for anonymous users" setting_user_format: "Users display format" + setting_user_default_timezone: "Users default timezone" setting_users_deletable_by_admins: "User accounts deletable by admins" setting_users_deletable_by_self: "Users allowed to delete their accounts" setting_welcome_text: "Welcome block text" @@ -2335,6 +2336,8 @@ en: title_remove_and_delete_user: Remove the invited user from the project and delete him/her. + tooltip_user_default_timezone: > + The default timezone for new users. Can be changed in a user's settings. tooltip_resend_invitation: > Sends another invitation email with a fresh token in case the old one expired or the user did not get the original email. diff --git a/config/settings.yml b/config/settings.yml index 0222e79643..2bfee5bcda 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -305,6 +305,8 @@ emails_header: en: '' work_package_startdate_is_adddate: default: 1 +user_default_timezone: + default: "" users_deletable_by_admins: default: 0 users_deletable_by_self: diff --git a/spec/models/users/default_timezone_spec.rb b/spec/models/users/default_timezone_spec.rb new file mode 100644 index 0000000000..204f25f93d --- /dev/null +++ b/spec/models/users/default_timezone_spec.rb @@ -0,0 +1,45 @@ +#-- copyright +# OpenProject is a project management system. +# Copyright (C) 2012-2018 the OpenProject Foundation (OPF) +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2017 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See docs/COPYRIGHT.rdoc for more details. +#++ + +require "spec_helper" + +describe User, "default time zone" do + let(:user) { FactoryGirl.create :user } + + context "with no system default set" do + it "is not set" do + expect(user.pref.time_zone).to be_nil + end + end + + context "with a system default set", with_settings: { user_default_timezone: "Edinburgh" } do + it "is set to the default" do + expect(user.pref.time_zone).to eq "Edinburgh" + end + end +end