Adding habits

This commit is contained in:
Tyler Renelle 2012-01-31 23:09:00 -05:00
parent d970ed4135
commit db54c6b4b5
18 changed files with 342 additions and 2 deletions

View file

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

View file

@ -0,0 +1,3 @@
// Place all the styles related to the Habits controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View file

@ -0,0 +1,56 @@
body {
background-color: #fff;
color: #333;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px; }
p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px; }
pre {
background-color: #eee;
padding: 10px;
font-size: 11px; }
a {
color: #000;
&:visited {
color: #666; }
&:hover {
color: #fff;
background-color: #000; } }
div {
&.field, &.actions {
margin-bottom: 10px; } }
#notice {
color: green; }
.field_with_errors {
padding: 2px;
background-color: red;
display: table; }
#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px;
padding-bottom: 0;
margin-bottom: 20px;
background-color: #f0f0f0;
h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px;
margin-bottom: 0px;
background-color: #c00;
color: #fff; }
ul li {
font-size: 12px;
list-style: square; } }

View file

@ -0,0 +1,86 @@
class HabitsController < ApplicationController
before_filter :authenticate_user!
# GET /habits
# GET /habits.json
def index
@habits = Habit.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @habits }
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
respond_to do |format|
format.html # new.html.erb
format.json { render json: @habit }
end
end
# GET /habits/1/edit
def edit
@habit = Habit.find(params[:id])
end
# POST /habits
# POST /habits.json
def create
@habit = Habit.new(params[:habit])
respond_to do |format|
if @habit.save
format.html { redirect_to @habit, notice: 'Habit was successfully created.' }
format.json { render json: @habit, status: :created, location: @habit }
else
format.html { render action: "new" }
format.json { render json: @habit.errors, status: :unprocessable_entity }
end
end
end
# PUT /habits/1
# PUT /habits/1.json
def update
@habit = Habit.find(params[:id])
respond_to do |format|
if @habit.update_attributes(params[:habit])
format.html { redirect_to @habit, notice: 'Habit was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @habit.errors, status: :unprocessable_entity }
end
end
end
# DELETE /habits/1
# DELETE /habits/1.json
def destroy
@habit = Habit.find(params[:id])
@habit.destroy
respond_to do |format|
format.html { redirect_to habits_url }
format.json { head :no_content }
end
end
end

View file

@ -0,0 +1,2 @@
module HabitsHelper
end

2
app/models/habit.rb Normal file
View file

@ -0,0 +1,2 @@
class Habit < ActiveRecord::Base
end

View file

@ -0,0 +1,33 @@
<%= form_for(@habit) do |f| %>
<% if @habit.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@habit.errors.count, "error") %> prohibited this habit from being saved:</h2>
<ul>
<% @habit.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</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>
<% end %>

View file

@ -0,0 +1,6 @@
<h1>Editing habit</h1>
<%= render 'form' %>
<%= link_to 'Show', @habit %> |
<%= link_to 'Back', habits_path %>

View file

@ -0,0 +1,29 @@
<h1>Listing habits</h1>
<table>
<tr>
<th>Name</th>
<th>Notes</th>
<th>Daily</th>
<th>Value</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @habits.each do |habit| %>
<tr>
<td><%= habit.name %></td>
<td><%= habit.notes %></td>
<td><%= habit.daily %></td>
<td><%= habit.value %></td>
<td><%= link_to 'Show', habit %></td>
<td><%= link_to 'Edit', edit_habit_path(habit) %></td>
<td><%= link_to 'Destroy', habit, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Habit', new_habit_path %>

View file

@ -0,0 +1,5 @@
<h1>New habit</h1>
<%= render 'form' %>
<%= link_to 'Back', habits_path %>

View file

@ -0,0 +1,25 @@
<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

@ -7,7 +7,8 @@
<%= csrf_meta_tags %>
</head>
<body>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
</body>

View file

@ -1,4 +1,8 @@
HabitTracker::Application.routes.draw do
resources :habits
devise_for :users
# The priority is based upon order of creation:
# first created -> highest priority.
@ -48,7 +52,7 @@ HabitTracker::Application.routes.draw do
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
# root :to => 'welcome#index'
root :to => 'habits#index'
# See how all your routes lay out with "rake routes"

View file

@ -0,0 +1,12 @@
class CreateHabits < ActiveRecord::Migration
def change
create_table :habits do |t|
t.string :name
t.text :notes
t.boolean :daily
t.integer :value
t.timestamps
end
end
end

13
test/fixtures/habits.yml vendored Normal file
View file

@ -0,0 +1,13 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
one:
name: MyString
notes: MyText
daily: false
value: 1
two:
name: MyString
notes: MyText
daily: false
value: 1

View file

@ -0,0 +1,49 @@
require 'test_helper'
class HabitsControllerTest < ActionController::TestCase
setup do
@habit = habits(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:habits)
end
test "should get new" do
get :new
assert_response :success
end
test "should create habit" do
assert_difference('Habit.count') do
post :create, habit: @habit.attributes
end
assert_redirected_to habit_path(assigns(:habit))
end
test "should show habit" do
get :show, id: @habit
assert_response :success
end
test "should get edit" do
get :edit, id: @habit
assert_response :success
end
test "should update habit" do
put :update, id: @habit, habit: @habit.attributes
assert_redirected_to habit_path(assigns(:habit))
end
test "should destroy habit" do
assert_difference('Habit.count', -1) do
delete :destroy, id: @habit
end
assert_redirected_to habits_path
end
end

7
test/unit/habit_test.rb Normal file
View file

@ -0,0 +1,7 @@
require 'test_helper'
class HabitTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -0,0 +1,4 @@
require 'test_helper'
class HabitsHelperTest < ActionView::TestCase
end