Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Naming. Should be named CartController (if US-based team)
- class BasketController
- # cart
- def add_to_basket
- # find_by_id's O(n) = log, find_by_title's O(n) = n
- # item should be an instance variable
- item = Item.find_by_title(params[:item_title])
- # global variable instead of instance variable
- basket = $basket
- # should be inverted to Item class => item.add_to_cart(params([:basket_id])
- basket.add_item(item)
- # Logic should be in Cart model
- if basket.count_items > 10
- basket.error_messages.add("Слишком много товаров в корзине")
- end
- # ExtractMethod to model.
- total = 0
- # ExtractMethod to model.
- basket.items.each do |basket_item|
- total += basket_item.price
- end
- # naming
- session[:basket_total] = total
- # no need to use SQL when @cart = Cart.new can do the job
- query = "INSERT INTO basket_items (basket_id, item_title, basket_type) VALUES '" +
- (:basket=>basket_id.to_s) +
- "', '" + params[:item_title] + "', 3)"
- # no need to use SQL when @cart = Cart.new can do the job
- results = ActiveRecord::Base.connection.execute(query)
- # naming + too much knowledge about delivery. This is logic of Order class.
- Basket.add_free_delivery(basket)
- # returning html insted of JS-alert.
- return "<h1>Добавлен товар в корзину: #{@item.title}</h1>"
- end
- end
Add Comment
Please, Sign In to add comment