{"id":13483656,"url":"https://github.com/cgriego/active_attr","last_synced_at":"2025-04-23T20:54:03.684Z","repository":{"id":45377885,"uuid":"2484608","full_name":"cgriego/active_attr","owner":"cgriego","description":"What ActiveModel left out","archived":false,"fork":false,"pushed_at":"2024-12-26T22:50:34.000Z","size":501,"stargazers_count":1198,"open_issues_count":49,"forks_count":90,"subscribers_count":18,"default_branch":"master","last_synced_at":"2025-04-23T20:53:58.824Z","etag":null,"topics":["activemodel","models","rails"],"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/cgriego.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2011-09-29T20:14:06.000Z","updated_at":"2025-04-19T21:22:41.000Z","dependencies_parsed_at":"2024-06-20T09:25:41.616Z","dependency_job_id":"a0f9fc31-7dd6-4a1d-aa69-31275700aab5","html_url":"https://github.com/cgriego/active_attr","commit_stats":{"total_commits":352,"total_committers":26,"mean_commits":"13.538461538461538","dds":"0.18465909090909094","last_synced_commit":"cd5b59bbae6b13e52f62cd5da6ee915f8e015150"},"previous_names":[],"tags_count":37,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cgriego%2Factive_attr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cgriego%2Factive_attr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cgriego%2Factive_attr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cgriego%2Factive_attr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cgriego","download_url":"https://codeload.github.com/cgriego/active_attr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250514767,"owners_count":21443208,"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":["activemodel","models","rails"],"created_at":"2024-07-31T17:01:13.834Z","updated_at":"2025-04-23T20:54:03.667Z","avatar_url":"https://github.com/cgriego.png","language":"Ruby","readme":"# ActiveAttr #\n\n[![Build History][build badge]][build history]\n[![Code Climate][codeclimate badge]][codeclimate]\n\nActiveAttr is a set of modules that makes it easy to create plain old Ruby\nmodels with functionality found in ORMs, like ActiveRecord, without\nreinventing the wheel. Think of ActiveAttr as the stuff ActiveModel left out.\n\nActiveAttr is distributed as a Ruby gem [on rubygems.org][rubygems].\n\n[![ActiveAttr Railscast][railscast poster]][railscast]\n\n* [Slides][speakerdeck]\n* [RailsCast][railscast]\n* [API Documentation][api]\n* [Contributors][contributors]\n\n[api]: http://rubydoc.info/gems/active_attr\n[codeclimate badge]: https://codeclimate.com/github/cgriego/active_attr.svg\n[codeclimate]: https://codeclimate.com/github/cgriego/active_attr\n[contributors]: https://github.com/cgriego/active_attr/contributors\n[railscast poster]: http://railscasts.com/static/episodes/stills/326-activeattr.png\n[railscast]: http://railscasts.com/episodes/326-activeattr\n[rubygems]: http://rubygems.org/gems/active_attr\n[protected_attributes]: https://github.com/westonganger/protected_attributes_continued\n[strong_parameters]: https://github.com/rails/strong_parameters\n[speakerdeck]: https://speakerdeck.com/u/cgriego/p/models-models-every-where\n[build badge]: https://github.com/cgriego/active_attr/workflows/Test/badge.svg?event=push\n[build history]: https://github.com/cgriego/active_attr/actions?query=workflow%3ATest\n\n## Modules ##\n\n### Attributes ###\n\nIncluding the Attributes module into your class gives you a DSL for defining\nthe attributes of your model.\n\n```ruby\nclass Person\n  include ActiveAttr::Attributes\n\n  attribute :first_name\n  attribute :last_name\nend\n\nperson = Person.new\nperson.first_name = \"Chris\"\nperson.last_name = \"Griego\"\nperson.attributes #=\u003e {\"first_name\"=\u003e\"Chris\", \"last_name\"=\u003e\"Griego\"}\n```\n\n#### AttributeDefaults ####\n\nIncluding the AttributeDefaults module into your class builds on Attributes by\nallowing defaults to be declared with attributes.\n\n```ruby\nclass Person\n  include ActiveAttr::AttributeDefaults\n\n  attribute :first_name, :default =\u003e \"John\"\n  attribute :last_name, :default =\u003e \"Doe\"\nend\n\nperson = Person.new\nperson.first_name #=\u003e \"John\"\nperson.last_name #=\u003e \"Doe\"\n```\n\n#### QueryAttributes ####\n\nIncluding the QueryAttributes module into your class builds on Attributes by\nproviding instance methods for querying your attributes.\n\n```ruby\nclass Person\n  include ActiveAttr::QueryAttributes\n\n  attribute :first_name\n  attribute :last_name\nend\n\nperson = Person.new\nperson.first_name = \"Chris\"\nperson.first_name? #=\u003e true\nperson.last_name? #=\u003e false\n```\n\n#### TypecastedAttributes ####\n\nIncluding the TypecastedAttributes module into your class builds on Attributes\nby providing type conversion for your attributes.\n\n```ruby\nclass Person\n  include ActiveAttr::TypecastedAttributes\n  attribute :age, :type =\u003e Integer\nend\n\nperson = Person.new\nperson.age = \"29\"\nperson.age #=\u003e 29\n```\n\n### BasicModel ###\n\nIncluding the BasicModel module into your class gives you the bare minimum\nrequired for your model to meet the ActiveModel API requirements.\n\n```ruby\nclass Person\n  include ActiveAttr::BasicModel\nend\n\nPerson.model_name.plural #=\u003e \"people\"\nperson = Person.new\nperson.valid? #=\u003e true\nperson.errors.full_messages #=\u003e []\n```\n\n### BlockInitialization ###\n\nIncluding the BlockInitialization module into your class will yield the model\ninstance to a block passed to when creating a new instance.\n\n```ruby\nclass Person\n  include ActiveAttr::BlockInitialization\n  attr_accessor :first_name, :last_name\nend\n\nperson = Person.new do |p|\n  p.first_name = \"Chris\"\n  p.last_name = \"Griego\"\nend\n\nperson.first_name #=\u003e \"Chris\"\nperson.last_name #=\u003e \"Griego\"\n```\n\n### Logger ###\n\nIncluding the Logger module into your class will give you access to a\nconfigurable logger in model classes and instances. Your preferred logger can\nbe configured on an instance, subclass, class, parent class, and globally by\nsetting ActiveAttr::Logger.logger. When using Rails, the Rails framework\nlogger will be configured by default.\n\n```ruby\nclass Person\n  include ActiveAttr::Logger\nend\n\nPerson.logger = Logger.new(STDOUT)\nPerson.logger? #=\u003e true\nPerson.logger.info \"Logging an informational message\"\n\nperson = Person.new\nperson.logger? #=\u003e true\nperson.logger = Logger.new(STDERR)\nperson.logger.warn \"Logging a warning message\"\n```\n\n### MassAssignment ###\n\nIncluding the MassAssignment module into your class gives you methods for bulk\ninitializing and updating the attributes of your model. Any unknown attributes\nare silently ignored.\n\n```ruby\nclass Person\n  include ActiveAttr::MassAssignment\n  attr_accessor :first_name, :last_name, :age\nend\n\nperson = Person.new(:first_name =\u003e \"Christopher\", :last_name =\u003e \"Griego\")\nperson.attributes = { :first_name =\u003e \"Chris\", :age =\u003e 21 }\nperson.first_name #=\u003e \"Chris\"\nperson.last_name #=\u003e \"Griego\"\n```\n\nMassAssignment supports mass assignment security/sanitization if a sanitizer\nis included in the model. If using Rails 4.0, include ActiveModel's forbidden\nattributes protection module to get support for strong parameters.\n\n```ruby\nclass Person\n  include ActiveAttr::MassAssignment\n  include ActiveModel::ForbiddenAttributesProtection\n  attr_accessor :first_name, :last_name\nend\n\nperson = Person.new(ActionController::Parameters.new({\n  :first_name =\u003e \"Chris\",\n  :last_name =\u003e \"Griego\",\n}).permit(:first_name))\nperson.first_name #=\u003e \"Chris\"\nperson.last_name #=\u003e nil\n```\n\nIf using Rails 3.x or the [Protected Attributes gem][protected_attributes],\ninclude ActiveModel's mass assignment security module to get support for\nprotected attributes, including support for mass assignment roles.\n\n```ruby\nclass Person\n  include ActiveAttr::MassAssignment\n  include ActiveModel::MassAssignmentSecurity\n  attr_accessor :first_name, :last_name\n  attr_protected :last_name\nend\n\nperson = Person.new(:first_name =\u003e \"Chris\", :last_name =\u003e \"Griego\")\nperson.first_name #=\u003e \"Chris\"\nperson.last_name #=\u003e nil\n```\n\nIf using the [Strong Parameters gem][strong_parameters] with Rails 3.2,\ninclude the forbidden attributes protection module after including\nthe mass assignment security module.\n\n```ruby\nclass Person\n  include ActiveAttr::MassAssignment\n  include ActiveModel::MassAssignmentSecurity\n  include ActiveModel::ForbiddenAttributesProtection\nend\n```\n\n### Serialization ###\n\nThe Serialization module is a shortcut for incorporating ActiveModel's\nserialization functionality into your model with one include.\n\n```ruby\nclass Person\n  include ActiveAttr::Serialization\nend\n```\n\n### Model ###\n\nThe Model module is a shortcut for incorporating the most common model\nfunctionality into your model with one include. All of the above modules\nare included when you include Model.\n\n```ruby\nclass Person\n  include ActiveAttr::Model\nend\n```\n\n## Integrations ##\n\n### Ruby on Rails ###\n\nWhen using ActiveAttr inside a Rails application, ActiveAttr will configure\nyour models' default logger to use the Rails logger automatically. Just\ninclude ActiveAttr in your Gemfile.\n\n```ruby\ngem \"active_attr\"\n```\n\n### RSpec ###\n\nActiveAttr comes with matchers and RSpec integration to assist you in testing\nyour models. The matchers also work with compatible frameworks like Shoulda.\n\n```ruby\nrequire \"active_attr/rspec\"\n\ndescribe Person do\n  it do\n    should have_attribute(:first_name).\n      of_type(String).\n      with_default_value_of(\"John\")\n  end\nend\n```\n","funding_links":[],"categories":["Ruby","Core Extensions"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcgriego%2Factive_attr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcgriego%2Factive_attr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcgriego%2Factive_attr/lists"}