diff --git a/app/controllers/habits_controller.rb b/app/controllers/habits_controller.rb
index 217ab47c47..0378ceb7ee 100644
--- a/app/controllers/habits_controller.rb
+++ b/app/controllers/habits_controller.rb
@@ -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|
diff --git a/app/models/habit.rb b/app/models/habit.rb
index 905ba69e4d..4fbed23103 100644
--- a/app/models/habit.rb
+++ b/app/models/habit.rb
@@ -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
diff --git a/app/models/user.rb b/app/models/user.rb
index b2f7c8ec92..7d096199e5 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -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
diff --git a/app/views/habits/_form.html.erb b/app/views/habits/_form.html.erb
index 0570a4890d..a5b4a55b8b 100644
--- a/app/views/habits/_form.html.erb
+++ b/app/views/habits/_form.html.erb
@@ -15,18 +15,14 @@
<%= f.label :name %>
<%= f.text_field :name %>
+
<%= notice %>
- -- Name: - <%= @habit.name %> -
- -- Notes: - <%= @habit.notes %> -
- -- Daily: - <%= @habit.daily %> -
- -- Value: - <%= @habit.value %> -
- - -<%= link_to 'Edit', edit_habit_path(@habit) %> | -<%= link_to 'Back', habits_path %> diff --git a/db/migrate/20120201040610_create_habits.rb b/db/migrate/20120201040610_create_habits.rb index f275bf3fd5..e47ad81886 100644 --- a/db/migrate/20120201040610_create_habits.rb +++ b/db/migrate/20120201040610_create_habits.rb @@ -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