Document Number lets you automacally assign number sequences for your rails models.
-
Add Document Number to your
Gemfile
gem 'document_number', '~> 0.10.0'
-
Generate a migration which will add a
document_numbers
table to your databasebundle exec rails generate document_numbers:install
-
Run the migration
bundle exec rake db:migrate
-
Declare
has_document_number
in your model to automatic document number assignment:
class Invoice < ActiveRecord::Base
has_document_number
end
Document Number has several configuration options, which you can configure, both globally and on a per-model basis:
# config/initializers/document_number.rb
DocumentNumber.prefix = 'server/'
Or alternatively:
class Invoice < ActiveRecord::Base
has_document_number prefix: 'server/', start: 5000
end
Available options:
Option | Description |
---|---|
column | The column name to update. Default value is :number |
prefix | The prefix for number |
start | The start value for number. Default is 1 |
Document Number automacally assigns number when you saving your model. If you want to create number just after initialization use :with_number
attribute:
Invoice.new with_number: true
If you want to get a bunch of numbers use model method get_numbers
:
irb(main):001>Invoice.get_numbers(3)
=> ['server/1', 'server/2', 'server/3']
Feel free to add any new features or fix bugs by creating a new pull request to this repository
bundle exec rspec