-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from KOH6/cart
カート機能実装
- Loading branch information
Showing
29 changed files
with
472 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 © 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 © KOH's Shop 2023</p> | ||
</div> | ||
</footer> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 © 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 © KOH's Shop 2023</p> | ||
</div> | ||
</footer> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 © 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 © KOH's Shop 2023</p> | ||
</div> | ||
</footer> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 © 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 © KOH's Shop 2023</p> | ||
</div> | ||
</footer> |
Oops, something went wrong.