Added new purchase scaffold.

This commit is contained in:
James Skemp 2014-08-10 06:13:40 -05:00
parent 6407a18514
commit 2fc8218d29
20 changed files with 339 additions and 0 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://coffeescript.org/

View File

@ -0,0 +1,3 @@
// Place all the styles related to the purchases 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,69 @@
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,74 @@
class PurchasesController < ApplicationController
before_action :set_purchase, only: [:show, :edit, :update, :destroy]
# GET /purchases
# GET /purchases.json
def index
@purchases = Purchase.all
end
# GET /purchases/1
# GET /purchases/1.json
def show
end
# GET /purchases/new
def new
@purchase = Purchase.new
end
# GET /purchases/1/edit
def edit
end
# POST /purchases
# POST /purchases.json
def create
@purchase = Purchase.new(purchase_params)
respond_to do |format|
if @purchase.save
format.html { redirect_to @purchase, notice: 'Purchase was successfully created.' }
format.json { render :show, status: :created, location: @purchase }
else
format.html { render :new }
format.json { render json: @purchase.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /purchases/1
# PATCH/PUT /purchases/1.json
def update
respond_to do |format|
if @purchase.update(purchase_params)
format.html { redirect_to @purchase, notice: 'Purchase was successfully updated.' }
format.json { render :show, status: :ok, location: @purchase }
else
format.html { render :edit }
format.json { render json: @purchase.errors, status: :unprocessable_entity }
end
end
end
# DELETE /purchases/1
# DELETE /purchases/1.json
def destroy
@purchase.destroy
respond_to do |format|
format.html { redirect_to purchases_url, notice: 'Purchase was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_purchase
@purchase = Purchase.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def purchase_params
params.require(:purchase).permit(:name, :cost)
end
end

View File

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

2
app/models/purchase.rb Normal file
View File

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

View File

@ -0,0 +1,25 @@
<%= form_for(@purchase) do |f| %>
<% if @purchase.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@purchase.errors.count, "error") %> prohibited this purchase from being saved:</h2>
<ul>
<% @purchase.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 :cost %><br>
<%= f.text_field :cost %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

View File

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

View File

@ -0,0 +1,27 @@
<h1>Listing purchases</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Cost</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @purchases.each do |purchase| %>
<tr>
<td><%= purchase.name %></td>
<td><%= purchase.cost %></td>
<td><%= link_to 'Show', purchase %></td>
<td><%= link_to 'Edit', edit_purchase_path(purchase) %></td>
<td><%= link_to 'Destroy', purchase, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Purchase', new_purchase_path %>

View File

@ -0,0 +1,4 @@
json.array!(@purchases) do |purchase|
json.extract! purchase, :id, :name, :cost
json.url purchase_url(purchase, format: :json)
end

View File

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

View File

@ -0,0 +1,14 @@
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @purchase.name %>
</p>
<p>
<strong>Cost:</strong>
<%= @purchase.cost %>
</p>
<%= link_to 'Edit', edit_purchase_path(@purchase) %> |
<%= link_to 'Back', purchases_path %>

View File

@ -0,0 +1 @@
json.extract! @purchase, :id, :name, :cost, :created_at, :updated_at

View File

@ -1,4 +1,6 @@
Rails.application.routes.draw do
resources :purchases
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

View File

@ -0,0 +1,10 @@
class CreatePurchases < ActiveRecord::Migration
def change
create_table :purchases do |t|
t.string :name
t.float :cost
t.timestamps
end
end
end

23
db/schema.rb Normal file
View File

@ -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 that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140810105503) do
create_table "purchases", force: true do |t|
t.string "name"
t.float "cost"
t.datetime "created_at"
t.datetime "updated_at"
end
end

View File

@ -0,0 +1,49 @@
require 'test_helper'
class PurchasesControllerTest < ActionController::TestCase
setup do
@purchase = purchases(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:purchases)
end
test "should get new" do
get :new
assert_response :success
end
test "should create purchase" do
assert_difference('Purchase.count') do
post :create, purchase: { cost: @purchase.cost, name: @purchase.name }
end
assert_redirected_to purchase_path(assigns(:purchase))
end
test "should show purchase" do
get :show, id: @purchase
assert_response :success
end
test "should get edit" do
get :edit, id: @purchase
assert_response :success
end
test "should update purchase" do
patch :update, id: @purchase, purchase: { cost: @purchase.cost, name: @purchase.name }
assert_redirected_to purchase_path(assigns(:purchase))
end
test "should destroy purchase" do
assert_difference('Purchase.count', -1) do
delete :destroy, id: @purchase
end
assert_redirected_to purchases_path
end
end

9
test/fixtures/purchases.yml vendored Normal file
View File

@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
name: MyString
cost: 1.5
two:
name: MyString
cost: 1.5

View File

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

View File

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