Class: AnnotateRb::ModelAnnotator::CheckConstraintAnnotation::AnnotationBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/annotate_rb/model_annotator/check_constraint_annotation/annotation_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(model, options) ⇒ AnnotationBuilder

Returns a new instance of AnnotationBuilder.



7
8
9
10
# File 'lib/annotate_rb/model_annotator/check_constraint_annotation/annotation_builder.rb', line 7

def initialize(model, options)
  @model = model
  @options = options
end

Instance Method Details

#buildObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/annotate_rb/model_annotator/check_constraint_annotation/annotation_builder.rb', line 12

def build
  return Components::NilComponent.new if !@options[:show_check_constraints]
  return Components::NilComponent.new unless @model.connection.respond_to?(:supports_check_constraints?) &&
    @model.connection.supports_check_constraints? && @model.connection.respond_to?(:check_constraints)

  check_constraints = @model.connection.check_constraints(@model.table_name)
  return Components::NilComponent.new if check_constraints.empty?

  max_size = check_constraints.map { |check_constraint| check_constraint.name.size }.max + 1

  constraints = check_constraints.sort_by(&:name).map do |check_constraint|
    expression = if check_constraint.expression
      if check_constraint.validated?
        "(#{check_constraint.expression.squish})"
      else
        "(#{check_constraint.expression.squish}) NOT VALID".squish
      end
    end

    CheckConstraintComponent.new(check_constraint.name, expression, max_size)
  end

  _annotation = Annotation.new(constraints)
end