jeudi 26 février 2015

How to Integrate :missed Days with :level Days?

This is a multi-faceted question that leads to the same problem:


How to integrate t.integer :missed with t.integer :level so that when a User checks off he missed a day he gets a strike and has to make up the day before moving onto the next level?


Each habit has 5 levels before hitting mastery!





class Habit < ActiveRecord::Base
belongs_to :user
before_save :set_level
acts_as_taggable
serialize :committed, Array

scope :missed, -> { where(missed: 1) }

def levels
committed_wdays = committed.map { |day| Date::DAYNAMES.index(day.titleize) }
n_days = ((date_started.to_date)..Date.today).count { |date| committed_wdays.include? date.wday }

case n_days
when 0..9
1
when 10..24
2
when 25..44
3
when 45..69
4
when 70..99
5
else
"Mastery"
end
end

protected
def set_level
self.level = levels
end
end



Here's a Question Breakdown:


If a User checks off a box how can we save it?


If a User checks off a box how can we add +1 day to the level?


If a User checks off a box how can we add an additional box using AJAX?


If a User checks off 3 boxes in a level (aka 3 strikes you're out) how can we restart him in that level? Ex, if he's on day 66 and checked off :missed three times in Level 4 as shown below how can we recalculate the day to be 45 again?


If a User succeeds to the next level (aka not getting 3 strikes) then how can we show the following level using AJAX?


Form Layout Example:



Missed:
Level 1: [ ]
Level 2: [√] [√] [ ]
Level 3: [√] [ ]
Level 4: [√] [√] [√] [ ]
*Level 5: [ ]
*Mastered!
*Not shown to User yet bc we'll assume he hasn't completed level 4.


Controller





class HabitsController < ApplicationController
before_action :set_habit, only: [:show, :edit, :update, :destroy]
before_action :logged_in_user, only: [:create, :destroy]

def index
if params[:tag]
@habits = Habit.tagged_with(params[:tag])
else
@habits = Habit.all.order("date_started DESC")
@habits = current_user.habits
end
end

def show
end

def new
@habit = current_user.habits.build
end

def edit
end

def create
@habit = current_user.habits.build(habit_params)
if @habit.save
redirect_to @habit, notice: 'Habit was successfully created.'
else
@feed_items = []
render 'pages/home'
end
end

def update
if @habit.update(habit_params)
redirect_to @habit, notice: 'Habit was successfully updated.'
else
render action: 'edit'
end
end

def destroy
@habit.destroy
redirect_to habits_url
end

private
def set_habit
@habit = Habit.find(params[:id])
end

def correct_user
@habit = current_user.habits.find_by(id: params[:id])
redirect_to habits_path, notice: "Not authorized to edit this habit" if @habit.nil?
end

def habit_params
params.require(:habit).permit(:missed, :left, :level, :date_started, :trigger, :target, :positive, :negative, :tag_list, :committed => [])
end
end



Github: http://ift.tt/1yCWXeU


I know this is a tall order so any help would be greatly appreciated!





Aucun commentaire:

Enregistrer un commentaire