Guest User

Untitled

a guest
May 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. # two ways to create bugs so far
  2. # one involves deleting a varation and then editing an activity that was formerly associated with it, the wrong variation list will autoload
  3. # the second involves doing a refresh during an edit in which you've changed the exercise, the wrong variations load
  4.  
  5. class ActivitiesController < ApplicationController
  6. resource_controller
  7. before_filter :login_required
  8.  
  9. index.wants.html do
  10. if params[:user_id]
  11. @activities = Activity.find(:all, :conditions => { :user_id => params[:user_id] }, :order => 'performed desc' )
  12. else
  13. render :action => 'home'
  14. end
  15. end
  16.  
  17. new_action.before do
  18. # allow user to insert record similar to a previously done exercise
  19. if params[:activity_id]
  20. @autoload = Activity.find params[:activity_id]
  21. @variation = @autoload.variation
  22. ['reps','rest','weight','tempo'].each do |moo|
  23. @activity.write_attribute(moo,@autoload.read_attribute(moo.to_sym))
  24. end
  25. else
  26. # find most commonly done variation
  27. greatest = [0,0]
  28. Activity.find(:all, :conditions => { :user_id => 1 }).group_by(&:variation_id).each { |vid,ex| greatest = [ex.length,vid] if ex.length }
  29. if greatest[1] > 0
  30. @variation = Variation.find greatest[1]
  31. @activity.weight = Activity.find(:first, :order => 'performed desc', :conditions => { :variation_id => @variation.id }).weight if @variation and @variation.exercise.name.match('pullup')
  32. end
  33. @variation ||= Variation.first
  34. end
  35.  
  36. @variations = @variation.exercise.variations_collection
  37. @exercise_selected = @variation.exercise.id
  38. @variations = @variation.exercise.variations_collection
  39. @variation_selected = @variation.id
  40.  
  41. # autoload the date from the last inserted record (if < 5 minutes ago)
  42. # for say when you're adding exercises done on a past day
  43. @previous = Activity.find :first, :order => 'created_at desc', :conditions => { :user_id => current_user.id }
  44. if @previous and @previous.performed != Date.today and Time.now < @previous.created_at + 300
  45. @activity.write_attribute('performed',@previous.performed)
  46. end
  47.  
  48. end
  49.  
  50. # it's possible for a variation associated with an activity to get deleted
  51. # rather than force an activity delete this handles it
  52. edit.before do
  53. if @activity.variation_id
  54. @variations = @activity.exercise.variations_collection
  55. @variation_selected = @activity.variation.id
  56. @exercise_selected = @activity.exercise.id
  57. else
  58. @variations = [['standard','']]
  59. end
  60. end
  61.  
  62. [create,update,destroy].each do |action|
  63. action.wants.html do
  64. redirect_to my_activities_path(current_user)
  65. end
  66. end
  67.  
  68. def find_variations
  69. exercise = Exercise.find(params[:exercise_id])
  70. render :update do |page|
  71. page.replace 'activity_variation_id', select(:activity, :variation_id, exercise.variations_collection, :selected => exercise.standard_variation)
  72. end
  73. end
  74.  
  75. private
  76. def collection
  77. @collection ||= Activity.by_date
  78. end
  79.  
  80. end
  81. ~
Add Comment
Please, Sign In to add comment