Refactoring vote to habit.rb
This commit is contained in:
@@ -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.' }
|
||||
|
||||
+6
-2
@@ -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
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user