From 830de39c86c5db344d97b69f36cb4ad9c3e16e68 Mon Sep 17 00:00:00 2001 From: Zee Spencer <50284+zspencer@users.noreply.github.com> Date: Wed, 9 Nov 2022 18:50:21 -0800 Subject: [PATCH 1/2] Marketplace: Cart can Total Price Co-Authored-By: Ana Ulin --- app/furniture/marketplace/cart.rb | 8 +++++++- config/initializers/money.rb | 2 +- spec/furniture/marketplace/cart_spec.rb | 17 ++++++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/app/furniture/marketplace/cart.rb b/app/furniture/marketplace/cart.rb index f0303412b..3fd7e6b1c 100644 --- a/app/furniture/marketplace/cart.rb +++ b/app/furniture/marketplace/cart.rb @@ -6,11 +6,17 @@ class Cart < ApplicationRecord belongs_to :marketplace delegate :space, :room, to: :marketplace - has_many :cart_products + has_many :cart_products, dependent: :destroy has_many :products, through: :cart_products def self.model_name @_model_name ||= ActiveModel::Name.new(self, ::Marketplace) end + + def price_total + cart_products.sum do |cart_product| + cart_product.product.price * cart_product.quantity + end + end end end diff --git a/config/initializers/money.rb b/config/initializers/money.rb index 575df2cdb..c53457916 100644 --- a/config/initializers/money.rb +++ b/config/initializers/money.rb @@ -70,7 +70,7 @@ # # set to BigDecimal::ROUND_HALF_EVEN by default # - # config.rounding_mode = BigDecimal::ROUND_HALF_UP + config.rounding_mode = BigDecimal::ROUND_HALF_EVEN # Set default money format globally. # Default value is nil meaning "ignore this option". diff --git a/spec/furniture/marketplace/cart_spec.rb b/spec/furniture/marketplace/cart_spec.rb index faf99e70f..43d383cf4 100644 --- a/spec/furniture/marketplace/cart_spec.rb +++ b/spec/furniture/marketplace/cart_spec.rb @@ -1,9 +1,24 @@ require "rails_helper" RSpec.describe Marketplace::Cart, type: :model do - it { is_expected.to have_many(:cart_products) } + it { is_expected.to have_many(:cart_products).dependent(:destroy) } it { is_expected.to have_many(:products).through(:cart_products) } it { is_expected.to belong_to(:marketplace).class_name("Marketplace::Marketplace") } + + describe "#price_total" do + subject(:price_total) { cart.price_total } + + let(:cart) { create(:marketplace_cart) } + let(:product_a) { create(:marketplace_product, marketplace: cart.marketplace) } + let(:product_b) { create(:marketplace_product, marketplace: cart.marketplace) } + + before do + cart.cart_products.create!(product: product_a, quantity: 1) + cart.cart_products.create!(product: product_b, quantity: 2) + end + + it { is_expected.to eql(product_a.price + product_b.price + product_b.price) } + end end From d8edd6c370c75b6e919149c54ba01fd56bfd47f0 Mon Sep 17 00:00:00 2001 From: Zee Spencer <50284+zspencer@users.noreply.github.com> Date: Wed, 9 Nov 2022 19:08:24 -0800 Subject: [PATCH 2/2] Marketplace: Adding and Removing Products updates Cart's Total Co-authored-by: Ana Ulin --- .../marketplace/cart_products_controller.rb | 18 ++++++++++-- .../marketplace/carts/_cart.html.erb | 24 ++++++++++++++++ .../marketplace/carts/_footer.html.erb | 9 ++++++ .../marketplaces/_marketplace.html.erb | 28 +++---------------- 4 files changed, 52 insertions(+), 27 deletions(-) create mode 100644 app/furniture/marketplace/carts/_cart.html.erb create mode 100644 app/furniture/marketplace/carts/_footer.html.erb diff --git a/app/furniture/marketplace/cart_products_controller.rb b/app/furniture/marketplace/cart_products_controller.rb index d230e5145..3d6c96343 100644 --- a/app/furniture/marketplace/cart_products_controller.rb +++ b/app/furniture/marketplace/cart_products_controller.rb @@ -19,7 +19,11 @@ def create end format.turbo_stream do - render turbo_stream: turbo_stream.replace("cart-product-#{cart_product.product_id}", cart_product) + render turbo_stream: [ + turbo_stream.replace("cart-product-#{cart_product.product_id}", cart_product), + turbo_stream.replace("cart-footer-#{cart.id}", + partial: "marketplace/carts/footer", locals: {cart: cart}), + ] end end end @@ -41,7 +45,11 @@ def update redirect_to [marketplace.space, marketplace.room] end format.turbo_stream do - render turbo_stream: turbo_stream.replace("cart-product-#{cart_product.product_id}", cart_product) + render turbo_stream: [ + turbo_stream.replace("cart-product-#{cart_product.product_id}", cart_product), + turbo_stream.replace("cart-footer-#{cart.id}", + partial: "marketplace/carts/footer", locals: {cart: cart}), + ] end end end @@ -63,7 +71,11 @@ def destroy end format.turbo_stream do - render turbo_stream: turbo_stream.replace("cart-product-#{cart_product.product_id}", cart.cart_products.new(product: cart_product.product)) + render turbo_stream: [ + turbo_stream.replace("cart-product-#{cart_product.product_id}", cart.cart_products.new(product: cart_product.product)), + turbo_stream.replace("cart-footer-#{cart.id}", + partial: "marketplace/carts/footer", locals: {cart: cart}) + ] end end end diff --git a/app/furniture/marketplace/carts/_cart.html.erb b/app/furniture/marketplace/carts/_cart.html.erb new file mode 100644 index 000000000..7a216ae34 --- /dev/null +++ b/app/furniture/marketplace/carts/_cart.html.erb @@ -0,0 +1,24 @@ +
+ + + + + + + + + + + <%- cart.marketplace.products.each do |product| %> + <%= render cart.cart_products.find_or_initialize_by(product: product) %> + <%- end %> + + <%= render "marketplace/carts/footer", cart: cart %> +
+ <%= Marketplace::Product.human_attribute_name(:name) %> +  
+
diff --git a/app/furniture/marketplace/carts/_footer.html.erb b/app/furniture/marketplace/carts/_footer.html.erb new file mode 100644 index 000000000..cb3e09db3 --- /dev/null +++ b/app/furniture/marketplace/carts/_footer.html.erb @@ -0,0 +1,9 @@ + + + + Total: + + <%= humanized_money_with_symbol(cart.price_total) %> + + + - - - - - - - - - - - <%- cart = marketplace.carts.find_or_create_by(id: session[:current_cart] ||= SecureRandom.uuid) %> - <%- marketplace.products.each do |product| %> - <%= render cart.cart_products.find_or_initialize_by(product: product) %> - <%- end %> - -
- <%= Marketplace::Product.human_attribute_name(:name) %> -  
- +<%- cart = marketplace.carts.find_or_create_by(id: session[:current_cart] ||= SecureRandom.uuid) %> + +<%= render cart %> +
<%- if policy(marketplace.products.new).create? %> <%= link_to t('.manage_products'), marketplace.location(:products) %>