{"id":13880107,"url":"https://github.com/fnando/defaults","last_synced_at":"2025-04-16T22:50:27.043Z","repository":{"id":416138,"uuid":"35720","full_name":"fnando/defaults","owner":"fnando","description":"Add default value for ActiveRecord attributes","archived":false,"fork":false,"pushed_at":"2023-09-05T08:51:27.000Z","size":60,"stargazers_count":18,"open_issues_count":1,"forks_count":8,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-29T05:41:40.795Z","etag":null,"topics":["activerecord","default-value","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/fnando.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":["fnando"],"custom":["https://paypal.me/fnando/🍕"]}},"created_at":"2008-07-21T01:24:10.000Z","updated_at":"2023-06-16T01:02:25.000Z","dependencies_parsed_at":"2024-01-13T20:58:04.431Z","dependency_job_id":"64983d33-6001-484b-9614-eb18410b7f81","html_url":"https://github.com/fnando/defaults","commit_stats":{"total_commits":35,"total_committers":3,"mean_commits":"11.666666666666666","dds":0.4,"last_synced_commit":"76678de94f0917734d0e46d3e74859aa7e52ef0e"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fdefaults","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fdefaults/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fdefaults/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fdefaults/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fnando","download_url":"https://codeload.github.com/fnando/defaults/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249284458,"owners_count":21243964,"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":["activerecord","default-value","ruby"],"created_at":"2024-08-06T08:02:47.399Z","updated_at":"2025-04-16T22:50:27.019Z","avatar_url":"https://github.com/fnando.png","language":"Ruby","readme":"# Defaults\n\n[![Tests](https://github.com/fnando/defaults/workflows/ruby-tests/badge.svg)](https://github.com/fnando/defaults)\n[![Gem](https://img.shields.io/gem/v/defaults.svg)](https://rubygems.org/gems/defaults)\n[![Gem](https://img.shields.io/gem/dt/defaults.svg)](https://rubygems.org/gems/defaults)\n\n## Instalation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem \"defaults\"\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install defaults\n\n## Usage\n\nHere are the rules:\n\n1. Assigned attributes have precedence over default values\n2. Default values have precedence over database defaults.\n\nAdd the method call `defaults` to your model.\n\n```ruby\nclass Page \u003c ActiveRecord::Base\n  defaults title: \"New page\",\n           body: \"Put your text here\"\nend\n```\n\nAttributes will be set only if it's a new record and the attribute is blank.\n\nRetrieve the default attribute with the `default_for` instance method:\n\n```ruby\n@page.default_for(:title)\n```\n\nYou can pass callables (any objects that respond to `.call()` or\n`.call(record)`) as attribute:\n\n```ruby\nclass Expiration\n  def self.call\n    Time.now\n  end\nend\n\nclass Checksum\n  def self.call(user)\n    Digest::SHA1.hexdigest(user.name.to_s, user.email.to_s)\n  end\nend\n\nclass User \u003c ApplicationRecord\n  defaults checksum: Checksum,\n           expires_at: -\u003e { Time.now }\nend\n```\n\nYou can override the default attributes as follow:\n\n```ruby\nPage.default_values = {\n  title: \"Here's your new page\",\n  body: \"Write your page text\"\n}\n```\n\n## Maintainer\n\n- [Nando Vieira](https://github.com/fnando)\n\n## Contributors\n\n- \u003chttps://github.com/fnando/defaults/contributors\u003e\n\n## Contributing\n\nFor more details about how to contribute, please read\n\u003chttps://github.com/fnando/defaults/blob/main/CONTRIBUTING.md\u003e.\n\n## License\n\nThe gem is available as open source under the terms of the\n[MIT License](https://opensource.org/licenses/MIT). A copy of the license can be\nfound at \u003chttps://github.com/fnando/defaults/blob/main/LICENSE.md\u003e.\n\n## Code of Conduct\n\nEveryone interacting in the defaults project's codebases, issue trackers, chat\nrooms and mailing lists is expected to follow the\n[code of conduct](https://github.com/fnando/defaults/blob/main/CODE_OF_CONDUCT.md).\n","funding_links":["https://github.com/sponsors/fnando","https://paypal.me/fnando/🍕"],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffnando%2Fdefaults","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffnando%2Fdefaults","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffnando%2Fdefaults/lists"}