{"id":13747281,"url":"https://github.com/calebhearth/time_for_a_boolean","last_synced_at":"2025-10-08T11:53:27.746Z","repository":{"id":9801927,"uuid":"11781027","full_name":"calebhearth/time_for_a_boolean","owner":"calebhearth","description":"Back boolean concepts like deleted?, published?, or active? with timestamps","archived":false,"fork":false,"pushed_at":"2024-05-21T22:11:53.000Z","size":40,"stargazers_count":111,"open_issues_count":0,"forks_count":10,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-04-05T02:17:21.758Z","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/calebhearth.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},"funding":{"github":"calebhearth","ko_fi":"calebhearth"}},"created_at":"2013-07-31T02:59:29.000Z","updated_at":"2025-01-05T17:44:15.000Z","dependencies_parsed_at":"2024-05-21T23:25:19.269Z","dependency_job_id":"fc8883ac-371b-434f-a72d-13c04762d3ee","html_url":"https://github.com/calebhearth/time_for_a_boolean","commit_stats":null,"previous_names":["calebthompson/time_for_a_boolean"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calebhearth%2Ftime_for_a_boolean","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calebhearth%2Ftime_for_a_boolean/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calebhearth%2Ftime_for_a_boolean/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calebhearth%2Ftime_for_a_boolean/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/calebhearth","download_url":"https://codeload.github.com/calebhearth/time_for_a_boolean/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247276185,"owners_count":20912288,"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":"2024-08-03T06:01:23.754Z","updated_at":"2025-10-08T11:53:22.709Z","avatar_url":"https://github.com/calebhearth.png","language":"Ruby","readme":"Time for A Boolean\n==================\n\n[![Build Status](https://api.travis-ci.org/calebthompson/time_for_a_boolean.svg?branch=master)](https://travis-ci.org/calebthompson/time_for_a_boolean)\n[![Code Climate](https://codeclimate.com/github/calebthompson/time_for_a_boolean.svg)](https://codeclimate.com/github/calebthompson/time_for_a_boolean)\n[![Coverage Status](https://coveralls.io/repos/calebthompson/time_for_a_boolean/badge.svg)](https://coveralls.io/r/calebthompson/time_for_a_boolean)\n\n\n\u003e Sally: Hey, we need to add a flag to Post\n\n\u003e Jean: What for?\n\n\u003e Sally: Well, we want to let users \"delete\" posts, but not actually lose the\n         data.\n\n\u003e Jean: Sounds reasonable. But what about later, when we have to know _when_ a\n  post was deleted?\n\n\u003e Sally: That's a good point, but if we add a timestamp now we have to write all\n         sorts of methods to keep a nice interface on Post...\n\n\u003e Jean: Time for A Boolean!\n\nWait, what?\n-----------\n\n```\nrails generate migration AddDeletedAtToPosts deleted_at:timestamp\n```\n\n```ruby\nclass Post \u003c ActiveRecord::Base\n  time_for_a_boolean :deleted\n  ...\nend\n```\n\n```ruby\nclass PostsController \u003c ApplicationController\n  def show\n    @post = Post.find(params[:id])\n    if @post.deleted?\n      raise ActiveRecord::RecordNotFound\n    end\n  end\n\n  def destroy\n    post = Post.find(params[:id])\n    post.deleted = true\n    post.save\n    redirect_to posts_url\n  end\nend\n```\n\nYou keep on saying things and I don't get it.\n---------------------------------------------\n\nOkay, let's take a look at what happens.\n\nWhen we call `time_for_a_boolean :deleted` in the Post class definition, several\nmethods are defined:\n\n| Method          | Description\n| --------------- | -----------\n| `Post#deleted`  | `true` if `Post#deleted_at` is set to a time before `Time.current`, `false` otherwise\n| `Post#deleted?` | Alias for `Post#deleted`\n| `Post#deleted=` | Sets the timestamp to `Time.current` if the new value is true, and `nil` otherwise\n\nThese methods allow you to use a timestamp as you would a boolean value in your\napplication.\n\nOkay... why?\n------------\n\n* Audit for when a flag was set. Future you wants this.\n* `COUNT(posts.deleted_at)` gives you the count of deleted posts, which is\n  useful when writing a report. Define and use `Post.deleted.count` when you\n  have Ruby available.\n\nOther Options\n-------------\n\nIf you have a date or time column that does not follow the `attribute_at` convention,\nyou can specify the attribute name:\n\n```\nclass User \u003c ActiveRecord::Base\n  time_for_a_boolean :expires, :expires_on\nend\n```\n\nThis is especially useful when using date only columns.\n","funding_links":["https://github.com/sponsors/calebhearth","https://ko-fi.com/calebhearth"],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcalebhearth%2Ftime_for_a_boolean","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcalebhearth%2Ftime_for_a_boolean","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcalebhearth%2Ftime_for_a_boolean/lists"}