2012-02-01 04:09:00 +00:00
|
|
|
class HabitsController < ApplicationController
|
|
|
|
|
|
|
|
|
|
before_filter :authenticate_user!
|
|
|
|
|
|
|
|
|
|
def index
|
2012-02-11 04:04:30 +00:00
|
|
|
@user = current_user
|
2012-02-01 04:09:00 +00:00
|
|
|
respond_to do |format|
|
|
|
|
|
format.html # index.html.erb
|
2012-02-11 04:04:30 +00:00
|
|
|
format.json { render json: @user }
|
2012-02-01 04:09:00 +00:00
|
|
|
end
|
|
|
|
|
end
|
2012-02-04 21:32:12 +00:00
|
|
|
|
2012-02-01 04:09:00 +00:00
|
|
|
def new
|
2012-02-11 07:26:09 +00:00
|
|
|
render :json => Habit.new
|
2012-02-01 04:09:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def edit
|
2012-02-11 07:26:09 +00:00
|
|
|
render :json => current_user.habits.find(params[:id])
|
2012-02-01 04:09:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create
|
|
|
|
|
@habit = Habit.new(params[:habit])
|
2012-02-11 07:26:09 +00:00
|
|
|
@habit.position ||= (Habit.maximum('position') || 0) + 1
|
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
|
|
|
|
|
format.json { render json: @habit, status: :created, location: @habit }
|
|
|
|
|
else
|
|
|
|
|
format.json { render json: @habit.errors, status: :unprocessable_entity }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update
|
2012-02-01 14:50:26 +00:00
|
|
|
@habit = current_user.habits.find(params[:id])
|
2012-02-11 07:31:33 +00:00
|
|
|
user_stats = params[:habit].delete('user_stats')
|
|
|
|
|
@habit.user.update_attributes({:lvl => user_stats['lvl'], :exp => user_stats['exp'], :money => user_stats['money']})
|
2012-02-01 04:09:00 +00:00
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
if @habit.update_attributes(params[:habit])
|
|
|
|
|
format.json { head :no_content }
|
|
|
|
|
else
|
|
|
|
|
format.json { render json: @habit.errors, status: :unprocessable_entity }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
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.json { head :no_content }
|
|
|
|
|
end
|
|
|
|
|
end
|
2012-02-01 05:39:04 +00:00
|
|
|
|
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
|