diff --git a/app/assets/javascripts/backbone/models/habit.js.coffee b/app/assets/javascripts/backbone/models/habit.js.coffee
index 0ba6b46415..8607201e08 100644
--- a/app/assets/javascripts/backbone/models/habit.js.coffee
+++ b/app/assets/javascripts/backbone/models/habit.js.coffee
@@ -11,17 +11,23 @@ class HabitTracker.Models.Habit extends Backbone.Model
done: false
position: 0
- isHabit: ->
+ isHabit: =>
@get("habit_type")==1
- isDaily: ->
+ isDaily: =>
@get("habit_type")==2
+
+ isTodo: =>
+ @get('habit_type')==3
- isDoneTodo: ->
- @get("habit_type")==3 and @get("done")
+ isDoneTodo: =>
+ @isTodo() and @get("done")
- isRemainingTodo: ->
- @get("habit_type")==3 and !@get("done")
+ isRemainingTodo: =>
+ @isTodo() and !@get("done")
+
+ isReward: =>
+ @get('habit_type')==4
vote: (direction) ->
# For negative values, use a line: something like y=-.1x+1
@@ -34,18 +40,20 @@ class HabitTracker.Models.Habit extends Backbone.Model
delta = (( -0.1 * score + 1 ) * sign)
else
delta = (( Math.pow(0.9, score) ) * sign)
-
- score += delta
+
+ score += delta unless @isReward
# up/down -voting as checkbox & assigning as done, 2 birds one stone
done = @get("done")
if !@isHabit()
done = true if direction=="up"
done = false if direction=="down"
-
- @save({ score: score, done: done })
+ @set({ score: score, done: done })
window.userStats.updateStats(this, delta)
+ #send all the update information, as well as tack on userStats which will save to Users
+ @save({ user_stats: window.userStats })
+
class HabitTracker.Collections.HabitsCollection extends Backbone.Collection
model: HabitTracker.Models.Habit
url: '/habits'
diff --git a/app/assets/javascripts/backbone/models/user.js.coffee b/app/assets/javascripts/backbone/models/user.js.coffee
index aa5d4053ed..a8008b9052 100644
--- a/app/assets/javascripts/backbone/models/user.js.coffee
+++ b/app/assets/javascripts/backbone/models/user.js.coffee
@@ -15,8 +15,11 @@ class HabitTracker.Models.User extends Backbone.Model
@set({ lvl: @get('lvl') + 1 })
# also add money. Only take away money if it was a mistake (aka, a checkbox)
- if delta>0 or !habit.isHabit()
+ if delta>0 or (habit.isDaily() or habit.isTodo())
@set({money: @get('money')+delta})
+ # if buying an item, deduct cost
+ if habit.isReward()
+ @set({money: @get('money')-habit.get('score')})
@trigger('updatedStats')
diff --git a/app/assets/javascripts/backbone/templates/habits/reward.jst.ejs b/app/assets/javascripts/backbone/templates/habits/reward.jst.ejs
new file mode 100644
index 0000000000..dce520f6f8
--- /dev/null
+++ b/app/assets/javascripts/backbone/templates/habits/reward.jst.ejs
@@ -0,0 +1,7 @@
+
+
+<%= name %>
+($<%= score %>)
+
#{money[1]}
")
@@ -26,6 +27,9 @@ class HabitTracker.Views.Habits.IndexView extends Backbone.View
if habit.isDaily() then @$("#habits-daily").append(view.render().el)
if habit.isDoneTodo() then @$("#habits-todos-done").append(view.render().el)
if habit.isRemainingTodo() then @$("#habits-todos-remaining").append(view.render().el)
+ if habit.isReward()
+ view = new HabitTracker.Views.Habits.RewardView({model : habit})
+ @$("#rewards").append(view.render().el)
render: =>
$(@el).html(@template(habits: @options.habits.toJSON() ))
diff --git a/app/assets/javascripts/backbone/views/habits/reward_view.js.coffee b/app/assets/javascripts/backbone/views/habits/reward_view.js.coffee
new file mode 100644
index 0000000000..2726c5b042
--- /dev/null
+++ b/app/assets/javascripts/backbone/views/habits/reward_view.js.coffee
@@ -0,0 +1,32 @@
+HabitTracker.Views.Habits ||= {}
+
+class HabitTracker.Views.Habits.RewardView extends Backbone.View
+ template: JST["backbone/templates/habits/reward"]
+
+ events:
+ "click .destroy" : "destroy"
+ "click .buy-link" : "voteDown"
+
+ destroy: () ->
+ @model.destroy()
+ this.remove()
+ return false
+
+ voteDown: =>
+ if @model.get('score') > window.userStats.get('money')
+ $('#money').effect("pulsate", 100);
+ else
+ @model.vote("down")
+
+ tagName: "li"
+ className: "reward"
+
+ render: ->
+ $(@el).attr('id', "habit_#{@model.get('id')}")
+ $(@el).html(@template(@model.toJSON() ))
+
+ @$(".comment").qtip content:
+ text: (api) ->
+ $(this).next().html()
+
+ return this
diff --git a/app/controllers/habits_controller.rb b/app/controllers/habits_controller.rb
index ec95ac8ef2..a2f72e2f1c 100644
--- a/app/controllers/habits_controller.rb
+++ b/app/controllers/habits_controller.rb
@@ -2,7 +2,6 @@ class HabitsController < ApplicationController
before_filter :authenticate_user!
- # GET /habits.json
def index
@user = current_user
respond_to do |format|
@@ -11,84 +10,56 @@ class HabitsController < ApplicationController
end
end
- # GET /habits/new
- # GET /habits/new.json
def new
- @habit = Habit.new
- @habit.position = (Habit.maximum('position') || 0) + 1
-
- respond_to do |format|
- format.html # new.html.erb
- format.json { render json: @habit }
- end
+ render :json => Habit.new
end
- # GET /habits/1/edit
def edit
- @habit = current_user.habits.find(params[:id])
+ render :json => current_user.habits.find(params[:id])
end
- # POST /habits
- # POST /habits.json
def create
@habit = Habit.new(params[:habit])
+ @habit.position ||= (Habit.maximum('position') || 0) + 1
@habit.user_id = current_user.id
respond_to do |format|
if @habit.save
- format.html { redirect_to habits_url, notice: 'Habit was successfully created.' }
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
@habit = current_user.habits.find(params[:id])
+ test = params[:habit]
+ user_stats = params[:habit][:user_stats]
+ params[:habit].delete('user_stats')
+ @habit.user.lvl = user_stats['lvl']
+ @habit.user.exp = user_stats['exp']
+ @habit.user.money = user_stats['money']
+ @habit.user.save
respond_to do |format|
if @habit.update_attributes(params[:habit])
- format.html { redirect_to habits_url, notice: 'Habit was successfully updated.' }
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
@habit = current_user.habits.find(params[:id])
@habit.destroy
respond_to do |format|
- format.html { redirect_to habits_url }
format.json { head :no_content }
end
end
- def vote
- @habit = current_user.habits.find(params[:id])
-
- # habit.vote() saves money to the user, this hack prevents having to
- # reload current_user to catch that change
- @habit.user=current_user
-
- @habit.vote(params[:vote])
-
- respond_to do |format|
- # format.html { render action: "edit" }
- # format.json { render json: @habit.errors, status: :unprocessable_entity }
- format.js
- end
- end
-
def sort
current_user.habits.each do |habit|
logger.fatal params['habit'].index(habit.id.to_s)
diff --git a/app/models/habit.rb b/app/models/habit.rb
index 2a7a1a7320..4c171d3d19 100644
--- a/app/models/habit.rb
+++ b/app/models/habit.rb
@@ -2,6 +2,7 @@ class Habit < ActiveRecord::Base
ALWAYS = 1
DAILY = 2
ONE_TIME = 3
+ REWARD = 4
belongs_to :user
default_scope :order => 'position ASC'
@@ -45,4 +46,4 @@ class Habit < ActiveRecord::Base
save
end
-end
+end
\ No newline at end of file
diff --git a/db/migrate/20120211054432_merge_rewards_into_habits.rb b/db/migrate/20120211054432_merge_rewards_into_habits.rb
index f1a561cc44..86d6579a99 100644
--- a/db/migrate/20120211054432_merge_rewards_into_habits.rb
+++ b/db/migrate/20120211054432_merge_rewards_into_habits.rb
@@ -32,4 +32,4 @@ class MergeRewardsIntoHabits < ActiveRecord::Migration
end
end
-end
+end
\ No newline at end of file