Giter Club home page Giter Club logo

auto_increment's Introduction

auto_increment

Build Status Coverage Status Code Climate

auto_increment provides automatic incrementation for a integer or string fields in Rails.

Installation

You can use auto_increment as a gem from Rails 4.2 to Rails 6.1.

To use the gem version, put the following gem requirement in your Gemfile:

gem "auto_increment"

Usage

To work with a auto increment column you used to do something like this in your model:

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:

auto_increment :code

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:

auto_increment :letter, scope: [:account_id, :job_id], model_scope: :in_account, initial: 'C', force: true, lock: false, before: :create

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)

Compatibility

Tested with Rails 6.1.0, 6.0.3.4, 5.2.2.1, 5.1.6.2, 5.0.7.2 and 4.2.11.1 in Ruby 2.6.6.

License

MIT License

auto_increment's People

Contributors

felipediesel avatar michaelachrisco avatar aldesantis avatar abaldwin88 avatar tenshiamd avatar n-rodriguez avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.