🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

simple_form_object

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple_form_object

0.0.8
Rubygems
Version published
Maintainers
1
Created
Source

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:

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

It gives you:

  • Default values for your form attributes.
  • Integration with simple_form so you don't need to specify the field type on the form.
  • 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.

Todo:

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

Contributing

  • Fork it ( http://github.com/reInteractive-open/simple_form_objects/fork )
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'Add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request

FAQs

Package last updated on 04 May 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts