Ruby On Rails - Active interaction: Have A Generic Parameter In Interaction

Viewed 106

I'm using a gem called active-interaction.

I've got an interaction that inherits ActiveInteraction with filters:

module Api
 module V2
  class MyInteraction < ActiveInteraction::Base
   
   string :sort_string
   array :ids, default: []

   # Here I want to have a third parameter, and for it to be either 
   # An integer, a string or a float.

I'm trying to find a way to have a parameter that can accept 2 or more types / a dynamic "any" type.

Any suggestions?

Thanks.

1 Answers

You're looking for a concept called: "overloading", I'm not so familiar with the active-interaction gem, but in statically typed languages such as C#, Java, etc... You can use overloading.

How can it help? For example, if you have a route called "send-details" that can accept a string/int. You can do something like (C# for the example):

[HttpPost]
public void doSomething(String param)
{
    BL.doSomething()
}

[HttpPost]
public void doSomething(int param)
{
   BL.doSomething()
}

Now, we overloaded the doSomething function and the route will match the correct one, keeping it DRY and OOP friendly.

From the docs, it looks very weird the way active-interaction behaves, because ruby is a dynamically-typed language, and active-interaction uses a filter to make it statically typed. I believe your best option will be:

Move all BL to a separate Service, and create 2 interactions with different params (the signature) => in both just call the newly created service.

Yes, it's a lot of code, but I believe it will be your best resolution for the situation.

On a personal note, I would not use active interaction for production products due to lack of community and the fact it goes against ruby standards.

you can read more about overloading here: https://en.wikipedia.org/wiki/Function_overloading

Related