Sha256: 149c2250cf67246f751fe5716fdf2ee6e027e77bfc89b48326fe7cc0fa08b104

Contents?: true

Size: 1.37 KB

Versions: 10

Compression:

Stored size: 1.37 KB

Contents

# encoding: utf-8
module Mongoid # :nodoc:
  module Relations #:nodoc:
    module Builders #:nodoc:
      module Referenced #:nodoc:
        class Many < Builder

          # This builder either takes a hash and queries for the
          # object or an array of documents, where it will just return tem.
          #
          # @example Build the documents.
          #   Builder.new(meta, attrs).build
          #
          # @param [ String ] type The type of document to query for.
          #
          # @return [ Array<Document> ] The documents.
          def build(type = nil)
            return object unless query?
            return [] if object.is_a?(Array)
            metadata.criteria(convertable(metadata, object))
          end

          private

          # Get the value for the foreign key in convertable or unconvertable
          # form.
          #
          # @example Get the value.
          #   builder.convertable
          #
          # @return [ String, Unconvertable, BSON::ObjectId ] The string or object id.
          #
          # @since 2.0.2
          def convertable(metadata, object)
            inverse = metadata.inverse_klass
            if inverse.using_object_ids? || object.is_a?(BSON::ObjectId)
              object
            else
              Criterion::Unconvertable.new(object)
            end
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
mongoid-2.1.9 lib/mongoid/relations/builders/referenced/many.rb
mongoid-2.1.8 lib/mongoid/relations/builders/referenced/many.rb
mongoid-2.1.7 lib/mongoid/relations/builders/referenced/many.rb
mongoid-2.1.6 lib/mongoid/relations/builders/referenced/many.rb
mongoid-2.1.5 lib/mongoid/relations/builders/referenced/many.rb
mongoid-2.1.4 lib/mongoid/relations/builders/referenced/many.rb
mongoid-2.1.3 lib/mongoid/relations/builders/referenced/many.rb
mongoid-2.1.2 lib/mongoid/relations/builders/referenced/many.rb
mongoid-2.1.1 lib/mongoid/relations/builders/referenced/many.rb
mongoid-2.1.0 lib/mongoid/relations/builders/referenced/many.rb