@@ -249,47 +249,7 @@ def get_schema_info(klass, header, options = {})
249
249
cols = columns ( klass , options )
250
250
cols . each do |col |
251
251
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 )
293
253
col_name = if with_comments? ( klass , options ) && col . comment
294
254
"#{ col . name } (#{ col . comment } )"
295
255
else
@@ -994,6 +954,55 @@ def ignored_translation_table_colums(klass)
994
954
foreign_column_name
995
955
]
996
956
end
957
+
958
+ ##
959
+ # Get the list of attributes that should be included in the annotation for
960
+ # a given column.
961
+ def get_attributes ( column , column_type , klass , options )
962
+ attrs = [ ]
963
+ attrs << "default(#{ schema_default ( klass , column ) } )" unless column . default . nil? || hide_default? ( column_type , options )
964
+ attrs << 'unsigned' if column . respond_to? ( :unsigned? ) && column . unsigned?
965
+ attrs << 'not null' unless column . null
966
+ 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 )
967
+
968
+ if column_type == 'decimal'
969
+ column_type << "(#{ column . precision } , #{ column . scale } )"
970
+ elsif !%w[ spatial geometry geography ] . include? ( column_type )
971
+ if column . limit && !options [ :format_yard ]
972
+ if column . limit . is_a? Array
973
+ attrs << "(#{ column . limit . join ( ', ' ) } )"
974
+ else
975
+ column_type << "(#{ column . limit } )" unless hide_limit? ( column_type , options )
976
+ end
977
+ end
978
+ end
979
+
980
+ # Check out if we got an array columnumn
981
+ attrs << 'is an Array' if column . respond_to? ( :array ) && column . array
982
+
983
+ # Check out if we got a geometric columnumn
984
+ # and print the type and SRID
985
+ if column . respond_to? ( :geometry_type )
986
+ attrs << "#{ column . geometry_type } , #{ column . srid } "
987
+ elsif column . respond_to? ( :geometric_type ) && column . geometric_type . present?
988
+ attrs << "#{ column . geometric_type . to_s . downcase } , #{ column . srid } "
989
+ end
990
+
991
+ # Check if the column has indices and print "indexed" if true
992
+ # If the index includes another column, print it too.
993
+ if options [ :simple_indexes ] && klass . table_exists? # Check out if this column is indexed
994
+ indices = retrieve_indexes_from_table ( klass )
995
+ if indices = indices . select { |ind | ind . columns . include? column . name }
996
+ indices . sort_by ( &:name ) . each do |ind |
997
+ next if ind . columns . is_a? ( String )
998
+ ind = ind . columns . reject! { |i | i == column . name }
999
+ attrs << ( ind . empty? ? "indexed" : "indexed => [#{ ind . join ( ", " ) } ]" )
1000
+ end
1001
+ end
1002
+ end
1003
+
1004
+ attrs
1005
+ end
997
1006
end
998
1007
999
1008
class BadModelFileError < LoadError
0 commit comments