Habits belong to users, voting on habits, habit_type attribute

This commit is contained in:
Tyler Renelle 2012-02-01 09:50:26 -05:00
parent 495d492ba5
commit aa80f24ba1
7 changed files with 33 additions and 59 deletions

View file

@ -5,8 +5,8 @@ class HabitsController < ApplicationController
# GET /habits
# GET /habits.json
def index
@habits = Habit.where(:daily=>false)
@todos = Habit.where(:daily=>true)
@habits = current_user.habits.where(:habit_type => Habit::ALWAYS)
@todos = current_user.habits.where(:habit_type => Habit::DAILY)
respond_to do |format|
format.html # index.html.erb
@ -14,21 +14,11 @@ class HabitsController < ApplicationController
end
end
# GET /habits/1
# GET /habits/1.json
def show
@habit = Habit.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @habit }
end
end
# GET /habits/new
# GET /habits/new.json
def new
@habit = Habit.new
@habit = Habit.new(:user_id => current_user.id)
respond_to do |format|
format.html # new.html.erb
@ -38,17 +28,18 @@ class HabitsController < ApplicationController
# GET /habits/1/edit
def edit
@habit = Habit.find(params[:id])
@habit = current_user.habits.find(params[:id])
end
# POST /habits
# POST /habits.json
def create
@habit = Habit.new(params[:habit])
@habit.user_id = current_user.id
respond_to do |format|
if @habit.save
format.html { redirect_to @habit, notice: 'Habit was successfully created.' }
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" }
@ -60,7 +51,7 @@ class HabitsController < ApplicationController
# PUT /habits/1
# PUT /habits/1.json
def update
@habit = Habit.find(params[:id])
@habit = current_user.habits.find(params[:id])
respond_to do |format|
if @habit.update_attributes(params[:habit])
@ -76,7 +67,7 @@ class HabitsController < ApplicationController
# DELETE /habits/1
# DELETE /habits/1.json
def destroy
@habit = Habit.find(params[:id])
@habit = current_user.habits.find(params[:id])
@habit.destroy
respond_to do |format|
@ -86,7 +77,7 @@ class HabitsController < ApplicationController
end
def vote
@habit = Habit.find(params[:id])
@habit = current_user.habits.find(params[:id])
@habit.score += params[:vote].to_i
respond_to do |format|

View file

@ -1,2 +1,11 @@
class Habit < ActiveRecord::Base
ALWAYS = 1
DAILY = 2
ONE_TIME = 3
belongs_to :user
def next_vote
1 #TODO return log or linear based on current score
end
end

View file

@ -6,4 +6,6 @@ class User < ActiveRecord::Base
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :habits, :dependent => :destroy
end

View file

@ -15,18 +15,14 @@
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :type %><br />
<%= f.select :habit_type, options_for_select([['Always',1], ['Daily',2], ['One-time',3]]) %>
</div>
<div class="field">
<%= f.label :notes %><br />
<%= f.text_area :notes %>
</div>
<div class="field">
<%= f.label :daily %><br />
<%= f.check_box :daily %>
</div>
<div class="field">
<%= f.label :value %><br />
<%= f.number_field :value %>
</div>
<div class="actions">
<%= f.submit %>
</div>

View file

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

View file

@ -1,25 +0,0 @@
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= @habit.name %>
</p>
<p>
<b>Notes:</b>
<%= @habit.notes %>
</p>
<p>
<b>Daily:</b>
<%= @habit.daily %>
</p>
<p>
<b>Value:</b>
<%= @habit.value %>
</p>
<%= link_to 'Edit', edit_habit_path(@habit) %> |
<%= link_to 'Back', habits_path %>

View file

@ -1,13 +1,14 @@
class CreateHabits < ActiveRecord::Migration
def change
create_table :habits do |t|
t.string :name
t.text :notes
t.boolean :daily, :default => true
t.references :user
t.string :name
t.integer :habit_type, :default => 1
t.integer :score, :default => 0
t.boolean :up, :default => true
t.boolean :down, :default => true
t.integer :value, :default => 1
t.integer :score, :default => 0
t.text :notes
t.datetime :votedate
t.timestamps
end