Skip to content

Commit cb9405c

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 cb9405c

File tree

2 files changed

+87
-12
lines changed

2 files changed

+87
-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.to_sym
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.to_sym
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

spec/lib/annotate/annotate_models_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,39 @@ def mock_column(name, type, options = {})
788788
EOS
789789
end
790790

791+
it 'should work with the Globalize gem', :focus do
792+
klass = mock_class(:posts,
793+
:id,
794+
[
795+
mock_column(:id, :integer, limit: 8),
796+
mock_column(:author_name, :string, limit: 50),
797+
])
798+
799+
options = {
800+
to_s: 'Post::Translation',
801+
columns: [
802+
mock_column(:id, :integer, limit: 8),
803+
mock_column(:post_id, :integer, limit: 8),
804+
mock_column(:locale, :string, limit: 50),
805+
mock_column(:title, :string, limit: 50),
806+
]
807+
}
808+
translation_klass = double('Post::Translation', options)
809+
allow(klass).to receive(:translation_class).and_return(translation_klass)
810+
811+
expected_schema_info = <<~EOS
812+
# Schema Info
813+
#
814+
# Table name: posts
815+
#
816+
# id :integer not null, primary key
817+
# author_name :string(50) not null
818+
# title :string(50) not null
819+
#
820+
EOS
821+
expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(expected_schema_info)
822+
end
823+
791824
describe '#set_defaults' do
792825
it 'should default show_complete_foreign_keys to false' do
793826
expect(Annotate.true?(ENV['show_complete_foreign_keys'])).to be(false)

0 commit comments

Comments
 (0)