6

Add accepts_nested_attributes_for support for delegated_type by xtr3me · Pull Re...

 3 years ago
source link: https://github.com/rails/rails/pull/41717
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Copy link

Contributor

xtr3me commented on Mar 21

Summary

This PR adds nested_attributes_for support to delegated_type, this allows a developer to create and update records easily without needing to write specific methods like:

class Entry < ApplicationRecord
  delegated_type :entryable, types: %w[ Message Comment ]

  def self.create_with_comment(content, creator: Current.user)
    create! entryable: Comment.new(content: content), creator: creator
  end
end

Using nested_attributes_for allows to execute the following:

class Entry < ApplicationRecord
  delegated_type :entryable, types: %w[ Message Comment ]
  accepts_nested_attributes_for :entryable
end

params = { entry: { entryable_type: 'Comment', entryable_attributes: { content: 'Smiling' } } }
entry = Entry.create(params[:entry])

Nested forms based on accepts_nested_attributes_for are very powerful and this is the last piece missing to also be able to use it on Delegated Types.

Question

Since this is a polymorphic belongs_to relationship, what other tests would we like to have? As it is already tested in TestNestedAttributesOnABelongsToAssociation


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK