Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation_helpers model plugin, which adds instance level valida…
…tion support similar to previously standard validations, with a different API This plugin should satisfy most validation needs. All previously standard validations were ported except for acceptance_of and confirmation_of, both of which are anti-patterns, as they encourage error checking in the model that should be done in the controller. Model validations should deal with the errors in the model's data, not whether a user checked a box or put an identical value in a confirmation field. validates_each was not ported as it is much easier to just write your own validation code if a standard validation doesn't handle it: # Old class level validation validates_each(:date) do |o,a,v| o.errors[a] << '...' unless v > Date.today end # New instance level validation def validate errors[:date] << '...' unless date > Date.today end The other validations were mostly renamed: # Old class level validations validates_format_of :col, :with=>/.../ validates_length_of :col, :maximum=>5 validates_length_of :col, :minimum=>3 validates_length_of :col, :is=>4 validates_length_of :col, :within=>3..5 validates_not_string :col validates_numericality_of :col validates_numericality_of :col, :only_integer=>true validates_presence_of :col validates_inclusion_of :col, :in=>[3, 4, 5] validates_uniqueness_of :col, :col2 validates_uniqueness_of([:col, :col2]) # New instance level validations def validate validates_format /.../, :col validates_max_length 5, :col validates_min_length 3, :col validates_exact_length 4, :col validates_length_range 3..5, :col validates_not_string :col validates_numeric :col validates_integer :col validates_presence :col validates_includes([3,4,5], :col) validates_unique :col, :col2 validates_unique([:col, :col2]) end Another change made is to specify the same type of validation on multiple attributes, you must use an array: # Old validates_length_of :name, :password, :within=>3..5 # New def validate validates_length_range 3..5, [:name, :password] end The :message, :allow_blank, :allow_missing, and :allow_nil options are still respected. The :tag option is not needed as instance level validations work with code reloading without workarounds. The :if option is also not needed for instance level validations: # Old validates_presence_of :name, :if=>:new? validates_presence_of :pass, :if=>{flag > 3} # New def validate validates_presence(:name) if new? validates_presence(:pass) if flag > 3 end validates_unique is a little different than the other new validations. It doesn't accept the :allow_* options, and you can specify multiple attributes instead of using an array. If you use an array, it is intepreted that you want the combination of values unique, instead of each value unique, which is how it operated previously. The new uniqueness logic is also much simpler and hopefully more robust, though you should still have a unique database index for integrity purposes. The validation_helpers plugin is half the size of the validation_class_methods plugin, and has better specs, IMO. This commit also removes some duplicative code from the validation_class_methods specs.
- Loading branch information