← Back to Upcase

Interactor

(Charlieanna) #1

Has any one of you used https://github.com/collectiveidea/interactor-rails?

Basically I have these methods in the controller of mine which I am trying to remove.

 private

  def award_badge_to_user(steps_covered)
    notice = nil
    if current_user.daily_activities.where.not(steps_covered:nil).size == 1
      if steps_covered >= 7500 && steps_covered < 10000
        current_user.award_badge("Evolving")
        notice = "Thanks for entering your steps. Congrats you just won your first badge. Evolving badge."
      end
    end
    daily_activities = DailyActivity.where("steps_covered < ?",10000)
    if daily_activities.count == 0
      if steps_covered >= 10000 && steps_covered <15000
        current_user.award_badge("Debutant")
        notice =  "Thanks for entering your steps. Congrats you just won your first badge. Debutant badge."
      end
    end

    daily_activities = DailyActivity.where("steps_covered < ?",15000)
    if daily_activities.count == 0
      if steps_covered >= 15000
        current_user.award_badge("MasterBlaster")
        notice =  "Congrats you just won another badge. MasterBlaster badge."
      end
    end
    daily_activities = DailyActivity.where("steps_covered >= ? and start_time < ? and start_time > ?" ,10000, Time.now,Time.now - 4.days)
    if daily_activities.count == 3
      if steps_covered >= 10000
        current_user.award_badge("Hattrick")
        notice =  "Congrats you just won another badge. Hattrick badge."
      end
    end

    daily_activities = DailyActivity.where("steps_covered >= ? and start_time < ? and start_time > ?",10000, Time.now,Time.now - 8.days)
    if daily_activities.count == 7
      if steps_covered >= 10000 
        current_user.award_badge("Maiden")
       notice =  "Congrats you just won another badge. Maiden badge."
      end
    end 
    return notice
  end

  def award_points_to_user_if_activity_completed(user_activity)
    if user_activity.completed?
      if user_activity.total_steps_covered >= (user_activity.challenge.number_of_steps + 100000)
        points = award_points_to_user(user_activity)
        return "Congrats for finishing the challenge. You just got #{points} points."
      end
      if user_activity.total_steps_covered < (user_activity.challenge.number_of_steps + 100000)
        points = award_points_to_user(user_activity)
        return "Congrats for finishing the challenge. You just got #{points} points."
      end
    end
  end

  def award_points_to_user(user_activity)
    points = user_activity.total_steps_covered
    current_user.award_points(points)
    return points
  end
0 Likes

(Ben Orenstein) #2

Haven’t tried that gem, but at the very least I’d move those methods from the controller to a model.

Also, it looks like there’s a lot of duplication in award_badge_to_user that could be cleaned up if you extracted a few methods.

0 Likes