Skip to content

Commit

Permalink
Merge pull request #6 from KOH6/cart
Browse files Browse the repository at this point in the history
カート機能実装
  • Loading branch information
KOH6 authored Sep 9, 2023
2 parents 4cc81bf + 50765a9 commit fa67790
Show file tree
Hide file tree
Showing 29 changed files with 472 additions and 231 deletions.
11 changes: 11 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
# frozen_string_literal: true

class ApplicationController < ActionController::Base
before_action :set_session_cart_id, :set_cart_size

private

def set_session_cart_id
session[:cart_id] ||= Cart.create.id
end

def set_cart_size
@cart_size = Cart.find(session[:cart_id]).cart_products.size
end
end
35 changes: 35 additions & 0 deletions app/controllers/cart_products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

class CartProductsController < ApplicationController
before_action :set_cart_id

def index
@cart_products = Cart.find(@cart_id).cart_products.order(created_at: :desc)
@total = @cart_products.inject(0) { |total, cart_product| total + cart_product.subtotal }
end

def create
product_id = params[:product_id]
quantity = params[:quantity].to_i
cart_product = CartProduct.find_by(product_id:, cart_id: @cart_id)

if cart_product
cart_product.increment(:quantity, quantity)
cart_product.save
else
CartProduct.create(product_id:, cart_id: @cart_id, quantity:)
end
redirect_to request.referer
end

def destroy
CartProduct.find(params[:id]).destroy
redirect_to request.referer
end

private

def set_cart_id
@cart_id = session[:cart_id]
end
end
4 changes: 4 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ module ApplicationHelper
def hidden_field_tag(_name, _value = nil, _options = {})
raise 'Happiness chainではhidden_field_tagの使用を禁止しています'
end

def convert_to_jpy(price)
"#{price.to_fs(:delimited)}円"
end
end
7 changes: 0 additions & 7 deletions app/helpers/products_helper.rb

This file was deleted.

6 changes: 6 additions & 0 deletions app/models/cart.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class Cart < ApplicationRecord
has_many :cart_products, dependent: :destroy
has_many :products, through: :cart_products
end
11 changes: 11 additions & 0 deletions app/models/cart_product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class CartProduct < ApplicationRecord
belongs_to :cart
belongs_to :product
delegate :name, :price, to: :product, prefix: true

def subtotal
product.price * quantity
end
end
2 changes: 2 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class Product < ApplicationRecord
include Discard::Model

has_one_attached :image
has_many :cart_products, dependent: :destroy
has_many :carts, through: :cart_products

