module Sequel::Plugins::AutoValidations::ClassMethods
Attributes
The columns with automatic not_null validations for columns present in the values.
The columns or sets of columns with automatic max_length validations, as an array of pairs, with the first entry being the column name and second entry being the maximum length.
The columns with automatic not_null validations
Inherited options
The columns or sets of columns with automatic unique validations
Public Instance Methods
Whether to use a presence validation for not null columns
# File lib/sequel/plugins/auto_validations.rb 128 def auto_validate_presence? 129 @auto_validate_presence 130 end
Whether to automatically validate schema types for all columns
# File lib/sequel/plugins/auto_validations.rb 133 def auto_validate_types? 134 @auto_validate_types 135 end
Freeze auto_validation settings when freezing model class.
# File lib/sequel/plugins/auto_validations.rb 138 def freeze 139 @auto_validate_not_null_columns.freeze 140 @auto_validate_explicit_not_null_columns.freeze 141 @auto_validate_max_length_columns.freeze 142 @auto_validate_unique_columns.freeze 143 144 super 145 end
Skip automatic validations for the given validation type (:not_null, :types, :unique). If :all is given as the type, skip all auto validations.
# File lib/sequel/plugins/auto_validations.rb 149 def skip_auto_validations(type) 150 case type 151 when :all 152 [:not_null, :types, :unique, :max_length].each{|v| skip_auto_validations(v)} 153 when :not_null 154 auto_validate_not_null_columns.clear 155 auto_validate_explicit_not_null_columns.clear 156 when :types 157 @auto_validate_types = false 158 else 159 public_send("auto_validate_#{type}_columns").clear 160 end 161 end
Private Instance Methods
Parse the database schema and indexes and record the columns to automatically validate.
# File lib/sequel/plugins/auto_validations.rb 166 def setup_auto_validations 167 not_null_cols, explicit_not_null_cols = db_schema.select{|col, sch| sch[:allow_null] == false}.partition{|col, sch| sch[:default].nil?}.map{|cs| cs.map{|col, sch| col}} 168 @auto_validate_not_null_columns = not_null_cols - Array(primary_key) 169 explicit_not_null_cols += Array(primary_key) 170 @auto_validate_explicit_not_null_columns = explicit_not_null_cols.uniq 171 @auto_validate_max_length_columns = db_schema.select{|col, sch| sch[:type] == :string && sch[:max_length].is_a?(Integer)}.map{|col, sch| [col, sch[:max_length]]} 172 table = dataset.first_source_table 173 @auto_validate_unique_columns = if db.supports_index_parsing? && [Symbol, SQL::QualifiedIdentifier, SQL::Identifier, String].any?{|c| table.is_a?(c)} 174 db.indexes(table).select{|name, idx| idx[:unique] == true}.map{|name, idx| idx[:columns].length == 1 ? idx[:columns].first : idx[:columns]} 175 else 176 [] 177 end 178 end