Skip to content

Commit 97a5232

Browse files
committed
Handle missing expression
Signed-off-by: Lovro Bikic <[email protected]>
1 parent 7cc5481 commit 97a5232

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

lib/annotate/annotate_models.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,14 @@ def get_check_constraint_info(klass, options = {})
371371

372372
max_size = check_constraints.map { |check_constraint| check_constraint.name.size }.max + 1
373373
check_constraints.sort_by(&:name).each do |check_constraint|
374+
expression = check_constraint.expression ? "(#{check_constraint.expression.squish})" : nil
375+
374376
cc_info << if options[:format_markdown]
375-
sprintf("# * `%s`: `(%s)`\n", check_constraint.name, check_constraint.expression.squish)
377+
cc_info_markdown = sprintf("# * `%s`", check_constraint.name)
378+
cc_info_markdown << sprintf(": `%s`", expression) if expression
379+
cc_info_markdown << "\n"
376380
else
377-
sprintf("# %-#{max_size}.#{max_size}s (%s)\n", check_constraint.name, check_constraint.expression.squish)
381+
sprintf("# %-#{max_size}.#{max_size}s %s", check_constraint.name, expression).rstrip + "\n"
378382
end
379383
end
380384

spec/lib/annotate/annotate_models_spec.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ def mock_column(name, type, options = {})
788788
[
789789
mock_check_constraint('alive', 'age < 150'),
790790
mock_check_constraint('must_be_adult', 'age >= 18'),
791+
mock_check_constraint('missing_expression', nil),
791792
mock_check_constraint('multiline_test', <<~SQL)
792793
CASE
793794
WHEN (age >= 18) THEN (age <= 21)
@@ -808,9 +809,10 @@ def mock_column(name, type, options = {})
808809
#
809810
# Check Constraints
810811
#
811-
# alive (age < 150)
812-
# multiline_test (CASE WHEN (age >= 18) THEN (age <= 21) ELSE true END)
813-
# must_be_adult (age >= 18)
812+
# alive (age < 150)
813+
# missing_expression
814+
# multiline_test (CASE WHEN (age >= 18) THEN (age <= 21) ELSE true END)
815+
# must_be_adult (age >= 18)
814816
#
815817
EOS
816818
end
@@ -1589,6 +1591,7 @@ def mock_column(name, type, options = {})
15891591
let :check_constraints do
15901592
[
15911593
mock_check_constraint('min_name_length', 'LENGTH(name) > 2'),
1594+
mock_check_constraint('missing_expression', nil),
15921595
mock_check_constraint('multiline_test', <<~SQL)
15931596
CASE
15941597
WHEN (age >= 18) THEN (age <= 21)
@@ -1614,6 +1617,7 @@ def mock_column(name, type, options = {})
16141617
# ### Check Constraints
16151618
#
16161619
# * `min_name_length`: `(LENGTH(name) > 2)`
1620+
# * `missing_expression`
16171621
# * `multiline_test`: `(CASE WHEN (age >= 18) THEN (age <= 21) ELSE true END)`
16181622
#
16191623
EOS

0 commit comments

Comments
 (0)