Sha256: a165d08a37d0dec3b02d63e3a38a3a995a837ad3ff6532d9802d02577a336b9a

Contents?: true

Size: 1.17 KB

Versions: 29

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

module Mongoid
  module Association
    module Referenced
      class HasAndBelongsToMany

        # The Builder behavior for has_and_belongs_to_many associations.
        module Buildable

          # This builder either takes a hash and queries for the
          # object or an array of documents, where it will just return them.
          #
          # @example Build the documents.
          #   relation.build(association, attrs)
          #
          # @param [ Object ] base The base object.
          # @param [ Object ] object The object to use to build the association.
          # @param [ String ] type Not used in this context.
          # @param [ nil ] selected_fields Must be nil.
          #
          # @return [ Array<Document> ] The documents.
          def build(base, object, type = nil, selected_fields = nil)
            if query?(object)
              query_criteria(object)
            else
              object.try(:dup)
            end
          end

          private

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

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
mongoid-8.0.10 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-8.1.10 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-8.1.9 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-8.0.9 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-8.1.8 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-8.1.7 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-8.1.6 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-8.0.8 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-8.1.5 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-8.1.4 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-8.0.7 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-8.1.3 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-8.1.2 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-8.0.6 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-7.5.4 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-8.1.1 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-8.0.5 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-8.1.0 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-7.5.3 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-8.0.4 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb