Skip to content

Commit c86ed63

Browse files
peterficationvfonic
authored andcommitted
Add columns managed by Globalize gem (ctran#602)
* 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. * Disable Rubocop Metrics/BlockLength for spec files RSpec spec files can contain long blocks easily because of the outher describe methods. So this rule makes not too much sense for these files.
1 parent 76dddea commit c86ed63

File tree

4 files changed

+94
-15
lines changed

4 files changed

+94
-15
lines changed

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ AllCops:
66
- 'vendor/**/*'
77
- 'spec/fixtures/**/*'
88
- 'tmp/**/*'
9+
10+
Metrics/BlockLength:
11+
Exclude:
12+
- 'spec/**/*.rb'

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: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
require 'active_support/core_ext/string'
66
require 'files'
77

8-
describe AnnotateModels do # rubocop:disable Metrics/BlockLength
8+
describe AnnotateModels do
99
MAGIC_COMMENTS = [
1010
'# encoding: UTF-8',
1111
'# coding: UTF-8',
@@ -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'])
@@ -1350,7 +1383,7 @@ def self.when_called_with(options = {})
13501383
end
13511384
end
13521385

1353-
describe '.get_model_class' do # rubocop:disable Metrics/BlockLength
1386+
describe '.get_model_class' do
13541387
before :all do
13551388
require 'tmpdir'
13561389
AnnotateModels.model_dir = Dir.mktmpdir('annotate_models')

spec/lib/annotate/parser_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require_relative '../../spec_helper'
22

33
module Annotate # rubocop:disable Metrics/ModuleLength
4-
describe Parser do # rubocop:disable Metrics/BlockLength
4+
describe Parser do
55
before(:example) do
66
ENV.clear
77
end

0 commit comments

Comments
 (0)