Commit 53941460 by Daniel Hanke Committed by Cuong Tran

Optional fk-truncation and allow rake 12 (#458)

parent 8910f041
...@@ -43,6 +43,6 @@ Gem::Specification.new do |s| ...@@ -43,6 +43,6 @@ Gem::Specification.new do |s|
s.summary = 'Annotates Rails Models, routes, fixtures, and others based on the database schema.' s.summary = 'Annotates Rails Models, routes, fixtures, and others based on the database schema.'
s.specification_version = 4 if s.respond_to? :specification_version s.specification_version = 4 if s.respond_to? :specification_version
s.add_runtime_dependency(%q<rake>, ['>= 10.4']) s.add_runtime_dependency(%q<rake>, ['>= 10.4', '< 13.0'])
s.add_runtime_dependency(%q<activerecord>, ['>= 3.2', '< 6.0']) s.add_runtime_dependency(%q<activerecord>, ['>= 3.2', '< 6.0'])
end end
...@@ -374,7 +374,7 @@ module AnnotateModels ...@@ -374,7 +374,7 @@ module AnnotateModels
foreign_keys = klass.connection.foreign_keys(klass.table_name) foreign_keys = klass.connection.foreign_keys(klass.table_name)
return '' if foreign_keys.empty? return '' if foreign_keys.empty?
format_name = ->(fk) { fk.name.gsub(/(?<=^fk_rails_)[0-9a-f]{10}$/, '...') } format_name = ->(fk) { options[:show_complete_foreign_keys] ? fk.name : fk.name.gsub(/(?<=^fk_rails_)[0-9a-f]{10}$/, '...') }
max_size = foreign_keys.map(&format_name).map(&:size).max + 1 max_size = foreign_keys.map(&format_name).map(&:size).max + 1
foreign_keys.sort_by {|fk| [format_name.call(fk), fk.column]}.each do |fk| foreign_keys.sort_by {|fk| [format_name.call(fk), fk.column]}.each do |fk|
......
...@@ -15,6 +15,7 @@ if Rails.env.development? ...@@ -15,6 +15,7 @@ if Rails.env.development?
'position_in_factory' => 'before', 'position_in_factory' => 'before',
'position_in_serializer' => 'before', 'position_in_serializer' => 'before',
'show_foreign_keys' => 'true', 'show_foreign_keys' => 'true',
'show_complete_foreign_keys' => 'false',
'show_indexes' => 'true', 'show_indexes' => 'true',
'simple_indexes' => 'false', 'simple_indexes' => 'false',
'model_dir' => 'app/models', 'model_dir' => 'app/models',
......
...@@ -18,6 +18,7 @@ task annotate_models: :environment do ...@@ -18,6 +18,7 @@ task annotate_models: :environment do
options[:position_in_test] = Annotate.fallback(ENV['position_in_test'], ENV['position']) options[:position_in_test] = Annotate.fallback(ENV['position_in_test'], ENV['position'])
options[:position_in_serializer] = Annotate.fallback(ENV['position_in_serializer'], ENV['position']) options[:position_in_serializer] = Annotate.fallback(ENV['position_in_serializer'], ENV['position'])
options[:show_foreign_keys] = Annotate.true?(ENV['show_foreign_keys']) options[:show_foreign_keys] = Annotate.true?(ENV['show_foreign_keys'])
options[:show_complete_foreign_keys] = Annotate.true?(ENV['show_complete_foreign_keys'])
options[:show_indexes] = Annotate.true?(ENV['show_indexes']) options[:show_indexes] = Annotate.true?(ENV['show_indexes'])
options[:simple_indexes] = Annotate.true?(ENV['simple_indexes']) options[:simple_indexes] = Annotate.true?(ENV['simple_indexes'])
options[:model_dir] = ENV['model_dir'] ? ENV['model_dir'].split(',') : ['app/models'] options[:model_dir] = ENV['model_dir'] ? ENV['model_dir'].split(',') : ['app/models']
......
...@@ -229,6 +229,42 @@ EOS ...@@ -229,6 +229,42 @@ EOS
EOS EOS
end end
it 'should get complete foreign key info' do
klass = mock_class(:users,
:id,
[
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer)
],
[],
[
mock_foreign_key('fk_rails_cf2568e89e',
'foreign_thing_id',
'foreign_things'),
mock_foreign_key('custom_fk_name',
'other_thing_id',
'other_things'),
mock_foreign_key('fk_rails_a70234b26c',
'third_thing_id',
'third_things')
])
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_foreign_keys: true, show_complete_foreign_keys: true)).to eql(<<-EOS)
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# foreign_thing_id :integer not null
#
# Foreign Keys
#
# custom_fk_name (other_thing_id => other_things.id)
# fk_rails_a70234b26c (third_thing_id => third_things.id)
# fk_rails_cf2568e89e (foreign_thing_id => foreign_things.id)
#
EOS
end
it 'should get foreign key info if on_delete/on_update options present' do it 'should get foreign key info if on_delete/on_update options present' do
klass = mock_class(:users, klass = mock_class(:users,
:id, :id,
......
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