From 3fee071a1090cc14671dacacbb69ebb0338bdef1 Mon Sep 17 00:00:00 2001 From: wvengen Date: Wed, 25 Jun 2014 17:32:26 +0200 Subject: [PATCH] specify timezone in configuration (closes foodcoops#282) --- app/controllers/application_controller.rb | 14 +++++++++++++- app/helpers/admin/configs_helper.rb | 4 ++++ app/views/admin/configs/_tab_language.html.haml | 1 + config/app_config.yml.SAMPLE | 2 ++ config/locales/en.yml | 1 + config/locales/nl.yml | 1 + 6 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 3574ef44..6c14f1c9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -6,6 +6,7 @@ class ApplicationController < ActionController::Base protect_from_forgery before_filter :select_foodcoop, :authenticate, :store_controller, :items_per_page after_filter :remove_controller + around_filter :set_time_zone # Returns the controller handling the current request. @@ -167,5 +168,16 @@ class ApplicationController < ActionController::Base def default_url_options(options = {}) {foodcoop: FoodsoftConfig.scope} end - + + # Set timezone according to foodcoop preference. + # @see http://stackoverflow.com/questions/4362663/timezone-with-rails-3 + # @see http://archives.ryandaigle.com/articles/2008/1/25/what-s-new-in-edge-rails-easier-timezones + def set_time_zone + old_time_zone = Time.zone + Time.zone = FoodsoftConfig[:time_zone] if FoodsoftConfig[:time_zone] + yield + ensure + Time.zone = old_time_zone + end + end diff --git a/app/helpers/admin/configs_helper.rb b/app/helpers/admin/configs_helper.rb index 056f5cff..68d759fc 100644 --- a/app/helpers/admin/configs_helper.rb +++ b/app/helpers/admin/configs_helper.rb @@ -7,6 +7,7 @@ module Admin::ConfigsHelper # @param options [Hash] Options passed to the form builder. # @option options [Boolean] :required Wether field is shown as required (default not). # @return [String] Form input for configuration key. + # @todo find way to pass current value to time_zone input without using default def config_input(form, key, options = {}, &block) return unless @cfg.allowed_key? key options[:label] = config_input_label(form, key) @@ -21,6 +22,9 @@ module Admin::ConfigsHelper elsif options[:collection] or options[:as] == :select options[:selected] = options[:input_html].delete(:value) return form.input key, options, &block + elsif options[:as] == :time_zone + options[:default] = options[:input_html].delete(:value) + return form.input key, options, &block end form.input key, options, &block end diff --git a/app/views/admin/configs/_tab_language.html.haml b/app/views/admin/configs/_tab_language.html.haml index 4debea6a..c2c956a4 100644 --- a/app/views/admin/configs/_tab_language.html.haml +++ b/app/views/admin/configs/_tab_language.html.haml @@ -1,3 +1,4 @@ = config_input form, :default_locale, collection: I18n.available_locales.map {|l| [t("simple_form.options.settings.profile.language.#{l}"), l]} = config_input form, :ignore_browser_locale, as: :boolean += config_input form, :time_zone, as: :time_zone, include_blank: true, input_html: {class: 'input-xlarge'} diff --git a/config/app_config.yml.SAMPLE b/config/app_config.yml.SAMPLE index fbb4731e..a485c6e2 100644 --- a/config/app_config.yml.SAMPLE +++ b/config/app_config.yml.SAMPLE @@ -37,6 +37,8 @@ default: &defaults # In case you really want foodsoft in a certain language by default, set this to true. # When members are logged in, the language from their profile settings is still used. #ignore_browser_locale: false + # Default timezone, e.g. UTC, Amsterdam, Berlin, etc. + #time_zone: Berlin # price markup in percent price_markup: 2.0 diff --git a/config/locales/en.yml b/config/locales/en.yml index 79e956b7..88374665 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -504,6 +504,7 @@ en: pdf_font_size: Font size pdf_page_size: Page size pdf_add_page_breaks: Page breaks + time_zone: Time zone deliveries: add_stock_change: how_many_units: 'How many units (%{unit}) to deliver? Stock article name: %{name}.' diff --git a/config/locales/nl.yml b/config/locales/nl.yml index 56313c7a..a53baea2 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -487,6 +487,7 @@ nl: pdf_font_size: Tekstgrootte pdf_page_size: Paginaformaat pdf_add_page_breaks: "Nieuwe pagina's" + time_zone: Tijdzone deliveries: add_stock_change: how_many_units: 'Hoeveel eenheden (%{unit}) leveren? Voorraadartikel: %{name}.'