瀏覽代碼

Iteration D1.

master
Nils Dittberner 9 年之前
父節點
當前提交
f9f4b22d7f
共有 20 個文件被更改,包括 235 次插入1 次删除
  1. +3
    -0
      app/assets/javascripts/carts.coffee
  2. +3
    -0
      app/assets/stylesheets/carts.scss
  3. +74
    -0
      app/controllers/carts_controller.rb
  4. +9
    -0
      app/controllers/concerns/current_cart.rb
  5. +2
    -0
      app/helpers/carts_helper.rb
  6. +2
    -0
      app/models/cart.rb
  7. +2
    -0
      app/views/carts/_cart.json.jbuilder
  8. +17
    -0
      app/views/carts/_form.html.erb
  9. +6
    -0
      app/views/carts/edit.html.erb
  10. +25
    -0
      app/views/carts/index.html.erb
  11. +1
    -0
      app/views/carts/index.json.jbuilder
  12. +5
    -0
      app/views/carts/new.html.erb
  13. +4
    -0
      app/views/carts/show.html.erb
  14. +1
    -0
      app/views/carts/show.json.jbuilder
  15. +1
    -0
      config/routes.rb
  16. +8
    -0
      db/migrate/20161115210150_create_carts.rb
  17. +6
    -1
      db/schema.rb
  18. +48
    -0
      test/controllers/carts_controller_test.rb
  19. +11
    -0
      test/fixtures/carts.yml
  20. +7
    -0
      test/models/cart_test.rb

+ 3
- 0
app/assets/javascripts/carts.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/carts.scss 查看文件

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

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

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

# GET /carts
# GET /carts.json
def index
@carts = Cart.all
end

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

# GET /carts/new
def new
@cart = Cart.new
end

# GET /carts/1/edit
def edit
end

# POST /carts
# POST /carts.json
def create
@cart = Cart.new(cart_params)

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

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

# DELETE /carts/1
# DELETE /carts/1.json
def destroy
@cart.destroy
respond_to do |format|
format.html { redirect_to carts_url, notice: 'Cart was successfully destroyed.' }
format.json { head :no_content }
end
end

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

# Never trust parameters from the scary internet, only allow the white list through.
def cart_params
params.fetch(:cart, {})
end
end

+ 9
- 0
app/controllers/concerns/current_cart.rb 查看文件

@@ -0,0 +1,9 @@
module CurrentCard

private
def set_cart
@cart = Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
@cart = Cart.create
session[:cart_id] = @cart.id
end

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

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

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

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

+ 2
- 0
app/views/carts/_cart.json.jbuilder 查看文件

@@ -0,0 +1,2 @@
json.extract! cart, :id, :created_at, :updated_at
json.url cart_url(cart, format: :json)

+ 17
- 0
app/views/carts/_form.html.erb 查看文件

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

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

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

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

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

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

<%= link_to 'Show', @cart %> |
<%= link_to 'Back', carts_path %>

+ 25
- 0
app/views/carts/index.html.erb 查看文件

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

<h1>Carts</h1>

<table>
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @carts.each do |cart| %>
<tr>
<td><%= link_to 'Show', cart %></td>
<td><%= link_to 'Edit', edit_cart_path(cart) %></td>
<td><%= link_to 'Destroy', cart, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Cart', new_cart_path %>

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

@@ -0,0 +1 @@
json.array! @carts, partial: 'carts/cart', as: :cart

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

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

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

<%= link_to 'Back', carts_path %>

+ 4
- 0
app/views/carts/show.html.erb 查看文件

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

<%= link_to 'Edit', edit_cart_path(@cart) %> |
<%= link_to 'Back', carts_path %>

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

@@ -0,0 +1 @@
json.partial! "carts/cart", cart: @cart

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

@@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :carts
root 'store#index', as: 'store_index'

resources :products


+ 8
- 0
db/migrate/20161115210150_create_carts.rb 查看文件

@@ -0,0 +1,8 @@
class CreateCarts < ActiveRecord::Migration[5.0]
def change
create_table :carts do |t|

t.timestamps
end
end
end

+ 6
- 1
db/schema.rb 查看文件

@@ -10,7 +10,12 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20161115133900) do
ActiveRecord::Schema.define(version: 20161115210150) do

create_table "carts", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "products", force: :cascade do |t|
t.string "title"


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

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

class CartsControllerTest < ActionDispatch::IntegrationTest
setup do
@cart = carts(:one)
end

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

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

test "should create cart" do
assert_difference('Cart.count') do
post carts_url, params: { cart: { } }
end

assert_redirected_to cart_url(Cart.last)
end

test "should show cart" do
get cart_url(@cart)
assert_response :success
end

test "should get edit" do
get edit_cart_url(@cart)
assert_response :success
end

test "should update cart" do
patch cart_url(@cart), params: { cart: { } }
assert_redirected_to cart_url(@cart)
end

test "should destroy cart" do
assert_difference('Cart.count', -1) do
delete cart_url(@cart)
end

assert_redirected_to carts_url
end
end

+ 11
- 0
test/fixtures/carts.yml 查看文件

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

# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value

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

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

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

Loading…
取消
儲存