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
33
# File 'lib/document_number/has_document_number.rb', line 20

def has_document_number(options = {})
  options.reverse_merge! :column => :number, :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)
    prefix = options[:prefix].present? ? options[:prefix] : ::DocumentNumber.configuration.prefix
    write_attribute options[:column], "#{prefix}#{number}"
  end
end