Ruby on Rails - Trailblazer Tutorial: Validations and Classes - ruby on rails tutorial - rails guides - rails tutorial - ruby rails
What is Trailblazer?
- Trailblazer is a framework on top of Rails to serve as architectural guidance.
- It’s like a mesh-up of all the gems I’ve done before with some new concepts like operation, which will model every high level domain action.
- A high level domain operation, as I call it, is something like updating a comment, deleting a user, getting a list of all the recent comments, and so on.
- In essence, it encompasses everything the user can do through the user interface or via the API.
Validations
- Validation in happens in the validate method, and only there.
- Reform will deserialize the fragments and their values to the form and its nested subforms, and once this is done, run validations.
- It returns the result boolean, and provide potential errors via errors.
ruby on rails tutorial tags - ruby , rail , ruby on rails , rail forum , ruby on rails tutorial , ruby tutorial , rails guides , rails tutorial , learn ruby
Validation Groups
- Grouping validations enables you to run them conditionally, or in a specific order. You can
use :if
to specify what group had to be successful for it to be validated
- This will only run the database-consuming :unique validation group if the :default group was valid.
- Chaining groups works via the :after option. This will run the group regardless of the former result. Note that it still can be combined with :if.
- At any time you can extend an existing group using :inherit
- This appends validations to the existing :email group.
Dry-validation:
- Dry-validation is the preferred backend for defining and executing validations.
- The purest form of defining validations with this backend is by using a validation group. A group provides the exact same API as a Dry::Validation::Schema. You can learn all the details on the gem’s website
- Custom predicates have to be defined in the validation group. If you need access to your form you must add option :form to your configure block
- In addition to dry-validation’s API, you have access to the form that contains the group via form
Dry: Error Messages
- You need to provide custom error messages via dry-validation mechanics.
- A simple error messages file might look as follows.
ruby on rails tutorial tags - ruby , rail , ruby on rails , rail forum , ruby on rails tutorial , ruby tutorial , rails guides , rails tutorial , learn ruby
ActiveModel:
- In Rails environments, the AM support will be automatically loaded.
- In other frameworks, you need to include
Reform::Form::ActiveModel::Validations
either into a particular form class, or simply into Reform::Form and make it available for all subclasses.
Uniqueness Validation
- Both ActiveRecord and Mongoid modules will support “native” uniqueness support where the validation is basically delegated to the “real” model class. This happens when you use validates_uniqueness_of and will respect options like :scope, etc.
- Be warned, though, that those validators write to the model instance. Even though this usually is not persisted, this will mess up your application state, as in case of an invalid validation your model will have unexpected values
- This is not Reform’s fault but a design flaw in ActiveRecord’s validators.
ruby on rails tutorial tags - ruby , rail , ruby on rails , rail forum , ruby on rails tutorial , ruby tutorial , rails guides , rails tutorial , learn ruby
Unique Validation:
- You’re encouraged to use Reform’s non-writing unique: true validation, though.
- This will only validate the uniqueness of title.
- For uniqueness validation of multiple fields, use the :scope option.
Confirm Validation
- Likewise, the confirm: true validation from Active Resource is considered dangerous and should not be used. It also writes to the model and probably changes application state.
- Instead, use your own virtual fields.
Validations for File Uploads
- In case you’re processing uploaded files with your form using Carrier Wave, Paperclip, Dragonfly or Paper dragon we recommend using the awesome file_ validators gem for file type and size validations.
Classes
- Every step in Trailblazer needs to be separated into a different class such as
Comment::Operation::Update
andComment::Operation::
- Delete.
- The better way to handle classifying you can use inheritance to make creating classes easy in Trailblazer. So, you have the same validations and forms for
create
andupdate,
you can just inherit acreate
operation from theupdate
operation. - No other code needs to be written, as it will inherit the
contract
and the code, so there is nothing you need to override. - It may sound crazy because you’ll need a class for every step, but if you use inheritance, you can also have reforms in your models.
- Working on something that automatically creates classes for you, so if you have really simple objects, you can use
runtime generator
for the classes so you won’t have to write 8 different operations for a model that just has an email.