Allow embeds_one to accept a class as a second argument to build

XMLWordPrintableJSON

    • None
    • Fully Compatible
    • Ruby Drivers
    • Needed
    • Hide

      1. What would you like to communicate to the user about this feature?

      See PR description at https://github.com/mongodb/mongoid/pull/6132

      2. Would you like the user to see examples of the syntax and/or executable code and its output?

      Yes, see PR.

      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? See PR description at https://github.com/mongodb/mongoid/pull/6132 2. Would you like the user to see examples of the syntax and/or executable code and its output? Yes, see PR. 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      For example:

      # Shows the discrepancy between the `embeds_one` & `embeds_many` build API:
      #
      # - `embeds_many` accepts a class as a second argument on `build`
      # - `embeds_one` does not
      #
      # After some experimentation, you can work around this with `_type` in the attrs,
      # but I would expect the build API to be consistent between the two
      # 
      # Issue exists in upstream v6, v7, v8
      class BatchOperation
        include Mongoid::Document
      
        embeds_one :fetcher
        embeds_many :fetchers
      end
      
      class Fetcher
        include Mongoid::Document
        embedded_in :batch_operation
      end
      
      class FileFetcher < Fetcher
      end
      
      class PageFetcher < Fetcher
      end
      
      BatchOperation.collection.drop
      
      operation = BatchOperation.create
      operation.build_fetcher({}, FileFetcher)
      operation.fetchers.build({}, PageFetcher)
      # Expected: [ FileFetcher, PageFetcher ]
      # Upstream: [ Fetcher, PageFetcher ]
      pp [operation.fetcher.class, operation.fetchers[0].class]
      
      # This saves immediately so isn't suitable:
      # operation.fetcher = FileFetcher.new
      
      # This can work, but is inconsistent with the embeds_many API
      # operation.build_fetcher(_type: "FileFetcher")
      

            Assignee:
            Jamis Buck
            Reporter:
            Alex Bevilacqua
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: