![Build Status](https://camo.githubusercontent.com/63edb07724adb4aeadb62a801d5834062fdb2efe8b731db3e22bf4fbd06d9bd5/68747470733a2f2f7472617669732d63692e6f72672f746f6e79746f6e796a616e2f696e7465726163746f72322e7376673f6272616e63683d6d6173746572)
Method |
Summary |
Interactor2.perform |
::new and then #perform , all arguments will be passed to #initialize . |
Interactor2#fail! |
Fail the interactor. |
Interactor2#perform |
Must be overridden in child class, and should not call it directly, use ::perform instead. |
Interactor2#error |
Returns the error message. |
Interactor2#success? |
Returns true if there is no error. |
require 'interactor2'
class AddToCart < Interactor2
attr_reader :line_item, :cart # should be any attribute you want to expose
def initialize(product, cart)
@cart = cart
@line_item = cart.line_items.new(product: product)
end
# business logic here.
def perform
unless @line_item.save
fail! 'oops'
end
end
end
add_to_cart = AddToCart.perform(@product)
if add_to_cart.success?
add_to_cart.item
add_to_cart.cart
else
add_to_cart.error # => 'oops'
end