File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -552,6 +552,32 @@ validates_length_of :email, maximum: 100
552
552
validates :email, presence: true, length: { maximum: 100 }
553
553
----
554
554
555
+ === Custom Validation Methods
556
+
557
+ When naming custom validation methods, adhere to the simple rules:
558
+
559
+ - `validate :method_name` reads like a natural statement
560
+ - the method name explains what it checks
561
+ - the method is recognizable as a validation method by its name, not a predicate method
562
+
563
+ [source,ruby]
564
+ ----
565
+ # good
566
+ validate :expiration_date_cannot_be_in_the_past
567
+ validate :discount_cannot_be_greater_than_total_value
568
+ validate :ensure_same_topic_is_chosen
569
+
570
+ # also good - explicit prefix
571
+ validate :validate_birthday_in_past
572
+ validate :validate_sufficient_quantity
573
+ validate :must_have_owner_with_no_other_items
574
+ validate :must_have_shipping_units
575
+
576
+ # bad
577
+ validate :birthday_in_past
578
+ validate :owner_has_no_other_items
579
+ ----
580
+
555
581
=== Single-attribute Validations [[single-attribute-validations]]
556
582
557
583
To make validations easy to read, don't list multiple attributes per validation.
You can’t perform that action at this time.
0 commit comments