Skip to content

Commit a78ca7d

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 3333c35 commit a78ca7d

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
@@ -246,16 +246,7 @@ def get_schema_info(klass, header, options = {})
246246
info << "# #{ '-' * ( max_size + md_names_overhead ) } | #{'-' * md_type_allowance} | #{ '-' * 27 }\n"
247247
end
248248

249-
cols = if ignore_columns = options[:ignore_columns]
250-
klass.columns.reject do |col|
251-
col.name.match(/#{ignore_columns}/)
252-
end
253-
else
254-
klass.columns
255-
end
256-
257-
cols = cols.sort_by(&:name) if options[:sort]
258-
cols = classified_sort(cols) if options[:classified_sort]
249+
cols = columns(klass, options)
259250
cols.each do |col|
260251
col_type = get_col_type(col)
261252
attrs = []
@@ -903,13 +894,15 @@ def with_comments?(klass, options)
903894
end
904895

905896
def max_schema_info_width(klass, options)
897+
cols = columns(klass, options)
898+
906899
if with_comments?(klass, options)
907-
max_size = klass.columns.map do |column|
900+
max_size = cols.map do |column|
908901
column.name.size + (column.comment ? width(column.comment) : 0)
909902
end.max || 0
910903
max_size += 2
911904
else
912-
max_size = klass.column_names.map(&:size).max
905+
max_size = cols.map(&:name).map(&:size).max
913906
end
914907
max_size += options[:format_rdoc] ? 5 : 1
915908

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

942984
class BadModelFileError < LoadError

spec/lib/annotate/annotate_models_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,39 @@ def mock_column(name, type, options = {})
865865
EOS
866866
end
867867

868+
it 'should work with the Globalize gem' do
869+
klass = mock_class(:posts,
870+
:id,
871+
[
872+
mock_column(:id, :integer, limit: 8),
873+
mock_column(:author_name, :string, limit: 50),
874+
])
875+
876+
options = {
877+
to_s: 'Post::Translation',
878+
columns: [
879+
mock_column(:id, :integer, limit: 8),
880+
mock_column(:post_id, :integer, limit: 8),
881+
mock_column(:locale, :string, limit: 50),
882+
mock_column(:title, :string, limit: 50),
883+
]
884+
}
885+
translation_klass = double('Post::Translation', options)
886+
allow(klass).to receive(:translation_class).and_return(translation_klass)
887+
888+
expected_schema_info = <<~EOS
889+
# Schema Info
890+
#
891+
# Table name: posts
892+
#
893+
# id :integer not null, primary key
894+
# author_name :string(50) not null
895+
# title :string(50) not null
896+
#
897+
EOS
898+
expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(expected_schema_info)
899+
end
900+
868901
describe '.set_defaults' do
869902
subject do
870903
Annotate::Helpers.true?(ENV['show_complete_foreign_keys'])

0 commit comments

Comments
 (0)