Skip to content

Deeply nested attributes#697

Merged
tagliala merged 2 commits intoDavyJonesLocker:masterfrom
Springest:deeply-nested-attributes
Mar 7, 2017
Merged

Deeply nested attributes#697
tagliala merged 2 commits intoDavyJonesLocker:masterfrom
Springest:deeply-nested-attributes

Conversation

@libc
Copy link
Copy Markdown
Contributor

@libc libc commented Mar 7, 2017

Consider the following example:

class User < ActiveRecord::Base
  has_many :phone_numbers

  accepts_nested_attributes_for :phone_numbers
end
    
class PhoneNumber < ActiveRecord::Base
  belongs_to :user
  # This is a bit contrived, because probably it should be many-to-many
  has_many :labels

  accepts_nested_attributes_for :labels
end

class Label < ActiveRecord::Base
  belongs_to :phone_number
end

If you render this on 1 page to edit labels you end up with the following attributes name:

user[phone_numbers_attributes][0][labels_attributes][0][label]

Validation on this field will be generated automatically as:

user[phone_numbers_attributes][][label_attributes][][label]

The current nested attributes javascript looks like /\[(\w+_attributes)\].*\[(\w+)\]$/, which doesn't properly process this nested attributes, with this PR it'll be supported.

There's one use-case that is covered by the current deeply-nested validation, that I do not cover.
user[phone_numbers_attributes][deeply][nested][][attribute] searches for /\[phone_numbers_attributes\].*\[\]\[attribute\]$/ in the validators. So it can match something unintended. I left it as it is right now and only process

I left the initial commit of my colleague @Arie in.

Any feedback welcome.

Arie and others added 2 commits February 17, 2017 11:33
Consider the following example:

  class User < ActiveRecord::Base
    has_many :phone_numbers
    accepts_nested_attributes_for :phone_numbers
  end

  class PhoneNumber < ActiveRecord::Base
    # This is a bit contrived, because probably it should be
    # many-to-many
    has_many :labels
    accepts_nested_attributes_for :labels
  end

  class Label < ActiveRecord::Base
    belongs_to :phone_number
  end

If you render this on 1 page to edit labels you end up with the
following attributes name

  user[phone_numbers_attributes][0][labels_attributes][0][label]

And validation is on field
  user[phone_numbers_attributes][][label_attributes][][label]

At the moment the most current version of client side validation only
supports 1 level of nesting of accepts_nested_attributes_for. This
commit makes it support any level of nesting.
@coveralls
Copy link
Copy Markdown

coveralls commented Mar 7, 2017

Coverage Status

Coverage remained the same at 100.0% when pulling 580e024 on Springest:deeply-nested-attributes into 2a24a36 on DavyJonesLocker:master.

@tagliala tagliala merged commit 3e4f857 into DavyJonesLocker:master Mar 7, 2017
@tagliala
Copy link
Copy Markdown
Contributor

tagliala commented Mar 7, 2017

@libc 👏 👏 👏

@tagliala tagliala added this to the 9.1.0 milestone Mar 7, 2017
@tagliala
Copy link
Copy Markdown
Contributor

tagliala commented Mar 7, 2017

@libc please give the master branch a try, I will then release a new version of the gem

@libc
Copy link
Copy Markdown
Contributor Author

libc commented Mar 7, 2017

We're still using 4.2 in production (cherry-picked version of this).

I tried master on our experimental rails-5.0.2-branch and it seems to work fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants