{"id":13879106,"url":"https://github.com/FidMe/active_form_objects","last_synced_at":"2025-07-16T15:31:33.012Z","repository":{"id":54229655,"uuid":"158369911","full_name":"FidMe/active_form_objects","owner":"FidMe","description":"Clean up your controllers, slim up your models, handle more use cases","archived":false,"fork":false,"pushed_at":"2023-01-20T14:06:23.000Z","size":231,"stargazers_count":15,"open_issues_count":4,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-21T03:51:42.680Z","etag":null,"topics":["form-objects","hexagonal-architecture","rails","ruby"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FidMe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-20T10:17:00.000Z","updated_at":"2023-01-11T15:55:58.000Z","dependencies_parsed_at":"2023-02-12T02:45:18.456Z","dependency_job_id":null,"html_url":"https://github.com/FidMe/active_form_objects","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/FidMe/active_form_objects","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FidMe%2Factive_form_objects","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FidMe%2Factive_form_objects/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FidMe%2Factive_form_objects/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FidMe%2Factive_form_objects/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FidMe","download_url":"https://codeload.github.com/FidMe/active_form_objects/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FidMe%2Factive_form_objects/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265521426,"owners_count":23781500,"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":["form-objects","hexagonal-architecture","rails","ruby"],"created_at":"2024-08-06T08:02:10.061Z","updated_at":"2025-07-16T15:31:32.706Z","avatar_url":"https://github.com/FidMe.png","language":"Ruby","readme":"# ActiveFormObjects\n\n[![Build Status](https://travis-ci.org/FidMe/active_form_objects.svg?branch=master)](https://travis-ci.org/FidMe/active_form_objects)\n[![Gem Version](https://badge.fury.io/rb/active_form_objects.svg)](https://badge.fury.io/rb/active_form_objects)\n\nForm objects are a great way to clean up your controllers and models.\n\nWhenever your Rails application grows, you will eventually end up with bloated models and controllers. Even though Rails is awesome, it often leads you toward putting unnecessary logic in your controllers and models.\n\n**Take the bull by the horns, use ActiveFormObjects, and start cleaning up your mess ! 💪**\n\nA few benefits :\n\n- Keep business logic out of the Controller and models\n- Add validation support to plain Ruby object using ActiveModel\n- Display data validation errors in the form\n\nFor more infos regarding this pattern, [see this blog post](https://medium.com/selleo/essential-rubyonrails-patterns-form-objects-b199aada6ec9)\n\n## Menu\n\n**Getting started**\n\n- [Installation](https://github.com/FidMe/active_form_objects#installation)\n- [The Form Layer](https://github.com/FidMe/active_form_objects#the-form-layer)\n- [A basic example](https://github.com/FidMe/active_form_objects#a-basic-example)\n- [Usage](https://github.com/FidMe/active_form_objects#usage)\n\n**Documentation**\n\n- [Resource](https://github.com/FidMe/active_form_objects/blob/master/docs/Resource.md)\n- [Attributes](https://github.com/FidMe/active_form_objects/blob/master/docs/Attributes.md)\n- [Relations](https://github.com/FidMe/active_form_objects/blob/master/docs/Relations.md)\n- [Polymorphs](https://github.com/FidMe/active_form_objects/blob/master/docs/Polymorphs.md)\n- [Scopes](https://github.com/FidMe/active_form_objects/blob/master/docs/Scopes.md)\n- [Hooks](https://github.com/FidMe/active_form_objects/blob/master/docs/Hooks.md)\n- [Debugging](https://github.com/FidMe/active_form_objects/blob/master/docs/Debugging.md)\n- [Error handling](https://github.com/FidMe/active_form_objects/blob/master/docs/Errors.md)\n- [Saving the form](https://github.com/FidMe/active_form_objects/blob/master/docs/Savings.md)\n- [ActiveModel](https://api.rubyonrails.org/classes/ActiveModel/Model.html)\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'active_form_objects'\n```\n\nExecute\n\n```bash\n$ bundle install\n```\n\nThen, depending on your usage you may want to create an `app/forms` folder in your Rails application.\n\nYou will put all your forms inside of it.\n\nA form is just a class that extends `ActiveFormObjects::Base`.\n\nOn top of all its features, `ActiveFormObjects` gives you access to the entire [Active Model](https://guides.rubyonrails.org/active_model_basics.html) stack.\n\n## The Form layer\n\n![The form layer](https://raw.githubusercontent.com/FidMe/active_form_objects/master/docs/images/form_layer.png)\n\nA form object can be decoupled into three parts.\n\n- Params filtering and validating\n- Any business logic\n- Communication with model\n\nEven though it seems to be a lot to handle for a single class, remember that most of it used to be (poorly done) in your controller.\n\n### A basic example\n\nYou have a `User` model.\nThis model has a password field and on creation you want to verify that password and `password_confirm` match.\n\nThis logic does not belong to your `User` model.\n\nIndeed, `User` only needs to know that the `User` has a password, furthemore, `User` does not have a `password_confirm` field, so you would need to add attr_accessor and custom validation into your `User` model.\n\nSeems a bit to much to handle for a `User` model that is also used in a thousand other use cases...\n\n`ActiveFormObjects` allows you to refactor this logic and put it where it belongs.\n\nIn this case :\n\n```ruby\nclass RegistrationController\n  def create\n    RegistrationForm.new(params).save!\n  end\nend\n\nclass RegistrationForm \u003c ActiveFormObjects::Base\n  resource User\n  attributes :email, :password, :password_confirm\n  validate :confirmation_match\n\n  def confirmations_match\n    errors.add(:password, \"must match password_confirm\") if password != password_confirm\n  end\nend\n\nclass User\n  validates :email, :password, presence: true\nend\n```\n\n### Another example\n\nAnother great example where a `FormObject` becomes necessary is when you have several ways to create or update a model.\n\nA typical use case would be as follow :\n\n![form example](https://raw.githubusercontent.com/FidMe/active_form_objects/master/docs/images/registration_example.png)\n\nIn the above example, you have two distinct ways of creating your User.\n\nTherefore you need distinct validations to handle those cases, and your model must not handle them.\n\n## Usage\n\nUsing a declarated form is very simple.\nConsider this form :\n\n```ruby\nclass ExampleForm \u003c ActiveFormObjects::Base\n  resource Example\n  attributes :name\n\n  before_save :capitalize_name\n\n  def capitalize_name\n    name.capitalize!\n  end\nend\n```\n\nYou have two ways of using it :\n\n**Without resource**\n\n```ruby\nform = ExampleForm.new(name: 'Michael')\n\n# Will create an instance of Example\n@user = form.save!\n# =\u003e Example#{ name: 'Michael' }\n```\n\n**With resource**\n\n```ruby\nform = ExampleForm.new({ name: 'Nicolas' }, @user)\n\n# Will update the given resource\nform.save!\n# =\u003e Example#{ name: 'Nicolas' }\n```\n\nNote that you can of course override the `save!` method\n\n```ruby\nclass ExampleForm \u003c ActiveFormObjects::Base\n  def save!\n   # do nothing\n  end\nend\n```\n\nThe provided `save!` method is just a helper that does\n\n- validate!\n- Uses ActiveRecord::Base.transaction\n- Returns the resource\n\nFor more informations on saving, please [read the dedicated section](https://github.com/FidMe/active_form_objects/blob/master/docs/Savings.md)\n\n## Anything is missing ?\n\nFile an issue\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFidMe%2Factive_form_objects","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FFidMe%2Factive_form_objects","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFidMe%2Factive_form_objects/lists"}