Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ you can do so with a simple environment variable, instead of editing the
--ignore-model-subdirects Ignore subdirectories of the models directory
--sort Sort columns alphabetically, rather than in creation order
--classified-sort Sort columns alphabetically, but first goes id, then the rest columns, then the timestamp columns and then the association columns
--classified_sort_fks_second Sort columns alphabetically, but first goes id, then foreign keysm then the rest columns, then the timestamp columns and then the association columns
-R, --require path Additional file to require before loading models, may be used multiple times
-e [tests,fixtures,factories,serializers],
--exclude Do not annotate fixtures, test files, factories, and/or serializers
Expand Down
23 changes: 23 additions & 0 deletions lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,28 @@ def classified_sort(cols)
([id] << rest_cols << timestamps << associations).flatten.compact
end

def classified_sort_fks_second(cols)
rest_cols = []
timestamps = []
associations = []
id = nil

cols.each do |c|
if c.name.eql?('id')
id = c
elsif c.name.eql?('created_at') || c.name.eql?('updated_at')
timestamps << c
elsif c.name[-3, 3].eql?('_id')
associations << c
else
rest_cols << c
end
end
[rest_cols, timestamps, associations].each { |a| a.sort_by!(&:name) }

([id] << associations << rest_cols << timestamps).flatten.compact
end

private

def with_comments?(klass, options)
Expand Down Expand Up @@ -960,6 +982,7 @@ def columns(klass, options)

cols = cols.sort_by(&:name) if options[:sort]
cols = classified_sort(cols) if options[:classified_sort]
cols = classified_sort_fks_second(cols) if options[:classified_sort_fks_second]

cols
end
Expand Down
2 changes: 1 addition & 1 deletion lib/annotate/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Constants
:show_indexes, :simple_indexes, :include_version, :exclude_tests,
:exclude_fixtures, :exclude_factories, :ignore_model_sub_dir,
:format_bare, :format_rdoc, :format_yard, :format_markdown, :sort, :force, :frozen,
:trace, :timestamp, :exclude_serializers, :classified_sort,
:trace, :timestamp, :exclude_serializers, :classified_sort, :classified_sort_fks_second,
:show_foreign_keys, :show_complete_foreign_keys,
:exclude_scaffolds, :exclude_controllers, :exclude_helpers,
:exclude_sti_subclasses, :ignore_unknown_models, :with_comment
Expand Down
5 changes: 5 additions & 0 deletions lib/annotate/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength
env['classified_sort'] = 'yes'
end

option_parser.on('--classified-sort-fks-second',
"Sort columns alphabetically, but first goes id, then foreign keys, then the rest columns, then the timestamp columns and then the association columns") do
env['classified_sort_fks_second'] = 'yes'
end

option_parser.on('-R',
'--require path',
"Additional file to require before loading models, may be used multiple times") do |path|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ if Rails.env.development?
'force' => 'false',
'frozen' => 'false',
'classified_sort' => 'true',
'classified_sort_fks_second' => 'true',
'trace' => 'false',
'wrapper_open' => nil,
'wrapper_close' => nil,
Expand Down
1 change: 1 addition & 0 deletions lib/tasks/annotate_models.rake
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ task annotate_models: :environment do
options[:force] = Annotate::Helpers.true?(ENV['force'])
options[:frozen] = Annotate::Helpers.true?(ENV['frozen'])
options[:classified_sort] = Annotate::Helpers.true?(ENV['classified_sort'])
options[:classified_sort_fks_second] = Annotate::Helpers.true?(ENV['classified_sort_fks_second'])
options[:trace] = Annotate::Helpers.true?(ENV['trace'])
options[:wrapper_open] = Annotate::Helpers.fallback(ENV['wrapper_open'], ENV['wrapper'])
options[:wrapper_close] = Annotate::Helpers.fallback(ENV['wrapper_close'], ENV['wrapper'])
Expand Down