Permalink
Browse files

Adds items

  • Loading branch information...
1 parent e91a4fa commit d7564a9c5b4690fe815268a322480a07ed4ae180 @aubreyrhodes committed Apr 23, 2012
@@ -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/
@@ -0,0 +1,3 @@
+// Place all the styles related to the Items controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -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; } }
@@ -0,0 +1,83 @@
+class ItemsController < ApplicationController
+ # GET /items
+ # GET /items.json
+ def index
+ @items = Item.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render json: @items }
+ end
+ end
+
+ # GET /items/1
+ # GET /items/1.json
+ def show
+ @item = Item.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render json: @item }
+ end
+ end
+
+ # GET /items/new
+ # GET /items/new.json
+ def new
+ @item = Item.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render json: @item }
+ end
+ end
+
+ # GET /items/1/edit
+ def edit
+ @item = Item.find(params[:id])
+ end
+
+ # POST /items
+ # POST /items.json
+ def create
+ @item = Item.new(params[:item])
+
+ respond_to do |format|
+ if @item.save
+ format.html { redirect_to @item, notice: 'Item was successfully created.' }
+ format.json { render json: @item, status: :created, location: @item }
+ else
+ format.html { render action: "new" }
+ format.json { render json: @item.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /items/1
+ # PUT /items/1.json
+ def update
+ @item = Item.find(params[:id])
+
+ respond_to do |format|
+ if @item.update_attributes(params[:item])
+ format.html { redirect_to @item, notice: 'Item was successfully updated.' }
+ format.json { head :no_content }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @item.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /items/1
+ # DELETE /items/1.json
+ def destroy
+ @item = Item.find(params[:id])
+ @item.destroy
+
+ respond_to do |format|
+ format.html { redirect_to items_url }
+ format.json { head :no_content }
+ end
+ end
+end
@@ -0,0 +1,2 @@
+module ItemsHelper
+end
View
@@ -0,0 +1,3 @@
+class Item < ActiveRecord::Base
+ attr_accessible :completed, :title
+end
@@ -0,0 +1,25 @@
+<%= form_for(@item) do |f| %>
+ <% if @item.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@item.errors.count, "error") %> prohibited this item from being saved:</h2>
+
+ <ul>
+ <% @item.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="field">
+ <%= f.label :title %><br />
+ <%= f.text_field :title %>
+ </div>
+ <div class="field">
+ <%= f.label :completed %><br />
+ <%= f.check_box :completed %>
+ </div>
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
@@ -0,0 +1,6 @@
+<h1>Editing item</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @item %> |
+<%= link_to 'Back', items_path %>
@@ -0,0 +1,25 @@
+<h1>Listing items</h1>
+
+<table>
+ <tr>
+ <th>Title</th>
+ <th>Completed</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @items.each do |item| %>
+ <tr>
+ <td><%= item.title %></td>
+ <td><%= item.completed %></td>
+ <td><%= link_to 'Show', item %></td>
+ <td><%= link_to 'Edit', edit_item_path(item) %></td>
+ <td><%= link_to 'Destroy', item, confirm: 'Are you sure?', method: :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Item', new_item_path %>
@@ -0,0 +1,5 @@
+<h1>New item</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', items_path %>
@@ -0,0 +1,15 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+ <b>Title:</b>
+ <%= @item.title %>
+</p>
+
+<p>
+ <b>Completed:</b>
+ <%= @item.completed %>
+</p>
+
+
+<%= link_to 'Edit', edit_item_path(@item) %> |
+<%= link_to 'Back', items_path %>
View
@@ -1,4 +1,6 @@
ToDone::Application.routes.draw do
+ resources :items
+
# The priority is based upon order of creation:
# first created -> highest priority.
@@ -0,0 +1,10 @@
+class CreateItems < ActiveRecord::Migration
+ def change
+ create_table :items do |t|
+ t.string :title
+ t.boolean :completed
+
+ t.timestamps
+ end
+ end
+end
View
@@ -0,0 +1,23 @@
+# encoding: UTF-8
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 20120423214033) do
+
+ create_table "items", :force => true do |t|
+ t.string "title"
+ t.boolean "completed"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+end
@@ -0,0 +1,9 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ title: MyString
+ completed: false
+
+two:
+ title: MyString
+ completed: false
@@ -0,0 +1,49 @@
+require 'test_helper'
+
+class ItemsControllerTest < ActionController::TestCase
+ setup do
+ @item = items(:one)
+ end
+
+ test "should get index" do
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:items)
+ end
+
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+ test "should create item" do
+ assert_difference('Item.count') do
+ post :create, item: { completed: @item.completed, title: @item.title }
+ end
+
+ assert_redirected_to item_path(assigns(:item))
+ end
+
+ test "should show item" do
+ get :show, id: @item
+ assert_response :success
+ end
+
+ test "should get edit" do
+ get :edit, id: @item
+ assert_response :success
+ end
+
+ test "should update item" do
+ put :update, id: @item, item: { completed: @item.completed, title: @item.title }
+ assert_redirected_to item_path(assigns(:item))
+ end
+
+ test "should destroy item" do
+ assert_difference('Item.count', -1) do
+ delete :destroy, id: @item
+ end
+
+ assert_redirected_to items_path
+ end
+end
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class ItemsHelperTest < ActionView::TestCase
+end
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class ItemTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end

0 comments on commit d7564a9

Please sign in to comment.