Skip to content

Commit bc0d5ef

Browse files
committed
1 parent f2c02d0 commit bc0d5ef

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

lib/annotate_rb/model_annotator.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ module ModelAnnotator
2626
autoload :AnnotatedFile, "annotate_rb/model_annotator/annotated_file"
2727
autoload :FileParser, "annotate_rb/model_annotator/file_parser"
2828
autoload :ZeitwerkClassGetter, "annotate_rb/model_annotator/zeitwerk_class_getter"
29+
autoload :CheckConstraintAnnotation, "annotate_rb/model_annotator/check_constraint_annotation"
2930
end
3031
end

lib/annotate_rb/model_annotator/annotation_builder.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ def build
4444
@info += ForeignKeyAnnotation::AnnotationBuilder.new(@model, @options).build
4545
end
4646

47+
if @options[:show_check_constraints] && @model.table_exists?
48+
@info += CheckConstraintAnnotation::AnnotationBuilder.new(@model, @options).build
49+
end
50+
4751
@info += schema_footer_text
4852

4953
@info
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
module AnnotateRb
4+
module ModelAnnotator
5+
module CheckConstraintAnnotation
6+
autoload :AnnotationBuilder, "annotate_rb/model_annotator/check_constraint_annotation/annotation_builder"
7+
end
8+
end
9+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
module AnnotateRb
4+
module ModelAnnotator
5+
module CheckConstraintAnnotation
6+
class AnnotationBuilder
7+
def initialize(model, options)
8+
@model = model
9+
@options = options
10+
end
11+
12+
def build
13+
constraint_info = if @options[:format_markdown]
14+
"#\n# ### Check Constraints\n#\n"
15+
else
16+
"#\n# Check Constraints\n#\n"
17+
end
18+
19+
klass = @model.instance_variable_get(:@klass)
20+
21+
return "" unless klass.connection.respond_to?(:supports_check_constraints?) &&
22+
klass.connection.supports_check_constraints? && klass.connection.respond_to?(:check_constraints)
23+
24+
check_constraints = klass.connection.check_constraints(klass.table_name)
25+
return "" if check_constraints.empty?
26+
27+
max_size = check_constraints.map { |check_constraint| check_constraint.name.size }.max + 1
28+
check_constraints.sort_by(&:name).each do |check_constraint|
29+
expression = check_constraint.expression ? "(#{check_constraint.expression.squish})" : nil
30+
31+
constraint_info += if @options[:format_markdown]
32+
cc_info_markdown = sprintf("# * `%s`", check_constraint.name)
33+
cc_info_markdown += sprintf(": `%s`", expression) if expression
34+
cc_info_markdown += "\n"
35+
36+
cc_info_markdown
37+
else
38+
# standard:disable Lint/FormatParameterMismatch
39+
sprintf("# %-#{max_size}.#{max_size}s %s", check_constraint.name, expression).rstrip + "\n"
40+
# standard:enable Lint/FormatParameterMismatch
41+
end
42+
end
43+
44+
constraint_info
45+
end
46+
47+
private
48+
end
49+
end
50+
end
51+
end

0 commit comments

Comments
 (0)