Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # two ways to create bugs so far
- # one involves deleting a varation and then editing an activity that was formerly associated with it, the wrong variation list will autoload
- # the second involves doing a refresh during an edit in which you've changed the exercise, the wrong variations load
- class ActivitiesController < ApplicationController
- resource_controller
- before_filter :login_required
- index.wants.html do
- if params[:user_id]
- @activities = Activity.find(:all, :conditions => { :user_id => params[:user_id] }, :order => 'performed desc' )
- else
- render :action => 'home'
- end
- end
- new_action.before do
- # allow user to insert record similar to a previously done exercise
- if params[:activity_id]
- @autoload = Activity.find params[:activity_id]
- @variation = @autoload.variation
- ['reps','rest','weight','tempo'].each do |moo|
- @activity.write_attribute(moo,@autoload.read_attribute(moo.to_sym))
- end
- else
- # find most commonly done variation
- greatest = [0,0]
- Activity.find(:all, :conditions => { :user_id => 1 }).group_by(&:variation_id).each { |vid,ex| greatest = [ex.length,vid] if ex.length }
- if greatest[1] > 0
- @variation = Variation.find greatest[1]
- @activity.weight = Activity.find(:first, :order => 'performed desc', :conditions => { :variation_id => @variation.id }).weight if @variation and @variation.exercise.name.match('pullup')
- end
- @variation ||= Variation.first
- end
- @variations = @variation.exercise.variations_collection
- @exercise_selected = @variation.exercise.id
- @variations = @variation.exercise.variations_collection
- @variation_selected = @variation.id
- # autoload the date from the last inserted record (if < 5 minutes ago)
- # for say when you're adding exercises done on a past day
- @previous = Activity.find :first, :order => 'created_at desc', :conditions => { :user_id => current_user.id }
- if @previous and @previous.performed != Date.today and Time.now < @previous.created_at + 300
- @activity.write_attribute('performed',@previous.performed)
- end
- end
- # it's possible for a variation associated with an activity to get deleted
- # rather than force an activity delete this handles it
- edit.before do
- if @activity.variation_id
- @variations = @activity.exercise.variations_collection
- @variation_selected = @activity.variation.id
- @exercise_selected = @activity.exercise.id
- else
- @variations = [['standard','']]
- end
- end
- [create,update,destroy].each do |action|
- action.wants.html do
- redirect_to my_activities_path(current_user)
- end
- end
- def find_variations
- exercise = Exercise.find(params[:exercise_id])
- render :update do |page|
- page.replace 'activity_variation_id', select(:activity, :variation_id, exercise.variations_collection, :selected => exercise.standard_variation)
- end
- end
- private
- def collection
- @collection ||= Activity.by_date
- end
- end
- ~
Add Comment
Please, Sign In to add comment