Sha256: 1c89835a10be8e9dcb8bbb0a8bdfd36018dc32828104c52d20255ac35cb98b77

Contents?: true

Size: 1.21 KB

Versions: 27

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

module Mongoid
  module Association
    module Referenced
      class HasAndBelongsToMany

        # The Builder behavior for has_and_belongs_to_many associations.
        #
        # @since 7.0
        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

27 entries across 27 versions & 2 rubygems

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