{"id":13878759,"url":"https://github.com/bsm/attribute-defaults","last_synced_at":"2025-04-09T12:05:28.531Z","repository":{"id":1071266,"uuid":"912198","full_name":"bsm/attribute-defaults","owner":"bsm","description":"Simple ActiveRecord plugin that allows to specify default values for attributes","archived":false,"fork":false,"pushed_at":"2024-08-01T23:54:26.000Z","size":50,"stargazers_count":52,"open_issues_count":2,"forks_count":6,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-02T09:07:16.286Z","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/bsm.png","metadata":{"files":{"readme":"README.rdoc","changelog":null,"contributing":null,"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":"2010-09-15T10:07:52.000Z","updated_at":"2022-11-06T22:54:33.000Z","dependencies_parsed_at":"2024-11-13T05:01:03.226Z","dependency_job_id":"f401f970-ba81-4fd9-99d4-2d4461f66cbc","html_url":"https://github.com/bsm/attribute-defaults","commit_stats":{"total_commits":45,"total_committers":6,"mean_commits":7.5,"dds":"0.24444444444444446","last_synced_commit":"8557448bb22446ab88f29acf05be04b3c66e5284"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsm%2Fattribute-defaults","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsm%2Fattribute-defaults/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsm%2Fattribute-defaults/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsm%2Fattribute-defaults/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bsm","download_url":"https://codeload.github.com/bsm/attribute-defaults/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248036063,"owners_count":21037092,"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-06T08:01:59.043Z","updated_at":"2025-04-09T12:05:28.503Z","avatar_url":"https://github.com/bsm.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"= Attribute Defaults (attribute-defaults)\n\n{\u003cimg src=\"https://travis-ci.org/bsm/attribute-defaults.png?branch=master\" alt=\"Build Status\" /\u003e}[https://travis-ci.org/bsm/attribute-defaults]\n\nSimple ActiveRecord plugin that allows to specify default values for attributes.\n\n== Installation\n\nJust add \u003ctt\u003egem 'attribute-defaults'\u003c/tt\u003e to your Gemfile.\n\nAlternatively, run \u003ctt\u003esudo gem install attribute-defaults\u003c/tt\u003e and add\n\u003ctt\u003erequire 'attribute_defaults'\u003c/tt\u003e to your app.\n\n== Examples\n\nFirst, a simple case ...\n\n  class Foo \u003c ActiveRecord::Base\n    attr_default :age, 18\n    attr_default :last_seen do\n      Time.now\n    end\n  end\n  Foo.new # =\u003e age: 18, last_seen: '2010-09-15 10:30'\n\n... or the same via mass-definition ...\n\n  class Foo \u003c ActiveRecord::Base\n    attr_defaults :age =\u003e 18, :last_seen =\u003e lambda { Time.now }\n  end\n  Foo.new # =\u003e age: 18, last_seen: '2010-09-15 10:30'\n\nIt doesn't override values that are already set ...\n\n  class Foo \u003c ActiveRecord::Base\n    attr_defaults :age =\u003e 18\n  end\n  Foo.new(:age =\u003e 25) # =\u003e age: 25\n\n... but allows you to set your own conditions e.g. blank? ...\n\n  class Foo \u003c ActiveRecord::Base\n    attr_defaults  :numbers =\u003e { :default =\u003e [1], :if =\u003e :blank? }\n  end\n  Foo.new(:numbers =\u003e nil) # =\u003e numbers: [1]\n  Foo.new(:numbers =\u003e [])  # =\u003e numbers: [1]\n  Foo.new(:numbers =\u003e [2]) # =\u003e numbers: [2]\n\n... and it respects protected attributes ...\n\n  class Foo \u003c ActiveRecord::Base\n    attr_protected :age\n    attr_defaults  :age =\u003e 18\n  end\n  Foo.new(:age =\u003e 25) # =\u003e age: 18\n\nWhen attributes aren't database columns ...\n\n  class Foo \u003c ActiveRecord::Base\n    attr_accessor  :birth_year\n    attr_defaults  :birth_year =\u003e lambda {|r| r.age ? Time.now.year - r.age : nil }\n  end\n  Foo.new(:age =\u003e 30) # =\u003e age: 30, birth_year: 1980\n\nIt works with persisted records ...\n\n  # DB table 'foos': [{id: 1, age: NULL}, {id: 2, age: 25}]\n  class Foo \u003c ActiveRecord::Base\n    attr_defaults  :age =\u003e 18\n  end\n  Foo.all # =\u003e [{id: 1, age: 18}, {id: 2, age: 25}]\n\n... or not ...\n\n  class Foo \u003c ActiveRecord::Base\n    attr_defaults  :age =\u003e { :default =\u003e 18, :persisted =\u003e false }\n  end\n  Foo.all # =\u003e [{id: 1, age: nil}, {id: 2, age: 25}]\n\nTo specify a Hash as a default value, use ...\n\n  class Foo \u003c ActiveRecord::Base\n    attr_accessor  :some_hash\n    attr_default   :some_hash, :default =\u003e {}\n  end\n  Foo.new.some_hash # =\u003e {}\n\n== Acknowledgements\n\nInspired by the default_value_for plugin\nhttp://github.com/FooBarWidget/default_value_for\n\n= LICENSE\n\n(The MIT License)\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n'Software'), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbsm%2Fattribute-defaults","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbsm%2Fattribute-defaults","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbsm%2Fattribute-defaults/lists"}