{"id":38542291,"url":"https://github.com/mitigate-dev/periodic_records","last_synced_at":"2026-01-17T07:12:18.722Z","repository":{"id":56887815,"uuid":"43420594","full_name":"mitigate-dev/periodic_records","owner":"mitigate-dev","description":"Support functions for ActiveRecord models with periodic entries","archived":false,"fork":false,"pushed_at":"2024-02-21T13:32:41.000Z","size":41,"stargazers_count":7,"open_issues_count":0,"forks_count":2,"subscribers_count":8,"default_branch":"master","last_synced_at":"2026-01-13T06:20:47.241Z","etag":null,"topics":[],"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/mitigate-dev.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-09-30T08:15:50.000Z","updated_at":"2024-05-29T10:04:10.000Z","dependencies_parsed_at":"2023-01-27T20:16:27.737Z","dependency_job_id":null,"html_url":"https://github.com/mitigate-dev/periodic_records","commit_stats":null,"previous_names":["mak-it/periodic_records"],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/mitigate-dev/periodic_records","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitigate-dev%2Fperiodic_records","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitigate-dev%2Fperiodic_records/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitigate-dev%2Fperiodic_records/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitigate-dev%2Fperiodic_records/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mitigate-dev","download_url":"https://codeload.github.com/mitigate-dev/periodic_records/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitigate-dev%2Fperiodic_records/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28503176,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-17T06:57:29.758Z","status":"ssl_error","status_checked_at":"2026-01-17T06:56:03.931Z","response_time":85,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":[],"created_at":"2026-01-17T07:12:18.617Z","updated_at":"2026-01-17T07:12:18.703Z","avatar_url":"https://github.com/mitigate-dev.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PeriodicRecords\n\nSupport functions for ActiveRecord models with periodic entries.\n\n* Supports periods where the smallest unit is a whole day\n* Adjusts and splits overlapping records\n* Preloads currently active records to avoid N+1 queries\n* Easy querying within history - join returns 0..1 records (no grouping needed)\n  `LEFT JOIN ... ON ... AND \u003cdate\u003e BETWEEN start_at AND end_at`\n\nFor example you have employees table and assignments table that stores all the\nemployment history.\n\nEmployees:\n\n| id | name |\n|:---|:-----|\n| 1  | John |\n\nEmployee assignments:\n\n| id | employee_id | start_at   | end_at     | job_title |\n|:---|:------------|:-----------|:-----------|:----------|\n| 1  | 1           | 2014-01-01 | 9999-01-01 | Developer |\n\nNow John is promoted to \"Senior Developer\" and you create a new employee\nassignment record and this gem will take care of adjusting and splitting\noverlapping records. In this case it will adjust the `end_at` field for the\nprevious assignment.\n\n| id | employee_id | start_at   | end_at     | job_title        |\n|:---|:------------|:-----------|:-----------|:-----------------|\n| 1  | 1           | 2014-01-01 | 2018-05-04 | Developer        |\n| 2  | 1           | 2018-05-05 | 9999-01-01 | Senior Developer |\n\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'periodic_records'\n```\n\nAnd then execute:\n\n```bash\n$ bundle\n```\n\nOr install it yourself as:\n\n```bash\n$ gem install periodic_records\n```\n\n## Preparation\n\nEnsure `start_at` and `end_at` date columns on the model that will have\nperiodic versions.\nInclude `PeriodicRecords::Model` and define `siblings` method:\n\n```ruby\nclass EmployeeAssignment \u003c ActiveRecord::Base\n  include PeriodicRecords::Model\n\n  belongs_to :employee\n\n  def siblings\n    self.class.where(employee_id: employee_id).where.not(id: id)\n  end\nend\n```\n\nInclude `PeriodicRecords::Associations` in the model that has periodic\nassociations, and call `has_periodic`:\n\n```ruby\nclass Employee \u003c ActiveRecord::Base\n  include PeriodicRecords::Associations\n\n  has_many :employee_assignments, inverse_of: :employee\n  has_periodic :employee_assignments, as: :assignments\nend\n```\n\n## Usage\n\nLook up the currently active record with `model.current_association`:\n\n```ruby\nemployee.current_assignment\n```\n\nLook up records for specific date or period\nwith `within_date` and `within_interval`:\n\n```ruby\nemployee.employee_assignments.within_date(Date.tomorrow)\n```\n\n```ruby\nemployee.employee_assignments.within_interval(Date.current.beginning_of_month, Date.current.end_of_month)\n```\n\nLook up records starting with specific date with `from_date`\n\n```ruby\nemployee.employee_assignments.from_date(Date.tomorrow)\n```\n\nPreload currently active records, to avoid N+1 queries on `current_assignment`.\n\n```ruby\nemployees = Employee.all\nEmployee.preload_current_assignments(employees)\nemployees.each do |employee|\n  puts employee.current_assignment.to_s\nend\n```\n\n## Database Constraints\n\nTo avoid inconsistent data in race conditions, you can add database constraint\nthat checks overlapping periods.\n\nPostgres:\n\n```ruby\nclass AddEmployeeAssignmentsOverlappingDatesConstraint \u003c ActiveRecord::Migration\n  def up\n    execute \"CREATE EXTENSION IF NOT EXISTS btree_gist\"\n    execute \u003c\u003c-SQL\n      ALTER TABLE employee_assignments\n      ADD CONSTRAINT employee_assignments_overlapping_dates\n      EXCLUDE USING GIST(\n        employee_id WITH =,\n        DATERANGE(start_at, end_at, '[]') WITH \u0026\u0026\n      )\n    SQL\n  end\n\n  def down\n    execute \u003c\u003c-SQL.squish\n      ALTER TABLE employee_assignments\n      DROP CONSTRAINT employee_assignments_overlapping_dates\n    SQL\n  end\nend\n```\n\n## Time sensitive records\n\nIf you need your records to be split with time component, then set `start_at` and `end_at` columns to `datetime` type.\n\nUse `TSRANGE` instead of `DATERANGE` when creating database constraint.\n\n## Gapless records\n\nIf you want to avoid gaps between records, you can include also `PeriodicRecords::Gapless`.\n\n```ruby\nclass EmployeeAssignment \u003c ActiveRecord::Base\n  include PeriodicRecords::Model\n  include PeriodicRecords::Gapless\n\n  belongs_to :employee\n\n  def siblings\n    self.class.where(employee_id: employee_id).where.not(id: id)\n  end\nend\n```\n\nExample:\n\n| id | employee_id | start_at   | end_at     | job_title        |\n|:---|:------------|:-----------|:-----------|:-----------------|\n| 1  | 1           | 0001-01-01 | 2018-01-15 | Junior Developer |\n| 2  | 1           | 2018-01-16 | 2018-02-15 | Developer        |\n| 3  | 1           | 2018-02-16 | 9999-01-01 | Senior Developer |\n\nIf you update #2 from `2018-01-16 - 2018-02-15` to `2018-01-20 - 2018-02-10`, it\nwill also adjust end at for #1 and start at for #3 to avoid gaps between records.\n\nAfter (with `Gapless`):\n\n| id | employee_id | start_at   | end_at     | job_title        |\n|:---|:------------|:-----------|:-----------|:-----------------|\n| 1  | 1           | 0001-01-01 | 2018-01-19 | Junior Developer |\n| 2  | 1           | 2018-01-20 | 2018-02-10 | Developer        |\n| 3  | 1           | 2018-02-11 | 9999-01-01 | Senior Developer |\n\nAfter (without `Gapless`):\n\n| id | employee_id | start_at   | end_at     | job_title        |\n|:---|:------------|:-----------|:-----------|:-----------------|\n| 1  | 1           | 0001-01-01 | 2018-01-15 | Junior Developer |\n| 2  | 1           | 2018-01-20 | 2018-02-10 | Developer        |\n| 3  | 1           | 2018-02-16 | 9999-01-01 | Senior Developer |\n\nIf you delete #2 then it will adjust end at for #1.\n\nYou will not be able to delete entry that is at the beginning (#1) or at the end (#3).\n\nYou will not be able to adjust start at for the beginning entry (#1).\n\nYou will not be able to adjust end at for the ending entry (#3).\n\nFor more examples see [gapless_spec.rb](spec/periodic_records/gapless_spec.rb).\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies.\nThen, run `bin/console` for an interactive prompt that will allow you to\nexperiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`.\nTo release a new version, update the version number in `version.rb`,\nand then run `bundle exec rake release` to create a git tag for the version,\npush git commits and tags, and push the `.gem` file\nto [rubygems.org](https://rubygems.org).\n\n## Contributing\n\n1. Fork it ( https://github.com/mak-it/periodic_records/fork )\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmitigate-dev%2Fperiodic_records","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmitigate-dev%2Fperiodic_records","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmitigate-dev%2Fperiodic_records/lists"}