Sha256: 47fb2f0d00b59978bb76753347d2c9139379a789327b07e318689da31237fd9f

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

module DocumentNumber
  module Model

    def self.included(base)
      base.send :extend, ClassMethods
    end

    module ClassMethods
      # 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
      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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
document_number-0.9.4 lib/document_number/has_document_number.rb
document_number-0.9.3 lib/document_number/has_document_number.rb