trailblazer / reform

Form objects decoupled from models.
https://trailblazer.to/2.1/docs/reform.html
MIT License
2.49k stars 184 forks source link

populator adding just one record #520

Closed CarlosEMedrano27 closed 4 years ago

CarlosEMedrano27 commented 4 years ago

Complete Description of Issue

using populator on collection is only saving one new record, I send the following JSON: "student_special_documents": [ { "document_type_id": 2, "document_uploader": { "first_name": "Carlos", "last_name": "Medrano" }, "college_ids": [5,9,8], "uploaded_at": "" }, { "document_type_id": 1, "document_uploader": { "first_name": "Carlos", "last_name": "Medrano" }, "college_ids": [5,9,8] } ] and it's only saving the last one

Steps to reproduce

I created a form with a collection and the following method being called on the populator

def populator!(fragment:, **) item = student_special_documents.find { |document| document.id.to_s == fragment["id"].to_s } if fragment["delete"] == "1" student_special_documents.delete(item) return skip! end item ? item : student_special_documents.append(StudentSpecialDocument.new) end

Expected behavior

It should be adding 2 new records to the relation

Actual behavior

It's adding just the last one

emaglio commented 4 years ago

@CarlosEMedrano27 which reform's version are you using? and can you please share the complete reform object?

CarlosEMedrano27 commented 4 years ago

Hi @emaglio! Thanks for your help

Reform


module Contracts
  class RequestStudentSpecialDocumentsContract < Reform::Form
    collection :student_special_documents, model_identifier: :id, form: Contracts::RequestStudentSpecialDocumentContract, populator: :populator!

    def populator!(fragment:, **)
      item = student_special_documents.find { |document| document.id.to_s == fragment["id"].to_s }
      if fragment["delete"] == "1"
        student_special_documents.delete(item)
        return skip!
      end
      item || student_special_documents.append(StudentSpecialDocument.new)
    end
  end
end

Version

I'm using the reform (2.2.4)

emaglio commented 4 years ago

@CarlosEMedrano27 just added a test case really similar to yours for v2.3 and it seems populate correctly (https://github.com/trailblazer/reform/commit/df8cb725dcf84b159480364dacba1efc98178b76) - please update reform version and try again. Thanks