瀏覽代碼

Iteration A1.

master
Nils Dittberner 9 年之前
父節點
當前提交
7825d7cccd
共有 20 個檔案被更改,包括 387 行新增0 行删除
  1. +3
    -0
      app/assets/javascripts/products.coffee
  2. +3
    -0
      app/assets/stylesheets/products.scss
  3. +89
    -0
      app/assets/stylesheets/scaffolds.scss
  4. +74
    -0
      app/controllers/products_controller.rb
  5. +2
    -0
      app/helpers/products_helper.rb
  6. +2
    -0
      app/models/product.rb
  7. +37
    -0
      app/views/products/_form.html.erb
  8. +2
    -0
      app/views/products/_product.json.jbuilder
  9. +6
    -0
      app/views/products/edit.html.erb
  10. +33
    -0
      app/views/products/index.html.erb
  11. +1
    -0
      app/views/products/index.json.jbuilder
  12. +5
    -0
      app/views/products/new.html.erb
  13. +24
    -0
      app/views/products/show.html.erb
  14. +1
    -0
      app/views/products/show.json.jbuilder
  15. +1
    -0
      config/routes.rb
  16. +12
    -0
      db/migrate/20161115133900_create_products.rb
  17. +24
    -0
      db/schema.rb
  18. +48
    -0
      test/controllers/products_controller_test.rb
  19. +13
    -0
      test/fixtures/products.yml
  20. +7
    -0
      test/models/product_test.rb

+ 3
- 0
app/assets/javascripts/products.coffee 查看文件

@@ -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/

+ 3
- 0
app/assets/stylesheets/products.scss 查看文件

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

+ 89
- 0
app/assets/stylesheets/scaffolds.scss 查看文件

@@ -0,0 +1,89 @@
body {
background-color: #fff;
color: #333;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
margin: 33px;
}

p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
margin: 33px;
}

pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}

a {
color: #000;

&:visited {
color: #666;
}

&:hover {
color: #fff;
background-color: #000;
}
}

th {
padding-bottom: 5px;
}

td {
padding-bottom: 7px;
padding-left: 5px;
padding-right: 5px;
}

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: 0;
background-color: #c00;
color: #fff;
}

ul li {
font-size: 12px;
list-style: square;
}
}

label {
display: block;
}

+ 74
- 0
app/controllers/products_controller.rb 查看文件

@@ -0,0 +1,74 @@
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]

# GET /products
# GET /products.json
def index
@products = Product.all
end

# GET /products/1
# GET /products/1.json
def show
end

# GET /products/new
def new
@product = Product.new
end

# GET /products/1/edit
def edit
end

# POST /products
# POST /products.json
def create
@product = Product.new(product_params)

respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: @product }
else
format.html { render :new }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: @product }
else
format.html { render :edit }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end

# DELETE /products/1
# DELETE /products/1.json
def destroy
@product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:title, :description, :image_url, :price)
end
end

+ 2
- 0
app/helpers/products_helper.rb 查看文件

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

+ 2
- 0
app/models/product.rb 查看文件

@@ -0,0 +1,2 @@
class Product < ApplicationRecord
end

+ 37
- 0
app/views/products/_form.html.erb 查看文件

@@ -0,0 +1,37 @@
<%= form_for(product) do |f| %>
<% if product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(product.errors.count, "error") %> prohibited this product from being saved:</h2>

<ul>
<% product.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :title %>
<%= f.text_field :title %>
</div>

<div class="field">
<%= f.label :description %>
<%= f.text_area :description, rows: 10, cols: 60 %>
</div>

<div class="field">
<%= f.label :image_url %>
<%= f.text_field :image_url %>
</div>

<div class="field">
<%= f.label :price %>
<%= f.text_field :price %>
</div>

<div class="actions">
<%= f.submit %>
</div>
<% end %>

+ 2
- 0
app/views/products/_product.json.jbuilder 查看文件

@@ -0,0 +1,2 @@
json.extract! product, :id, :title, :description, :image_url, :price, :created_at, :updated_at
json.url product_url(product, format: :json)

