FormObject
TODO: Write a gem description
Installation
Add this line to your application's Gemfile:
gem 'form_object'
And then execute:
$ bundle
Or install it yourself as:
$ gem install form_object
Usage
We have a model
class User < ActiveRecord::Base
end
# attributes => {email: "test@example.com", name: "name"}
Define forms:
# For base auth
class BaseAuthForm < FormObject::Base
map_model User, as: :base_auth
attribute :email, String # => map form attribute to model attribute
validates :email, presence: true # => validate email in form ONLY!
end
# For any other auth
class TwitterAuthForm < FormObject::Base
map_model User # name => twitter_auth (name will be generated
# automatically from class name)
# There is nothing to validate
end
In any place:
@user = User.find(params[:id])
@form = @user.form( :base_auth ) # This retrive BaseAuthForm instance
# with attributes from model
@form.assign_attributes(params[:user]) # assign attributes from hash
if @form.valid?
@form.persist_model # TODO: maybe other method name?
end
Can I help you?
Discuss the existing features and offer new features you can here.
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