2012-02-01 04:09:00 +00:00
|
|
|
class HabitsController < ApplicationController
|
|
|
|
|
|
|
|
|
|
before_filter :authenticate_user!
|
|
|
|
|
|
|
|
|
|
# GET /habits
|
|
|
|
|
# GET /habits.json
|
|
|
|
|
def index
|
2012-02-01 14:50:26 +00:00
|
|
|
@habits = current_user.habits.where(:habit_type => Habit::ALWAYS)
|
2012-02-01 20:13:52 +00:00
|
|
|
@daily = current_user.habits.where(:habit_type => Habit::DAILY)
|
2012-02-04 21:32:12 +00:00
|
|
|
@one_time = current_user.habits.where(:habit_type => Habit::ONE_TIME).where(:done=>false)
|
2012-02-04 22:03:22 +00:00
|
|
|
@rewards = current_user.rewards
|
2012-02-01 04:09:00 +00:00
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html # index.html.erb
|
|
|
|
|
format.json { render json: @habits }
|
|
|
|
|
end
|
|
|
|
|
end
|
2012-02-04 21:32:12 +00:00
|
|
|
|
|
|
|
|
# GET /habits/completed
|
|
|
|
|
# GET /habits/completed.json
|
|
|
|
|
def completed
|
|
|
|
|
@one_time = current_user.habits.where(:habit_type => Habit::ONE_TIME).where(:done=>true)
|
|
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html # completed.html.erb
|
|
|
|
|
format.json { render json: @one_time }
|
|
|
|
|
end
|
|
|
|
|
end
|
2012-02-01 04:09:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# GET /habits/new
|
|
|
|
|
# GET /habits/new.json
|
|
|
|
|
def new
|
2012-02-01 16:16:12 +00:00
|
|
|
@habit = Habit.new
|
2012-02-01 16:37:29 +00:00
|
|
|
@habit.position = (Habit.maximum('position') || 0) + 1
|
2012-02-01 04:09:00 +00:00
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html # new.html.erb
|
|
|
|
|
format.json { render json: @habit }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# GET /habits/1/edit
|
|
|
|
|
def edit
|
2012-02-01 14:50:26 +00:00
|
|
|
@habit = current_user.habits.find(params[:id])
|
2012-02-01 04:09:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# POST /habits
|
|
|
|
|
# POST /habits.json
|
|
|
|
|
def create
|
|
|
|
|
@habit = Habit.new(params[:habit])
|
2012-02-01 14:50:26 +00:00
|
|
|
@habit.user_id = current_user.id
|
2012-02-01 04:09:00 +00:00
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
if @habit.save
|
2012-02-01 14:50:26 +00:00
|
|
|
format.html { redirect_to habits_url, notice: 'Habit was successfully created.' }
|
2012-02-01 04:09:00 +00:00
|
|
|
format.json { render json: @habit, status: :created, location: @habit }
|
|
|
|
|
else
|
|
|
|
|
format.html { render action: "new" }
|
|
|
|
|
format.json { render json: @habit.errors, status: :unprocessable_entity }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# PUT /habits/1
|
|
|
|
|
# PUT /habits/1.json
|
|
|
|
|
def update
|
2012-02-01 14:50:26 +00:00
|
|
|
@habit = current_user.habits.find(params[:id])
|
2012-02-01 04:09:00 +00:00
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
if @habit.update_attributes(params[:habit])
|
2012-02-01 19:41:22 +00:00
|
|
|
format.html { redirect_to habits_url, notice: 'Habit was successfully updated.' }
|
2012-02-01 04:09:00 +00:00
|
|
|
format.json { head :no_content }
|
|
|
|
|
else
|
|
|
|
|
format.html { render action: "edit" }
|
|
|
|
|
format.json { render json: @habit.errors, status: :unprocessable_entity }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# DELETE /habits/1
|
|
|
|
|
# DELETE /habits/1.json
|
|
|
|
|
def destroy
|
2012-02-01 14:50:26 +00:00
|
|
|
@habit = current_user.habits.find(params[:id])
|
2012-02-01 04:09:00 +00:00
|
|
|
@habit.destroy
|
|
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html { redirect_to habits_url }
|
|
|
|
|
format.json { head :no_content }
|
|
|
|
|
end
|
|
|
|
|
end
|
2012-02-01 05:39:04 +00:00
|
|
|
|
|
|
|
|
def vote
|
2012-02-01 14:50:26 +00:00
|
|
|
@habit = current_user.habits.find(params[:id])
|
2012-02-04 19:40:45 +00:00
|
|
|
|
|
|
|
|
# habit.vote() saves money to the user, this hack prevents having to
|
|
|
|
|
# reload current_user to catch that change
|
|
|
|
|
@habit.user=current_user
|
|
|
|
|
|
2012-02-01 15:03:27 +00:00
|
|
|
@habit.vote(params[:vote])
|
|
|
|
|
|
2012-02-01 05:39:04 +00:00
|
|
|
respond_to do |format|
|
2012-02-02 05:48:20 +00:00
|
|
|
# format.html { render action: "edit" }
|
|
|
|
|
# format.json { render json: @habit.errors, status: :unprocessable_entity }
|
|
|
|
|
format.js
|
2012-02-01 05:39:04 +00:00
|
|
|
end
|
|
|
|
|
end
|
2012-02-01 21:55:33 +00:00
|
|
|
|
|
|
|
|
def sort
|
|
|
|
|
current_user.habits.each do |habit|
|
|
|
|
|
logger.fatal params['habit'].index(habit.id.to_s)
|
|
|
|
|
habit.position = params['habit'].index(habit.id.to_s)
|
|
|
|
|
habit.save
|
|
|
|
|
end
|
|
|
|
|
render :nothing => true
|
|
|
|
|
end
|
2012-02-01 04:09:00 +00:00
|
|
|
end
|