diff --git a/app/controllers/habits_controller.rb b/app/controllers/habits_controller.rb index 2906c73101..00e18bff75 100644 --- a/app/controllers/habits_controller.rb +++ b/app/controllers/habits_controller.rb @@ -7,7 +7,7 @@ class HabitsController < ApplicationController def index @habits = current_user.habits.where(:habit_type => Habit::ALWAYS) @daily = current_user.habits.where(:habit_type => Habit::DAILY) - @score = current_user.habits.sum('score') + @score = current_user.habits.sum('score').to_i respond_to do |format| format.html # index.html.erb @@ -81,18 +81,12 @@ class HabitsController < ApplicationController def vote @habit = current_user.habits.find(params[:id]) @habit.vote(params[:vote]) - @score = current_user.habits.sum('score') + @score = current_user.habits.sum('score').to_i respond_to do |format| - if @habit.save - # format.html { redirect_to @habit, notice: 'Habit was successfully updated.' } - # format.json { head :no_content } - format.js - else - # format.html { render action: "edit" } - # format.json { render json: @habit.errors, status: :unprocessable_entity } - format.js - end + # format.html { render action: "edit" } + # format.json { render json: @habit.errors, status: :unprocessable_entity } + format.js end end diff --git a/app/models/habit.rb b/app/models/habit.rb index 6ced909a3e..2c2852f81f 100644 --- a/app/models/habit.rb +++ b/app/models/habit.rb @@ -17,12 +17,19 @@ class Habit < ActiveRecord::Base end def vote(direction) - next_vote = 1 #TODO return log or linear based on current score - next_vote *= -1 if(direction=='down') - self.score += next_vote + # For negative values, use a line: something like y=-.1x+1 + # For positibe values, taper off with inverse log: y=.9^x + # Would love to use inverse log for the whole thing, but after 13 fails it hits infinity + sign = ( direction=='up' ? 1 : -1 ) + if self.score < 0 + self.score += ( ( -0.1 * self.score + 1 ) * sign ) + else + self.score += ( ( 0.9 ** self.score ) * sign ) + end if(self.habit_type==Habit::DAILY) self.done = true if direction=='up' self.done = false if direction=='down' end + save end end diff --git a/app/views/habits/_habit.html.erb b/app/views/habits/_habit.html.erb index ca0725c343..4d3203957d 100644 --- a/app/views/habits/_habit.html.erb +++ b/app/views/habits/_habit.html.erb @@ -1,5 +1,4 @@
  • - <% s = habit.score @@ -13,19 +12,17 @@ name = "#{h(habit.name)}" %> - - <% if habit.habit_type==Habit::ALWAYS %> <%= link_to("√", { :action => "vote", :id => habit.id, :vote => 'up' }, :remote=>true) if habit.up %> <%= link_to("X", { :action => "vote", :id => habit.id, :vote => 'down' }, :remote=>true) if habit.down %> - <%= raw name %> (<%= habit.score %>) + <%= raw name %> (<%= habit.score.to_i %>) <% elsif habit.habit_type==Habit::DAILY %> <% if habit.done %> <%= link_to("[X]", { :action => "vote", :id => habit.id, :vote => 'down' }, :remote=>true) %> - <%= raw name %> (<%= habit.score %>) + <%= raw name %> (<%= habit.score.to_i %>) <% else %> <%= link_to("[ ]", { :action => "vote", :id => habit.id, :vote => 'up' }, :remote=>true) %> - <%= raw name %> (<%= habit.score %>) + <%= raw name %> (<%= habit.score.to_i %>) <% end %> <% end %> <%= link_to 'Edit', edit_habit_path(habit) %> diff --git a/db/migrate/20120202045135_change_habits_score_to_float.rb b/db/migrate/20120202045135_change_habits_score_to_float.rb new file mode 100644 index 0000000000..b51e648eda --- /dev/null +++ b/db/migrate/20120202045135_change_habits_score_to_float.rb @@ -0,0 +1,9 @@ +class ChangeHabitsScoreToFloat < ActiveRecord::Migration + def up + change_column :habits, :score, :float + end + + def down + change_column :habits, :score, :integer + end +end