Commit d894fa29 by Peter Gundel Committed by Andrew W. Lee

Add columns managed by Globalize gem (#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.
parent 3333c359
...@@ -6,3 +6,7 @@ AllCops: ...@@ -6,3 +6,7 @@ AllCops:
- 'vendor/**/*' - 'vendor/**/*'
- 'spec/fixtures/**/*' - 'spec/fixtures/**/*'
- 'tmp/**/*' - 'tmp/**/*'
Metrics/BlockLength:
Exclude:
- 'spec/**/*.rb'
...@@ -246,16 +246,7 @@ module AnnotateModels ...@@ -246,16 +246,7 @@ module AnnotateModels
info << "# #{ '-' * ( max_size + md_names_overhead ) } | #{'-' * md_type_allowance} | #{ '-' * 27 }\n" info << "# #{ '-' * ( max_size + md_names_overhead ) } | #{'-' * md_type_allowance} | #{ '-' * 27 }\n"
end end
cols = if ignore_columns = options[:ignore_columns] cols = columns(klass, options)
klass.columns.reject do |col|
col.name.match(/#{ignore_columns}/)
end
else
klass.columns
end
cols = cols.sort_by(&:name) if options[:sort]
cols = classified_sort(cols) if options[:classified_sort]
cols.each do |col| cols.each do |col|
col_type = get_col_type(col) col_type = get_col_type(col)
attrs = [] attrs = []
...@@ -903,13 +894,15 @@ module AnnotateModels ...@@ -903,13 +894,15 @@ module AnnotateModels
end end
def max_schema_info_width(klass, options) def max_schema_info_width(klass, options)
cols = columns(klass, options)
if with_comments?(klass, options) if with_comments?(klass, options)
max_size = klass.columns.map do |column| max_size = cols.map do |column|
column.name.size + (column.comment ? width(column.comment) : 0) column.name.size + (column.comment ? width(column.comment) : 0)
end.max || 0 end.max || 0
max_size += 2 max_size += 2
else else
max_size = klass.column_names.map(&:size).max max_size = cols.map(&:name).map(&:size).max
end end
max_size += options[:format_rdoc] ? 5 : 1 max_size += options[:format_rdoc] ? 5 : 1
...@@ -937,6 +930,55 @@ module AnnotateModels ...@@ -937,6 +930,55 @@ module AnnotateModels
def non_ascii_length(string) def non_ascii_length(string)
string.to_s.chars.reject(&:ascii_only?).length string.to_s.chars.reject(&:ascii_only?).length
end end
def columns(klass, options)
cols = klass.columns
cols += translated_columns(klass)
if ignore_columns = options[:ignore_columns]
cols = cols.reject do |col|
col.name.match(/#{ignore_columns}/)
end
end
cols = cols.sort_by(&:name) if options[:sort]
cols = classified_sort(cols) if options[:classified_sort]
cols
end
##
# Add columns managed by the globalize gem if this gem is being used.
def translated_columns(klass)
return [] unless klass.respond_to? :translation_class
ignored_cols = ignored_translation_table_colums(klass)
klass.translation_class.columns.reject do |col|
ignored_cols.include? col.name.to_sym
end
end
##
# These are the columns that the globalize gem needs to work but
# are not necessary for the models to be displayed as annotations.
def ignored_translation_table_colums(klass)
# Construct the foreign column name in the translations table
# eg. Model: Car, foreign column name: car_id
foreign_column_name = [
klass.translation_class.to_s
.gsub('::Translation', '').gsub('::', '_')
.downcase,
'_id'
].join.to_sym
[
:id,
:created_at,
:updated_at,
:locale,
foreign_column_name
]
end
end end
class BadModelFileError < LoadError class BadModelFileError < LoadError
......
...@@ -5,7 +5,7 @@ require 'annotate/active_record_patch' ...@@ -5,7 +5,7 @@ require 'annotate/active_record_patch'
require 'active_support/core_ext/string' require 'active_support/core_ext/string'
require 'files' require 'files'
describe AnnotateModels do # rubocop:disable Metrics/BlockLength describe AnnotateModels do
MAGIC_COMMENTS = [ MAGIC_COMMENTS = [
'# encoding: UTF-8', '# encoding: UTF-8',
'# coding: UTF-8', '# coding: UTF-8',
...@@ -865,6 +865,39 @@ EOS ...@@ -865,6 +865,39 @@ EOS
EOS EOS
end end
it 'should work with the Globalize gem' do
klass = mock_class(:posts,
:id,
[
mock_column(:id, :integer, limit: 8),
mock_column(:author_name, :string, limit: 50),
])
options = {
to_s: 'Post::Translation',
columns: [
mock_column(:id, :integer, limit: 8),
mock_column(:post_id, :integer, limit: 8),
mock_column(:locale, :string, limit: 50),
mock_column(:title, :string, limit: 50),
]
}
translation_klass = double('Post::Translation', options)
allow(klass).to receive(:translation_class).and_return(translation_klass)
expected_schema_info = <<~EOS
# Schema Info
#
# Table name: posts
#
# id :integer not null, primary key
# author_name :string(50) not null
# title :string(50) not null
#
EOS
expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(expected_schema_info)
end
describe '.set_defaults' do describe '.set_defaults' do
subject do subject do
Annotate::Helpers.true?(ENV['show_complete_foreign_keys']) Annotate::Helpers.true?(ENV['show_complete_foreign_keys'])
...@@ -1350,7 +1383,7 @@ EOS ...@@ -1350,7 +1383,7 @@ EOS
end end
end end
describe '.get_model_class' do # rubocop:disable Metrics/BlockLength describe '.get_model_class' do
before :all do before :all do
require 'tmpdir' require 'tmpdir'
AnnotateModels.model_dir = Dir.mktmpdir('annotate_models') AnnotateModels.model_dir = Dir.mktmpdir('annotate_models')
......
require_relative '../../spec_helper' require_relative '../../spec_helper'
module Annotate # rubocop:disable Metrics/ModuleLength module Annotate # rubocop:disable Metrics/ModuleLength
describe Parser do # rubocop:disable Metrics/BlockLength describe Parser do
before(:example) do before(:example) do
ENV.clear ENV.clear
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment