From 6f1154b12cc082d3022ccc751b1cd9208ad813ce Mon Sep 17 00:00:00 2001 From: xdite Date: Fri, 20 Jun 2014 00:34:57 +0800 Subject: [PATCH 01/11] add v2-#1 TODO --- TODO | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/TODO b/TODO index 02f35919..a366f82f 100644 --- a/TODO +++ b/TODO @@ -7,6 +7,11 @@ ✔ 身為消費者,當我下單時,我的信箱要能收到一張下單確認信 @done (14-06-13 17:44) ✔ 訂單狀態應分為未結帳、已結帳、出貨中、已出貨、辦理退貨 @done (14-06-13 17:44) ☐ 身為消費者,當我用信用卡結帳後,我的信箱要能收到一張訂購完成信 + ☐ 身為消費者,當我在結賬後,購物⾞應自動清空 + ☐ 身為消費者,我可以從購物車內刪掉某些物品 + ☐身為消費者,我可以從購物車內更新購買數量 + ☐ 購物車的總價 => 小計 x 數量 + ☐ 數量為 0 的貨物不能「購買」 Admin 管理訂單: ☐ 身為管理者,可以在後台看訂單,訂單狀態分為未結帳、已結帳、出貨中、已出貨、辦理退貨 From 499a9b199db0a3d8e130f686fbb754f7cbc2026e Mon Sep 17 00:00:00 2001 From: xdite Date: Fri, 20 Jun 2014 00:36:17 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E2=9C=94=20=E6=B8=85=E7=A9=BA=E8=B3=BC?= =?UTF-8?q?=E7=89=A9=E2=BE=9E=20@done=20(14-06-20=2000:36)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TODO | 1 + app/models/cart.rb | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/TODO b/TODO index a366f82f..4d761f4f 100644 --- a/TODO +++ b/TODO @@ -8,6 +8,7 @@ ✔ 訂單狀態應分為未結帳、已結帳、出貨中、已出貨、辦理退貨 @done (14-06-13 17:44) ☐ 身為消費者,當我用信用卡結帳後,我的信箱要能收到一張訂購完成信 ☐ 身為消費者,當我在結賬後,購物⾞應自動清空 + ✔ 清空購物⾞ @done (14-06-20 00:36) ☐ 身為消費者,我可以從購物車內刪掉某些物品 ☐身為消費者,我可以從購物車內更新購買數量 ☐ 購物車的總價 => 小計 x 數量 diff --git a/app/models/cart.rb b/app/models/cart.rb index 444036b9..572c0e4a 100644 --- a/app/models/cart.rb +++ b/app/models/cart.rb @@ -20,4 +20,8 @@ def add_product_to_cart(product) def total_price items.inject(0) {|sum, item| sum + item.price } end + + def clear! + cart_items.delete_all + end end From 9cf6e9701a9d555e3486a5e234f3d7eb5bce0a29 Mon Sep 17 00:00:00 2001 From: xdite Date: Fri, 20 Jun 2014 00:41:16 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=20=E2=9C=94=20=E8=BA=AB=E7=82=BA?= =?UTF-8?q?=E6=B6=88=E8=B2=BB=E8=80=85=EF=BC=8C=E6=88=91=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E5=BE=9E=E8=B3=BC=E7=89=A9=E8=BB=8A=E5=85=A7=E5=88=AA=E6=8E=89?= =?UTF-8?q?=E6=9F=90=E4=BA=9B=E7=89=A9=E5=93=81=20@done=20(14-06-20=2000:4?= =?UTF-8?q?1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TODO | 2 +- app/assets/javascripts/cart_items.js.coffee | 3 +++ app/assets/stylesheets/cart_items.css.scss | 3 +++ app/controllers/cart_items_controller.rb | 15 +++++++++++++++ app/helpers/cart_items_helper.rb | 2 ++ app/views/carts/index.html.erb | 14 ++++++++++---- config/routes.rb | 4 ++++ test/controllers/cart_items_controller_test.rb | 7 +++++++ test/helpers/cart_items_helper_test.rb | 4 ++++ 9 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 app/assets/javascripts/cart_items.js.coffee create mode 100644 app/assets/stylesheets/cart_items.css.scss create mode 100644 app/controllers/cart_items_controller.rb create mode 100644 app/helpers/cart_items_helper.rb create mode 100644 test/controllers/cart_items_controller_test.rb create mode 100644 test/helpers/cart_items_helper_test.rb diff --git a/TODO b/TODO index 4d761f4f..6cd02aed 100644 --- a/TODO +++ b/TODO @@ -9,7 +9,7 @@ ☐ 身為消費者,當我用信用卡結帳後,我的信箱要能收到一張訂購完成信 ☐ 身為消費者,當我在結賬後,購物⾞應自動清空 ✔ 清空購物⾞ @done (14-06-20 00:36) - ☐ 身為消費者,我可以從購物車內刪掉某些物品 + ✔ 身為消費者,我可以從購物車內刪掉某些物品 @done (14-06-20 00:41) ☐身為消費者,我可以從購物車內更新購買數量 ☐ 購物車的總價 => 小計 x 數量 ☐ 數量為 0 的貨物不能「購買」 diff --git a/app/assets/javascripts/cart_items.js.coffee b/app/assets/javascripts/cart_items.js.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/cart_items.js.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/cart_items.css.scss b/app/assets/stylesheets/cart_items.css.scss new file mode 100644 index 00000000..c8ba3800 --- /dev/null +++ b/app/assets/stylesheets/cart_items.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the cart_items 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/cart_items_controller.rb b/app/controllers/cart_items_controller.rb new file mode 100644 index 00000000..f1331791 --- /dev/null +++ b/app/controllers/cart_items_controller.rb @@ -0,0 +1,15 @@ +class CartItemsController < ApplicationController + before_action :authenticate_user! + + def destroy + @cart = current_cart + @item = @cart.cart_items.find(params[:id]) + + @item.destroy + + flash[:warning] = "成功清空物品" + redirect_to :back + + end + +end diff --git a/app/helpers/cart_items_helper.rb b/app/helpers/cart_items_helper.rb new file mode 100644 index 00000000..f30f6834 --- /dev/null +++ b/app/helpers/cart_items_helper.rb @@ -0,0 +1,2 @@ +module CartItemsHelper +end diff --git a/app/views/carts/index.html.erb b/app/views/carts/index.html.erb index 14c5a65f..3528c199 100644 --- a/app/views/carts/index.html.erb +++ b/app/views/carts/index.html.erb @@ -9,19 +9,25 @@ 商品資訊 單價 + - <% current_cart.items.each do |product| %> + <% current_cart.cart_items.each do |item| %> - <%= render_product_photo(product.default_photo) %> + <%= render_product_photo(item.product.default_photo) %> - <%= link_to(product.title, admin_product_path(product)) %> + <%= link_to(item.product.title, admin_product_path(item.product)) %> - <%= product.price %> + <%= item.product.price %> + + <%= link_to cart_item_path(current_cart, item), :method => :delete do %> + + <% end %> + <% end %> diff --git a/config/routes.rb b/config/routes.rb index 2b9eae51..359e41af 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,6 +16,9 @@ collection do post :checkout end + + resources :items, :controller => "cart_items" + end resources :orders do @@ -23,6 +26,7 @@ get :pay_with_credit_card end end + root :to => "products#index" diff --git a/test/controllers/cart_items_controller_test.rb b/test/controllers/cart_items_controller_test.rb new file mode 100644 index 00000000..76b2b022 --- /dev/null +++ b/test/controllers/cart_items_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class CartItemsControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/helpers/cart_items_helper_test.rb b/test/helpers/cart_items_helper_test.rb new file mode 100644 index 00000000..4cfc7e99 --- /dev/null +++ b/test/helpers/cart_items_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class CartItemsHelperTest < ActionView::TestCase +end From 1c8272a159cec41afcd650f757ebd96298dea4a0 Mon Sep 17 00:00:00 2001 From: xdite Date: Fri, 20 Jun 2014 00:44:22 +0800 Subject: [PATCH 04/11] add quantity to cart_items --- app/controllers/products_controller.rb | 2 +- app/models/cart.rb | 5 ++++- db/migrate/20140619164232_add_quantity_to_cart_item.rb | 5 +++++ db/schema.rb | 3 ++- 4 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20140619164232_add_quantity_to_cart_item.rb diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index 6e30196c..e46257e4 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -14,7 +14,7 @@ def add_to_cart @product = Product.find(params[:id]) if !current_cart.items.include?(@product) - current_cart.add_product_to_cart(@product) + current_cart.add_product_to_cart(@product, 1) flash[:notice] = "你已成功將 #{@product.title} 加入購物車" else flash[:warning] = "你的購物車內已有此物品" diff --git a/app/models/cart.rb b/app/models/cart.rb index 572c0e4a..e4c5ff1e 100644 --- a/app/models/cart.rb +++ b/app/models/cart.rb @@ -14,7 +14,10 @@ class Cart < ActiveRecord::Base def add_product_to_cart(product) - items << product + cart_item = cart_items.build + cart_item.product = product + cart_item.quantity = amount + cart_item.save end def total_price diff --git a/db/migrate/20140619164232_add_quantity_to_cart_item.rb b/db/migrate/20140619164232_add_quantity_to_cart_item.rb new file mode 100644 index 00000000..7d9acb60 --- /dev/null +++ b/db/migrate/20140619164232_add_quantity_to_cart_item.rb @@ -0,0 +1,5 @@ +class AddQuantityToCartItem < ActiveRecord::Migration + def change + add_column :cart_items, :quantity,:integer, :default => 1 + end +end diff --git a/db/schema.rb b/db/schema.rb index 4d7e540e..581c5d18 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,13 +11,14 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140613065223) do +ActiveRecord::Schema.define(version: 20140619164232) do create_table "cart_items", force: true do |t| t.integer "cart_id" t.integer "product_id" t.datetime "created_at" t.datetime "updated_at" + t.integer "quantity", default: 1 end create_table "carts", force: true do |t| From ca8ad312d6376f9ea0a979c6b0866167c7a91715 Mon Sep 17 00:00:00 2001 From: xdite Date: Fri, 20 Jun 2014 00:54:19 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=20=E2=9C=94=20=E8=BA=AB=E7=82=BA?= =?UTF-8?q?=E6=B6=88=E8=B2=BB=E8=80=85=EF=BC=8C=E6=88=91=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E5=BE=9E=E8=B3=BC=E7=89=A9=E8=BB=8A=E5=85=A7=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E8=B3=BC=E8=B2=B7=E6=95=B8=E9=87=8F=20@done=20(14-06-20=2000:5?= =?UTF-8?q?4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TODO | 2 +- app/controllers/cart_items_controller.rb | 17 +++++++++++++++++ app/models/cart.rb | 2 +- app/views/carts/index.html.erb | 19 ++++++++++++++++++- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/TODO b/TODO index 6cd02aed..3f2c9550 100644 --- a/TODO +++ b/TODO @@ -10,7 +10,7 @@ ☐ 身為消費者,當我在結賬後,購物⾞應自動清空 ✔ 清空購物⾞ @done (14-06-20 00:36) ✔ 身為消費者,我可以從購物車內刪掉某些物品 @done (14-06-20 00:41) - ☐身為消費者,我可以從購物車內更新購買數量 + ✔ 身為消費者,我可以從購物車內更新購買數量 @done (14-06-20 00:54) ☐ 購物車的總價 => 小計 x 數量 ☐ 數量為 0 的貨物不能「購買」 diff --git a/app/controllers/cart_items_controller.rb b/app/controllers/cart_items_controller.rb index f1331791..070864f4 100644 --- a/app/controllers/cart_items_controller.rb +++ b/app/controllers/cart_items_controller.rb @@ -1,6 +1,17 @@ class CartItemsController < ApplicationController before_action :authenticate_user! + + def update + @cart = current_cart + @item = @cart.cart_items.find(params[:id]) + + @item.update(item_params) + + redirect_to carts_path + end + + def destroy @cart = current_cart @item = @cart.cart_items.find(params[:id]) @@ -12,4 +23,10 @@ def destroy end + private + + def item_params + params.require(:cart_item).permit(:quantity) + end + end diff --git a/app/models/cart.rb b/app/models/cart.rb index e4c5ff1e..b9103545 100644 --- a/app/models/cart.rb +++ b/app/models/cart.rb @@ -13,7 +13,7 @@ class Cart < ActiveRecord::Base has_many :items, :through => :cart_items, :source => :product - def add_product_to_cart(product) + def add_product_to_cart(product, amount) cart_item = cart_items.build cart_item.product = product cart_item.quantity = amount diff --git a/app/views/carts/index.html.erb b/app/views/carts/index.html.erb index 3528c199..e6a6f74c 100644 --- a/app/views/carts/index.html.erb +++ b/app/views/carts/index.html.erb @@ -10,6 +10,7 @@ 單價 + @@ -23,8 +24,24 @@ <%= link_to(item.product.title, admin_product_path(item.product)) %> <%= item.product.price %> + + + + <%= form_for item, :url => cart_item_path(current_cart, item) do |f| %> + <%= f.select :quantity, [1,2,3,4,5] %> + + <%= button_tag(type: 'submit', class: "btn btn-default btn-xs") do %> + + <% end %> + + + <% end -%> + + + + - <%= link_to cart_item_path(current_cart, item), :method => :delete do %> + <%= link_to cart_item_path(current_cart, item), :method => :delete do %> <% end %> From 6cd3e92c1c1b9fb1eaf3e17600365fa01159d6aa Mon Sep 17 00:00:00 2001 From: xdite Date: Fri, 20 Jun 2014 00:57:06 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E6=95=B8=E9=87=8F=E7=82=BA=200=20?= =?UTF-8?q?=E7=9A=84=E8=B2=A8=E7=89=A9=E4=B8=8D=E8=83=BD=E3=80=8C=E8=B3=BC?= =?UTF-8?q?=E8=B2=B7=E3=80=8D=20@done=20(14-06-20=2000:56)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TODO | 4 ++-- app/controllers/products_controller.rb | 10 ++++++++-- app/models/cart.rb | 2 +- app/views/products/show.html.erb | 9 ++++++++- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/TODO b/TODO index 3f2c9550..1f507b67 100644 --- a/TODO +++ b/TODO @@ -11,8 +11,8 @@ ✔ 清空購物⾞ @done (14-06-20 00:36) ✔ 身為消費者,我可以從購物車內刪掉某些物品 @done (14-06-20 00:41) ✔ 身為消費者,我可以從購物車內更新購買數量 @done (14-06-20 00:54) - ☐ 購物車的總價 => 小計 x 數量 - ☐ 數量為 0 的貨物不能「購買」 + ✔ 購物車的總價 => 小計 x 數量 @done (14-06-20 00:54) + ✔ 數量為 0 的貨物不能「購買」 @done (14-06-20 00:56) Admin 管理訂單: ☐ 身為管理者,可以在後台看訂單,訂單狀態分為未結帳、已結帳、出貨中、已出貨、辦理退貨 diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index e46257e4..e2ed10ec 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -14,8 +14,14 @@ def add_to_cart @product = Product.find(params[:id]) if !current_cart.items.include?(@product) - current_cart.add_product_to_cart(@product, 1) - flash[:notice] = "你已成功將 #{@product.title} 加入購物車" + + if @product.quantity > 0 + current_cart.add_product_to_cart(@product, 1 ) + flash[:notice] = "你已成功將 #{@product.title} 加入購物車" + else + flash[:warning] = "此物品已停止銷售,你無法將它加入購物車" + end + else flash[:warning] = "你的購物車內已有此物品" end diff --git a/app/models/cart.rb b/app/models/cart.rb index b9103545..ebbdc623 100644 --- a/app/models/cart.rb +++ b/app/models/cart.rb @@ -21,7 +21,7 @@ def add_product_to_cart(product, amount) end def total_price - items.inject(0) {|sum, item| sum + item.price } + cart_items.inject(0) {|sum, item| sum + (item.product.price * item.quantity) } end def clear! diff --git a/app/views/products/show.html.erb b/app/views/products/show.html.erb index 736cd045..4942899b 100644 --- a/app/views/products/show.html.erb +++ b/app/views/products/show.html.erb @@ -17,7 +17,14 @@
$ <%= render_product_price(@product) %>
- <%= link_to("加入購物車", add_to_cart_product_path(@product) , :method => :post , :class => "btn btn-primary btn-lg btn-danger") %> + + <% if @product.quantity > 0 %> + <%= link_to("加入購物車", add_to_cart_product_path(@product) , :method => :post , :class => "btn btn-primary btn-lg btn-danger") %> + + <% else %> + 已銷售一空,無法購買 + <% end %> +
From 98b26a548f5ec001e93c6784bf6305c8b2d24bad Mon Sep 17 00:00:00 2001 From: xdite Date: Fri, 20 Jun 2014 01:01:49 +0800 Subject: [PATCH 07/11] =?UTF-8?q?=E2=9C=94=20=E8=BA=AB=E7=82=BA=E6=B6=88?= =?UTF-8?q?=E8=B2=BB=E8=80=85=EF=BC=8C=E7=95=B6=E6=88=91=E5=9C=A8=E7=B5=90?= =?UTF-8?q?=E8=B3=AC=E5=BE=8C=EF=BC=8C=E8=B3=BC=E7=89=A9=E2=BE=9E=E6=87=89?= =?UTF-8?q?=E8=87=AA=E5=8B=95=E6=B8=85=E7=A9=BA=20@done=20(14-06-20=2001:0?= =?UTF-8?q?1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TODO | 4 +++- app/controllers/orders_controller.rb | 2 +- app/models/order.rb | 8 ++++---- app/views/carts/checkout.html.erb | 8 +++++--- app/views/order_mailer/notify_order_placed.html.erb | 2 ++ app/views/orders/show.html.erb | 2 ++ 6 files changed, 17 insertions(+), 9 deletions(-) diff --git a/TODO b/TODO index 1f507b67..d7074e51 100644 --- a/TODO +++ b/TODO @@ -7,12 +7,14 @@ ✔ 身為消費者,當我下單時,我的信箱要能收到一張下單確認信 @done (14-06-13 17:44) ✔ 訂單狀態應分為未結帳、已結帳、出貨中、已出貨、辦理退貨 @done (14-06-13 17:44) ☐ 身為消費者,當我用信用卡結帳後,我的信箱要能收到一張訂購完成信 - ☐ 身為消費者,當我在結賬後,購物⾞應自動清空 + ✔ 身為消費者,當我在結賬後,購物⾞應自動清空 @done (14-06-20 01:01) ✔ 清空購物⾞ @done (14-06-20 00:36) ✔ 身為消費者,我可以從購物車內刪掉某些物品 @done (14-06-20 00:41) ✔ 身為消費者,我可以從購物車內更新購買數量 @done (14-06-20 00:54) ✔ 購物車的總價 => 小計 x 數量 @done (14-06-20 00:54) ✔ 數量為 0 的貨物不能「購買」 @done (14-06-20 00:56) + ✔ 訂單支援「多重購買數量」 @done (14-06-20 01:00) + Admin 管理訂單: ☐ 身為管理者,可以在後台看訂單,訂單狀態分為未結帳、已結帳、出貨中、已出貨、辦理退貨 diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index 32032fb4..c8434d34 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -8,7 +8,7 @@ def create if @order.save @order.build_item_cache_from_cart(current_cart) @order.calculate_total!(current_cart) - + current_cart.clear! OrderMailer.notify_order_placed(@order).deliver redirect_to order_path(@order.token) diff --git a/app/models/order.rb b/app/models/order.rb index 68cb5f06..f49db671 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -22,11 +22,11 @@ class Order < ActiveRecord::Base def build_item_cache_from_cart(cart) - cart.items.each do |cart_item| + cart.cart_items.each do |cart_item| item = items.build - item.product_name = cart_item.title - item.quantity = 1 - item.price = cart_item.price + item.product_name = cart_item.product.title + item.quantity = cart_item.quantity + item.price = cart_item.product.price item.save end end diff --git a/app/views/carts/checkout.html.erb b/app/views/carts/checkout.html.erb index a1637402..a6bfc7ac 100644 --- a/app/views/carts/checkout.html.erb +++ b/app/views/carts/checkout.html.erb @@ -8,16 +8,18 @@ 商品明細 單價 + 數量 - <% current_cart.items.each do |product| %> + <% current_cart.cart_items.each do |cart_item| %> - <%= link_to(product.title, admin_product_path(product)) %> + <%= link_to(cart_item.product.title, admin_product_path(cart_item.product)) %> - <%= product.price %> + <%= cart_item.product.price %> + <%= cart_item.quantity %> <% end %> diff --git a/app/views/order_mailer/notify_order_placed.html.erb b/app/views/order_mailer/notify_order_placed.html.erb index b84ea735..08a6e212 100644 --- a/app/views/order_mailer/notify_order_placed.html.erb +++ b/app/views/order_mailer/notify_order_placed.html.erb @@ -10,6 +10,7 @@ 商品明細 單價 + 數量 @@ -19,6 +20,7 @@ <%= order_item.product_name %> <%= order_item.price %> + <%= order_item.quantity %> <% end %> diff --git a/app/views/orders/show.html.erb b/app/views/orders/show.html.erb index 9da665ec..7c05f0ad 100644 --- a/app/views/orders/show.html.erb +++ b/app/views/orders/show.html.erb @@ -8,6 +8,7 @@ 商品明細 單價 + 數量 @@ -17,6 +18,7 @@ <%= order_item.product_name %> <%= order_item.price %> + <%= order_item.quantity %> <% end %> From 698f9ef2a35526f652030b94564aa85fa66cae55 Mon Sep 17 00:00:00 2001 From: xdite Date: Fri, 20 Jun 2014 01:08:42 +0800 Subject: [PATCH 08/11] =?UTF-8?q?=20=E5=BB=BA=E7=AB=8B=20account/orders=20?= =?UTF-8?q?=20=E5=8F=AF=E4=BB=A5=E7=9C=8B=E5=88=B0=E8=A9=B2=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E8=80=85=E9=81=8E=E5=8E=BB=E6=89=80=E6=9C=89=E8=A8=82?= =?UTF-8?q?=E5=96=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TODO | 3 ++- .../javascripts/account/orders.js.coffee | 3 +++ .../stylesheets/account/orders.css.scss | 3 +++ app/controllers/account/orders_controller.rb | 8 ++++++ app/helpers/account/orders_helper.rb | 2 ++ app/helpers/orders_helper.rb | 8 ++++++ app/models/order.rb | 1 + app/views/account/orders/index.html.erb | 27 +++++++++++++++++++ app/views/common/_navbar.html.erb | 1 + config/routes.rb | 4 +++ .../account/orders_controller_test.rb | 7 +++++ test/helpers/account/orders_helper_test.rb | 4 +++ 12 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/account/orders.js.coffee create mode 100644 app/assets/stylesheets/account/orders.css.scss create mode 100644 app/controllers/account/orders_controller.rb create mode 100644 app/helpers/account/orders_helper.rb create mode 100644 app/views/account/orders/index.html.erb create mode 100644 test/controllers/account/orders_controller_test.rb create mode 100644 test/helpers/account/orders_helper_test.rb diff --git a/TODO b/TODO index d7074e51..19ec183e 100644 --- a/TODO +++ b/TODO @@ -14,7 +14,8 @@ ✔ 購物車的總價 => 小計 x 數量 @done (14-06-20 00:54) ✔ 數量為 0 的貨物不能「購買」 @done (14-06-20 00:56) ✔ 訂單支援「多重購買數量」 @done (14-06-20 01:00) - + ☐ 建立 account/orders 可以看到該使用者過去所有訂單 + ☐ 結賬後跳轉到 account/orders#index Admin 管理訂單: ☐ 身為管理者,可以在後台看訂單,訂單狀態分為未結帳、已結帳、出貨中、已出貨、辦理退貨 diff --git a/app/assets/javascripts/account/orders.js.coffee b/app/assets/javascripts/account/orders.js.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/account/orders.js.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/account/orders.css.scss b/app/assets/stylesheets/account/orders.css.scss new file mode 100644 index 00000000..782d07c6 --- /dev/null +++ b/app/assets/stylesheets/account/orders.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the account::orders 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/account/orders_controller.rb b/app/controllers/account/orders_controller.rb new file mode 100644 index 00000000..1c02778b --- /dev/null +++ b/app/controllers/account/orders_controller.rb @@ -0,0 +1,8 @@ +class Account::OrdersController < ApplicationController + + before_action :authenticate_user! + + def index + @orders = current_user.orders.recent + end +end diff --git a/app/helpers/account/orders_helper.rb b/app/helpers/account/orders_helper.rb new file mode 100644 index 00000000..6f690c4e --- /dev/null +++ b/app/helpers/account/orders_helper.rb @@ -0,0 +1,2 @@ +module Account::OrdersHelper +end diff --git a/app/helpers/orders_helper.rb b/app/helpers/orders_helper.rb index 443227fd..8a76c079 100644 --- a/app/helpers/orders_helper.rb +++ b/app/helpers/orders_helper.rb @@ -1,2 +1,10 @@ module OrdersHelper + + def render_order_link(order) + link_to(order.token, order_path(order.token)) + end + + def render_order_created_time(order) + order.created_at.to_s(:short) + end end diff --git a/app/models/order.rb b/app/models/order.rb index f49db671..af666a67 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -20,6 +20,7 @@ class Order < ActiveRecord::Base accepts_nested_attributes_for :info + scope :recent, -> { order("id DESC")} def build_item_cache_from_cart(cart) cart.cart_items.each do |cart_item| diff --git a/app/views/account/orders/index.html.erb b/app/views/account/orders/index.html.erb new file mode 100644 index 00000000..938a3228 --- /dev/null +++ b/app/views/account/orders/index.html.erb @@ -0,0 +1,27 @@ + +

訂單列表

+ +
+ <%= link_to("新增產品", new_admin_product_path, :class => "btn btn-primary btn-sm") %> +
+ + + + + + + + + + + <% @orders.each do |order| %> + + + + + <% end %> + + +
#生成時間
<%= render_order_link(order) %> <%= render_order_created_time(order) %>
+ + diff --git a/app/views/common/_navbar.html.erb b/app/views/common/_navbar.html.erb index 4ecfa912..41356da3 100644 --- a/app/views/common/_navbar.html.erb +++ b/app/views/common/_navbar.html.erb @@ -37,6 +37,7 @@