Commit df91285

mo khan <mo@mokhan.ca>
2016-05-14 01:55:03
add time_zone to profile.
1 parent 169f760
app/controllers/profiles_controller.rb
@@ -20,6 +20,6 @@ class ProfilesController < ApplicationController
   private
 
   def profile_params
-    params.require(:profile).permit(:gender, :social_tolerance)
+    params.require(:profile).permit(:gender, :social_tolerance, :time_zone)
   end
 end
app/models/profile.rb
@@ -3,6 +3,10 @@ class Profile < ActiveRecord::Base
   enum social_tolerance: { low: 0, medium: 1, high: 2 }
   enum gender: { female: 0, male: 1, transgender: 2, other: nil }
 
+  def time_zone
+    @time_zone ||= ActiveSupport::TimeZone[read_attribute(:time_zone)]
+  end
+
   def to_param
     user.username
   end
app/models/user.rb
@@ -13,9 +13,10 @@ class User < ActiveRecord::Base
 
   after_create :create_profile
   before_validation :lowercase_account_fields
+  delegate :time_zone, to: :profile
 
-  def time_zone
-    TZInfo::Timezone.get('Canada/Mountain')
+  def first_training_session
+    training_sessions.order(occurred_at: :asc).first
   end
 
   def gravatar_id
app/views/profiles/edit.html.erb
@@ -4,19 +4,24 @@
     <h1><%= @current_user.username %></h1>
     <%= form_for(@profile) do |f| %>
       <fieldset>
-        <legend><%= t(".gender.title") %></legend>
+        <legend><%= Profile.human_attribute_name(:gender) %></legend>
         <% Profile.genders.keys.each do |gender| %>
           <%= f.radio_button(:gender, gender) %>
           <%= f.label("gender_#{gender}", Profile.human_attribute_name(gender)) %>
         <% end %>
       </fieldset>
       <fieldset>
-        <legend><%= t(".social_tolerance.title") %></legend>
+        <legend><%= Profile.human_attribute_name(:social_tolerance) %></legend>
         <% Profile.social_tolerances.keys.each do |social_tolerance| %>
           <%= f.radio_button(:social_tolerance, social_tolerance) %>
           <%= f.label("social_tolerance_#{social_tolerance}", Profile.human_attribute_name(social_tolerance)) %>
         <% end %>
       </fieldset>
+      <fieldset>
+        <legend><%= Profile.human_attribute_name(:time_zone) %></legend>
+          <%= f.select(:time_zone, ActiveSupport::TimeZone::MAPPING) %>
+          <%= f.label(:time_zone) %>
+      </fieldset>
       <%= f.submit t(".save"), class: "button"  %>
     <% end %>
   </div>
config/locales/en.yml
@@ -26,12 +26,14 @@ en:
     attributes:
       profile:
         female: Female
+        gender: Gender
         high: High
         low: Low
         male: Male
         medium: Medium
         other: Other
         transgender: Transgender
+        social_tolerance: Social Tolerance
   add: Add
   save: Save
   search: Search
@@ -64,10 +66,6 @@ en:
       success: "Thank you for registering."
   profiles:
     edit:
-      gender:
-        title: Gender
-      social_tolerance:
-        title: Social Tolerance
       save: Save Profile
       profile_update_success: "Profile updated. This is how your public profile appears."
     show:
db/migrate/20160512024722_add_time_zone_to_profiles.rb
@@ -0,0 +1,5 @@
+class AddTimeZoneToProfiles < ActiveRecord::Migration
+  def change
+    add_column :profiles, :time_zone, :string, default: "UTC", null: false
+  end
+end
db/schema.rb
@@ -11,7 +11,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20160430155822) do
+ActiveRecord::Schema.define(version: 20160512024722) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
@@ -64,11 +64,12 @@ ActiveRecord::Schema.define(version: 20160430155822) do
   add_index "locations", ["locatable_id", "locatable_type"], name: "index_locations_on_locatable_id_and_locatable_type", using: :btree
 
   create_table "profiles", id: :uuid, default: "uuid_generate_v4()", force: :cascade do |t|
-    t.uuid     "user_id",          null: false
+    t.uuid     "user_id",                          null: false
     t.integer  "gender"
     t.integer  "social_tolerance"
-    t.datetime "created_at",       null: false
-    t.datetime "updated_at",       null: false
+    t.datetime "created_at",                       null: false
+    t.datetime "updated_at",                       null: false
+    t.string   "time_zone",        default: "UTC", null: false
   end
 
   add_index "profiles", ["user_id"], name: "index_profiles_on_user_id", using: :btree