Module: DocumentNumber::Model::ClassMethods

Defined in:
lib/document_number/has_document_number.rb

Instance Method Summary collapse

Instance Method Details

#has_document_number(options = {}) ⇒ Object

Declare this in your model to automatic document number assignment

Usage: class Invoice < ActiveRecord::Base

has_document_number

end

Options: :column the column name to update. Default value is ‘:number`. :prefix the prefix for number. :start the start value for number



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/document_number/has_document_number.rb', line 20

def has_document_number(options = {})
  options.reverse_merge! column: :number, prefix: '', start: 1

  method_name = "auto_increment_#{options[:column]}"

  before_create method_name

  define_method method_name do
    return if send(options[:column]).present?
    number = Numerator.next_number(self, options)
    write_attribute options[:column], "#{options[:prefix]}#{number}"
  end
end