module Sequel::Plugins::ConstraintValidations

  1. lib/sequel/extensions/_model_constraint_validations.rb
  2. lib/sequel/plugins/constraint_validations.rb
  3. show all

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

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


# 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


Public Class

  1. apply
  2. configure


DEFAULT_CONSTRAINT_VALIDATIONS_TABLE = :sequel_constraint_validations  

The default constraint validation metadata table name.

OPERATOR_MAP = {:str_lt => :<, :str_lte => :<=, :str_gt => :>, :str_gte => :>=, :int_lt => :<, :int_lte => :<=, :int_gt => :>, :int_gte => :>=}.freeze  

Mapping of operator names in table to ruby operators

Public Class methods

apply(model, opts=OPTS)

Automatically load the validation_helpers plugin to run the actual validations.

[show source]
   # File lib/sequel/plugins/constraint_validations.rb
41 def self.apply(model, opts=OPTS)
42   model.instance_exec do
43     plugin :validation_helpers
44     @constraint_validations_table = DEFAULT_CONSTRAINT_VALIDATIONS_TABLE
45     @constraint_validation_options = {}
46   end
47 end
configure(model, opts=OPTS)

Parse the constraint validations metadata from the database. Options:


Override the name of the constraint validations metadata table. Should only be used if the table name was overridden when creating the constraint validations.


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.

[show source]
   # File lib/sequel/plugins/constraint_validations.rb
58 def self.configure(model, opts=OPTS)
59   model.instance_exec do
60     if table = opts[:constraint_validations_table]
61       @constraint_validations_table = table
62     end
63     if vos = opts[:validation_options]
64       vos.each do |k, v|
65         if existing_options = @constraint_validation_options[k]       
66           v = existing_options.merge(v)
67         end
68         @constraint_validation_options[k] = v
69       end
70     end
71     parse_constraint_validations
72   end
73 end