Sha256: aff9e16a9560a8d868522804d84d249a0ed36530ccf31821f62edf78f79e0f2d

Contents?: true

Size: 1.18 KB

Versions: 27

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

module Mongoid
  module Association
    module Referenced
      class HasMany

        # The Builder behavior for has_many associations.
        #
        # @since 7.0
        module Buildable

          # This method either takes an _id or an object and queries for the
          # inverse side using the id or sets the object.
          #
          # @example Build the document.
          #   relation.build(meta, attrs)
          #
          # @param [ Object ] base The base object.
          # @param [ Object ] object The object to use to build the association.
          # @param [ String ] type The type of document to query for.
          # @param [ nil ] selected_fields Must be nil.
          #
          # @return [ Document ] A single document.
          def build(base, object, type = nil, selected_fields = nil)
            return (object || []) unless query?(object)
            return [] if object.is_a?(Array)
            query_criteria(object, base)
          end

          private

          def query?(object)
            object && Array(object).all? { |d| !d.is_a?(Mongoid::Document) }
          end
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 2 rubygems

Version Path
mongoid-7.3.5 lib/mongoid/association/referenced/has_many/buildable.rb
mongoid-7.3.4 lib/mongoid/association/referenced/has_many/buildable.rb
mongoid-7.1.11 lib/mongoid/association/referenced/has_many/buildable.rb
mongoid-7.2.6 lib/mongoid/association/referenced/has_many/buildable.rb
mongoid-7.3.3 lib/mongoid/association/referenced/has_many/buildable.rb
mongoid-7.3.2 lib/mongoid/association/referenced/has_many/buildable.rb
mongoid-7.2.5 lib/mongoid/association/referenced/has_many/buildable.rb
mongoid-7.1.10 lib/mongoid/association/referenced/has_many/buildable.rb
mongoid-7.1.9 lib/mongoid/association/referenced/has_many/buildable.rb
mongoid-7.2.4 lib/mongoid/association/referenced/has_many/buildable.rb
mongoid-7.3.1 lib/mongoid/association/referenced/has_many/buildable.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/mongoid-7.1.7/lib/mongoid/association/referenced/has_many/buildable.rb
mongoid-7.3.0 lib/mongoid/association/referenced/has_many/buildable.rb
mongoid-7.2.3 lib/mongoid/association/referenced/has_many/buildable.rb
mongoid-7.1.8 lib/mongoid/association/referenced/has_many/buildable.rb
mongoid-7.2.2 lib/mongoid/association/referenced/has_many/buildable.rb
mongoid-7.2.1 lib/mongoid/association/referenced/has_many/buildable.rb
mongoid-7.1.7 lib/mongoid/association/referenced/has_many/buildable.rb
mongoid-7.2.0 lib/mongoid/association/referenced/has_many/buildable.rb
mongoid-7.1.6 lib/mongoid/association/referenced/has_many/buildable.rb