Sha256: b4599dbb18215ac0e2318e396b499b7c36f99028107f5bd352079f3ef833781e

Contents?: true

Size: 677 Bytes

Versions: 1

Compression:

Stored size: 677 Bytes

Contents

require "active_record/base"

module DocumentNumber
  class Numerator
    # Gets next number for document
    def self.next_number(document, options)
      DocumentNumber.transaction(requires_new: true) do
        if ActiveRecord::VERSION::MAJOR < 4
          document_number = DocumentNumber.lock(true).find_or_initialize_by_document(document)
        else
          document_number = DocumentNumber.lock(true).find_or_initialize_by(:document => document)
        end

        number = document_number.number == 1 ? options[:start] : document_number.number

        document_number.number = number + 1
        document_number.save!

        number
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
document_number-0.9.5 lib/document_number/numerator.rb