Skip to content

Add columns managed by Globalize gem #602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ AllCops:
- 'vendor/**/*'
- 'spec/fixtures/**/*'
- 'tmp/**/*'

Metrics/BlockLength:
Exclude:
- 'spec/**/*.rb'
66 changes: 54 additions & 12 deletions lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,7 @@ def get_schema_info(klass, header, options = {})
info << "# #{ '-' * ( max_size + md_names_overhead ) } | #{'-' * md_type_allowance} | #{ '-' * 27 }\n"
end

cols = if ignore_columns = options[:ignore_columns]
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 = columns(klass, options)
cols.each do |col|
col_type = get_col_type(col)
attrs = []
Expand Down Expand Up @@ -903,13 +894,15 @@ def with_comments?(klass, options)
end

def max_schema_info_width(klass, options)
cols = columns(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)
end.max || 0
max_size += 2
else
max_size = klass.column_names.map(&:size).max
max_size = cols.map(&:name).map(&:size).max
end
max_size += options[:format_rdoc] ? 5 : 1

Expand Down Expand Up @@ -937,6 +930,55 @@ def mb_chars_ljust(string, length)
def non_ascii_length(string)
string.to_s.chars.reject(&:ascii_only?).length
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

class BadModelFileError < LoadError
Expand Down
37 changes: 35 additions & 2 deletions spec/lib/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require 'active_support/core_ext/string'
require 'files'

describe AnnotateModels do # rubocop:disable Metrics/BlockLength
describe AnnotateModels do
MAGIC_COMMENTS = [
'# encoding: UTF-8',
'# coding: UTF-8',
Expand Down Expand Up @@ -865,6 +865,39 @@ def mock_column(name, type, options = {})
EOS
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
subject do
Annotate::Helpers.true?(ENV['show_complete_foreign_keys'])
Expand Down Expand Up @@ -1350,7 +1383,7 @@ def self.when_called_with(options = {})
end
end

describe '.get_model_class' do # rubocop:disable Metrics/BlockLength
describe '.get_model_class' do
before :all do
require 'tmpdir'
AnnotateModels.model_dir = Dir.mktmpdir('annotate_models')
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/annotate/parser_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require_relative '../../spec_helper'

module Annotate # rubocop:disable Metrics/ModuleLength
describe Parser do # rubocop:disable Metrics/BlockLength
describe Parser do
before(:example) do
ENV.clear
end
Expand Down