@@ -244,16 +244,7 @@ def get_schema_info(klass, header, options = {})
244
244
info << "# #{ '-' * ( max_size + md_names_overhead ) } | #{ '-' * md_type_allowance } | #{ '-' * 27 } \n "
245
245
end
246
246
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 )
257
248
cols . each do |col |
258
249
col_type = get_col_type ( col )
259
250
attrs = [ ]
@@ -905,13 +896,15 @@ def with_comments?(klass, options)
905
896
end
906
897
907
898
def max_schema_info_width ( klass , options )
899
+ cols = columns ( klass , options )
900
+
908
901
if with_comments? ( klass , options )
909
- max_size = klass . columns . map do |column |
902
+ max_size = cols . map do |column |
910
903
column . name . size + ( column . comment ? width ( column . comment ) : 0 )
911
904
end . max || 0
912
905
max_size += 2
913
906
else
914
- max_size = klass . column_names . map ( &:size ) . max
907
+ max_size = cols . map ( & :name ) . map ( &:size ) . max
915
908
end
916
909
max_size += options [ :format_rdoc ] ? 5 : 1
917
910
@@ -939,6 +932,55 @@ def mb_chars_ljust(string, length)
939
932
def non_ascii_length ( string )
940
933
string . to_s . chars . reject ( &:ascii_only? ) . length
941
934
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
942
984
end
943
985
944
986
class BadModelFileError < LoadError
0 commit comments