Skip to content

Commit

Permalink
Move more logic to Order model
Browse files Browse the repository at this point in the history
Moving this logic to model gives us false feeling about
correct design, as we "encapsulate" the order logic.
  • Loading branch information
stolarczykt committed Sep 7, 2024
1 parent 60de783 commit 0845c39
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
22 changes: 6 additions & 16 deletions rails_application/app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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|
Expand Down Expand Up @@ -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!

Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions rails_application/app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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? &&
Expand Down

0 comments on commit 0845c39

Please sign in to comment.