Sha256: 5dc020b7b8c250733447ecdc64c14ce152106953591369fa62e7cb0cee3293a4

Contents?: true

Size: 1.1 KB

Versions: 14

Compression:

Stored size: 1.1 KB

Contents

# 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 relation.
          # @param [ String ] type Not used in this context.
          #
          # @return [ Array<Document> ] The documents.
          def build(base, object, type = 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

14 entries across 14 versions & 1 rubygems

Version Path
mongoid-7.0.13 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-7.0.12 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-7.0.11 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-7.0.10 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-7.0.8 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-7.0.7 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-7.0.6 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-7.0.5 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-7.0.4 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-7.0.3 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-7.0.2 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-7.0.1 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-7.0.0 lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
mongoid-7.0.0.beta lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb