Module: Mongoid::Document

Defined in:
lib/mongoid_auto_increment_id.rb

Constant Summary collapse

ID_FIELD =
'_id'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


58
59
60
61
62
63
64
# File 'lib/mongoid_auto_increment_id.rb', line 58

def self.included(base)
  base.class_eval do
    # define Integer for id field
    Mongoid.register_model(self)
    field :_id, type: Integer, overwrite: true
  end
end

Instance Method Details

#as_documentObject


73
74
75
76
77
78
79
# File 'lib/mongoid_auto_increment_id.rb', line 73

def as_document
  result = super_as_document
  if result[ID_FIELD].blank?
    result[ID_FIELD] = Identity.generate_id(self)
  end
  result
end

#identifyObject

hack id nil when Document.new


67
68
69
70
# File 'lib/mongoid_auto_increment_id.rb', line 67

def identify
  Identity.new(self).create
  nil
end

#super_as_documentObject


72
# File 'lib/mongoid_auto_increment_id.rb', line 72

alias_method :super_as_document, :as_document