Created
January 16, 2012 17:38
-
-
Save bencrouse/1621962 to your computer and use it in GitHub Desktop.
Command Sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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