Skip to content

Commit 5394146

Browse files
thedanielhankectran
authored andcommitted
Optional fk-truncation and allow rake 12 (#458)
1 parent 8910f04 commit 5394146

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

annotate.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ Gem::Specification.new do |s|
4343
s.summary = 'Annotates Rails Models, routes, fixtures, and others based on the database schema.'
4444

4545
s.specification_version = 4 if s.respond_to? :specification_version
46-
s.add_runtime_dependency(%q<rake>, ['>= 10.4'])
46+
s.add_runtime_dependency(%q<rake>, ['>= 10.4', '< 13.0'])
4747
s.add_runtime_dependency(%q<activerecord>, ['>= 3.2', '< 6.0'])
4848
end

lib/annotate/annotate_models.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def get_foreign_key_info(klass, options = {})
374374
foreign_keys = klass.connection.foreign_keys(klass.table_name)
375375
return '' if foreign_keys.empty?
376376

377-
format_name = ->(fk) { fk.name.gsub(/(?<=^fk_rails_)[0-9a-f]{10}$/, '...') }
377+
format_name = ->(fk) { options[:show_complete_foreign_keys] ? fk.name : fk.name.gsub(/(?<=^fk_rails_)[0-9a-f]{10}$/, '...') }
378378

379379
max_size = foreign_keys.map(&format_name).map(&:size).max + 1
380380
foreign_keys.sort_by {|fk| [format_name.call(fk), fk.column]}.each do |fk|

lib/generators/annotate/templates/auto_annotate_models.rake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ if Rails.env.development?
1515
'position_in_factory' => 'before',
1616
'position_in_serializer' => 'before',
1717
'show_foreign_keys' => 'true',
18+
'show_complete_foreign_keys' => 'false',
1819
'show_indexes' => 'true',
1920
'simple_indexes' => 'false',
2021
'model_dir' => 'app/models',

lib/tasks/annotate_models.rake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ task annotate_models: :environment do
1818
options[:position_in_test] = Annotate.fallback(ENV['position_in_test'], ENV['position'])
1919
options[:position_in_serializer] = Annotate.fallback(ENV['position_in_serializer'], ENV['position'])
2020
options[:show_foreign_keys] = Annotate.true?(ENV['show_foreign_keys'])
21+
options[:show_complete_foreign_keys] = Annotate.true?(ENV['show_complete_foreign_keys'])
2122
options[:show_indexes] = Annotate.true?(ENV['show_indexes'])
2223
options[:simple_indexes] = Annotate.true?(ENV['simple_indexes'])
2324
options[:model_dir] = ENV['model_dir'] ? ENV['model_dir'].split(',') : ['app/models']

spec/annotate/annotate_models_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,42 @@ def mock_column(name, type, options = {})
229229
EOS
230230
end
231231

232+
it 'should get complete foreign key info' do
233+
klass = mock_class(:users,
234+
:id,
235+
[
236+
mock_column(:id, :integer),
237+
mock_column(:foreign_thing_id, :integer)
238+
],
239+
[],
240+
[
241+
mock_foreign_key('fk_rails_cf2568e89e',
242+
'foreign_thing_id',
243+
'foreign_things'),
244+
mock_foreign_key('custom_fk_name',
245+
'other_thing_id',
246+
'other_things'),
247+
mock_foreign_key('fk_rails_a70234b26c',
248+
'third_thing_id',
249+
'third_things')
250+
])
251+
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_foreign_keys: true, show_complete_foreign_keys: true)).to eql(<<-EOS)
252+
# Schema Info
253+
#
254+
# Table name: users
255+
#
256+
# id :integer not null, primary key
257+
# foreign_thing_id :integer not null
258+
#
259+
# Foreign Keys
260+
#
261+
# custom_fk_name (other_thing_id => other_things.id)
262+
# fk_rails_a70234b26c (third_thing_id => third_things.id)
263+
# fk_rails_cf2568e89e (foreign_thing_id => foreign_things.id)
264+
#
265+
EOS
266+
end
267+
232268
it 'should get foreign key info if on_delete/on_update options present' do
233269
klass = mock_class(:users,
234270
:id,

0 commit comments

Comments
 (0)