GithubHelp home page GithubHelp logo

aileron-inc / mongoid-autoinc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from suweller/mongoid-autoinc

0.0 3.0 0.0 92 KB

⬆ Auto incrementing fields for Mongoid documents

License: MIT License

Ruby 100.00%

mongoid-autoinc's Introduction

mongoid-autoinc

A mongoid plugin to add auto incrementing fields to your documents.

Inline docs Code Climate Build Status

Installation

in gemfile:

gem 'mongoid-autoinc'

in class:

require 'autoinc'

Usage

# app/models/user.rb
class User
  include Mongoid::Document
  include Mongoid::Autoinc
  field :name
  field :number, type: Integer

  increments :number
end

user = User.create(name: 'Dr. Percival "Perry" Ulysses Cox')
user.id # BSON::ObjectId('4d1d150d30f2246bc6000001')
user.number # 1

another_user = User.create(name: 'Bob Kelso')
another_user.number # 2

Scopes

You can scope on document fields. For example:

class PatientFile
  include Mongoid::Document
  include Mongoid::Autoinc

  field :name
  field :number, type: Integer

  increments :number, scope: :patient_id

  belongs_to :patient

end

Scope can also be a Proc:

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:

class Intern
  include Mongoid::Document
  include Mongoid::Autoinc

  field :name
  field :number

  increments :number, auto: false

  after_save :assign_number_to_jd

protected

  def assign_number_to_jd
    assign!(:number) if number.blank? && name == 'J.D.'
  end

end

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.

class Intern
  include Mongoid::Document
  include Mongoid::Autoinc

  field :name
  field :number

  increments :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'.

class Vehicle
  include Mongoid::Document
  include Mongoid::Autoinc

  field :model
  field :vin

  increments :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.

class Ticket
  include Mongoid::Document
  include Mongoid::Autoinc

  field :number

  increments :number, step: 5

end
first_ticket = Ticket.new
first_ticket.number # 5
second_ticket = Ticket.new
second_ticket.number # 10

The step option can also be a Proc:

increments :number, step: -> { 1 + rand(10) }

Development

$ gem install bundler (if you don't have it)
$ bundle install
$ bundle exec spec

Contributing

Contributions

Thanks to Johnny Shields (@johnnyshields) for implementing proc support to scopes And to Marcus Gartner (@mgartner) for implementing the seed functionality

Kris Martin (@krismartin) and Johnny Shields (@johnnyshields) for adding the overwritten model name feature

Copyright

See LICENSE for details

mongoid-autoinc's People

Contributors

aileron avatar andreale avatar dylanbromby avatar jacobvosmaer avatar johnnyshields avatar matsimitsu avatar suweller avatar

Watchers

 avatar  avatar  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.