Skip to content

Instantly share code, notes, and snippets.

@laserlemon
Created April 21, 2015 22:01
Show Gist options
  • Save laserlemon/71eff42b27e496db2a4f to your computer and use it in GitHub Desktop.
Save laserlemon/71eff42b27e496db2a4f to your computer and use it in GitHub Desktop.
Proposed Interactor 4.0 Syntax
class PlaceOrder
include Interactor
# What goes here?
before do
context.user ||= User.find(context.user_id)
end
def call
context.order = context.user.orders.create(context.attributes)
context.fail! unless context.order.persisted?
end
end
@jonstokes
Copy link

IMO the following is easier to read and reason about, but that may just my bias since I've been thinking along these lines recently:

class PlaceOrder
  include Interactor

  expects :user_id, :attributes

  allows(:user) do
    User.find(user_id)
  end

  provides(:order) do
    user.orders.create(attributes)
  end

  def call
    context.fail! unless order.persisted?
  end
end

@jonstokes
Copy link

Heck, I'd even like to do away with the call and any reference to context and replace it with something like:

fails_on(error: "Order not persisted") { !order.persisted? }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment