Skip to content

Easier fetching of given inputs #498

@jgrau

Description

@jgrau

Hi!

I recently started using AI and I've run into what seems like a topic that has already been discussed a few times: partial updates by using default: nil and given?(:my_attribute).

When reading previous issues I see that there's some pushback around using something like optional: true with the result that if an argument is optional and not passed to the interaction it is not present in the inputs hash. Fair enough.

I find myself doing something like

    listing.assign_attributes(
      inputs
        .slice(
          :a,
          :bunch,
          :of,
          :attrs,
        )
        .select { |k, _v| given?(k) },
    )

in order to support partial updates which works but is not super friendly.

How do you feel about adding a given method in ActiveInteraction::Inputs that returns a new ActiveInteraction::Inputs instance but only containing the keys that was given? It would make the above use case look like

    listing.assign_attributes(
      inputs
        .given
        .slice(
          :a,
          :bunch,
          :of,
          :attrs,
        )
    )

which imo is pretty slick 😎

I'm not familiar with this codebase yet but if you like the idea and would accept a pull request I could take a shot at it.

For the record: I do feel having something like optional: true would be great :)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions