module Mv::Core::Db::Helpers::ColumnValidators

Attributes

column_name[RW]

Public Instance Methods

column_validators() click to toggle source
# File lib/mv/core/db/helpers/column_validators.rb, line 13
def column_validators
  table_validators.where(column_name: column_name)
end
create_column_validator(validation_type, opts) click to toggle source
# File lib/mv/core/db/helpers/column_validators.rb, line 17
def create_column_validator validation_type, opts
  raise_column_validation_error(validation_type, opts) if opts == false

  update_column_validator(validation_type, opts)
end
delete_column_validator() click to toggle source
# File lib/mv/core/db/helpers/column_validators.rb, line 23
def delete_column_validator
  delete_validators(column_validators) > 0
end
rename_column(new_column_name) click to toggle source
# File lib/mv/core/db/helpers/column_validators.rb, line 35
def rename_column new_column_name
  column_validators.update_all(column_name: new_column_name)
end
update_column_validator(validation_type, opts) click to toggle source
# File lib/mv/core/db/helpers/column_validators.rb, line 27
def update_column_validator validation_type, opts
  return delete_validators(column_validators.where(validation_type: validation_type)) if opts == false

  column_validators.where(validation_type: validation_type).first_or_initialize.tap do |validator|
    validator.options = normalize_opts(validation_type, opts)
  end.save!
end

Private Instance Methods

normalize_opts(validation_type, opts) click to toggle source
# File lib/mv/core/db/helpers/column_validators.rb, line 41
def normalize_opts validation_type, opts
  Mv::Core::Validation::Factory.create_validation(table_name, column_name, validation_type, opts).options
end
raise_column_validation_error(validation_type, opts) click to toggle source
# File lib/mv/core/db/helpers/column_validators.rb, line 45
def raise_column_validation_error validation_type, opts
  raise Mv::Core::Error.new(
    table_name: table_name,
    column_name: column_name,
    validation_type: validation_type,
    options: opts,
    error: 'Validator can not be removed when new column is being added'
  )
end