Skip to content

Commit

Permalink
User accounts have a Time Zone
Browse files Browse the repository at this point in the history
Users' Time Zone is set as the default when creating events.
  • Loading branch information
tjgrathwell committed Mar 17, 2013
1 parent f9fe48f commit da7b5aa
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 13 deletions.
8 changes: 4 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ GEM
arel (3.0.2)
awesome_print (1.1.0)
bcrypt-ruby (3.0.1)
bootstrap-sass-rails (2.3.0.0)
bootstrap-sass-rails (2.3.1.0)
railties (>= 3.1.0)
sass-rails (>= 3.1.0)
bourne (1.1.2)
Expand Down Expand Up @@ -125,7 +125,7 @@ GEM
modernizr_rails (2.5.3)
railties (~> 3.0)
thor (~> 0.14)
multi_json (1.6.1)
multi_json (1.7.0)
multipart-post (1.2.0)
nested_form (0.3.1)
nokogiri (1.5.6)
Expand Down Expand Up @@ -203,7 +203,7 @@ GEM
rubyzip (0.9.9)
sanitize (2.0.3)
nokogiri (>= 1.4.4, < 1.6)
sass (3.2.6)
sass (3.2.7)
sass-rails (3.2.6)
railties (~> 3.2.0)
sass (>= 3.1.10)
Expand Down Expand Up @@ -236,7 +236,7 @@ GEM
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
thor (0.17.0)
tilt (1.3.4)
tilt (1.3.5)
treetop (1.4.12)
polyglot
polyglot (>= 0.3.1)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def show
end

def new
@event = Event.new(:public_email => current_user.email)
@event = Event.new(public_email: current_user.email, time_zone: current_user.time_zone)
@event.event_sessions << EventSession.new
end

Expand Down
6 changes: 2 additions & 4 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable

after_create :make_empty_profile

devise :database_authenticatable, :registerable,
Expand All @@ -13,9 +10,10 @@ class User < ActiveRecord::Base

has_one :profile

attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :remember_me
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :remember_me, :time_zone

validates_presence_of :first_name, :last_name
validates_inclusion_of :time_zone, in: ActiveSupport::TimeZone.all.map(&:name), allow_blank: true

def self.not_assigned_as_organizer(event)
users = order('last_name asc, first_name asc, email asc')
Expand Down
11 changes: 9 additions & 2 deletions app/views/devise/registrations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
</div>

<div>
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i>
<%= f.label :password %>
<%= f.password_field :password %>
<span class="help-block">(leave blank if you don't want to change it)</span>
</div>

<div>
Expand All @@ -41,8 +42,14 @@
</div>

<div>
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i>
<%= f.label :current_password %>
<%= f.password_field :current_password %>
<span class="help-block">(we need your current password to confirm your changes)</span>
</div>

<div>
<%= f.label :time_zone %>
<%= f.time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones, include_blank: 'Select Time Zone' %>
</div>

<div><%= f.submit "Update" %></div>
Expand Down
5 changes: 5 additions & 0 deletions app/views/devise/registrations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
<%= f.password_field :password_confirmation %>
</div>

<div>
<%= f.label :time_zone %>
<%= f.time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones, include_blank: 'Select Time Zone' %>
</div>

<div><%= f.submit "Sign up" %></div>
<% end %>
Expand Down
15 changes: 15 additions & 0 deletions db/migrate/20130317031753_add_time_zone_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class AddTimeZoneToUsers < ActiveRecord::Migration
class User < ActiveRecord::Base; end

def up
add_column :users, :time_zone, :string
User.find_each do |user|
user.time_zone = "Pacific Time (US & Canada)"
user.save!
end
end

def down
remove_column :users, :time_zone
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20130310073101) do
ActiveRecord::Schema.define(:version => 20130317031753) do

create_table "event_sessions", :force => true do |t|
t.datetime "starts_at"
Expand Down Expand Up @@ -117,6 +117,7 @@
t.string "first_name"
t.string "last_name"
t.integer "meetup_id"
t.string "time_zone"
end

add_index "users", ["confirmation_token"], :name => "index_users_on_confirmation_token", :unique => true
Expand Down
7 changes: 6 additions & 1 deletion spec/controllers/events_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def make_request

context "when a user is logged in" do
before do
@user = create(:user)
@user = create(:user, time_zone: 'Alaska')
sign_in @user
end

Expand All @@ -64,6 +64,11 @@ def make_request
get :new
assigns(:event).should be_new_record
end

it "uses the logged in user's time zone as the event's time zone" do
get :new
assigns(:event).time_zone.should == 'Alaska'
end
end
end

Expand Down
8 changes: 8 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
it { should validate_presence_of(:last_name) }
it { should validate_presence_of(:email) } # devise adds this

it "must have a valid time zone" do
user = build(:user, :time_zone => "xxx")
user.should have(1).error_on(:time_zone)

user = build(:user, :time_zone => 'Hawaii')
user.should have(0).errors_on(:time_zone)
end

it "creates a profile when the user is created" do
@user.profile.should_not be_nil
end
Expand Down

0 comments on commit da7b5aa

Please sign in to comment.