team-formalist / formalist-rb

Flexible form builder
MIT License
22 stars 4 forks source link

Reference injected dependencies in form definitions without speceial `dep` call #64

Closed timriley closed 3 years ago

timriley commented 7 years ago

Here's an example of something we do right now:

class MyForm < Formalist::Form
  include MyApp::Import["my_repo"]

  define do
    select_box :status, options: dep(:status_options)
  end

  private

  def status_options
    my_repo.status_options
  end
end

This is fine, but I don't like wrapping up the deferred calls to local methods or injected dependencies in dep.

What I want is to do this:

  define do
    # Look, no dep()!
    select_box :status, options: status_options
  end

I think this should be doable since we're capturing the definition into an AST first, anyway. We could have a #method_missing call as part of the DSL evaluation that checks to see if the missing method is actually present as an instance method and do some wrapping accordingly.

On thing I think we also want to make sure we cover is handling deferred calls to these instance methods deeper within structures that we pass to the elements in the form definition, e.g.

define do
  select_box :status, options: [default_status_option] + other_status_options
end

Right now I think that deferred values are only resolved if they're "top-level".

timriley commented 3 years ago

Fixed in #68