Skip to content

Commit 5c7c90a

Browse files
authored
Merge branch 'ctran:develop' into support-routing-frozen
2 parents 66ce208 + ea4cd00 commit 5c7c90a

File tree

8 files changed

+239
-24
lines changed

8 files changed

+239
-24
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ you can do so with a simple environment variable, instead of editing the
224224
-a, --active-admin Annotate active_admin models
225225
-v, --version Show the current version of this gem
226226
-m, --show-migration Include the migration version number in the annotation
227+
-c, --show-check-constraints List the table's check constraints in the annotation
227228
-k, --show-foreign-keys List the table's foreign key constraints in the annotation
228229
--ck, --complete-foreign-keys
229230
Complete foreign key names in the annotation

lib/annotate/annotate_models.rb

+34-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def retrieve_indexes_from_table(klass)
131131
# to create a comment block containing a line for
132132
# each column. The line contains the column name,
133133
# the type (and length), and any optional attributes
134-
def get_schema_info(klass, header, options = {})
134+
def get_schema_info(klass, header, options = {}) # rubocop:disable Metrics/MethodLength
135135
info = "# #{header}\n"
136136
info << get_schema_header_text(klass, options)
137137

@@ -178,6 +178,10 @@ def get_schema_info(klass, header, options = {})
178178
info << get_foreign_key_info(klass, options)
179179
end
180180

181+
if options[:show_check_constraints] && klass.table_exists?
182+
info << get_check_constraint_info(klass, options)
183+
end
184+
181185
info << get_schema_footer_text(klass, options)
182186
end
183187

@@ -352,6 +356,35 @@ def get_foreign_key_info(klass, options = {})
352356
fk_info
353357
end
354358

359+
def get_check_constraint_info(klass, options = {})
360+
cc_info = if options[:format_markdown]
361+
"#\n# ### Check Constraints\n#\n"
362+
else
363+
"#\n# Check Constraints\n#\n"
364+
end
365+
366+
return '' unless klass.connection.respond_to?(:supports_check_constraints?) &&
367+
klass.connection.supports_check_constraints? && klass.connection.respond_to?(:check_constraints)
368+
369+
check_constraints = klass.connection.check_constraints(klass.table_name)
370+
return '' if check_constraints.empty?
371+
372+
max_size = check_constraints.map { |check_constraint| check_constraint.name.size }.max + 1
373+
check_constraints.sort_by(&:name).each do |check_constraint|
374+
expression = check_constraint.expression ? "(#{check_constraint.expression.squish})" : nil
375+
376+
cc_info << if options[:format_markdown]
377+
cc_info_markdown = sprintf("# * `%s`", check_constraint.name)
378+
cc_info_markdown << sprintf(": `%s`", expression) if expression
379+
cc_info_markdown << "\n"
380+
else
381+
sprintf("# %-#{max_size}.#{max_size}s %s", check_constraint.name, expression).rstrip + "\n"
382+
end
383+
end
384+
385+
cc_info
386+
end
387+
355388
# Add a schema block to a file. If the file already contains
356389
# a schema info block (a comment starting with "== Schema Information"),
357390
# check if it matches the block that is already there. If so, leave it be.

lib/annotate/constants.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ module Constants
1818
:trace, :timestamp, :exclude_serializers, :classified_sort,
1919
:show_foreign_keys, :show_complete_foreign_keys,
2020
:exclude_scaffolds, :exclude_controllers, :exclude_helpers,
21-
:exclude_sti_subclasses, :ignore_unknown_models, :with_comment
21+
:exclude_sti_subclasses, :ignore_unknown_models, :with_comment,
22+
:show_check_constraints
2223
].freeze
2324

2425
OTHER_OPTIONS = [

lib/annotate/parser.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def commit
4848
end
4949
end
5050

51-
def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength
51+
def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
5252
has_set_position = {}
5353

5454
option_parser.banner = 'Usage: annotate [options] [model_file]*'
@@ -173,6 +173,12 @@ def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength
173173
env['include_version'] = 'yes'
174174
end
175175

176+
option_parser.on('-c',
177+
'--show-check-constraints',
178+
"List the table's check constraints in the annotation") do
179+
env['show_check_constraints'] = 'yes'
180+
end
181+
176182
option_parser.on('-k',
177183
'--show-foreign-keys',
178184
"List the table's foreign key constraints in the annotation") do

lib/generators/annotate/templates/auto_annotate_models.rake

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ if Rails.env.development?
1717
'position_in_fixture' => 'before',
1818
'position_in_factory' => 'before',
1919
'position_in_serializer' => 'before',
20+
'show_check_constraints' => 'false',
2021
'show_foreign_keys' => 'true',
2122
'show_complete_foreign_keys' => 'false',
2223
'show_indexes' => 'true',

lib/tasks/annotate_models.rake

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ task annotate_models: :environment do
1818
options[:position_in_factory] = Annotate::Helpers.fallback(ENV['position_in_factory'], ENV['position'])
1919
options[:position_in_test] = Annotate::Helpers.fallback(ENV['position_in_test'], ENV['position'])
2020
options[:position_in_serializer] = Annotate::Helpers.fallback(ENV['position_in_serializer'], ENV['position'])
21+
options[:show_check_constraints] = Annotate::Helpers.true?(ENV['show_check_constraints'])
2122
options[:show_foreign_keys] = Annotate::Helpers.true?(ENV['show_foreign_keys'])
2223
options[:show_complete_foreign_keys] = Annotate::Helpers.true?(ENV['show_complete_foreign_keys'])
2324
options[:show_indexes] = Annotate::Helpers.true?(ENV['show_indexes'])

0 commit comments

Comments
 (0)