Skip to content

Custom context classes #145

Description

@bobbytables

I would love to see the ability to define custom Context classes as long as they implement the proper methods (Similar to ActiveModel linting).

Reason being is that OpenStruct leaves a lot of room for error, and have a proper domain object for context would alleviate a lot of that. For example, it would be really nice to be able to do:

class AuthenticateUser
  include Interactor
  context_class AuthenticateUserContext

  def call; end
end

class AuthenticateUserContext < Interactor::BaseContext
  attr_accessor :email, :password
end

Explanation of what this example is doing:

Interactor::BaseContext would implement all of the necessary internals of a context, for example fail! and rollback!.

But since it doesn't include OpenStruct, the only values that can be set are email and password due to the attr_accessor in the custom context class.

I like this because it makes you think of your design more coherently and actually gives more options at the end of an interactor call. For example, errors:

class AuthenticateUserContext < Interactor::BaseContext
  attr_accessor :email, :password

  def formatted_errors
    errors.map {|error_key| I18n.translate(error_key) }
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions