Module: Sequel::Plugins::ConstraintValidations
- Defined in:
- lib/sequel/plugins/constraint_validations.rb
Overview
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
Defined Under Namespace
Modules: ClassMethods, DatabaseMethods, InstanceMethods
Constant Summary collapse
- DEFAULT_CONSTRAINT_VALIDATIONS_TABLE =
The default constraint validation metadata table name.
:sequel_constraint_validations
Class Method Summary collapse
-
.apply(model, opts = {}) ⇒ Object
Automatically load the validation_helpers plugin to run the actual validations.
-
.configure(model, opts = {}) ⇒ Object
Parse the constraint validations metadata from the database.
Class Method Details
.apply(model, opts = {}) ⇒ Object
Automatically load the validation_helpers plugin to run the actual validations.
35 36 37 |
# File 'lib/sequel/plugins/constraint_validations.rb', line 35 def self.apply(model, opts={}) model.plugin :validation_helpers end |
.configure(model, opts = {}) ⇒ Object
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.
44 45 46 47 |
# File 'lib/sequel/plugins/constraint_validations.rb', line 44 def self.configure(model, opts={}) model.instance_variable_set(:@constraint_validations_table, opts[:constraint_validations_table] || DEFAULT_CONSTRAINT_VALIDATIONS_TABLE) model.send(:parse_constraint_validations) end |