{"id":26091660,"url":"https://github.com/intercom/state_of_the_nation","last_synced_at":"2025-04-12T06:43:27.372Z","repository":{"id":3363042,"uuid":"49068956","full_name":"intercom/state_of_the_nation","owner":"intercom","description":"An easy way to model state changing over time with ActiveRecord.","archived":false,"fork":false,"pushed_at":"2023-08-01T02:23:00.000Z","size":62,"stargazers_count":25,"open_issues_count":7,"forks_count":6,"subscribers_count":145,"default_branch":"master","last_synced_at":"2025-04-12T06:43:21.798Z","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":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/intercom.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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":"2016-01-05T13:53:53.000Z","updated_at":"2023-09-26T11:43:40.000Z","dependencies_parsed_at":"2025-03-09T10:24:14.525Z","dependency_job_id":"694a590d-04d5-4c0a-995c-9856adcaf877","html_url":"https://github.com/intercom/state_of_the_nation","commit_stats":{"total_commits":51,"total_committers":9,"mean_commits":5.666666666666667,"dds":0.6274509803921569,"last_synced_commit":"0b374b29c9fe4b972234d5c9e487efc2c92b6c66"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/intercom%2Fstate_of_the_nation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/intercom%2Fstate_of_the_nation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/intercom%2Fstate_of_the_nation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/intercom%2Fstate_of_the_nation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/intercom","download_url":"https://codeload.github.com/intercom/state_of_the_nation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248530610,"owners_count":21119592,"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":[],"created_at":"2025-03-09T10:22:45.150Z","updated_at":"2025-04-12T06:43:27.344Z","avatar_url":"https://github.com/intercom.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# StateOfTheNation\n\n[![Build Status](https://travis-ci.org/intercom/state_of_the_nation.svg?branch=master)](https://travis-ci.org/intercom/state_of_the_nation)\n\nStateOfTheNation helps model data whose _active_ state changes over time. It provides out-of-the-box query methods to locate the record or records active at any moment. Optionally, it also enforces _uniquely_ active constraints at the application level – ensuring that only one record in a collection is active at once.\n\n## Example\n\nTake elected officials in the US Government: multiple Senators are in office (i.e. active) at any point in time, but there's only one President.\n\n```ruby\nclass Country \u003c ActiveRecord::Base\n  include StateOfTheNation\n  has_many :presidents\n  has_many :senators\n\n  has_active :senators\n  has_uniquely_active :president\nend\n\nclass President \u003c ActiveRecord::Base\n  belongs_to :country\n  considered_active.from(:entered_office_at).until(:left_office_at)\nend\n\nclass Senator \u003c ActiveRecord::Base\n  belongs_to :country\n  considered_active.from(:entered_office_at).until(:left_office_at)\nend\n```\n\nWith this collection of models we can easy record and query the list of elected officials at any point in time, and be confident that any new records that we create don't collide.\n\n```ruby\n\nusa = Country.create(name: \"United States of America\")\nobama = usa.presidents.create!(name: \"Barack Obama\", entered_office_at: Date.new(2009, 1, 20))\n\nusa.senators.create!(name: \"Ron Wyden\", entered_office_at: Date.new(1996, 2, 6))\nusa.senators.create!(name: \"Barbara Boxer\", entered_office_at: Date.new(1993, 1, 3))\nusa.senators.create!(name: \"Alan Cranston\", entered_office_at: Date.new(1969, 1, 3), left_office_at: Date.new(1993, 1, 3))\n\nusa.active_president(Date.new(2015)) \n# =\u003e President(id: 1, name: \"Barack Obama\", …)\n\nobama.active?\n#=\u003e true\n\nusa.active_senators(Date.new(2015))\n# =\u003e [Senator(id: 1, name: \"Ron Wyden\", …), Senator(id: 2, name: \"Barbara Boxer\", …)]\n\nusa.presidents.create!(name: \"Mitt Romney\", entered_office_at: Date.new(2013, 1, 20))\n# =\u003e StateOfTheNation::ConflictError\n```\n## IdentityCache Support\n\nStateOfTheNation optionally supports fetching records through [IdentityCache](https://github.com/Shopify/identity_cache)  instead of reading directly from the database. \n\nFor example if the `Country` model uses IdentityCache to cache the `has_many` relationship to `President`, you can instruct StateOfTheNation to fetch from the cache by calling `.with_identity_cache` on your `has_active` or `has_uniquely_active` definitions:\n\n```ruby\nclass Country\n  include IdentityCache\n  include StateOfTheNation\n  \n  has_many(:presidents)\n  cache_has_many(:presidents)\n  has_uniquely_active(:president).with_identity_cache\nend\n```\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'state_of_the_nation'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install state_of_the_nation\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/intercom/state_of_the_nation. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org/) code of conduct.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fintercom%2Fstate_of_the_nation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fintercom%2Fstate_of_the_nation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fintercom%2Fstate_of_the_nation/lists"}