Guest User

Untitled

a guest
Jan 18th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. # Naming. Should be named CartController (if US-based team)
  2. class BasketController
  3. # cart
  4. def add_to_basket
  5. # find_by_id's O(n) = log, find_by_title's O(n) = n
  6. # item should be an instance variable
  7. item = Item.find_by_title(params[:item_title])
  8.  
  9. # global variable instead of instance variable
  10. basket = $basket
  11.  
  12. # should be inverted to Item class => item.add_to_cart(params([:basket_id])
  13. basket.add_item(item)
  14.  
  15. # Logic should be in Cart model
  16. if basket.count_items > 10
  17. basket.error_messages.add("Слишком много товаров в корзине")
  18. end
  19.  
  20. # ExtractMethod to model.
  21. total = 0
  22.  
  23. # ExtractMethod to model.
  24. basket.items.each do |basket_item|
  25. total += basket_item.price
  26. end
  27.  
  28. # naming
  29. session[:basket_total] = total
  30.  
  31. # no need to use SQL when @cart = Cart.new can do the job
  32. query = "INSERT INTO basket_items (basket_id, item_title, basket_type) VALUES '" +
  33. (:basket=>basket_id.to_s) +
  34. "', '" + params[:item_title] + "', 3)"
  35. # no need to use SQL when @cart = Cart.new can do the job
  36. results = ActiveRecord::Base.connection.execute(query)
  37.  
  38. # naming + too much knowledge about delivery. This is logic of Order class.
  39. Basket.add_free_delivery(basket)
  40.  
  41. # returning html insted of JS-alert.
  42. return "<h1>Добавлен товар в корзину: #{@item.title}</h1>"
  43.  
  44. end
  45. end
Add Comment
Please, Sign In to add comment