~ubuntu-branches/ubuntu/vivid/ruby-sequel/vivid

« back to all changes in this revision

Viewing changes to lib/sequel/plugins/error_splitter.rb

  • Committer: Package Import Robot
  • Author(s): Dmitry Borodaenko, Dmitry Borodaenko, Cédric Boutillier
  • Date: 2013-08-10 18:38:17 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20130810183817-iqanz804j32i5myi
Tags: 4.1.1-1
[ Dmitry Borodaenko ]
* New upstream release.
* Standards-Version upgraded to 3.9.4 (no changes).
* Added Build-Depend on ruby-sqlite3.

[ Cédric Boutillier ]
* debian/control: remove obsolete DM-Upload-Allowed flag.
* use canonical URI in Vcs-* fields.
* debian/copyright: use DEP5 copyright-format/1.0 official URL for Format
  field.
* Update debian/watch. Thanks Bart Martens.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
module Sequel
 
2
  module Plugins
 
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:
 
6
    #
 
7
    #   validates_unique([:artist_id, :name])
 
8
    #
 
9
    # would by default result in errors entries such as:
 
10
    #
 
11
    #   {[:artist_id, :name]=>'is already taken'}
 
12
    #
 
13
    # This plugin transforms those errors into:
 
14
    #
 
15
    #   {:artist_id=>'is already taken', :name=>'is already taken'}
 
16
    #
 
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:
 
19
    #
 
20
    #   errors.on(:artist_id)
 
21
    #
 
22
    # would not return the uniqueness error.
 
23
    # 
 
24
    # Usage:
 
25
    #
 
26
    #   # Make all model subclass instances split errors (called before loading subclasses)
 
27
    #   Sequel::Model.plugin :error_splitter
 
28
    #
 
29
    #   # Make the Album class split errors
 
30
    #   Album.plugin :error_splitter
 
31
    module ErrorSplitter
 
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,
 
35
        # one per column.
 
36
        def _valid?(*)
 
37
          v = super
 
38
          unless v
 
39
            errors.keys.select{|k| k.is_a?(Array)}.each do |ks|
 
40
              msgs = errors.delete(ks)
 
41
              ks.each do |k|
 
42
                msgs.each do |msg|
 
43
                  errors.add(k, msg)
 
44
                end
 
45
              end
 
46
            end
 
47
          end
 
48
          v
 
49
        end
 
50
      end
 
51
    end
 
52
  end
 
53
end
 
54