with_options presence: true do
validates :name
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/products/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<%= f.label :image, class: "mb-2 block text-sm text-gray-600" %>
<%= f.file_field :image, class: "form-control" %>
</div>
<div class="text-center">
<div class="text-center my-3">
<%= link_to "一覧へ戻る", admin_products_path, class: "btn btn-secondary mt-auto" %>
<button type="submit" class="btn btn-primary">保存</button>
</div>
Expand Down
4 changes: 2 additions & 2 deletions app/views/admin/products/_navigation.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<nav class="navbar navbar-expand-lg navbar-light bg-danger">
<nav class="navbar navbar-expand-lg navbar-danger bg-danger fixed-top">
<div class="container px-4 px-lg-5">
<%= link_to "管理者用設定", admin_products_path, class: "navbar-brand" %>
<%= link_to "管理者用設定", admin_products_path, class: "navbar-brand fw-bolder" %>
<div class="justify-content-beetween">
<%= link_to "購入明細一覧", admin_products_path, class: "btn btn-primary mt-auto" %>
<%= link_to "新規作成", new_admin_product_path, class: "btn btn-primary mt-auto" %>
Expand Down
File renamed without changes.
46 changes: 18 additions & 28 deletions app/views/admin/products/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<title>KOH's Shop</title>
</head>
<body>
<!-- Navigation-->
<%= render 'navigation' %>
<!-- ErrorMessages-->
<% if @updated_product %>
<%= render 'shared/error_messages', :resource => @updated_product %>
<% end %>
<!-- Header-->
<header class="py-4 text-center">
<h1 class="display-6 fw-bolder">商品編集</h1>
</header>
<!-- form-->
<%= render 'form', :product => @product, :url => admin_product_path(@product) %>
<!-- Footer-->
<footer class="py-5 bg-dark">
<div class="container">
<p class="m-0 text-center text-white">Copyright &copy; KOH's Shop 2023</p>
</div>
</footer>
</body>
</html>
<!-- Navigation-->
<%= render 'navigation' %>
<!-- ErrorMessages-->
<% if @updated_product %>
<%= render 'shared/error_messages', :resource => @updated_product %>
<% end %>
<!-- Header-->
<header class="text-center py-5 my-5">
<h1 class="display-6 fw-bolder">商品編集</h1>
</header>
<!-- form-->
<%= render 'form', :product => @product, :url => admin_product_path(@product) %>
<!-- Footer-->
<footer class="py-5 bg-dark">
<div class="container">
<p class="m-0 text-center text-white">Copyright &copy; KOH's Shop 2023</p>
</div>
</footer>
56 changes: 22 additions & 34 deletions app/views/admin/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<title>KOH's Shop</title>
</head>
<body>
<!-- Navigation-->
<%= render 'navigation' %>
<!-- Flash-->
<%= render 'shared/flash' %>
<!-- Header-->
<header class="py-4 text-center">
<h1 class="display-6 fw-bolder">商品一覧</h1>
</header>
<!-- Section-->
<section>
<div class="container px-4 px-lg-5 mt-5">
<div class="row gx-4 gx-lg-5 row-cols-2 row-cols-md-3 row-cols-xl-4 justify-content-center">
<% @products.each do |product| %>
<%= render 'card', :product => product %>
<% end %>
</div>
</div>
</section>
<!-- Footer-->
<footer class="py-5 bg-dark">
<div class="container">
<p class="m-0 text-center text-white">Copyright &copy; KOH's Shop 2023</p>
</div>
</footer>
</body>
</html>
<!-- Navigation-->
<%= render 'navigation' %>
<!-- Flash-->
<%= render 'shared/flash' %>
<!-- Header-->
<header class="text-center py-5 my-5">
<h1 class="display-6 fw-bolder">商品一覧</h1>
</header>
<!-- Section-->
<section>
<div class="container px-4 px-lg-5 mt-5">
<div class="row gx-4 gx-lg-5 row-cols-2 row-cols-md-3 row-cols-xl-4 justify-content-center">
<%= render @products %>
</div>
</div>
</section>
<!-- Footer-->
<footer class="py-5 bg-dark">
<div class="container">
<p class="m-0 text-center text-white">Copyright &copy; KOH's Shop 2023</p>
</div>
</footer>
42 changes: 16 additions & 26 deletions app/views/admin/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<title>KOH's Shop</title>
</head>
<body>
<!-- Navigation-->
<%= render 'navigation' %>
<!-- ErrorMessages-->
<%= render 'shared/error_messages', :resource => @product %>
<!-- Header-->
<header class="py-4 text-center">
<h1 class="display-6 fw-bolder">商品作成</h1>
</header>
<!-- form-->
<%= render 'form', :product => @product, :url => admin_products_path %>
<!-- Footer-->
<footer class="py-5 bg-dark">
<div class="container">
<p class="m-0 text-center text-white">Copyright &copy; KOH's Shop 2023</p>
</div>
</footer>
</body>
</html>
<!-- Navigation-->
<%= render 'navigation' %>
<!-- ErrorMessages-->
<%= render 'shared/error_messages', :resource => @product %>
<!-- Header-->
<header class="text-center py-5 my-5">
<h1 class="display-6 fw-bolder">商品作成</h1>
</header>
<!-- form-->
<%= render 'form', :product => @product, :url => admin_products_path %>
<!-- Footer-->
<footer class="py-5 bg-dark">
<div class="container">
<p class="m-0 text-center text-white">Copyright &copy; KOH's Shop 2023</p>
</div>
</footer>
72 changes: 31 additions & 41 deletions app/views/admin/products/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,43 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<title>KOH's Shop</title>
</head>
<body>
<!-- Navigation-->
<%= render 'navigation' %>
<!-- Display alert if product is discarded-->
<% if @product.discarded? %>
<p class="text-center alert alert-danger">削除済みの商品のため編集できません</p>
<% end %>
<!-- Product section-->
<section class="py-5">
<div class="container px-4 px-lg-5 my-5">
<div class="row gx-4 gx-lg-5 align-items-center">
<div class="col-md-6"><%= image_tag @product.image %></div>
<div class="col-md-6">
<div class="small mb-1"><%= @product.sku %></div>
<h1 class="display-5 fw-bolder"><%= @product.name %></h1>
<div class="fs-5 mb-5">
<span><%= convert_to_jpy(@product.price) %></span>
</div>
<p class="lead"><%= @product.description %></p>
<div class="fs-5">在庫数<%= @product.stock %></div>
<%= link_to "一覧へ戻る", admin_products_path, class: "btn btn-secondary mt-auto" %>
<% if @product.undiscarded? %>
<%= link_to "編集", edit_admin_product_path(@product), class: "btn btn-primary mt-auto" %>
<% end %>
</div>
<!-- Navigation-->
<%= render 'navigation' %>
<!-- Display alert if product is discarded-->
<% if @product.discarded? %>
<p class="text-center alert alert-danger">削除済みの商品のため編集できません</p>
<% end %>
<!-- Product section-->
<section class="py-5 my-5">
<div class="container px-4 px-lg-5 my-5">
<div class="row gx-4 gx-lg-5 align-items-center">
<div class="col-md-6"><%= image_tag @product.image, class: "img-fluid" %></div>
<div class="col-md-6">
<div class="small mb-1"><%= @product.sku %></div>
<h1 class="display-5 fw-bolder"><%= @product.name %></h1>
<div class="fs-5 mb-5">
<span><%= convert_to_jpy(@product.price) %></span>
</div>
<p class="lead"><%= @product.description %></p>
<div class="fs-5">在庫数<%= @product.stock %></div>
<%= link_to "一覧へ戻る", admin_products_path, class: "btn btn-secondary mt-auto" %>
<% if @product.undiscarded? %>
<%= link_to "編集", edit_admin_product_path(@product), class: "btn btn-primary mt-auto" %>
<% end %>
</div>
</section>
<!-- Footer-->
<footer class="py-5 bg-dark">
<div class="container">
<p class="m-0 text-center text-white">Copyright &copy; KOH's Shop 2023</p>
</div>
</footer>
</body>
</html>
</div>
</div>
</section>
<!-- Footer-->
<footer class="py-5 bg-dark">
<div class="container">
<p class="m-0 text-center text-white">Copyright &copy; KOH's Shop 2023</p>
</div>
</footer>
Loading

0 comments on commit fa67790

Please sign in to comment.