Method: Iry::Macros#exclusion_constraint

Defined in:
lib/iry/macros.rb

#exclusion_constraint(key, name: nil, message: :taken) ⇒ void

This method returns an undefined value.

Tracks exclusion constraint for the given key and convert constraint errors into validation errors

Parameters:

  • key (Symbol)

    key to apply validation errors to

  • message (Symbol, String) (defaults to: :taken)

    the validation error message

  • name (nil, String) (defaults to: nil)

    constraint name. If omitted, it will be inferred using table name + key

Raises:

  • (ArgumentError)

    raised if constraint name already in use by another constraint of any type



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/iry/macros.rb', line 39

def exclusion_constraint(
  key,
  name: nil,
  message: :taken
)
  name ||= Constraint::Exclusion.infer_name(key, table_name)

  if constraints.key?(name)
    raise ArgumentError, "Constraint already exists"
  end

  self.constraints = constraints.dup
  constraints[name] = Constraint::Exclusion.new(
    key,
    message: message,
    name: name
  )
end