Commit 7a7d54d

mo khan <mo@mokhan.ca>
2015-02-28 17:06:24
scaffold up the items.
1 parent 9dad995
app/assets/stylesheets/items.scss
@@ -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/
app/controllers/items_controller.rb
@@ -0,0 +1,22 @@
+class ItemsController < ApplicationController
+  def index
+  end
+
+  def show
+  end
+
+  def new
+  end
+
+  def edit
+  end
+
+  def create
+  end
+
+  def update
+  end
+
+  def destroy
+  end
+end
app/models/item.rb
@@ -0,0 +1,3 @@
+class Item < ActiveRecord::Base
+  belongs_to :user
+end
app/views/items/_form.html.erb
@@ -0,0 +1,37 @@
+<%= 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 |message| %>
+        <li><%= message %></li>
+      <% end %>
+      </ul>
+    </div>
+  <% end %>
+
+  <div class="field">
+    <%= f.label :name %><br>
+    <%= f.text_field :name %>
+  </div>
+  <div class="field">
+    <%= f.label :description %><br>
+    <%= f.text_area :description %>
+  </div>
+  <div class="field">
+    <%= f.label :serial_number %><br>
+    <%= f.text_field :serial_number %>
+  </div>
+  <div class="field">
+    <%= f.label :purchase_price %><br>
+    <%= f.text_field :purchase_price %>
+  </div>
+  <div class="field">
+    <%= f.label :purchased_at %><br>
+    <%= f.datetime_select :purchased_at %>
+  </div>
+  <div class="actions">
+    <%= f.submit %>
+  </div>
+<% end %>
app/views/items/edit.html.erb
@@ -0,0 +1,6 @@
+<h1>Editing Item</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @item %> |
+<%= link_to 'Back', items_path %>
app/views/items/index.html.erb
@@ -0,0 +1,35 @@
+<p id="notice"><%= notice %></p>
+
+<h1>Listing Items</h1>
+
+<table>
+  <thead>
+    <tr>
+      <th>Name</th>
+      <th>Description</th>
+      <th>Serial number</th>
+      <th>Purchase price</th>
+      <th>Purchased at</th>
+      <th colspan="3"></th>
+    </tr>
+  </thead>
+
+  <tbody>
+    <% @items.each do |item| %>
+      <tr>
+        <td><%= item.name %></td>
+        <td><%= item.description %></td>
+        <td><%= item.serial_number %></td>
+        <td><%= item.purchase_price %></td>
+        <td><%= item.purchased_at %></td>
+        <td><%= link_to 'Show', item %></td>
+        <td><%= link_to 'Edit', edit_item_path(item) %></td>
+        <td><%= link_to 'Destroy', item, method: :delete, data: { confirm: 'Are you sure?' } %></td>
+      </tr>
+    <% end %>
+  </tbody>
+</table>
+
+<br>
+
+<%= link_to 'New Item', new_item_path %>
app/views/items/new.html.erb
@@ -0,0 +1,5 @@
+<h1>New Item</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', items_path %>
app/views/items/show.html.erb
@@ -0,0 +1,29 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+  <strong>Name:</strong>
+  <%= @item.name %>
+</p>
+
+<p>
+  <strong>Description:</strong>
+  <%= @item.description %>
+</p>
+
+<p>
+  <strong>Serial number:</strong>
+  <%= @item.serial_number %>
+</p>
+
+<p>
+  <strong>Purchase price:</strong>
+  <%= @item.purchase_price %>
+</p>
+
+<p>
+  <strong>Purchased at:</strong>
+  <%= @item.purchased_at %>
+</p>
+
+<%= link_to 'Edit', edit_item_path(@item) %> |
+<%= link_to 'Back', items_path %>
config/routes.rb
@@ -1,7 +1,9 @@
 Rails.application.routes.draw do
+
   root 'sessions#new'
   resources :sessions, only: [:new, :create, :destroy]
   resources :registrations, only: [:new, :create]
+  resources :items
   get '/' => 'sessions#new', as: :dashboard
   get "/terms" => "static_pages#terms"
 end
db/migrate/20150228162534_create_items.rb
@@ -0,0 +1,15 @@
+class CreateItems < ActiveRecord::Migration
+  def change
+    create_table :items, id: :uuid do |t|
+      t.uuid :user_id, null: false
+      t.string :name
+      t.text :description
+      t.string :serial_number
+      t.decimal :purchase_price
+      t.datetime :purchased_at
+
+      t.timestamps null: false
+    end
+    add_index :items, :user_id
+  end
+end
db/schema.rb
@@ -11,12 +11,25 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20150124163233) do
+ActiveRecord::Schema.define(version: 20150228162534) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
   enable_extension "uuid-ossp"
 
+  create_table "items", id: :uuid, default: "uuid_generate_v4()", force: :cascade do |t|
+    t.uuid     "user_id",        null: false
+    t.string   "name"
+    t.text     "description"
+    t.string   "serial_number"
+    t.decimal  "purchase_price"
+    t.datetime "purchased_at"
+    t.datetime "created_at",     null: false
+    t.datetime "updated_at",     null: false
+  end
+
+  add_index "items", ["user_id"], name: "index_items_on_user_id", using: :btree
+
   create_table "users", id: :uuid, default: "uuid_generate_v4()", force: :cascade do |t|
     t.string "username",        null: false
     t.string "email",           null: false
spec/controllers/items_controller_spec.rb
@@ -0,0 +1,5 @@
+require 'rails_helper'
+
+RSpec.describe ItemsController, type: :controller do
+
+end
spec/factories/items.rb
@@ -0,0 +1,9 @@
+FactoryGirl.define do
+  factory :item do
+    name Faker::Name.name
+    description Faker::HipsterIpsum.words(50)
+    serial_number SecureRandom.uuid
+    purchase_price { (rand(1_000) + rand()).round(2) }
+    purchased_at DateTime.now
+  end
+end
spec/models/item_spec.rb
@@ -0,0 +1,4 @@
+require 'rails_helper'
+
+RSpec.describe Item, type: :model do
+end
spec/requests/items_spec.rb
@@ -0,0 +1,10 @@
+require 'rails_helper'
+
+RSpec.describe "Items", :type => :request do
+  describe "GET /items" do
+    it "works! (now write some real specs)" do
+      get items_path
+      expect(response).to have_http_status(200)
+    end
+  end
+end
spec/routing/items_routing_spec.rb
@@ -0,0 +1,33 @@
+require "rails_helper"
+
+RSpec.describe ItemsController, type: :routing do
+  describe "routing" do
+    it "routes to #index" do
+      expect(:get => "/items").to route_to("items#index")
+    end
+
+    it "routes to #new" do
+      expect(:get => "/items/new").to route_to("items#new")
+    end
+
+    it "routes to #show" do
+      expect(:get => "/items/1").to route_to("items#show", :id => "1")
+    end
+
+    it "routes to #edit" do
+      expect(:get => "/items/1/edit").to route_to("items#edit", :id => "1")
+    end
+
+    it "routes to #create" do
+      expect(:post => "/items").to route_to("items#create")
+    end
+
+    it "routes to #update" do
+      expect(:put => "/items/1").to route_to("items#update", :id => "1")
+    end
+
+    it "routes to #destroy" do
+      expect(:delete => "/items/1").to route_to("items#destroy", :id => "1")
+    end
+  end
+end