Skip to content

Instantly share code, notes, and snippets.

@gpspake
Last active October 30, 2018 12:10
Show Gist options
  • Save gpspake/3c8814216b99e5f47904454aa48c492e to your computer and use it in GitHub Desktop.
Save gpspake/3c8814216b99e5f47904454aa48c492e to your computer and use it in GitHub Desktop.
Simple interactor example

do_this.rb

class DoThis
  include Interactor

  def call
    begin
      if context.fail_this
        context.fail!(error: "this failed")
      end

      puts "we're in the DoThis interactor and we're gonna save this object"
      this_object

    rescue => e
      context.fail!(error: e.message)
    end
  end

  private

  def this_object
    context.this = {title: "this", report: report}
  end

  def report
    "We're doing this"
  end
end

do_that.rb

class DoThat
  include Interactor

  def call
    begin
      if context.fail_that
        context.fail!(error: "that failed")
      end

      puts "we're in the DoThat interactor and we're gonna save that object"
      that_object

    rescue => e
      context.fail!(error: e.message)
    end
  end

  private

  def that_object
    context.that = {title: "that", report: report}
  end

  def report
    "We're doing that"
  end

end

do_this_and_that.rb

class DoThisAndThat
  include Interactor::Organizer

  organize DoThis, DoThat
end

accounts_controller.rb

class AccountsController < ApplicationController

  expose(:account, attributes: :account_params)

  def do_this(params = nil)
    DoThis.call(params)
  end

  def do_that(params = nil)
    DoThat.call(params)
  end

  def do_this_and_that(params = nil)
    DoThisAndThat.call(params)
  end

  ...

pry

[70] pry(main)> a = AccountsController.new
=> #<AccountsController:0x000000000f5e54e8 @_action_has_layout=true, @_headers={"Content-Type"=>"text/html"}, @_request=nil, @_response=nil, @_routes=nil, @_status=200>
[71] pry(main)> a.do_this
we're in the DoThis interactor and we're gonna save this object
=> #<Interactor::Context this={:title=>"this", :report=>"We're doing this"}>
[72] pry(main)> a.do_that
we're in the DoThat interactor and we're gonna save that object
=> #<Interactor::Context that={:title=>"that", :report=>"We're doing that"}>
[73] pry(main)> a.do_this_and_that
we're in the DoThis interactor and we're gonna save this object
we're in the DoThat interactor and we're gonna save that object
=> #<Interactor::Context this={:title=>"this", :report=>"We're doing this"}, that={:title=>"that", :report=>"We're doing that"}>
[74] pry(main)> a.do_this_and_that(fail_that: true)
we're in the DoThis interactor and we're gonna save this object
=> #<Interactor::Context fail_that=true, this={:title=>"this", :report=>"We're doing this"}, error="#<Interactor::Context fail_that=true, this={:title=>\"this\", :report=>\"We're doing this\"}, error=\"that failed\">">
[70] pry(main)> a = AccountsController.new
=> #<AccountsController:0x000000000f5e54e8 @_action_has_layout=true, @_headers={"Content-Type"=>"text/html"}, @_request=nil, @_response=nil, @_routes=nil, @_status=200>
[71] pry(main)> a.do_this
we're in the DoThis interactor and we're gonna save this object
=> #<Interactor::Context this={:title=>"this", :report=>"We're doing this"}>
[72] pry(main)> a.do_that
we're in the DoThat interactor and we're gonna save that object
=> #<Interactor::Context that={:title=>"that", :report=>"We're doing that"}>
[73] pry(main)> a.do_this_and_that
we're in the DoThis interactor and we're gonna save this object
we're in the DoThat interactor and we're gonna save that object
=> #<Interactor::Context this={:title=>"this", :report=>"We're doing this"}, that={:title=>"that", :report=>"We're doing that"}>
[74] pry(main)> a.do_this_and_that(fail_that: true)
we're in the DoThis interactor and we're gonna save this object
=> #<Interactor::Context fail_that=true, this={:title=>"this", :report=>"We're doing this"}, error="#<Interactor::Context fail_that=true, this={:title=>\"this\", :report=>\"We're doing this\"}, error=\"that failed\">">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment