Skip to content

stacksocial/simple_form_object

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SimpleFormObject

Allows you to make really simple non-persisted form objects or models.

Only suitable for Rails 4 applications.

You don't need to remember to:

  1. include ActiveModel::Model
  2. Set the class model_name so Rails url generation works for forms.

It gives you:

  1. Default values for your form attributes.
  2. Integration with simple_form so you don't need to specify the field type on the form.
  3. Thanks to ActiveModel::Model you can use standard Rails validations on your attributes.

Installation

Add this line to your application's Gemfile:

gem 'simple_form_object'

And then execute:

$ bundle

Usage

Create a form class. I like to put them inside app/forms/. If you prefer to create models then that will work too.

class PostForm
  include SimpleFormObject

  attribute :body, :text
  attribute :title, :string
  attribute :publish_date, :datetime, default: Time.now

  validates_presence_of :body
  validates_presence_of :title
end

SimpleFormObject includes ActiveModel::Model so you don't need to. It also intelligently sets the model_name on the class so that Rails routing works as expected. As an example:

<%= simple_form_for @post_form do |f| %>
  <%= f.input :title        # renders a simple string input %>
  <%= f.input :body         # renders a textarea %>
  <%= f.input :publish_date # renders a datetime select html element %>
<% end %>

Will create a HTML form which will POST to posts_path. In your controller, you can do something like this:

def new
  @post_form = PostForm.new
end

def create
  @post_form = PostForm.new(param[:post])
  if @post_form.valid?
    # save the data
    flash[:notice] = "Post created successfully."
    # redirect somewhere
  else
    flash[:error] = @post_form.errors.full_messages
    render 'new'
  end
end

Todo:

  1. Automatically add good validations for types.
  2. It's tested in spec but better tests wouldn't hurt.

Contributing

  1. Fork it ( http://github.com/reInteractive-open/simple_form_objects/fork )
  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

About

Simple form objects for your Rails application.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%