Commit 42789f670e92d815eb1013f9ca69d3cba31069e4

Authored by Nathan B
2 parents fb03e5a1 4075b575
Exists in master and in 1 other branch production

Merge pull request #90 from martinciu/4075b57526e2632bda8a61fed1dd7678d3859b79

Time zone support
app/controllers/application_controller.rb
@@ -2,6 +2,7 @@ class ApplicationController < ActionController::Base @@ -2,6 +2,7 @@ class ApplicationController < ActionController::Base
2 protect_from_forgery 2 protect_from_forgery
3 3
4 before_filter :authenticate_user! 4 before_filter :authenticate_user!
  5 + before_filter :set_time_zone
5 6
6 # Devise override - After login, if there is only one app, 7 # Devise override - After login, if there is only one app,
7 # redirect to that app's path instead of the root path (apps#index). 8 # redirect to that app's path instead of the root path (apps#index).
@@ -23,7 +24,10 @@ protected @@ -23,7 +24,10 @@ protected
23 def redirect_to_root 24 def redirect_to_root
24 redirect_to(root_path) 25 redirect_to(root_path)
25 end 26 end
26 - 27 +
  28 + def set_time_zone
  29 + Time.zone = current_user.time_zone if user_signed_in?
  30 + end
27 31
28 end 32 end
29 33
app/models/user.rb
@@ -11,6 +11,7 @@ class User @@ -11,6 +11,7 @@ class User
11 field :name 11 field :name
12 field :admin, :type => Boolean, :default => false 12 field :admin, :type => Boolean, :default => false
13 field :per_page, :type => Fixnum, :default => PER_PAGE 13 field :per_page, :type => Fixnum, :default => PER_PAGE
  14 + field :time_zone, :default => "UTC"
14 15
15 after_destroy :destroy_watchers 16 after_destroy :destroy_watchers
16 before_save :ensure_authentication_token 17 before_save :ensure_authentication_token
app/views/users/_fields.html.haml
@@ -18,6 +18,10 @@ @@ -18,6 +18,10 @@
18 = f.select :per_page, [10, 20, 30, 50, 75, 100] 18 = f.select :per_page, [10, 20, 30, 50, 75, 100]
19 19
20 .required 20 .required
  21 + = f.label :time_zone
  22 + = f.time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones
  23 +
  24 +.required
21 = f.label :password 25 = f.label :password
22 = f.password_field :password 26 = f.password_field :password
23 27
config/initializers/mongo.rb
@@ -6,6 +6,7 @@ if mongo = ENV['MONGOHQ_URL'] || ENV['MONGOLAB_URI'] @@ -6,6 +6,7 @@ if mongo = ENV['MONGOHQ_URL'] || ENV['MONGOLAB_URI']
6 config.master = Mongo::Connection.new(settings.host, settings.port).db(database_name) 6 config.master = Mongo::Connection.new(settings.host, settings.port).db(database_name)
7 config.master.authenticate(settings.user, settings.password) if settings.user 7 config.master.authenticate(settings.user, settings.password) if settings.user
8 config.allow_dynamic_fields = false 8 config.allow_dynamic_fields = false
  9 + config.use_activesupport_time_zone = true
9 end 10 end
10 end 11 end
11 12
spec/controllers/users_controller_spec.rb
@@ -16,6 +16,10 @@ describe UsersController do @@ -16,6 +16,10 @@ describe UsersController do
16 before do 16 before do
17 sign_in @user = Factory(:user) 17 sign_in @user = Factory(:user)
18 end 18 end
  19 +
  20 + it "should set a time zone" do
  21 + Time.zone.should.to_s == @user.time_zone
  22 + end
19 23
20 context "GET /users/:other_id/edit" do 24 context "GET /users/:other_id/edit" do
21 it "redirects to the home page" do 25 it "redirects to the home page" do
@@ -34,6 +38,11 @@ describe UsersController do @@ -34,6 +38,11 @@ describe UsersController do
34 get :edit, :id => @user.id 38 get :edit, :id => @user.id
35 response.body.should match(/id="user_per_page"/) 39 response.body.should match(/id="user_per_page"/)
36 end 40 end
  41 +
  42 + it "should have time_zone option" do
  43 + get :edit, :id => @user.id
  44 + response.body.should match(/id="user_time_zone"/)
  45 + end
37 end 46 end
38 47
39 context "PUT /users/:other_id" do 48 context "PUT /users/:other_id" do
@@ -64,6 +73,11 @@ describe UsersController do @@ -64,6 +73,11 @@ describe UsersController do
64 put :update, :id => @user.to_param, :user => {:per_page => 555} 73 put :update, :id => @user.to_param, :user => {:per_page => 555}
65 @user.reload.per_page.should == 555 74 @user.reload.per_page.should == 555
66 end 75 end
  76 +
  77 + it "should be able to set time_zone option" do
  78 + put :update, :id => @user.to_param, :user => {:time_zone => "Warsaw"}
  79 + @user.reload.time_zone.should == "Warsaw"
  80 + end
67 end 81 end
68 82
69 context "when the update is unsuccessful" do 83 context "when the update is unsuccessful" do