3
# The error_splitter plugin automatically splits errors entries related to
4
# multiple columns to have separate error entries, one per column. For example,
5
# a multiple column uniqueness entry:
7
# validates_unique([:artist_id, :name])
9
# would by default result in errors entries such as:
11
# {[:artist_id, :name]=>'is already taken'}
13
# This plugin transforms those errors into:
15
# {:artist_id=>'is already taken', :name=>'is already taken'}
17
# The main reason to split errors is if you have a list of fields that you
18
# are checking for validation errors. If you don't split the errors, then:
20
# errors.on(:artist_id)
22
# would not return the uniqueness error.
26
# # Make all model subclass instances split errors (called before loading subclasses)
27
# Sequel::Model.plugin :error_splitter
29
# # Make the Album class split errors
30
# Album.plugin :error_splitter
32
module InstanceMethods
33
# If the model instance is not valid, go through all of the errors entries. For
34
# any that apply to multiple columns, remove them and add separate error entries,
39
errors.keys.select{|k| k.is_a?(Array)}.each do |ks|
40
msgs = errors.delete(ks)