module ActiveInteractor::Interactor::Context::ClassMethods

Interactor context class methods. Because {ClassMethods} is a module classes should extend {ClassMethods} rather than inherit from it.

@author Aaron Allen <hello@aaronmallen.me> @since 0.1.0

Public Instance Methods

context_class() click to toggle source

Get the {Base interactor} class' {ActiveInteractor::Context::Base context} class. If no class is found or no class is set via the {#contextualize_with} method a new class is created.

@see ActiveInteractor::Context::Loader.find_or_create

@return [Const] the {Base interactor} class' {ActiveInteractor::Context::Base context} class

# File lib/active_interactor/interactor/context.rb, line 190
def context_class
  @context_class ||= ActiveInteractor::Context::Loader.find_or_create(self)
end
contextualize_with(klass) click to toggle source

Set the {Base interactor} class' {#context_class context class}

@since 1.0.0

@example

class User < ActiveRecord::Base
end

class MyInteractor < ActiveInteractor::Base
  contextualize_with :user
end

MyInteractor.context_class
#=> User

@param klass [Const, Symbol, String] the class to use as context @raise [Error::InvalidContextClass] if the class can not be found. @return [Const] the {Base interactor} class' {ActiveInteractor::Context::Base context} class

# File lib/active_interactor/interactor/context.rb, line 212
def contextualize_with(klass)
  @context_class = begin
    context_class = klass.to_s.camelize.safe_constantize
    raise(ActiveInteractor::Error::InvalidContextClass, klass) unless context_class

    context_class
  end
end