diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 948df939..5e540dbb 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,8 @@ class ApplicationController < ActionController::Base layout :api_or_html + before_filter :authenticate_user! + # CanCan - always check authorization check_authorization unless: :devise_controller? @@ -36,6 +38,8 @@ class ApplicationController < ActionController::Base after_filter :set_csrf_cookie_for_ng, :resource_representation_caching_fixes + before_filter :set_user_time_zone + # set and reset user stamper for each request # based on https://github.com/theepan/userstamp/tree/bf05d832ee27a717ea9455d685c83ae2cfb80310 around_filter :set_then_reset_user_stamper @@ -411,4 +415,8 @@ def set_then_reset_user_stamper end end + def set_user_time_zone + Time.zone = current_user.time_zone_name if user_signed_in? + end + end diff --git a/app/models/user.rb b/app/models/user.rb index fc304d63..e2edc114 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -12,7 +12,7 @@ class User < ActiveRecord::Base attr_accessible :user_name, :email, :password, :password_confirmation, :remember_me, :roles, :roles_mask, :preferences, - :image, :login + :image, :login, :time_zone_name # Virtual attribute for authenticating by either :user_name or :email # This is in addition to real persisted fields. diff --git a/app/views/devise/registrations/new.html.haml b/app/views/devise/registrations/new.html.haml index 91684319..0f79b17f 100644 --- a/app/views/devise/registrations/new.html.haml +++ b/app/views/devise/registrations/new.html.haml @@ -5,6 +5,7 @@ = f.input :user_name, required: true, autofocus: true = f.input :password, required: true = f.input :password_confirmation, required: true + = f.time_zone_select :time_zone_name, /Australia/, default: 'Brisbane', required: true = f.input :email, required: true .form-actions = f.button :submit, 'Sign up' diff --git a/config/application.rb b/config/application.rb index 16dc453c..0570b6c5 100644 --- a/config/application.rb +++ b/config/application.rb @@ -40,6 +40,8 @@ class Application < Rails::Application # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. # config.time_zone = 'Central Time (US & Canada)' #config.time_zone - 'UTC' + # used as default when user is not logged in + # for detecting user daylight savings time, see http://stackoverflow.com/a/11888430/31567 config.time_zone = 'Brisbane' # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. diff --git a/db/migrate/20140906231545_add_time_zone_name_to_user.rb b/db/migrate/20140906231545_add_time_zone_name_to_user.rb new file mode 100644 index 00000000..73683f8c --- /dev/null +++ b/db/migrate/20140906231545_add_time_zone_name_to_user.rb @@ -0,0 +1,5 @@ +class AddTimeZoneNameToUser < ActiveRecord::Migration + def change + add_column :users, :time_zone_name, :string, null:false, default: 'Brisbane' + end +end