Giter Club home page Giter Club logo

active_model_form_objects's Introduction

ActiveModel::FormObjects

Build Status Coverage Status

This Gem provides an ActiveModel::FormObject module that can be included into a PORO. Includes a Rails Generator for Form Objects.

Form Objects are used to encapsulate operations which are triggered by a form submission. They are particularly useful when multiple Models need to be updated by a single form submission. A common example would be a signup form that results in the creation of both a Company and a User.

##Install

In your Gemfile

gem 'active_model_form_objects'

And then execute:

$ bundle

Creating a FormObject with the Generator

The following example generates a UserLogin form-object (and test).

$ rails generate active_model:form_object user login

The test framework your project uses will determine the type of test created:

  • For RSpec it creates:

  • Form-object: app/form_objects/user_login.rb

  • Test: spec/form_objects/user_login_spec.rb

  • For MiniTest it creates:

  • Form-object: app/form_objects/user_login.rb

  • Test: test/form_objects/user_login_test.rb

  • For TestUnit it creates:

  • Form-object: app/form_objects/user_login.rb

  • Test: test/unit/form_objects/user_login_test.rb

User Signup Example

The following example demonstrates a user signup form-object and how this would be intergrated with a form.

  • Points of interest:
  • The use of the before_validation callback hook.
  • The use of the NestedValidator to run any existing validations defined on the User model.

user_signup.rb:

require 'active_model'

class UserSignup
  include ActiveModel::FormObject

  attr_accessor :name
  attr_reader :user

  validates :name, :length => { :minimum => 5 }
  validates :user, :nested => true

  before_validation :create_user

  private

  def create_user
    @user = User.new(:name => name)
  end

  def persist!
    user.save!
  end
end

users_controller.rb:

class UsersController < ApplicationController
  respond_to :html

  def signup
    @signup_form = UserSignup.new
  end

  def create
    @signup_form = UserSignup.new(params[:user])
    if @signup_form.save()
      @user = @signup_form.user
      respond_with @user
    else
      render "signup"
    end
  end
end

signup.html.erb:

<h1>Users#signup</h1>

<%= form_for @signup_form do |f| %>
  <%= f.text_field :name %>
  <%= f.submit "Submit" %>
<% end %>

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

This project rocks and uses MIT-LICENSE.

active_model_form_objects's People

Contributors

mattfreer avatar ocannings avatar

Watchers

 avatar  avatar  avatar

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.