GithubHelp home page GithubHelp logo

fidme / active_form_objects Goto Github PK

View Code? Open in Web Editor NEW
15.0 4.0 0.0 231 KB

Clean up your controllers, slim up your models, handle more use cases

Ruby 100.00%
rails ruby form-objects hexagonal-architecture

active_form_objects's Introduction

ActiveFormObjects

Build Status Gem Version

Form objects are a great way to clean up your controllers and models.

Whenever 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.

Take the bull by the horns, use ActiveFormObjects, and start cleaning up your mess ! ๐Ÿ’ช

A few benefits :

  • Keep business logic out of the Controller and models
  • Add validation support to plain Ruby object using ActiveModel
  • Display data validation errors in the form

For more infos regarding this pattern, see this blog post

Menu

Getting started

Documentation

Installation

Add this line to your application's Gemfile:

gem 'active_form_objects'

Execute

$ bundle install

Then, depending on your usage you may want to create an app/forms folder in your Rails application.

You will put all your forms inside of it.

A form is just a class that extends ActiveFormObjects::Base.

On top of all its features, ActiveFormObjects gives you access to the entire Active Model stack.

The Form layer

The form layer

A form object can be decoupled into three parts.

  • Params filtering and validating
  • Any business logic
  • Communication with model

Even 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.

A basic example

You have a User model. This model has a password field and on creation you want to verify that password and password_confirm match.

This logic does not belong to your User model.

Indeed, 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.

Seems a bit to much to handle for a User model that is also used in a thousand other use cases...

ActiveFormObjects allows you to refactor this logic and put it where it belongs.

In this case :

class RegistrationController
  def create
    RegistrationForm.new(params).save!
  end
end

class RegistrationForm < ActiveFormObjects::Base
  resource User
  attributes :email, :password, :password_confirm
  validate :confirmation_match

  def confirmations_match
    errors.add(:password, "must match password_confirm") if password != password_confirm
  end
end

class User
  validates :email, :password, presence: true
end

Another example

Another great example where a FormObject becomes necessary is when you have several ways to create or update a model.

A typical use case would be as follow :

form example

In the above example, you have two distinct ways of creating your User.

Therefore you need distinct validations to handle those cases, and your model must not handle them.

Usage

Using a declarated form is very simple. Consider this form :

class ExampleForm < ActiveFormObjects::Base
  resource Example
  attributes :name

  before_save :capitalize_name

  def capitalize_name
    name.capitalize!
  end
end

You have two ways of using it :

Without resource

form = ExampleForm.new(name: 'Michael')

# Will create an instance of Example
@user = form.save!
# => Example#{ name: 'Michael' }

With resource

form = ExampleForm.new({ name: 'Nicolas' }, @user)

# Will update the given resource
form.save!
# => Example#{ name: 'Nicolas' }

Note that you can of course override the save! method

class ExampleForm < ActiveFormObjects::Base
  def save!
   # do nothing
  end
end

The provided save! method is just a helper that does

  • validate!
  • Uses ActiveRecord::Base.transaction
  • Returns the resource

For more informations on saving, please read the dedicated section

Anything is missing ?

File an issue

active_form_objects's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

active_form_objects's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.