ShadowForm
Fat model with validation in all cases always causes problems in the future. So my solution is ShadowForm. Now you can have your original model without validation and the form when you need it.
Installation
Add this line to your application's Gemfile:
gem 'shadow_form'
And then execute:
$ bundle
Or install it yourself as:
$ gem install shadow_form
How it works
You have your active_record or active_model model:
class User
include ActiveModel::Model
attr_accessor :name, :email, :password
end
And for example register form
class UserRegistrationForm < ShadowForm::Core
shadow_of User
give_name 'RegisterForm` # optional
validation do
validates :email, presence: true
validates :password, presence: true
end
end
And now you have UserRegistrationForm
like User
+ Validation
Contributing
- Fork it ( https://github.com/[my-github-username]shadow_form/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 a new Pull Request