From 0845c3935b1e4462d2e57c1cd1d4626c33ecc0c3 Mon Sep 17 00:00:00 2001 From: Tomasz Stolarczyk Date: Sat, 7 Sep 2024 16:01:24 +0200 Subject: [PATCH] Move more logic to Order model Moving this logic to model gives us false feeling about correct design, as we "encapsulate" the order logic. --- .../app/controllers/orders_controller.rb | 22 +++++-------------- rails_application/app/models/order.rb | 21 ++++++++++++++++++ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/rails_application/app/controllers/orders_controller.rb b/rails_application/app/controllers/orders_controller.rb index 11f197d8..8b44dfb5 100644 --- a/rails_application/app/controllers/orders_controller.rb +++ b/rails_application/app/controllers/orders_controller.rb @@ -8,7 +8,7 @@ def show return not_found unless @order - @total = @order.total - ((@order.total * @order.discount) / 100) + @total = @order.total_after_discount @order_lines = @order.order_items end @@ -30,7 +30,7 @@ def edit @products = Product.all @customers = Customer.all @time_promotions = TimePromotion.current - discounted_value = @order.total - ((@order.total * @order.discount) / 100) + discounted_value = @order.total_after_discount if @time_promotions.any? @time_promotions.sum(&:discount).tap do |discount| @@ -76,13 +76,7 @@ def add_item end @order = Order.find(params[:id]) - if @order.order_items.any? { |order_item| order_item.product_id == params[:product_id].to_i } - @order.order_items.find_by(product_id: params[:product_id]).increment!(:quantity) - @order.total = @order.total + product.price - else - @order.order_items.create!(product_id: params[:product_id], quantity: 1) - @order.total = @order.total + product.price - end + @order.add_item(product) product.decrement!(:stock_level) @order.save! @@ -92,13 +86,9 @@ def add_item def remove_item product = Product.find(params[:product_id]) @order = Order.find(params[:id]) - order_item = @order.order_items.find_by(product_id: params[:product_id]) - if order_item && order_item.quantity > 0 - @order.order_items.find_by(product_id: params[:product_id]).decrement!(:quantity) - product.increment!(:stock_level) - @order.total = @order.total - product.price - @order.save! - end + @order.remove_item(product) + product.increment!(:stock_level) + @order.save! redirect_to edit_order_path(params[:id]) end diff --git a/rails_application/app/models/order.rb b/rails_application/app/models/order.rb index b0e03a60..fdb93139 100644 --- a/rails_application/app/models/order.rb +++ b/rails_application/app/models/order.rb @@ -22,6 +22,27 @@ def shipment_full_address "#{address}, #{city}, #{country} #{addressed_to}" end + def total_after_discount + total - ((total * discount) / 100) + end + + def add_item(product) + if order_items.any? { |order_item| order_item.product_id == product.id } + order_items.find_by(product_id: product.id).increment!(:quantity) + else + order_items.create!(product_id: product.id, quantity: 1) + end + self.total += product.price + end + + def remove_item(product) + order_item = order_items.find_by(product_id: product.id) + if order_item && order_item.quantity > 0 + order_items.find_by(product_id: product.id).decrement!(:quantity) + self.total -= product.price + end + end + def billing_address_specified? invoice_tax_id_number.present? && invoice_country.present? &&