Skip to content

Instantly share code, notes, and snippets.

@bencrouse
Created January 16, 2012 17:38
Show Gist options
  • Save bencrouse/1621962 to your computer and use it in GitHub Desktop.
Save bencrouse/1621962 to your computer and use it in GitHub Desktop.
Command Sketch
class AddItemToCartCommand
include Command
validate :sku, presence: true
validate :quantity, presence: true, numericality: { greater_than_or_equal_to: 1 }
def cart
Cart.from_session(params[:session_id])
end
def run
cart.add_item(params[:sku], params[:quantity])
end
end
class CartsController < AppllicationController
def add_item
run_command :add_item_to_cart, params.merge(session_id: session_id) do
success do
flash[:success] = 'This item has been added to your cart.'
redirect_to cart_path
end
failure do |errors|
flash[:error] = errors
redirect_to product_path(params[:id))
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment