Skip to content

Instantly share code, notes, and snippets.

@janjiss
Created March 24, 2011 17:15
Show Gist options
  • Save janjiss/885452 to your computer and use it in GitHub Desktop.
Save janjiss/885452 to your computer and use it in GitHub Desktop.
Can something be done better?
# AJAX request: This adds line items to cart.
def add_line_item_to_cart
#Get current_cart object
current_cart
quantity = params[:quantity].to_i
id = params[:id].to_i
line_item = LineItem.find(:first, :conditions => {:cartridge_id => id, :cart_id => current_cart.id})
#If there is line item wich maches conditions then just update quantity
#Returns quantity with total of items in cart
if line_item != nil
line_item.cart_id = current_cart.id
line_item.quantity = line_item.quantity+quantity
line_item.save
render :text => current_cart.line_items.count.to_s
#If there is no line item wich maches conditions create new LineItem object and add values to it
#Returns quantity with total of items in cart
else
line_item = LineItem.new
line_item.cartridge_id = id
line_item.cart_id = current_cart.id
line_item.quantity = quantity
line_item.save
render :text => current_cart.line_items.count.to_s
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment