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 2 protect_from_forgery
3 3  
4 4 before_filter :authenticate_user!
  5 + before_filter :set_time_zone
5 6  
6 7 # Devise override - After login, if there is only one app,
7 8 # redirect to that app's path instead of the root path (apps#index).
... ... @@ -23,7 +24,10 @@ protected
23 24 def redirect_to_root
24 25 redirect_to(root_path)
25 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 32 end
29 33  
... ...
app/models/user.rb
... ... @@ -11,6 +11,7 @@ class User
11 11 field :name
12 12 field :admin, :type => Boolean, :default => false
13 13 field :per_page, :type => Fixnum, :default => PER_PAGE
  14 + field :time_zone, :default => "UTC"
14 15  
15 16 after_destroy :destroy_watchers
16 17 before_save :ensure_authentication_token
... ...
app/views/users/_fields.html.haml
... ... @@ -18,6 +18,10 @@
18 18 = f.select :per_page, [10, 20, 30, 50, 75, 100]
19 19  
20 20 .required
  21 + = f.label :time_zone
  22 + = f.time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones
  23 +
  24 +.required
21 25 = f.label :password
22 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 config.master = Mongo::Connection.new(settings.host, settings.port).db(database_name)
7 7 config.master.authenticate(settings.user, settings.password) if settings.user
8 8 config.allow_dynamic_fields = false
  9 + config.use_activesupport_time_zone = true
9 10 end
10 11 end
11 12  
... ...
spec/controllers/users_controller_spec.rb
... ... @@ -16,6 +16,10 @@ describe UsersController do
16 16 before do
17 17 sign_in @user = Factory(:user)
18 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 24 context "GET /users/:other_id/edit" do
21 25 it "redirects to the home page" do
... ... @@ -34,6 +38,11 @@ describe UsersController do
34 38 get :edit, :id => @user.id
35 39 response.body.should match(/id="user_per_page"/)
36 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 46 end
38 47  
39 48 context "PUT /users/:other_id" do
... ... @@ -64,6 +73,11 @@ describe UsersController do
64 73 put :update, :id => @user.to_param, :user => {:per_page => 555}
65 74 @user.reload.per_page.should == 555
66 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 81 end
68 82  
69 83 context "when the update is unsuccessful" do
... ...