Commit fe2d8ff

Stephen Peasley <s@stephenpeasley.com>
2015-08-08 16:53:09
Get profile saving, tests passing
1 parent 71f6435
app/controllers/profiles_controller.rb
@@ -6,13 +6,13 @@ class ProfilesController < ApplicationController
   end
   
   def edit
-    @user = User.find_by(username: params[:id]) if @current_user.username == params[:id]
+    @user = @current_user
     @program = Program.stronglifts
   end
   
   def update
-    @user = User.find_by(username: params[:id]) if @current_user.username == params[:id]
-    if @user.profile.update_attributes(profile_params)
+#    @user = User.find_by(username: params[:id]) if @current_user.username == params[:id]
+    if @current_user.profile.update_attributes(profile_params)
       flash[:notice] = "Updated profile. This is how your public profile appears."
       redirect_to "/u/#{params[:id]}"
     else
@@ -23,7 +23,7 @@ class ProfilesController < ApplicationController
   private
 
     def profile_params
-      params.require(:profile).permit(:gender, :social_tolerance)
+      params.require(:profile).permit([:gender, :social_tolerance])
     end
       
 end
app/models/profile.rb
@@ -1,4 +1,5 @@
 class Profile < ActiveRecord::Base
   belongs_to :user
   enum social_tolerance: { low: 0, medium: 1, high: 2 }
+  enum gender: { other: nil,  male: 1, female: 0, transgender: 2 }
 end
app/models/user.rb
@@ -3,6 +3,7 @@ class User < ActiveRecord::Base
   has_many :training_sessions
   has_many :exercise_sessions, through: :training_sessions
   has_one :profile
+  accepts_nested_attributes_for(:profile, update_only: true)
   USERNAME_REGEX=/\A[-a-z0-9_.]*\z/i
 
   validates :username, presence: true, format: { with: USERNAME_REGEX }, uniqueness: true
config/routes.rb
@@ -8,8 +8,9 @@ Rails.application.routes.draw do
     end
   end
   resources :programs, only: [:show]
-  resources :profiles, only: [:new, :create, :show, :edit, :patch], constraints: { id: /[^\/]+/ }
+  resources :profiles, only: [:new, :create, :show, :edit], constraints: { id: /[^\/]+/ }
   get "/u/:id" => "profiles#show", constraints: { id: /[^\/]+/ }
+  patch "/profiles/:id/edit" => "profiles#update", constraints: { id: /[^\/]+/ }
   get "/dashboard" => "training_sessions#index", as: :dashboard
   get "/terms" => "static_pages#terms"
 end
db/migrate/20150616021904_create_profiles.rb
@@ -2,7 +2,7 @@ class CreateProfiles < ActiveRecord::Migration
   def change
     create_table :profiles do |t|
       t.uuid :user_id, null: false
-      t.integer :gender, default: 0
+      t.integer :gender, default: nil
       t.integer :social_tolerance, default: 0
       t.timestamps null: false
     end
spec/controllers/profiles_controller_spec.rb
@@ -44,7 +44,7 @@ describe ProfilesController do
       
       it "will not load the other user's profile into an edit view" do
         get :edit, id: other_user.to_param
-        expect(assigns(:user)).to eql(nil)
+        expect(assigns(:user)).to eql(user)
         expect(assigns(:program)).to eql(Program.stronglifts)
       end
       
@@ -62,7 +62,7 @@ describe ProfilesController do
       it "updates the user profile" do
         patch :update, id: user.to_param, profile: {gender: "male"}
         user.reload
-        expect(user.profile.gender).to eql("male")
+        expect(user.profile.male?).to be_truthy
       end
       
     end
spec/models/profile_spec.rb
@@ -12,7 +12,7 @@ describe Profile do
   
   describe "gender" do
     it "defaults to unset" do
-      expect(user.profile.gender).to eql(nil)
+      expect(user.profile.other?).to be_truthy
     end
   end