OCannings / active_model_form_objects

Provides an ActiveModel::FormObject module that can be included into a PORO. Includes a Generator for Form Objects.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

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

user_signup.rb:

require 'active_model'

class UserSignup
  include ActiveModel::FormObject

  attr_accessor :name
  attr_reader :user

  validates :name, :length => { :minimum => 10 }

  private

  def persist!
    @user = User.create!(:name => name)
  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.

About

Provides an ActiveModel::FormObject module that can be included into a PORO. Includes a Generator for Form Objects.

License:MIT License


Languages

Language:Ruby 96.9%Language:JavaScript 1.6%Language:CSS 1.5%