Module: MongoidAutoInc::ClassMethods

Defined in:
lib/mongoid_auto_inc.rb

Instance Method Summary collapse

Instance Method Details

#auto_increment(name, options = {}) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/mongoid_auto_inc.rb', line 7

def auto_increment(name, options={})
  field name, :type => Integer
  seq_name = "#{self.name.downcase}_#{name}"
  incrementor = MongoidAutoInc::Incrementor.new(options) 

  before_create { self.send("#{name}=", incrementor[seq_name].inc) unless self[name.to_sym].present? }
end