+ 6
- 0
app/views/products/edit.html.erb 查看文件

@@ -0,0 +1,6 @@
<h1>Editing Product</h1>

<%= render 'form', product: @product %>

<%= link_to 'Show', @product %> |
<%= link_to 'Back', products_path %>

+ 33
- 0
app/views/products/index.html.erb 查看文件

@@ -0,0 +1,33 @@
<p id="notice"><%= notice %></p>

<h1>Products</h1>

<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Image url</th>
<th>Price</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @products.each do |product| %>
<tr>
<td><%= product.title %></td>
<td><%= product.description %></td>
<td><%= product.image_url %></td>
<td><%= product.price %></td>
<td><%= link_to 'Show', product %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Product', new_product_path %>

+ 1
- 0
app/views/products/index.json.jbuilder 查看文件

@@ -0,0 +1 @@
json.array! @products, partial: 'products/product', as: :product

+ 5
- 0
app/views/products/new.html.erb 查看文件

@@ -0,0 +1,5 @@
<h1>New Product</h1>

<%= render 'form', product: @product %>

<%= link_to 'Back', products_path %>

+ 24
- 0
app/views/products/show.html.erb 查看文件

@@ -0,0 +1,24 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Title:</strong>
<%= @product.title %>
</p>

<p>
<strong>Description:</strong>
<%= @product.description %>
</p>

<p>
<strong>Image url:</strong>
<%= @product.image_url %>
</p>

<p>
<strong>Price:</strong>
<%= @product.price %>
</p>

<%= link_to 'Edit', edit_product_path(@product) %> |
<%= link_to 'Back', products_path %>

+ 1
- 0
app/views/products/show.json.jbuilder 查看文件

@@ -0,0 +1 @@
json.partial! "products/product", product: @product

+ 1
- 0
config/routes.rb 查看文件

@@ -1,3 +1,4 @@
Rails.application.routes.draw do
resources :products
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

+ 12
- 0
db/migrate/20161115133900_create_products.rb 查看文件

@@ -0,0 +1,12 @@
class CreateProducts < ActiveRecord::Migration[5.0]
def change
create_table :products do |t|
t.string :title
t.text :description
t.string :image_url
t.decimal :price, precision: 8, scale: 2

t.timestamps
end
end
end

+ 24
- 0
db/schema.rb 查看文件

@@ -0,0 +1,24 @@
# 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: 20161115133900) do

create_table "products", force: :cascade do |t|
t.string "title"
t.text "description"
t.string "image_url"
t.decimal "price", precision: 8, scale: 2
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end

+ 48
- 0
test/controllers/products_controller_test.rb 查看文件

@@ -0,0 +1,48 @@
require 'test_helper'

class ProductsControllerTest < ActionDispatch::IntegrationTest
setup do
@product = products(:one)
end

test "should get index" do
get products_url
assert_response :success
end

test "should get new" do
get new_product_url
assert_response :success
end

test "should create product" do
assert_difference('Product.count') do
post products_url, params: { product: { description: @product.description, image_url: @product.image_url, price: @product.price, title: @product.title } }
end

assert_redirected_to product_url(Product.last)
end

test "should show product" do
get product_url(@product)
assert_response :success
end

test "should get edit" do
get edit_product_url(@product)
assert_response :success
end

test "should update product" do
patch product_url(@product), params: { product: { description: @product.description, image_url: @product.image_url, price: @product.price, title: @product.title } }
assert_redirected_to product_url(@product)
end

test "should destroy product" do
assert_difference('Product.count', -1) do
delete product_url(@product)
end

assert_redirected_to products_url
end
end

+ 13
- 0
test/fixtures/products.yml 查看文件

@@ -0,0 +1,13 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
title: MyString
description: MyText
image_url: MyString
price: 9.99

two:
title: MyString
description: MyText
image_url: MyString
price: 9.99

+ 7
- 0
test/models/product_test.rb 查看文件

@@ -0,0 +1,7 @@
require 'test_helper'

class ProductTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

Loading…
取消
儲存