Skip to content

Commit 0304c13

Browse files
committed
Add columns managed by Globalize gem
Globalize hooks into the model and removes the translated columns from the `klass.columns`. This commit checks if globalize is hooked into the model and adds the necessary columns to the annotation array.
1 parent a05e458 commit 0304c13

File tree

1 file changed

+54
-12
lines changed

1 file changed

+54
-12
lines changed

lib/annotate/annotate_models.rb

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,7 @@ def get_schema_info(klass, header, options = {})
244244
info << "# #{ '-' * ( max_size + md_names_overhead ) } | #{'-' * md_type_allowance} | #{ '-' * 27 }\n"
245245
end
246246

247-
cols = if ignore_columns = options[:ignore_columns]
248-
klass.columns.reject do |col|
249-
col.name.match(/#{ignore_columns}/)
250-
end
251-
else
252-
klass.columns
253-
end
254-
255-
cols = cols.sort_by(&:name) if options[:sort]
256-
cols = classified_sort(cols) if options[:classified_sort]
247+
cols = columns(klass, options)
257248
cols.each do |col|
258249
col_type = get_col_type(col)
259250
attrs = []
@@ -905,13 +896,15 @@ def with_comments?(klass, options)
905896
end
906897

907898
def max_schema_info_width(klass, options)
899+
cols = columns(klass, options)
900+
908901
if with_comments?(klass, options)
909-
max_size = klass.columns.map do |column|
902+
max_size = cols.map do |column|
910903
column.name.size + (column.comment ? width(column.comment) : 0)
911904
end.max || 0
912905
max_size += 2
913906
else
914-
max_size = klass.column_names.map(&:size).max
907+
max_size = cols.map(&:name).map(&:size).max
915908
end
916909
max_size += options[:format_rdoc] ? 5 : 1
917910

@@ -939,6 +932,55 @@ def mb_chars_ljust(string, length)
939932
def non_ascii_length(string)
940933
string.to_s.chars.reject(&:ascii_only?).length
941934
end
935+
936+
def columns(klass, options)
937+
cols = klass.columns
938+
cols += translated_columns(klass)
939+
940+
if ignore_columns = options[:ignore_columns]
941+
cols = cols.reject do |col|
942+
col.name.match(/#{ignore_columns}/)
943+
end
944+
end
945+
946+
cols = cols.sort_by(&:name) if options[:sort]
947+
cols = classified_sort(cols) if options[:classified_sort]
948+
949+
cols
950+
end
951+
952+
##
953+
# Add columns managed by the globalize gem if this gem is being used.
954+
def translated_columns(klass)
955+
return [] unless klass.respond_to? :translation_class
956+
957+
ignored_cols = ignored_translation_table_colums(klass)
958+
klass.translation_class.columns.reject do |col|
959+
ignored_cols.include? col.name
960+
end
961+
end
962+
963+
##
964+
# These are the columns that the globalize gem needs to work but
965+
# are not necessary for the models to be displayed as annotations.
966+
def ignored_translation_table_colums(klass)
967+
# Construct the foreign column name in the translations table
968+
# eg. Model: Car, foreign column name: car_id
969+
foreign_column_name = [
970+
klass.translation_class.to_s
971+
.gsub('::Translation', '').gsub('::', '_')
972+
.downcase,
973+
'_id'
974+
].join
975+
976+
[
977+
'id',
978+
'created_at',
979+
'updated_at',
980+
'locale',
981+
foreign_column_name
982+
]
983+
end
942984
end
943985

944986
class BadModelFileError < LoadError

0 commit comments

Comments
 (0)