Refactoring vote to habit.rb

This commit is contained in:
Tyler Renelle 2012-02-01 10:03:27 -05:00
parent c056b31e13
commit 74a2e7eea5
3 changed files with 10 additions and 7 deletions

View file

@ -78,9 +78,8 @@ class HabitsController < ApplicationController
def vote
@habit = current_user.habits.find(params[:id])
@habit.score += params[:vote].to_i
@habit.votedate = Time.now
@habit.vote(params[:vote])
respond_to do |format|
if @habit.save
# format.html { redirect_to @habit, notice: 'Habit was successfully updated.' }

View file

@ -5,7 +5,11 @@ class Habit < ActiveRecord::Base
belongs_to :user
def next_vote
1 #TODO return log or linear based on current score
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
self.votedate = Time.now
#TODO if type=daily & votedate=today, set done back to false (have a done column?)
end
end

View file

@ -1,8 +1,8 @@
<div id="<%= habit.id %>">
<%= habit.name %>
(<%= habit.score %>)
<%= link_to "√", { :action => "vote", :id => habit.id, :vote => habit.next_vote }, :remote=>true %>
<%= link_to "X", { :action => "vote", :id => habit.id, :vote => -(habit.next_vote) }, :remote=>true %>
<%= link_to "√", { :action => "vote", :id => habit.id, :vote => 'up' }, :remote=>true %>
<%= link_to "X", { :action => "vote", :id => habit.id, :vote => 'down' }, :remote=>true %>
<!--<span class="popup"><%= habit.notes %></span>-->
<%= link_to 'Edit', edit_habit_path(habit) %>
</div>