AaronLasseigne / active_interaction

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

Pass a hash to run method without using filters #508

Closed kapso closed 3 years ago

kapso commented 3 years ago

I want to pass a hash to an interaction that is a nested hash, and it's already filtered in a Rails controller using params.permit(:key1, key2, { country_codes: [], expense: [:min, :max] }), so I would hate to filter it again in my interaction.

So something like this... Search.run(params: query_params) or Search.run(query_params)

Is something like this possible? i.e, not using filters and yet the contents of the hash are available.

I realized I can access the raw input using @_interaction_raw_inputs which will solve my issue but is there a cleaner approach to this?

AaronLasseigne commented 3 years ago

Generally you'd skip permitting the params and send them through to the interaction which will filter them. There's no way around using the filters and @_interaction_raw_inputs is an internal variable subject to change without notice in the CHANGELOG.

kapso commented 3 years ago

@AaronLasseigne thanks for the quick response, and your work on this gem.

So I tried passing params from Rails controller, and run into the following...

to_h': unable to convert unpermitted parameters to hash (ActionController::UnfilteredParameters)

I am guessing this issue is still in TBD state judging from this thread - https://github.com/AaronLasseigne/active_interaction/issues/381

Or perhaps to_unsafe_h is the way to go?

Unfortunately, we cannot disable strong parameters for the app, coz we are only using active_interaction for some of the controller actions for now.

AaronLasseigne commented 3 years ago

You can pass them through directly:

SomeInteraction.run(params)
GGD commented 2 years ago

I made it this way to "skip" the filters:

# controller
Interaction.run(params.merge(raw_params: params))

# interaction
class YourInteraction
  hash :raw_params, strip: false

  def execute
    raw_params
  end
end