From aa80f24ba14c4f3120d6ff3fa9685eab1affaa01 Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Wed, 1 Feb 2012 09:50:26 -0500 Subject: [PATCH] Habits belong to users, voting on habits, habit_type attribute --- app/controllers/habits_controller.rb | 27 ++++++++-------------- app/models/habit.rb | 9 ++++++++ app/models/user.rb | 2 ++ app/views/habits/_form.html.erb | 12 ++++------ app/views/habits/_habit.html.erb | 6 ++--- app/views/habits/show.html.erb | 25 -------------------- db/migrate/20120201040610_create_habits.rb | 11 +++++---- 7 files changed, 33 insertions(+), 59 deletions(-) delete mode 100644 app/views/habits/show.html.erb 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 %> +
+ <%= f.label :type %>
+ <%= f.select :habit_type, options_for_select([['Always',1], ['Daily',2], ['One-time',3]]) %> +
<%= f.label :notes %>
<%= f.text_area :notes %>
-
- <%= f.label :daily %>
- <%= f.check_box :daily %> -
-
- <%= f.label :value %>
- <%= f.number_field :value %> -
<%= f.submit %>
diff --git a/app/views/habits/_habit.html.erb b/app/views/habits/_habit.html.erb index 8bacc18d02..9a6282f4ef 100644 --- a/app/views/habits/_habit.html.erb +++ b/app/views/habits/_habit.html.erb @@ -1,8 +1,8 @@
<%= 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 %> - + <%= 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 'Edit', edit_habit_path(habit) %>
diff --git a/app/views/habits/show.html.erb b/app/views/habits/show.html.erb deleted file mode 100644 index 49552be51c..0000000000 --- a/app/views/habits/show.html.erb +++ /dev/null @@ -1,25 +0,0 @@ -

<%= 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