Class: Mongokit::Models::AutoIncrementCounter

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
lib/mongokit/models/auto_increment_counter.rb

Class Method Summary collapse

Class Method Details

.current_counter(options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/mongokit/models/auto_increment_counter.rb', line 28

def self.current_counter(options)
  record = Models::AutoIncrementCounter.find_by({
    counter_model_name: options[:model].collection_name,
    counter_field: options[:attribute]
  })

  return nil if record.nil?
  return record.counter if options[:pattern].nil?
  return Formater.new.format(record.counter, options)
end

.find_or_create_with_seed(options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mongokit/models/auto_increment_counter.rb', line 15

def self.find_or_create_with_seed(options)
  record = find_or_initialize_by({
    counter_model_name: options[:model].collection_name,
    counter_field: options[:attribute]
  })

  if record.new_record?
    record.counter = options[:start] > 0 ? options[:start] - 1 : options[:start]
  end

  record.save
end