{"id":15066831,"url":"https://github.com/suweller/mongoid-autoinc","last_synced_at":"2025-04-05T05:09:55.012Z","repository":{"id":56884327,"uuid":"3137304","full_name":"suweller/mongoid-autoinc","owner":"suweller","description":"⬆ Auto incrementing fields for Mongoid documents","archived":false,"fork":false,"pushed_at":"2024-06-25T10:59:24.000Z","size":100,"stargazers_count":62,"open_issues_count":5,"forks_count":45,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-03-29T04:11:19.809Z","etag":null,"topics":["mongoid","mongoid-plugin","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/suweller.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2012-01-09T14:59:47.000Z","updated_at":"2024-06-25T10:55:24.000Z","dependencies_parsed_at":"2024-03-22T17:48:26.991Z","dependency_job_id":"661bea6e-48b2-4635-8b3b-444514155afb","html_url":"https://github.com/suweller/mongoid-autoinc","commit_stats":{"total_commits":121,"total_committers":8,"mean_commits":15.125,"dds":0.4049586776859504,"last_synced_commit":"30cfe694da15ddfa2709249fdfdd5d22b93e63c1"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suweller%2Fmongoid-autoinc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suweller%2Fmongoid-autoinc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suweller%2Fmongoid-autoinc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suweller%2Fmongoid-autoinc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/suweller","download_url":"https://codeload.github.com/suweller/mongoid-autoinc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247289429,"owners_count":20914464,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mongoid","mongoid-plugin","ruby"],"created_at":"2024-09-25T01:12:50.740Z","updated_at":"2025-04-05T05:09:54.990Z","avatar_url":"https://github.com/suweller.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mongoid-autoinc\n\nA mongoid plugin to add auto incrementing fields to your documents.\n\n[![Inline docs](\nhttp://inch-ci.org/github/suweller/mongoid-autoinc.svg?branch=master\u0026style=flat\n)](http://inch-ci.org/github/suweller/mongoid-autoinc)\n[![Code Climate](\nhttp://img.shields.io/codeclimate/github/suweller/mongoid-autoinc.svg?style=flat\n)](https://codeclimate.com/github/suweller/mongoid-autoinc)\n[![Build Status](\nhttp://img.shields.io/travis/suweller/mongoid-autoinc.svg?style=flat\n)](https://travis-ci.org/suweller/mongoid-autoinc)\n\n## Installation\n\nin gemfile:\n\n``` ruby\ngem 'mongoid-autoinc'\n```\n\nin class:\n\n``` ruby\nrequire 'autoinc'\n```\n\n## Usage\n\n``` ruby\n# app/models/user.rb\nclass User\n  include Mongoid::Document\n  include Mongoid::Autoinc\n  field :name\n  field :number, type: Integer\n\n  increments :number\nend\n\nuser = User.create(name: 'Dr. Percival \"Perry\" Ulysses Cox')\nuser.id # BSON::ObjectId('4d1d150d30f2246bc6000001')\nuser.number # 1\n\nanother_user = User.create(name: 'Bob Kelso')\nanother_user.number # 2\n```\n\n### Scopes\n\nYou can scope on document fields. For example:\n\n``` ruby\nclass PatientFile\n  include Mongoid::Document\n  include Mongoid::Autoinc\n\n  field :name\n  field :number, type: Integer\n\n  increments :number, scope: :patient_id\n\n  belongs_to :patient\n\nend\n```\n\nScope can also be a Proc:\n\n``` ruby\nincrements :number, scope: -\u003e { patient.name }\n```\n\n### Custom Increment Trigger\n\nYou can trigger the assignment of an increment field manually by passing:\n`auto: false` to the increment field.\nThis allows for more flexible assignment of your increment number:\n\n``` ruby\nclass Intern\n  include Mongoid::Document\n  include Mongoid::Autoinc\n\n  field :name\n  field :number\n\n  increments :number, auto: false\n\n  after_save :assign_number_to_jd\n\nprotected\n\n  def assign_number_to_jd\n    assign!(:number) if number.blank? \u0026\u0026 name == 'J.D.'\n  end\n\nend\n```\n\n### Custom Model Name\n\nYou can override the model name used to generate the autoincrement keys. This can be useful\nwhen working with subclasses or namespaces.\n\n``` ruby\nclass Intern\n  include Mongoid::Document\n  include Mongoid::Autoinc\n\n  field :name\n  field :number\n\n  increments :number, model_name =\u003e :foo\nend\n```\n\n### Seeds\n\nYou can use a seed to start the incrementing field at a given value. The first\ndocument created will start at 'seed + 1'.\n\n``` ruby\nclass Vehicle\n  include Mongoid::Document\n  include Mongoid::Autoinc\n\n  field :model\n  field :vin\n\n  increments :vin, seed: 1000\n\nend\n\ncar = Vehicle.new(model: \"Coupe\")\ncar.vin # 1001\n```\n\n### Step\n\nThe step option can be used to specify the amount to increment the field every\ntime a new document is created. If no step is specified, it will increment by\n1.\n\n``` ruby\nclass Ticket\n  include Mongoid::Document\n  include Mongoid::Autoinc\n\n  field :number\n\n  increments :number, step: 5\n\nend\n```\n``` ruby\nfirst_ticket = Ticket.new\nfirst_ticket.number # 5\nsecond_ticket = Ticket.new\nsecond_ticket.number # 10\n```\n\nThe step option can also be a Proc:\n\n``` ruby\nincrements :number, step: -\u003e { 1 + rand(10) }\n```\n\n### Development\n\n```\n$ gem install bundler (if you don't have it)\n$ bundle install\n$ bundle exec spec\n```\n\n## Contributing\n\n* Fork and create a topic branch.\n* Follow the\n  [80beans styleguide](https://gist.github.com/b896eb9e66fc6ab3640d).\n  Basically the [rubystyleguide](https://github.com/bbatsov/ruby-style-guide/)\n  with some minor changes.\n* Submit a pull request\n\n## Contributions\n\nThanks to Johnny Shields (@johnnyshields) for implementing proc support to scopes\nAnd to Marcus Gartner (@mgartner) for implementing the seed functionality\n\nKris Martin (@krismartin) and Johnny Shields (@johnnyshields) for adding the\noverwritten model name feature\n\n## Copyright\n\nSee LICENSE for details\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuweller%2Fmongoid-autoinc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsuweller%2Fmongoid-autoinc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuweller%2Fmongoid-autoinc/lists"}