Sha256: ca6369473f116f37896af7d201f0d6d5a3082d3cf3b8c97a7815b15947af7553

Contents?: true

Size: 958 Bytes

Versions: 11

Compression:

Stored size: 958 Bytes

Contents

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

          # This builder either takes a foreign key and queries for the
          # object or a document, where it will just return it.
          #
          # @example Build the document.
          #   Builder.new(meta, attrs).build
          #
          # @param [ String ] type The type of document to query for.
          #
          # @return [ Document ] A single document.
          def build(type = nil)
            return object unless query?
            if object.is_a?(Hash)
              return Mongoid::Factory.build(metadata.klass, object)
            end
            begin
              (type ? type.constantize : metadata.klass).find(object)
            rescue Errors::DocumentNotFound
              return nil
            end
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
mongoid-2.0.1 lib/mongoid/relations/builders/referenced/in.rb
mongoid-2.0.0 lib/mongoid/relations/builders/referenced/in.rb
mongoid-2.0.0.rc.8 lib/mongoid/relations/builders/referenced/in.rb
mongoid-2.0.0.rc.7 lib/mongoid/relations/builders/referenced/in.rb
stonegao-mongoid-2.0.0.rc.6 lib/mongoid/relations/builders/referenced/in.rb
mongoid-2.0.0.rc.6 lib/mongoid/relations/builders/referenced/in.rb
mongoid-2.0.0.rc.5 lib/mongoid/relations/builders/referenced/in.rb
mongoid-2.0.0.rc.4 lib/mongoid/relations/builders/referenced/in.rb
mongoid-2.0.0.rc.3 lib/mongoid/relations/builders/referenced/in.rb
mongoid-2.0.0.rc.2 lib/mongoid/relations/builders/referenced/in.rb
mongoid-2.0.0.rc.1 lib/mongoid/relations/builders/referenced/in.rb