Commit 494106a

mo khan <mo@mokhan.ca>
2015-05-30 18:47:54
display personal records on the profile page.
1 parent 7ee9708
app/controllers/profiles_controller.rb
@@ -1,5 +1,6 @@
 class ProfilesController < PublicController
   def show
     @user = User.find_by(username: params[:id])
+    @program = Program.stronglifts
   end
 end
app/models/program.rb
@@ -7,6 +7,12 @@ class Program < ActiveRecord::Base
     slug
   end
 
+  class << self
+    def stronglifts
+      @stronglifts ||= Program.find_by(name: "StrongLifts 5×5")
+    end
+  end
+
   private
 
   def save_slug
app/models/training_session.rb
@@ -3,7 +3,7 @@ class TrainingSession < ActiveRecord::Base
   belongs_to :workout
   has_many :exercise_sessions
 
-  def self.create_workout_from(workout_row, program: Program.find_by(name: "StrongLifts 5×5"))
+  def self.create_workout_from(workout_row, program: Program.stronglifts)
     transaction do
       workout = program.workouts.find_by(name: workout_row.workout)
       matching_workouts = where(occurred_at: workout_row.date)
app/models/user.rb
@@ -1,6 +1,7 @@
 class User < ActiveRecord::Base
   has_secure_password
   has_many :training_sessions
+  has_many :exercise_sessions, through: :training_sessions
   USERNAME_REGEX=/\A[-a-z0-9_.]*\z/i
 
   validates :username, presence: true, format: { with: USERNAME_REGEX }, uniqueness: true
@@ -15,6 +16,10 @@ class User < ActiveRecord::Base
     username
   end
 
+  def personal_record(exercise)
+    exercise_sessions.joins(:exercise).where(exercises: { name: exercise.name }).pluck(:target_weight).max
+  end
+
   def self.authenticate(username,password)
     if user = User.where("email = :email OR username = :username", username: username, email: username).first
       user.authenticate(password)
app/views/profiles/show.html.erb
@@ -0,0 +1,19 @@
+<div class="small-12 columns">
+<div class="row">
+  <div class="small-3 columns">
+    &nbsp;
+  </div>
+  <div class="small-6 columns">
+    <%= gravatar_for(@user) %>
+    <h1><%= link_to @user.username, profile_path(@user) %></h1>
+    <p><%= t('.workouts_completed', count: @user.training_sessions.count) %></p>
+
+    <% @program.exercises.uniq.each do |exercise| %>
+      <p> <strong><%= exercise.name %></strong>: <%= @user.personal_record(exercise) %> lbs</p>
+    <% end %>
+  </div>
+  <div class="small-3 columns">
+    &nbsp;
+  </div>
+</div>
+</div>
config/locales/en.yml
@@ -31,6 +31,9 @@ en:
       login_link: "Already have an account?"
     create:
       success: "Thank you for registering."
+  profiles:
+    show:
+      workouts_completed: "%{count} workouts completed."
   sessions:
     create:
       invalid_login: "Sorry, we cannot find that account."
spec/controllers/profiles_controller_spec.rb
@@ -2,11 +2,14 @@ require "rails_helper"
 
 describe ProfilesController do
   describe "#show" do
+    include_context "stronglifts_program"
+
     let(:user) { create(:user) }
 
     it "loads the users profile" do
       get :show, id: user.username
       expect(assigns(:user)).to eql(user)
+      expect(assigns(:program)).to eql(Program.stronglifts)
     end
   end
 end
spec/models/program_spec.rb
@@ -5,4 +5,12 @@ describe Program do
     program = create(:program, name: 'Strong Lifts 5x5')
     expect(program.slug).to eql('strong-lifts-5x5')
   end
+
+  describe ".stronglifts" do
+    include_context "stronglifts_program"
+
+    it "returns the stronglifts program" do
+      expect(Program.stronglifts).to eql(program)
+    end
+  end
 end