Sha256: 47406870fcb4b36feedab586cc733f4458e15a15d59bbdd5f323a58cd8c87b7f

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module AnnotateRb
  module ModelAnnotator
    module CheckConstraintAnnotation
      class AnnotationBuilder
        def initialize(model, options)
          @model = model
          @options = options
        end

        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
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
annotaterb-4.13.0 lib/annotate_rb/model_annotator/check_constraint_annotation/annotation_builder.rb
annotaterb-4.12.0 lib/annotate_rb/model_annotator/check_constraint_annotation/annotation_builder.rb
annotaterb-4.11.0 lib/annotate_rb/model_annotator/check_constraint_annotation/annotation_builder.rb
annotaterb-4.10.2 lib/annotate_rb/model_annotator/check_constraint_annotation/annotation_builder.rb