-
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 #8 from KOH6/checkout
チェックアウト機能実装
- Loading branch information
Showing
43 changed files
with
577 additions
and
215 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin | ||
class ApplicationController < ApplicationController | ||
before_action :basic_auth | ||
|
||
private | ||
|
||
def basic_auth | ||
authenticate_or_request_with_http_basic do |username, password| | ||
username == ENV['BASIC_AUTH_USER'] && password == ENV['BASIC_AUTH_PASSWORD'] | ||
end | ||
end | ||
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,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin | ||
class OrdersController < Admin::ApplicationController | ||
def index | ||
@orders = Order.all.order(created_at: :desc) | ||
end | ||
|
||
def show | ||
@order = Order.find(params[:id]) | ||
@ordered_cart_products = @order.cart.cart_products.order(created_at: :desc) | ||
@total = @ordered_cart_products.inject(0) { |total, cart_product| total + cart_product.subtotal } | ||
end | ||
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
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,42 @@ | ||
# frozen_string_literal: true | ||
|
||
class OrdersController < ApplicationController | ||
before_action :set_cart | ||
before_action :set_cart_products, only: %i[index create] | ||
|
||
def index | ||
@order = Order.new | ||
end | ||
|
||
def create | ||
@order = Order.new(order_params) | ||
@order[:cart_id] = @cart.id | ||
|
||
error_messages = @cart.set_validate_error_messages | ||
if error_messages.size.positive? | ||
flash.now['danger'] = error_messages.join('<br/>') | ||
render :index | ||
return | ||
end | ||
|
||
if @order.save | ||
session[:cart_id] = nil | ||
flash[:success] = '購入ありがとうございます' | ||
redirect_to products_path | ||
else | ||
render :index | ||
end | ||
end | ||
|
||
private | ||
|
||
def order_params | ||
params.require(:order).permit(%i[last_name first_name user_name email country prefecture zip_code address1 address2 | ||
credit_name credit_number credit_expiration credit_cvv]) | ||
end | ||
|
||
def set_cart_products | ||
@cart_products = @cart.cart_products.order(created_at: :desc) | ||
@total = @cart_products.inject(0) { |total, cart_product| total + cart_product.subtotal } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationMailer < ActionMailer::Base | ||
default from: '[email protected]' | ||
default from: ENV['EMAIL'] | ||
layout 'mailer' | ||
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,13 @@ | ||
# frozen_string_literal: true | ||
|
||
class OrderMailer < ApplicationMailer | ||
helper ApplicationHelper | ||
|
||
def complete(order:) | ||
@order = order | ||
recipient = @order.email | ||
@ordered_cart_products = @order.cart.cart_products.order(created_at: :desc) | ||
@total = @ordered_cart_products.inject(0) { |total, cart_product| total + cart_product.subtotal } | ||
mail(to: recipient, subject: '注文内容ご確認') | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
class Order < ApplicationRecord | ||
belongs_to :cart | ||
|
||
with_options presence: true do | ||
validates :last_name | ||
validates :first_name | ||
validates :user_name | ||
validates :email | ||
validates :country | ||
validates :prefecture | ||
validates :zip_code | ||
validates :address1 | ||
validates :address2 | ||
validates :credit_name | ||
validates :credit_number | ||
validates :credit_expiration | ||
validates :credit_cvv | ||
end | ||
|
||
before_create do | ||
cart.decrease_product_stock | ||
OrderMailer.complete(order: @order).deliver_later | ||
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,40 @@ | ||
<!-- Navigation--> | ||
<%= render 'admin/shared/navigation' %> | ||
<!-- Header--> | ||
<header class="text-center py-5 my-5"> | ||
<h1 class="display-6 fw-bolder">購入明細一覧</h1> | ||
</header> | ||
<div class="container px-4 px-lg-5 mb-5"> | ||
<div class="table-responsive"> | ||
<table class="table table-striped "> | ||
<thead> | ||
<tr> | ||
<th scope="col">ID</th> | ||
<th scope="col">お名前</th> | ||
<th scope="col">メールアドレス</th> | ||
<th scope="col">お届け先</th> | ||
<th scope="col">購入日時</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<% @orders.each do |order| %> | ||
<tr> | ||
<th scope="row"><%= order.id %></th> | ||
<td> | ||
<%= link_to "#{order.last_name} #{order.first_name}様", admin_order_path(order), class: "link-dark" %> | ||
</td> | ||
<td><%= order.email %></td> | ||
<td><%= "#{order.address1} #{order.address2}" %></td> | ||
<td><%= l order.created_at %></td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
</div> | ||
</div> | ||
<!-- 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!-- Navigation--> | ||
<%= render 'admin/shared/navigation' %> | ||
<!-- Header--> | ||
<header class="text-center py-5 my-5"> | ||
<h1 class="display-6 fw-bolder">購入明細詳細</h1> | ||
</header> | ||
<div class="container px-4 px-lg-5 mb-5"> | ||
<div class="row gx-4 gx-lg-5 align-items-center"> | ||
<h1 class="display-5 fw-bolder"><%= "#{@order.last_name} #{@order.first_name}様" %></h1> | ||
<p class="lead">メールアドレス:<%= @order.email %></p> | ||
<p class="lead">お届け先:<%= "#{@order.address1} #{@order.address2}" %></p> | ||
<h3 class="fw-bolder">クレジットカード情報</h3> | ||
<p class="lead">名義:<%= @order.credit_name %></p> | ||
<p class="lead">有効期限:<%= @order.credit_expiration %></p> | ||
</div> | ||
<%= render 'shared/ordered_cart_products', :ordered_cart_products => @ordered_cart_products, :total => @total %> | ||
</div> | ||
<!-- 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
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
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
Oops, something went wrong.