Skip to content

Commit da1e605

Browse files
authored
Reactors AnnotateModels.get_schema_info (#791)
This is a bit of a cheat of a refactoring that simply extracts the logic for collecting a column's attributes out of `get_schema_info` and into its own method (`get_attributes`). I found that in PRs like #779 that the Rubocop ABC limit was being exceeded: ``` lib/annotate/annotate_models.rb:235:5: C: Metrics/AbcSize: Assignment Branch Condition size for get_schema_info is too high. [145/145] def get_schema_info(klass, header, options = {}) ... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``` Hopefully, this should break this up and reduce the complexity of the method. There are opportunities to go further, but I thought this could be a good place to start. I would be open and interested in discussing further refactoring opportunities if it would make sense (maybe creating some new classes to encapsulate some of this logic).
1 parent d89ddef commit da1e605

File tree

1 file changed

+50
-41
lines changed

1 file changed

+50
-41
lines changed

lib/annotate/annotate_models.rb

+50-41
Original file line numberDiff line numberDiff line change
@@ -249,47 +249,7 @@ def get_schema_info(klass, header, options = {})
249249
cols = columns(klass, options)
250250
cols.each do |col|
251251
col_type = get_col_type(col)
252-
attrs = []
253-
attrs << "default(#{schema_default(klass, col)})" unless col.default.nil? || hide_default?(col_type, options)
254-
attrs << 'unsigned' if col.respond_to?(:unsigned?) && col.unsigned?
255-
attrs << 'not null' unless col.null
256-
attrs << 'primary key' if klass.primary_key && (klass.primary_key.is_a?(Array) ? klass.primary_key.collect(&:to_sym).include?(col.name.to_sym) : col.name.to_sym == klass.primary_key.to_sym)
257-
258-
if col_type == 'decimal'
259-
col_type << "(#{col.precision}, #{col.scale})"
260-
elsif !%w[spatial geometry geography].include?(col_type)
261-
if col.limit && !options[:format_yard]
262-
if col.limit.is_a? Array
263-
attrs << "(#{col.limit.join(', ')})"
264-
else
265-
col_type << "(#{col.limit})" unless hide_limit?(col_type, options)
266-
end
267-
end
268-
end
269-
270-
# Check out if we got an array column
271-
attrs << 'is an Array' if col.respond_to?(:array) && col.array
272-
273-
# Check out if we got a geometric column
274-
# and print the type and SRID
275-
if col.respond_to?(:geometry_type)
276-
attrs << "#{col.geometry_type}, #{col.srid}"
277-
elsif col.respond_to?(:geometric_type) && col.geometric_type.present?
278-
attrs << "#{col.geometric_type.to_s.downcase}, #{col.srid}"
279-
end
280-
281-
# Check if the column has indices and print "indexed" if true
282-
# If the index includes another column, print it too.
283-
if options[:simple_indexes] && klass.table_exists?# Check out if this column is indexed
284-
indices = retrieve_indexes_from_table(klass)
285-
if indices = indices.select { |ind| ind.columns.include? col.name }
286-
indices.sort_by(&:name).each do |ind|
287-
next if ind.columns.is_a?(String)
288-
ind = ind.columns.reject! { |i| i == col.name }
289-
attrs << (ind.empty? ? "indexed" : "indexed => [#{ind.join(", ")}]")
290-
end
291-
end
292-
end
252+
attrs = get_attributes(col, col_type, klass, options)
293253
col_name = if with_comments?(klass, options) && col.comment
294254
"#{col.name}(#{col.comment})"
295255
else
@@ -996,6 +956,55 @@ def ignored_translation_table_colums(klass)
996956
foreign_column_name
997957
]
998958
end
959+
960+
##
961+
# Get the list of attributes that should be included in the annotation for
962+
# a given column.
963+
def get_attributes(column, column_type, klass, options)
964+
attrs = []
965+
attrs << "default(#{schema_default(klass, column)})" unless column.default.nil? || hide_default?(column_type, options)
966+
attrs << 'unsigned' if column.respond_to?(:unsigned?) && column.unsigned?
967+
attrs << 'not null' unless column.null
968+
attrs << 'primary key' if klass.primary_key && (klass.primary_key.is_a?(Array) ? klass.primary_key.collect(&:to_sym).include?(column.name.to_sym) : column.name.to_sym == klass.primary_key.to_sym)
969+
970+
if column_type == 'decimal'
971+
column_type << "(#{column.precision}, #{column.scale})"
972+
elsif !%w[spatial geometry geography].include?(column_type)
973+
if column.limit && !options[:format_yard]
974+
if column.limit.is_a? Array
975+
attrs << "(#{column.limit.join(', ')})"
976+
else
977+
column_type << "(#{column.limit})" unless hide_limit?(column_type, options)
978+
end
979+
end
980+
end
981+
982+
# Check out if we got an array columnumn
983+
attrs << 'is an Array' if column.respond_to?(:array) && column.array
984+
985+
# Check out if we got a geometric columnumn
986+
# and print the type and SRID
987+
if column.respond_to?(:geometry_type)
988+
attrs << "#{column.geometry_type}, #{column.srid}"
989+
elsif column.respond_to?(:geometric_type) && column.geometric_type.present?
990+
attrs << "#{column.geometric_type.to_s.downcase}, #{column.srid}"
991+
end
992+
993+
# Check if the column has indices and print "indexed" if true
994+
# If the index includes another column, print it too.
995+
if options[:simple_indexes] && klass.table_exists?# Check out if this column is indexed
996+
indices = retrieve_indexes_from_table(klass)
997+
if indices = indices.select { |ind| ind.columns.include? column.name }
998+
indices.sort_by(&:name).each do |ind|
999+
next if ind.columns.is_a?(String)
1000+
ind = ind.columns.reject! { |i| i == column.name }
1001+
attrs << (ind.empty? ? "indexed" : "indexed => [#{ind.join(", ")}]")
1002+
end
1003+
end
1004+
end
1005+
1006+
attrs
1007+
end
9991008
end
10001009

10011010
class BadModelFileError < LoadError

0 commit comments

Comments
 (0)