| @@ -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/ | |||||
| @@ -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/ | |||||
| @@ -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 | |||||
| @@ -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 | |||||
| @@ -0,0 +1,2 @@ | |||||
| module CartsHelper | |||||
| end | |||||
| @@ -0,0 +1,2 @@ | |||||
| class Cart < ApplicationRecord | |||||
| end | |||||
| @@ -0,0 +1,2 @@ | |||||
| json.extract! cart, :id, :created_at, :updated_at | |||||
| json.url cart_url(cart, format: :json) | |||||
| @@ -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 %> | |||||
| @@ -0,0 +1,6 @@ | |||||
| <h1>Editing Cart</h1> | |||||
| <%= render 'form', cart: @cart %> | |||||
| <%= link_to 'Show', @cart %> | | |||||
| <%= link_to 'Back', carts_path %> | |||||
| @@ -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 %> | |||||
| @@ -0,0 +1 @@ | |||||
| json.array! @carts, partial: 'carts/cart', as: :cart | |||||
| @@ -0,0 +1,5 @@ | |||||
| <h1>New Cart</h1> | |||||
| <%= render 'form', cart: @cart %> | |||||
| <%= link_to 'Back', carts_path %> | |||||
| @@ -0,0 +1,4 @@ | |||||
| <p id="notice"><%= notice %></p> | |||||
| <%= link_to 'Edit', edit_cart_path(@cart) %> | | |||||
| <%= link_to 'Back', carts_path %> | |||||
| @@ -0,0 +1 @@ | |||||
| json.partial! "carts/cart", cart: @cart | |||||
| @@ -1,4 +1,5 @@ | |||||
| Rails.application.routes.draw do | Rails.application.routes.draw do | ||||
| resources :carts | |||||
| root 'store#index', as: 'store_index' | root 'store#index', as: 'store_index' | ||||
| resources :products | resources :products | ||||
| @@ -0,0 +1,8 @@ | |||||
| class CreateCarts < ActiveRecord::Migration[5.0] | |||||
| def change | |||||
| create_table :carts do |t| | |||||
| t.timestamps | |||||
| end | |||||
| end | |||||
| end | |||||
| @@ -10,7 +10,12 @@ | |||||
| # | # | ||||
| # It's strongly recommended that you check this file into your version control system. | # 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| | create_table "products", force: :cascade do |t| | ||||
| t.string "title" | t.string "title" | ||||
| @@ -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 | |||||
| @@ -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 | |||||
| @@ -0,0 +1,7 @@ | |||||
| require 'test_helper' | |||||
| class CartTest < ActiveSupport::TestCase | |||||
| # test "the truth" do | |||||
| # assert true | |||||
| # end | |||||
| end | |||||