Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module that has 'unnest' throws an error if included more than once #81

Open
ivked85 opened this issue Jan 1, 2021 · 1 comment · May be fixed by #92
Open

Module that has 'unnest' throws an error if included more than once #81

ivked85 opened this issue Jan 1, 2021 · 1 comment · May be fixed by #92

Comments

@ivked85
Copy link

ivked85 commented Jan 1, 2021

I have a Reform::Form::Module where I'm using unnest. If I try to include it more than once I get an error on startup: gems/disposable-0.4.7/lib/disposable/twin/property/unnest.rb:14:in `unnest': undefined method `[]' for nil:NilClass (NoMethodError)

A setup similar to this:

module Contract::Component
  module Foo
    include Reform::Form::Module

    property :foo do
      property :bar
    end

    unnest :bar, from: :foo
  end
end

module Contract
  class Create < Reform::Form
    include Component::Foo
  end
end

module Contract
  class Update < Reform::Form
    include Component::Foo
  end
end

This is the relevant part of the code in disposable

    def unnest(name, options)
      from = options.delete(:from)
      # needed to make reform process this field.

      options = definitions.get(from)[:nested].definitions.get(name).instance_variable_get(:@options) # FIXME.
      options = options.merge(virtual: true, _inherited: true, private_name: nil)

      property(name, options)
      delegates from, name, "#{name}="
    end

Since unnest is a class method, options.delete(:from) will actually delete the key from the method argument itself, causing each subsequent call to fail.
Changing that line to from = options[:from] seems to fix the issue for me, without introducing any noticeable side-effects.

@yogeshjain999
Copy link
Collaborator

yogeshjain999 commented Apr 11, 2021

Makes sense.

@apotonick I think we should not mutate the options ? We can now access from as a kwarg too.

def unnest(name, from:, **options)
  ...
end

yogeshjain999 added a commit to yogeshjain999/disposable that referenced this issue Jan 20, 2023
@yogeshjain999 yogeshjain999 linked a pull request Jan 20, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants