Formup
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
source :sales, :attributes => [:id, :price, :tax]
source :customer, :attributes => [:id, :name], :aliases => { :email => :contact }
attr_accessor :items
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)
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
- Fork it
- 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