Schoolgirl Uniform
:feet: Multistep form concept for Rails projects. Allows to create complex forms for a few models simultaneously. Supports selectable per step validations without data persistence into db.
Installation
To start using it just add this line to your application's Gemfile:
gem 'schoolgirl_uniform'
Then you need to generate scaffold for future multistep form:
$ rails generate schoolgirl_uniform:install CatgirlsSurvey
You can also use snake case, so catgirls_survey
would be identical to CatgirlsSurvey
and will generate the same output during scaffolding.
Usage and Config
To achieve working multistep form you need to configure FVC:
:womans_clothes: Form
e.g. CatgirlsSurveyForm - app/forms/catgirls_survey_form.rb
- Declare the steps:
def self.steps
%w[first second third]
end
- Define form fields
attribute :username, String
- Define validation and select appropriate step for it
validates :username, presence: true, if: proc { on_step('second') }
-
Inside save!
method build your records, set them with form attributes and save them in transaction.
Use .save!(validate: false)
to skip native validations on model.
In order to return the result set the @identifier
with created records reference/references
( e.g. simple 1234
or complex {user_id: 1234, personal_data_id: 5678}
)
def save!
user = User.new(username: username)
personal_data = user.build_personal_data(email: email)
ActiveRecord::Base.transaction do
user.save!(validate: false)
personal_data.save!(validate: false)
end
@identifier = user.id
end
:dress: View
- Scaffolding will generate example structure of view files:
- show.html.erb
- finish.html.erb
- _wizard.html.erb
- _form_errors.html.erb
and steps/:
- _first.html.erb
- _second.html.erb
- _third.html.erb
:exclamation: Please notice that show and finish are action views, others are partials.
:art: Feel free to modify html and styles around the form.
:infinity: Steps
By default Scaffolding generates 3 steps, but you can modify, delete them or add new steps. Just make sure that steps are _partials and match corresponded names inside Form (e.g. CatgirlsSurveyForm):
<%= form.label :username %>
<%= form.text_field :username %>
<br>
<%= form.label :password %>
<%= form.text_field :password %>
:school_satchel: Controller
e.g. CatgirlsSurveyController - app/controllers/catgirls_survey_controller.rb
- Make sure you have listed all form fields (used for permit params)
def form_attributes
[:username, :password, :email, :phone]
end
- Fetch resource/resources from DB using identifier, which you set in
.save!
def finish
@record = User.find_by(uuid: params[:identifier])
...
...
@record1 = Book.find_by(title: params[:identifier][:title])
@record2 = Author.find_by(id: params[:identifier][:author_id])
end
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/vergilet/schoolgirl_uniform
Feel free to contribute:
- Fork it (https://github.com/vergilet/schoolgirl_uniform/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 new Pull Request
License
The gem is available as open source under the terms of the MIT License.
Copyright © 2016 Yaro.
