summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/controllers/profiles_controller.rb8
-rw-r--r--app/models/profile.rb1
-rw-r--r--app/models/user.rb1
-rw-r--r--config/routes.rb3
-rw-r--r--db/migrate/20150616021904_create_profiles.rb2
-rw-r--r--spec/controllers/profiles_controller_spec.rb4
-rw-r--r--spec/models/profile_spec.rb2
7 files changed, 12 insertions, 9 deletions
diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb
index 4a62f0d..132d860 100644
--- a/app/controllers/profiles_controller.rb
+++ b/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
diff --git a/app/models/profile.rb b/app/models/profile.rb
index 0299bc5..166ad49 100644
--- a/app/models/profile.rb
+++ b/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
diff --git a/app/models/user.rb b/app/models/user.rb
index 141effc..94df51a 100644
--- a/app/models/user.rb
+++ b/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
diff --git a/config/routes.rb b/config/routes.rb
index 0986da5..1b3de46 100644
--- a/config/routes.rb
+++ b/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
diff --git a/db/migrate/20150616021904_create_profiles.rb b/db/migrate/20150616021904_create_profiles.rb
index 5415b34..e40e24a 100644
--- a/db/migrate/20150616021904_create_profiles.rb
+++ b/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
diff --git a/spec/controllers/profiles_controller_spec.rb b/spec/controllers/profiles_controller_spec.rb
index 03a5fc3..1a981ce 100644
--- a/spec/controllers/profiles_controller_spec.rb
+++ b/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
diff --git a/spec/models/profile_spec.rb b/spec/models/profile_spec.rb
index 919deaf..196d013 100644
--- a/spec/models/profile_spec.rb
+++ b/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