Better Interactor is a small gem that aims to improve a lot of stuff that is missing in the main interactor gem
Find a file
Gamberi Elia bfddf0ed30
📝 Fix typos and improve README formatting
Corrected typos and formatting in the README.
2025-10-30 14:50:41 +01:00
.github/workflows Added Conditional Interactor calls, with implicit and explicit condition names 2025-10-29 13:54:25 +01:00
bin Added Conditional Interactor calls, with implicit and explicit condition names 2025-10-29 13:54:25 +01:00
lib Added method as interactors 2025-10-29 15:41:55 +01:00
spec Added method as interactors 2025-10-29 15:41:55 +01:00
.gitignore Added Conditional Interactor calls, with implicit and explicit condition names 2025-10-29 13:54:25 +01:00
.rspec Added Conditional Interactor calls, with implicit and explicit condition names 2025-10-29 13:54:25 +01:00
.rubocop.yml Added Conditional Interactor calls, with implicit and explicit condition names 2025-10-29 13:54:25 +01:00
better_interactor.gemspec Added Conditional Interactor calls, with implicit and explicit condition names 2025-10-29 13:54:25 +01:00
Gemfile Added Conditional Interactor calls, with implicit and explicit condition names 2025-10-29 13:54:25 +01:00
Gemfile.lock ⬆️ removed pry as dependency 2025-10-29 13:56:18 +01:00
LICENSE.txt Added Conditional Interactor calls, with implicit and explicit condition names 2025-10-29 13:54:25 +01:00
Rakefile Added Conditional Interactor calls, with implicit and explicit condition names 2025-10-29 13:54:25 +01:00
README.md 📝 Fix typos and improve README formatting 2025-10-30 14:50:41 +01:00

BetterInteractor

Better Interactor is a gem that aims to extend the default usage for the Interactor gem. The default Interactor usage is unchanged, extended with new possibilities:

  • adding a condition to your interactors, in the organizer, to skip its call, if that returns false
  • defining a default condition for each interactor call, in the organizer
  • defining a method inside the organizer and call that instead of an organizer

Installation

Install the gem and add to the application's Gemfile by executing:

bundle add better_interactor

If bundler is not being used to manage dependencies, install the gem by executing:

gem install better_interactor

Usage

Conditional Call

passing an hash instead of an interactor allows you to define 2 keys inside of it:

  • class: with the interactor class name
  • if: with a symbol with the same name as the method that responds to our condition
class PlaceOrder
  include Interactor::Organizer

  def should_send_thank_you?
    context.client.is_a_good_client?
  end

  organize CreateOrder,
           { class: ChargeCard }
           { class: SendThankYou, if: :should_send_thank_you? }
end

In this example:

  • CreateOrder will always be called, because you passed only the class
  • ChargeCard will always be called, because there's no "if"
  • SendThankYou will be called only if the method should_send_thank_you?, with the passed context, returns true

Default Condition

This grants a default condition name for the method to call, defined as follows: "can_[interactor class name underscored]?"

class PlaceOrder
  include Interactor::Organizer

  def can_send_thank_you?
    context.client.is_a_good_client?
  end

  organize CreateOrder,
           { class: ChargeCard }
           { class: SendThankYou }
end

In this example:

  • CreateOrder will always be called, because you passed only the class
  • ChargeCard will always be called, because there's no method named can_charge_card?
  • SendThankYou will be called only if the method can_send_thank_you?, with the passed context, returns true

In case of Interactors inside of Modules (Client::SendThankYou) the modules are included inside the method name (can_client_send_thank_you?)

Interactor-like method

If you feel like an interactor, sometimes, is too much for a small non-reusable piece of logic, you can define a method and use it as an interactor, just pass a symbol with the method name as you would with a normal interactor

class PlaceOrder
  include Interactor::Organizer

  def create_order
    Order.create(context.order_attr)
  end

  def send_thank_you
    Email.new(to: context.client.email, content: "thanks :D")
  end

  organize :create_order,
           { class: :send_thank_you }
end

In this example we use 0 Interactors, and rely only on methods.

Conditional logic defined above still applies!!!

Development

Clone this repo, run bundle and you are good to go.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/gimbardo/better_interactor.

To contribute:

  1. Fork the project.
  2. Write a failing test.
  3. Commit changes that fix the tests.
  4. Be patient.

License

The gem is available as open source under the terms of the MIT License.