collectiveidea / interactor

Interactor provides a common interface for performing complex user interactions.
MIT License
3.36k stars 212 forks source link

Accessing context from Interactor::Organizer #88

Closed michaelminter closed 9 years ago

michaelminter commented 9 years ago

I have something like this:

module Domains
  class Rename
    include Interactor::Organizer

    @interactors = [Domains::Install, Domains::UpdateName]
    if context.domain.ssl_cert.present?
      context.ssl_cert = context.domain.ssl_cert
       @interactors << SslCerts::Disable
    end

    organize @interactors
  end
end

But getting:

undefined local variable or method `context' for Domains::Rename:Class

Is there a way to access context from this class?

emilford commented 9 years ago

@michaelminter

It is possible, but I would not recommend it. Would updating SslCerts::Disable to include a guard clause (e.g., return unless context.domain.ssl_cert.present?) and then always including it in Domains::Rename's organized interactors work for you instead?

michaelminter commented 9 years ago

Yeah, that's what I'm doing now as a workaround. But for low level insight, how do you call context in the organized interactor?

mengqing commented 9 years ago

@michaelminter you call context on the instance, eg

class Organizer
  include Interactor::Organizer

  organize [A, B]

  def call
    # Access context here
    super
  end
end