https://github.com/jbox-web/auto_increment
Automaticaly increments a field in ActiveRecord, easy ;)
https://github.com/jbox-web/auto_increment
activerecord autoincrement rails ruby
Last synced: 3 months ago
JSON representation
Automaticaly increments a field in ActiveRecord, easy ;)
- Host: GitHub
- URL: https://github.com/jbox-web/auto_increment
- Owner: jbox-web
- License: mit
- Created: 2019-02-26T07:26:37.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-09-06T16:06:26.000Z (10 months ago)
- Last Synced: 2024-09-06T19:13:41.791Z (10 months ago)
- Topics: activerecord, autoincrement, rails, ruby
- Language: Ruby
- Homepage:
- Size: 208 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# AutoIncrement
[](https://github.com/jbox-web/auto_increment/blob/master/LICENSE)
[](https://github.com/jbox-web/auto_increment/releases/latest)
[](https://github.com/jbox-web/auto_increment/actions)
[](https://codeclimate.com/github/jbox-web/auto_increment)
[](https://codeclimate.com/github/jbox-web/auto_increment/coverage)AutoIncrement provides automatic incrementation for a integer or string fields in Rails.
## Installation
Put this in your `Gemfile` :
```ruby
git_source(:github){ |repo_name| "https://github.com/#{repo_name}.git" }gem 'auto_increment', github: 'jbox-web/auto_increment', tag: '1.8.0'
```then run `bundle install`.
## Usage
To work with a auto increment column you used to do something like this in your model:
```ruby
before_create :set_code
def set_code
max_code = Operation.maximum(:code)
self.code = max_code.to_i + 1
end
```Looks fine, but not when you need to do it over and over again. In fact auto_increment does it under the cover.
All you need to do is this:
```ruby
class Project < ApplicationRecord
auto_increment :code
end
```And your code field will be incremented :)
## Customizing
So you have a different column or need a scope. auto_increment provides options. You can use it like this:
```ruby
class Project < ApplicationRecord
auto_increment :letter, scope: [:account_id, :job_id], model_scope: :in_account, initial: 'C', force: true, lock: false, before: :create
end
```First argument is the column that will be incremented. Can be integer or string.
* **scope:** you can define columns that will be scoped and you can use as many as you want (default: nil)
* **model_scope:** you can define model scopes that will be executed and you can use as many as you want (default: nil)
* **initial:** initial value of column (default: 1)
* **force:** you can set a value before create and auto_increment will not change that, but if you do want this, set force to true (default: false)
* **lock:** you can set a lock on the max query. (default: false)
* **before:** you can choose a different callback to be used (:create, :save, :validation) (default: create)