Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-5400

Mongoid::Criteria#map should yield its field args to the block

    • Type: Icon: Improvement Improvement
    • Resolution: Won't Do
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None

      Currently, Mongoid::Criteria#map ignores its field args if a block is given:

      # current Mongoid 7 behavior
      Dinosaur.all.map(:ignored1, :ignored2) { |dino| "A scary #{dino.color} #{dino.name}!" }
      #=> [ "A scary green Velociraptor!", "A scary brown T-Rex!" ]
      

       I think this is counter-intuitive. Users would expect these args to be honored and passed into the block:

      # proposed behavior
      Dinosaur.all.map(:name, :color) { |result| "A scary #{result[1]} #{result[0]}!" }
      #=> [ "A scary green Velociraptor!", "A scary brown T-Rex!" ]
      

      Using a block without field args would still work the old way:

      # this behavior will remain as-is
      Dinosaur.all.map { |dino| "A scary #{dino.color} #{dino.name}!" } #=> [ "A scary green Velociraptor!", "A scary brown T-Rex!" ]
      

      This can be supported done easily with the #pluck_each refactor in PR https://github.com/mongodb/mongoid/pull/5332.

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            shields@tablecheck.com Johnny Shields
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: