Permalink
Browse files

adding user time zone preference

  • Loading branch information...
1 parent 8db51b1 commit ae215704380d31b26dae13f3d38d99d28044589c @ryanb committed Oct 31, 2009
@@ -9,6 +9,8 @@ class ApplicationController < ActionController::Base
# Scrub sensitive parameters from your log
filter_parameter_logging :password
+ before_filter :set_user_time_zone
+
private
def current_user_or_guest
@@ -17,4 +19,8 @@ def current_user_or_guest
end
current_user
end
+
+ def set_user_time_zone
+ Time.zone = current_user.time_zone if logged_in? && !current_user.time_zone.blank?
+ end
end
@@ -11,7 +11,7 @@ def index
def show
@stamp = Stamp.find(params[:id])
- @date = params[:month] ? Date.parse(params[:month]) : Date.today
+ @date = params[:month] ? Date.parse(params[:month]) : Time.zone.today
raise "Date is too far in the future" if @date.to_time > 5.years.from_now
if current_user && @stamp.user_id == current_user.id
current_user.current_stamp_id = @stamp.id
@@ -13,7 +13,7 @@ def generate_graphics
STAMP_COLORS.each do |color|
generate_graphic_for_color(color)
end
- update_attribute(:generated_at, Time.now)
+ update_attribute(:generated_at, Time.zone.now)
end
def generate_graphic_for_color(color)
@@ -14,7 +14,7 @@
<div class="score_bar" style="background-position: <%= -200 + (favorite.stamp.goal_progress*2) %>px 0">score <%=h favorite.stamp.score %></div>
<table border="0" cellspacing="0" cellpadding="0" class="days">
<tr>
- <% (7.days.ago.to_date..Date.today).each do |date| %>
+ <% (7.days.ago.to_date..Time.zone.today).each do |date| %>
<td class="day">
<%= small_mark_image(favorite.stamp.marks.first(:conditions => {:marked_on => date})) %>
</td>
@@ -3,7 +3,7 @@
<%=h @date.strftime("%B %Y") %>
<%= link_to ">", :month => (@date.end_of_month+1).strftime("%Y-%m") %>
</h2>
-<% calendar_for(@stamp.marks, :year => @date.year, :month => @date.month) do |calendar| %>
+<% calendar_for(@stamp.marks, :year => @date.year, :month => @date.month, :today => Time.zone.today) do |calendar| %>
<%= calendar.head('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday') %>
<% calendar.day(:day_method => :marked_on, :id => "day_%Y%m%d") do |day, marks| %>
<% if day.month == @date.month %>
@@ -8,6 +8,10 @@
<%= f.label :email, "Email Address" %><br />
<%= f.text_field :email %>
</p>
+ <p>
+ <%= f.label :time_zone %><br />
+ <%= f.time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones, :include_blank => true %>
+ </p>
<% if @user.openid_identifier.blank? %>
<p>
<%= f.label :password %><br />
View
@@ -38,7 +38,7 @@
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names.
- config.time_zone = 'UTC'
+ config.time_zone = 'Pacific Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
@@ -0,0 +1,9 @@
+class AddTimeZoneToUsers < ActiveRecord::Migration
+ def self.up
+ add_column :users, :time_zone, :string
+ end
+
+ def self.down
+ remove_column :users, :time_zone
+ end
+end
View
@@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20090912015155) do
+ActiveRecord::Schema.define(:version => 20091031172658) do
create_table "delayed_jobs", :force => true do |t|
t.integer "priority", :default => 0
@@ -102,6 +102,7 @@
t.integer "current_stamp_id"
t.string "openid_identifier"
t.boolean "guest", :default => false, :null => false
+ t.string "time_zone"
end
end
View
@@ -4,13 +4,13 @@
it "should clear score cache of stamp when creating" do
stamp = Factory(:stamp)
stamp.update_attribute(:score_cache, 123)
- stamp.marks.create!(:marked_on => Date.today)
+ stamp.marks.create!(:marked_on => Time.zone.today)
stamp.reload.score_cache.should be_nil
end
it "should clear score cache of stamp when destroying" do
stamp = Factory(:stamp)
- stamp.marks.create!(:marked_on => Date.today)
+ stamp.marks.create!(:marked_on => Time.zone.today)
stamp.update_attribute(:score_cache, 123)
stamp.marks.first.destroy
stamp.reload.score_cache.should be_nil
@@ -27,8 +27,8 @@
it "should clear future month cache of stamp when creating" do
stamp = Factory(:stamp)
cache1 = stamp.month_caches.create!(:for_month => 2.months.ago)
- cache2 = stamp.month_caches.create!(:for_month => Date.today.beginning_of_month)
- stamp.marks.create!(:marked_on => Date.today)
+ cache2 = stamp.month_caches.create!(:for_month => Time.zone.beginning_of_month)
+ stamp.marks.create!(:marked_on => Time.zone.today)
MonthCache.exists?(cache1).should be_true
MonthCache.exists?(cache2).should be_false
end

0 comments on commit ae21570

Please sign in to comment.