https://github.com/wizardone/forminator
Form wizard for Ruby applications
https://github.com/wizardone/forminator
dry form form-wizard hanami persistence rails ruby validations wizard
Last synced: 2 months ago
JSON representation
Form wizard for Ruby applications
- Host: GitHub
- URL: https://github.com/wizardone/forminator
- Owner: wizardone
- License: mit
- Created: 2017-07-21T07:17:51.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-10-03T08:09:24.000Z (over 7 years ago)
- Last Synced: 2025-03-18T17:57:41.709Z (3 months ago)
- Topics: dry, form, form-wizard, hanami, persistence, rails, ruby, validations, wizard
- Language: Ruby
- Size: 23.4 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Forminator
[](https://travis-ci.org/wizardone/forminator)
[](https://codecov.io/gh/wizardone/forminator)## Installation
Add this line to your application's Gemfile:
```ruby
gem 'forminator'
```And then execute:
$ bundle
Or install it yourself as:
$ gem install forminator
## Usage
First you need to configure the wizard so it knows the object that you
interact with (usually a used model). If you like to persist the model you can
supply a callable object that takes care of the persistence. Whether you
want to persist after each step or after the final one is up to you.```ruby
Forminator.configure do |config|
config.klass = :user
config.persist = -> (user) { user.save }
end
```
To build a form wizard step you need to subclass the `Forminator::Step`
class```ruby
class FirstStep < Forminator::Step
validations do
required(:email) { filled? }
required(:password) { filled? }
end# By default this returns false, so if you want to persist the object
# after each step you need to overwrite the method
def persist?
true
end
end
```
Then you can do:
```ruby
user = User.new
some_params = { email: '[email protected]', password: 'ineedtocryptmypassword' }
FirstStep.(user, some_params)
=> [{ valid: true }, some_params]
```
Each step always returns an array of 2 elements: a hash with the
validity and the initially passed params. This allows for easy chaining
of events.If you want to call a custom persistence logic for a certain step you
can pass an optionable callable object like so:```ruby
class CustomPersistenceLogic
def call(user)
# persistence logic goes here
end
end
user = User.new
FirstStep.(user, some_params, persist: CustomPersistenceLogic.new)
```
The callable object will receive the original object that you interact
with.Want to build a whole flow of steps? Just use the `Forminator::Flow`
class
```ruby
steps = [FirstStep, SecondStep, LastStep]
flow = Forminator::Flow.new(steps: steps)
flow.current_step
=> FirstStep
flow.next_step
=> SecondStep
```
You can also add steps on the way with:
```ruby
flow.add(step: IntermediateStep)
```
Keep in mind that you can only add steps that inherit from
`Forminator::Step`.
You can also remove steps:
```ruby
flow.remove(step: LastStep)
=> LastStep
```
Calling `remove` will return the removed step.`Forminator` uses `Hanami::Validations` which in it's own term uses
`Dry::Validation` :heart. You can [refer](https://github.com/hanami/validations) them for a detailed DSL about
validations.## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/wizardone/forminator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).