Sha256: 36b6b31c30d81c3109050a90f538821c01d12041625a7e8d12430862ba813d1e

Contents?: true

Size: 1.95 KB

Versions: 8

Compression:

Stored size: 1.95 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module Keys
    extend ActiveSupport::Concern
    included do
      cattr_accessor :primary_key, :_identity
      self._identity = { :type => BSON::ObjectID }

      delegate \
        :_id_type,
        :primary_key,
        :using_object_ids?, :to => "self.class"
    end

    module ClassMethods #:nodoc:

      # Convenience method for returning the type of the id for this class.
      #
      # Example:
      #
      # <tt>Person._id_type</tt>
      #
      # Returns:
      #
      # The type of the id.
      def _id_type
        _identity[:type]
      end

      # Used for telling Mongoid on a per model basis whether to override the
      # default +BSON::ObjectID+ and use a different type. This will be
      # expanded in the future for requiring a PkFactory if the type is not a
      # +BSON::ObjectID+ or +String+.
      #
      # Example:
      #
      #   class Person
      #     include Mongoid::Document
      #     identity :type => String
      #   end
      def identity(options = {})
        self._identity = options
      end

      # Defines the field that will be used for the id of this +Document+. This
      # set the id of this +Document+ before save to a parameterized version of
      # the field that was supplied. This is good for use for readable URLS in
      # web applications.
      #
      # Example:
      #
      #   class Person
      #     include Mongoid::Document
      #     key :first_name, :last_name
      #   end
      def key(*fields)
        self.primary_key = fields
        identity(:type => String)
        set_callback :save, :before, :identify
      end

      # Convenience method for determining if we are using +BSON::ObjectIDs+ as
      # our id.
      #
      # Example:
      #
      # <tt>person.using_object_ids?</tt>
      #
      # Returns:
      #
      # true if we are using BSON::ObjectIDs
      def using_object_ids?
        _id_type == BSON::ObjectID
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mongoid-2.0.0.beta.18 lib/mongoid/keys.rb
mongoid-2.0.0.beta.17 lib/mongoid/keys.rb
mongoid-2.0.0.beta.16 lib/mongoid/keys.rb
mongoid-2.0.0.beta.15 lib/mongoid/keys.rb
mongoid-2.0.0.beta.14 lib/mongoid/keys.rb
mongoid-2.0.0.beta.13 lib/mongoid/keys.rb
mongoid-2.0.0.beta.11 lib/mongoid/keys.rb
mongoid-2.0.0.beta.12 lib/mongoid/keys.rb