From f9f4b22d7f163930ad915f71a5bd4ccb1f0b2e6b Mon Sep 17 00:00:00 2001 From: Nils Dittberner Date: Tue, 15 Nov 2016 22:10:37 +0100 Subject: [PATCH] Iteration D1. --- app/assets/javascripts/carts.coffee | 3 ++ app/assets/stylesheets/carts.scss | 3 ++ app/controllers/carts_controller.rb | 74 +++++++++++++++++++++++++++++++ app/controllers/concerns/current_cart.rb | 9 ++++ app/helpers/carts_helper.rb | 2 + app/models/cart.rb | 2 + app/views/carts/_cart.json.jbuilder | 2 + app/views/carts/_form.html.erb | 17 +++++++ app/views/carts/edit.html.erb | 6 +++ app/views/carts/index.html.erb | 25 +++++++++++ app/views/carts/index.json.jbuilder | 1 + app/views/carts/new.html.erb | 5 +++ app/views/carts/show.html.erb | 4 ++ app/views/carts/show.json.jbuilder | 1 + config/routes.rb | 1 + db/migrate/20161115210150_create_carts.rb | 8 ++++ db/schema.rb | 7 ++- test/controllers/carts_controller_test.rb | 48 ++++++++++++++++++++ test/fixtures/carts.yml | 11 +++++ test/models/cart_test.rb | 7 +++ 20 files changed, 235 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/carts.coffee create mode 100644 app/assets/stylesheets/carts.scss create mode 100644 app/controllers/carts_controller.rb create mode 100644 app/controllers/concerns/current_cart.rb create mode 100644 app/helpers/carts_helper.rb create mode 100644 app/models/cart.rb create mode 100644 app/views/carts/_cart.json.jbuilder create mode 100644 app/views/carts/_form.html.erb create mode 100644 app/views/carts/edit.html.erb create mode 100644 app/views/carts/index.html.erb create mode 100644 app/views/carts/index.json.jbuilder create mode 100644 app/views/carts/new.html.erb create mode 100644 app/views/carts/show.html.erb create mode 100644 app/views/carts/show.json.jbuilder create mode 100644 db/migrate/20161115210150_create_carts.rb create mode 100644 test/controllers/carts_controller_test.rb create mode 100644 test/fixtures/carts.yml create mode 100644 test/models/cart_test.rb diff --git a/app/assets/javascripts/carts.coffee b/app/assets/javascripts/carts.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/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/ diff --git a/app/assets/stylesheets/carts.scss b/app/assets/stylesheets/carts.scss new file mode 100644 index 0000000..2fe1b9e --- /dev/null +++ b/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/ diff --git a/app/controllers/carts_controller.rb b/app/controllers/carts_controller.rb new file mode 100644 index 0000000..2b7fa3b --- /dev/null +++ b/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 diff --git a/app/controllers/concerns/current_cart.rb b/app/controllers/concerns/current_cart.rb new file mode 100644 index 0000000..c492b4c --- /dev/null +++ b/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 diff --git a/app/helpers/carts_helper.rb b/app/helpers/carts_helper.rb new file mode 100644 index 0000000..d99c380 --- /dev/null +++ b/app/helpers/carts_helper.rb @@ -0,0 +1,2 @@ +module CartsHelper +end diff --git a/app/models/cart.rb b/app/models/cart.rb new file mode 100644 index 0000000..f3c5441 --- /dev/null +++ b/app/models/cart.rb @@ -0,0 +1,2 @@ +class Cart < ApplicationRecord +end diff --git a/app/views/carts/_cart.json.jbuilder b/app/views/carts/_cart.json.jbuilder new file mode 100644 index 0000000..5b78e6a --- /dev/null +++ b/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) \ No newline at end of file diff --git a/app/views/carts/_form.html.erb b/app/views/carts/_form.html.erb new file mode 100644 index 0000000..22652ab --- /dev/null +++ b/app/views/carts/_form.html.erb @@ -0,0 +1,17 @@ +<%= form_for(cart) do |f| %> + <% if cart.errors.any? %> +
+

<%= pluralize(cart.errors.count, "error") %> prohibited this cart from being saved:

+ + +
+ <% end %> + +
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/carts/edit.html.erb b/app/views/carts/edit.html.erb new file mode 100644 index 0000000..7b5b750 --- /dev/null +++ b/app/views/carts/edit.html.erb @@ -0,0 +1,6 @@ +

Editing Cart

+ +<%= render 'form', cart: @cart %> + +<%= link_to 'Show', @cart %> | +<%= link_to 'Back', carts_path %> diff --git a/app/views/carts/index.html.erb b/app/views/carts/index.html.erb new file mode 100644 index 0000000..0bda77d --- /dev/null +++ b/app/views/carts/index.html.erb @@ -0,0 +1,25 @@ +

<%= notice %>

+ +

Carts

+ + + + + + + + + + <% @carts.each do |cart| %> + + + + + + <% end %> + +
<%= link_to 'Show', cart %><%= link_to 'Edit', edit_cart_path(cart) %><%= link_to 'Destroy', cart, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Cart', new_cart_path %> diff --git a/app/views/carts/index.json.jbuilder b/app/views/carts/index.json.jbuilder new file mode 100644 index 0000000..da17637 --- /dev/null +++ b/app/views/carts/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @carts, partial: 'carts/cart', as: :cart \ No newline at end of file diff --git a/app/views/carts/new.html.erb b/app/views/carts/new.html.erb new file mode 100644 index 0000000..ca1aeb4 --- /dev/null +++ b/app/views/carts/new.html.erb @@ -0,0 +1,5 @@ +

New Cart

+ +<%= render 'form', cart: @cart %> + +<%= link_to 'Back', carts_path %> diff --git a/app/views/carts/show.html.erb b/app/views/carts/show.html.erb new file mode 100644 index 0000000..32f7a23 --- /dev/null +++ b/app/views/carts/show.html.erb @@ -0,0 +1,4 @@ +

<%= notice %>

+ +<%= link_to 'Edit', edit_cart_path(@cart) %> | +<%= link_to 'Back', carts_path %> diff --git a/app/views/carts/show.json.jbuilder b/app/views/carts/show.json.jbuilder new file mode 100644 index 0000000..a9d214c --- /dev/null +++ b/app/views/carts/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "carts/cart", cart: @cart \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 95be047..51eaebf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :carts root 'store#index', as: 'store_index' resources :products diff --git a/db/migrate/20161115210150_create_carts.rb b/db/migrate/20161115210150_create_carts.rb new file mode 100644 index 0000000..e029bfe --- /dev/null +++ b/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 diff --git a/db/schema.rb b/db/schema.rb index 366c59d..27e07e1 100644 --- a/db/schema.rb +++ b/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" diff --git a/test/controllers/carts_controller_test.rb b/test/controllers/carts_controller_test.rb new file mode 100644 index 0000000..35e350b --- /dev/null +++ b/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 diff --git a/test/fixtures/carts.yml b/test/fixtures/carts.yml new file mode 100644 index 0000000..80aed36 --- /dev/null +++ b/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 diff --git a/test/models/cart_test.rb b/test/models/cart_test.rb new file mode 100644 index 0000000..9fad43a --- /dev/null +++ b/test/models/cart_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class CartTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end