Project

formup

0.0
No commit activity in last 3 years
No release in over 3 years
formup is rubygem for creating data model based form class
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0
>= 0

Runtime

>= 3.2.0
 Project Readme

Formup

Build Status Coverage Status Code Climate

Formup is rubygem for creating data model based form class. Formup use activemodel gem.(>= 3.2.0) You can get the benefits of ActiveModel. ex) validations, application to form_for etc.

Installation

Add this line to your application's Gemfile:

gem 'formup'

And then execute:

$ bundle

Or install it yourself as:

$ gem install formup

Usage

Include Formup to your class.

class SalesForm
  include Formup

  # define attributes (sales_id, sales_price, sales_tax)
  source :sales, :attributes => [:id, :price, :tax]
  # define attributes (customer_id, customer_name, contact)
  source :customer, :attributes => [:id, :name], :aliases => { :email => :contact }

  attr_accessor :items

  # Handle hash parameters that were not processed in initialize method.
  def handle_extra_params(params)
    self.items = {}
    params.each do |k, v|
      self.items[v] = params[k.to_s.gsub(/^code/, "count")] if k.to_s.match(/^code/)
    end
  end
end

Initialize with hash parameters.(like ActiveRecord)

# params => { :sales_id => 1, :sales_price => 1000, ... }
form = SalesForm.new(params)

Load from hash and data model that have some accessor methods.

form = SalesForm.new
sales = Sales.find(1)
form.load(:sales => sales, :customer => { :id => 1, :name => "John Doe", :email => "john@example.com"})

Get parameters for data model by params_for method.

form = SalesForm.new(params)
sales = Sales.new(form.params_for(:sales))
sales.save

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