Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/suweller/mongoid-autoinc
⬆ Auto incrementing fields for Mongoid documents
https://github.com/suweller/mongoid-autoinc
mongoid mongoid-plugin ruby
Last synced: 20 days ago
JSON representation
⬆ Auto incrementing fields for Mongoid documents
- Host: GitHub
- URL: https://github.com/suweller/mongoid-autoinc
- Owner: suweller
- License: mit
- Created: 2012-01-09T14:59:47.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2024-06-25T10:59:24.000Z (5 months ago)
- Last Synced: 2024-10-13T02:21:04.628Z (about 1 month ago)
- Topics: mongoid, mongoid-plugin, ruby
- Language: Ruby
- Homepage:
- Size: 97.7 KB
- Stars: 62
- Watchers: 8
- Forks: 45
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# mongoid-autoinc
A mongoid plugin to add auto incrementing fields to your documents.
[![Inline docs](
http://inch-ci.org/github/suweller/mongoid-autoinc.svg?branch=master&style=flat
)](http://inch-ci.org/github/suweller/mongoid-autoinc)
[![Code Climate](
http://img.shields.io/codeclimate/github/suweller/mongoid-autoinc.svg?style=flat
)](https://codeclimate.com/github/suweller/mongoid-autoinc)
[![Build Status](
http://img.shields.io/travis/suweller/mongoid-autoinc.svg?style=flat
)](https://travis-ci.org/suweller/mongoid-autoinc)## Installation
in gemfile:
``` ruby
gem 'mongoid-autoinc'
```in class:
``` ruby
require 'autoinc'
```## Usage
``` ruby
# app/models/user.rb
class User
include Mongoid::Document
include Mongoid::Autoinc
field :name
field :number, type: Integerincrements :number
enduser = User.create(name: 'Dr. Percival "Perry" Ulysses Cox')
user.id # BSON::ObjectId('4d1d150d30f2246bc6000001')
user.number # 1another_user = User.create(name: 'Bob Kelso')
another_user.number # 2
```### Scopes
You can scope on document fields. For example:
``` ruby
class PatientFile
include Mongoid::Document
include Mongoid::Autoincfield :name
field :number, type: Integerincrements :number, scope: :patient_id
belongs_to :patient
end
```Scope can also be a Proc:
``` ruby
increments :number, scope: -> { patient.name }
```### Custom Increment Trigger
You can trigger the assignment of an increment field manually by passing:
`auto: false` to the increment field.
This allows for more flexible assignment of your increment number:``` ruby
class Intern
include Mongoid::Document
include Mongoid::Autoincfield :name
field :numberincrements :number, auto: false
after_save :assign_number_to_jd
protected
def assign_number_to_jd
assign!(:number) if number.blank? && name == 'J.D.'
endend
```### Custom Model Name
You can override the model name used to generate the autoincrement keys. This can be useful
when working with subclasses or namespaces.``` ruby
class Intern
include Mongoid::Document
include Mongoid::Autoincfield :name
field :numberincrements :number, model_name => :foo
end
```### Seeds
You can use a seed to start the incrementing field at a given value. The first
document created will start at 'seed + 1'.``` ruby
class Vehicle
include Mongoid::Document
include Mongoid::Autoincfield :model
field :vinincrements :vin, seed: 1000
end
car = Vehicle.new(model: "Coupe")
car.vin # 1001
```### Step
The step option can be used to specify the amount to increment the field every
time a new document is created. If no step is specified, it will increment by
1.``` ruby
class Ticket
include Mongoid::Document
include Mongoid::Autoincfield :number
increments :number, step: 5
end
```
``` ruby
first_ticket = Ticket.new
first_ticket.number # 5
second_ticket = Ticket.new
second_ticket.number # 10
```The step option can also be a Proc:
``` ruby
increments :number, step: -> { 1 + rand(10) }
```### Development
```
$ gem install bundler (if you don't have it)
$ bundle install
$ bundle exec spec
```## Contributing
* Fork and create a topic branch.
* Follow the
[80beans styleguide](https://gist.github.com/b896eb9e66fc6ab3640d).
Basically the [rubystyleguide](https://github.com/bbatsov/ruby-style-guide/)
with some minor changes.
* Submit a pull request## Contributions
Thanks to Johnny Shields (@johnnyshields) for implementing proc support to scopes
And to Marcus Gartner (@mgartner) for implementing the seed functionalityKris Martin (@krismartin) and Johnny Shields (@johnnyshields) for adding the
overwritten model name feature## Copyright
See LICENSE for details