AaronLasseigne / active_interaction

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

Missing some documentation on nested hash default options #553

Closed omkarmoghe closed 1 year ago

omkarmoghe commented 1 year ago

Small request for additional documentation for the hash filter, specifically around nested options.

It seems like if you want to provide a nested option to a hash filter with a default proc, you need to set the default of the hash itself to also be a lambda, otherwise you get an error when attempting to define the filters.

Error

class Foo < ActiveInteraction::Base
  string :other_input, default: "some value"

  hash :options, default: {} do 
    string :test, default: -> { other_input&.upcase }
  end
end

Results in some NameError: NameError: undefined local variable or method `other_input' for nil:NilClass

Correct(?)

class Foo < ActiveInteraction::Base
  string :other_input, default: "some value"

  hash :options, default: -> { {} } do # lazy load the hash default as well (proc)
    string :test, default: -> { other_input&.upcase }
  end
end

This seems to work as expected.

I don't think this is necessarily a bug, but isn't immediately apparent, so hoping for some documentation around this usage if possible. Happy to open a PR to add to docs as well, if this is accepted.

AaronLasseigne commented 1 year ago

That's an interesting edge case. I think you're right that the docs should mention it. I also think it should throw a more informative error message than that.

omkarmoghe commented 1 year ago

@AaronLasseigne Is either the doc update or better error message something you'd consider a PR for? Or is this something you'd prefer to handle yourself?

AaronLasseigne commented 1 year ago

I'd take a PR for either or both.

AaronLasseigne commented 1 year ago

This is fixed in the 5.3.0 release.