Module: Sequel::Plugins::AutoValidations::InstanceMethods

Defined in:
lib/sequel/plugins/auto_validations.rb

Instance Method Summary collapse

Instance Method Details

#skip_auto_validations(*types) ⇒ Object

Skip the given types of auto validations on this instance inside the block.


183
184
185
186
187
188
189
# File 'lib/sequel/plugins/auto_validations.rb', line 183

def skip_auto_validations(*types)
  types << :all if types.empty?
  @_skip_auto_validations = types
  yield
ensure
  @_skip_auto_validations = nil
end

#validateObject

Validate the model's auto validations columns


192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/sequel/plugins/auto_validations.rb', line 192

def validate
  super
  skip = @_skip_auto_validations || EMPTY_ARRAY
  return if skip.include?(:all)
  opts = model.auto_validate_options


  unless skip.include?(:not_null)
    not_null_method = model.auto_validate_presence? ? :validates_presence : :validates_not_null
    unless (not_null_columns = model.auto_validate_not_null_columns).empty?
      public_send(not_null_method, not_null_columns, opts[:not_null])
    end
    unless (not_null_columns = model.auto_validate_explicit_not_null_columns).empty?
      public_send(not_null_method, not_null_columns, opts[:explicit_not_null])
    end
  end

  unless skip.include?(:max_length) || (max_length_columns = model.auto_validate_max_length_columns).empty?
    max_length_columns.each do |col, len|
      validates_max_length(len, col, opts[:max_length])
    end
  end

  unless skip.include?(:types) || !model.auto_validate_types?
    validates_schema_types(keys, opts[:schema_types])
  end

  unless skip.include?(:unique)
    unique_opts = Hash[opts[:unique]]
    if model.respond_to?(:sti_dataset)
      unique_opts[:dataset] = model.sti_dataset
    end
    model.auto_validate_unique_columns.each{|cols| validates_unique(cols, unique_opts)}
  end
end