AaronLasseigne / active_interaction

:briefcase: Manage application specific business logic.
MIT License
2.07k stars 137 forks source link

3.8.3 -> 4.1 upgrade interface #521

Closed cpanderson closed 2 years ago

cpanderson commented 2 years ago

Hello,

Trying to make sense of the interface filter in 4.1. It won't recognize the constant which works in 3.8.3. Example...

module MyModule
  class CompileSomething < ActiveInteraction::Base
    interface :params

    class Params < ActiveInteraction::Base
    end
  end
end

uninitialized constant MyModule::CompileSomething::Params

This is legacy code that goes back to 2016 by another developer so there's a good chance this wasn't properly implemented in the first place but has worked until now including Rails 5 and now trying to upgrade to Rails 6.1.

Any insight would be helpful. Thanks!

antulik commented 2 years ago

@cpanderson have you tried changing the order and define class first?

module MyModule
  class CompileSomething < ActiveInteraction::Base
    class Params < ActiveInteraction::Base
    end

    interface :params
  end
end
cpanderson commented 2 years ago

Thanks but no that makes no difference.

cpanderson commented 2 years ago

To give this case more clarity the Params class has filters that I want to validate and use and inputs into the CompileSomething. If I use the "from" argument for interface AND put the Params class above the interface statement then I can get a "is not a valid interface" error.

module MyModule
  class CompileSomething < ActiveInteraction::Base
    class Params < ActiveInteraction::Base
      string :some_string
    end

    interface :params, from: MyModule::CompileSomething::Params

    def execute
      ...
    end
  end
end

I can then do...

some_params = MyModule::CompileSomething::Params.new(some_string: "hey")

result = MyModule::CompileSomething.run(params: some_params)

but then I get the invalid interface error. The code has several places where it uses this Params pattern because some classes reference other classes Params as inputs.

The code I'm working on needs refactoring so I'm not sure how to use interface at this point. Any recommendations?

AaronLasseigne commented 2 years ago

You might find success switching to object :params, class: MyModule::CompileSomething::Params.

It's difficult to make other recommendations without seeing more of how it all works together.