Module Sequel::Plugins::ConstraintValidations
In: lib/sequel/plugins/constraint_validations.rb

The constraint_validations plugin is designed to be used with databases that used the constraint_validations extension when creating their tables. The extension adds validation metadata for constraints created, and this plugin reads that metadata and automatically creates validations for all of the constraints. For example, if you used the extension and created your albums table like this:

  DB.create_table(:albums) do
    primary_key :id
    String :name
    validate do
      min_length 5, :name
    end
  end

Then when you went to save an album that uses this plugin:

  Album.create(:name=>'abc')
  # raises Sequel::ValidationFailed: name is shorter than 5 characters

Usage:

  # Make all model subclasses use constraint validations (called before loading subclasses)
  Sequel::Model.plugin :constraint_validations

  # Make the Album class use constraint validations
  Album.plugin :constraint_validations

Methods

apply   configure  

Classes and Modules

Module Sequel::Plugins::ConstraintValidations::ClassMethods
Module Sequel::Plugins::ConstraintValidations::DatabaseMethods
Module Sequel::Plugins::ConstraintValidations::InstanceMethods

Constants

DEFAULT_CONSTRAINT_VALIDATIONS_TABLE = :sequel_constraint_validations   The default constraint validation metadata table name.

Public Class methods

Automatically load the validation_helpers plugin to run the actual validations.

[Source]

    # File lib/sequel/plugins/constraint_validations.rb, line 35
35:       def self.apply(model, opts=OPTS)
36:         model.instance_eval do
37:           plugin :validation_helpers
38:           @constraint_validations_table = DEFAULT_CONSTRAINT_VALIDATIONS_TABLE
39:           @constraint_validation_options = {}
40:         end
41:       end

Parse the constraint validations metadata from the database. Options:

:constraint_validations_table :Override the name of the constraint validations metadata table. Should only be used if the table name was overridden when creating the constraint validations.
:validation_options :Override/augment the options stored in the database with the given options. Keys should be validation type symbols (e.g. :presence) and values should be hashes of options specific to that validation type.

[Source]

    # File lib/sequel/plugins/constraint_validations.rb, line 52
52:       def self.configure(model, opts=OPTS)
53:         model.instance_eval do
54:           if table = opts[:constraint_validations_table]
55:             @constraint_validations_table = table
56:           end
57:           if vos = opts[:validation_options]
58:             vos.each do |k, v|
59:               if existing_options = @constraint_validation_options[k]       
60:                 v = existing_options.merge(v)
61:               end
62:               @constraint_validation_options[k] = v
63:             end
64:           end
65:           parse_constraint_validations
66:         end
67:       end

[Validate]