Skip to content

alexsiri7/syrup_form_object

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#Syrup::FormObject

Gem Version Build Status

This is a simple implementation of the FormObject... pattern?

##Installation

$ gem install syrup_form_object

or in the Gemfile

gem 'syrup_form_object'

##Examples

Note: The following example can be found in syrup_form_example

To update the Event class in your model

class Event < ActiveRecord::Base
  validates :start_date, presence: true
  validates :end_date, presence: true
end

You create the follwing form

class EventForm < Syrup::FormObject
  wraps :event

  attribute :length_of_the_event, Integer
  validates :length_of_the_event, numericality: {greater_than: 0}

  before_validation :before_validation

  def before_validation
    self.end_date = event.start_date + length_of_the_event.to_i.hours
  end
end

Create a controller similar to this one

class EventController < ApplicationController
  def new
    @event_form = EventForm.new
  end

  def create
    @event_form = EventForm.new(create_params)
    if @event_form.save
      redirect_to @event_form.event
    else
      render :new
    end
  end

  def create_params
    params.require(:event)
      .permit(:length_of_the_event, :start_date)
  end
end

And in the template:

<%= form_for @event_form do %>
  <%= input_tag :start_date  %>
  <%= input_tag :length_of_the_event  %>
<% end %>

##Some sources for Form Objects

https://github.com/apotonick/reform An implementation of Form Objects

http://railscasts.com/episodes/416-form-objects

http://pivotallabs.com/form-backing-objects-for-fun-and-profit/

http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/