Skip to content

Commit f2216b8

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 f2216b8

File tree

1 file changed

+41
-12
lines changed

1 file changed

+41
-12
lines changed

lib/annotate/annotate_models.rb

Lines changed: 41 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,42 @@ 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+
939+
if ignore_columns = options[:ignore_columns]
940+
cols = cols.reject do |col|
941+
col.name.match(/#{ignore_columns}/)
942+
end
943+
end
944+
945+
# Add columns managed by the globalize gem
946+
if klass.respond_to? :translation_class
947+
# These are the columns that Globalize needs to work but
948+
# are not necessary for the models to be displayed as annotations
949+
ignored_translation_table_colums = [
950+
'id',
951+
'created_at',
952+
'updated_at',
953+
'locale',
954+
# Construct the foreign column name in the translations table
955+
# eg. Model: Car, foreign column name: car_id
956+
klass.translation_class.to_s
957+
.gsub('::Translation', '').gsub('::', '_')
958+
.downcase + '_id'
959+
]
960+
961+
cols += klass.translation_class.columns.reject do |col|
962+
ignored_translation_table_colums.include? col.name
963+
end
964+
end
965+
966+
cols = cols.sort_by(&:name) if options[:sort]
967+
cols = classified_sort(cols) if options[:classified_sort]
968+
969+
cols
970+
end
942971
end
943972

944973
class BadModelFileError < LoadError

0 commit comments

Comments
 (0)