-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #257 from avo-hq/custom_errors
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Custom errors | ||
|
||
Actions such as create, update, attach, etc... will not be completed if the record contains any errors. This ensures that only valid data is processed and saved, maintaining the integrity of your application. Custom validations can be added to your models to enforce specific rules and provide meaningful error messages to users. | ||
|
||
## Adding Custom Errors | ||
|
||
To add custom errors, you can define a validation method in your model. If the validation fails it adds an error to the record. These errors will prevent the action from completing and will be displayed as notifications to the user. | ||
|
||
## In a Simple Record | ||
|
||
Consider a simple `User` model where you want to enforce a custom validation rule, such as ensuring that the user's age is over a certain value. | ||
|
||
```ruby | ||
# app/models/user.rb | ||
class User < ApplicationRecord | ||
validate :age_must_be_over_18 | ||
|
||
private | ||
|
||
def age_must_be_over_18 | ||
# Add a custom error to the record if age is less than 18. | ||
if age < 18 | ||
errors.add(:age, "must be over 18.") | ||
end | ||
end | ||
end | ||
``` | ||
|
||
In this example, the `age_must_be_over_18` method checks if the user's age is less than 18. If so, it adds an error to the `age` attribute with a custom message. This error prevents any further Avo action on the record and notifies the user of the issue. | ||
|
||
## In a Join Table | ||
|
||
Consider a join table `TeamMembership` which links `Team` and `User` models. You might want to add a custom validation to ensure some business logic is enforced. | ||
|
||
```ruby | ||
# app/models/team_membership.rb | ||
class TeamMembership < ApplicationRecord | ||
belongs_to :team | ||
belongs_to :user | ||
|
||
validate :custom_validation | ||
|
||
private | ||
|
||
def custom_validation | ||
if user.banned? | ||
errors.add(:user, "is banned.") | ||
end | ||
end | ||
end | ||
``` | ||
|
||
In this example, the `custom_validation` method is called whenever a `TeamMembership` record is validated. If the conditions in this method are not met, an error is added to the `user` attribute with a custom message. This error prevents any further Avo action on the record and notifies the user of the issue. |