Skip to content

Commit d260a19

Browse files
committed
Merge pull request #209 from vasilakisfil/classified_sort
Add option for classified annotation (id first, timestamps/assotiactions last)
2 parents 6464fc0 + 34c963e commit d260a19

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

bin/annotate

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ OptionParser.new do |opts|
134134
ENV['sort'] = "yes"
135135
end
136136

137+
opts.on('--classified-sort',
138+
"Sort columns alphabetically, but first goes id, then the rest columns, then the timestamp columns and then the association columns") do |dir|
139+
ENV['classified_sort'] = "yes"
140+
end
141+
137142
opts.on('-R', '--require path',
138143
"Additional file to require before loading models, may be used multiple times") do |path|
139144
if !ENV['require'].blank?

lib/annotate.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
begin
77
# ActiveSupport 3.x...
88
require 'active_support/hash_with_indifferent_access'
9+
require 'active_support/core_ext/object/blank'
910
rescue Exception => e
1011
# ActiveSupport 2.x...
1112
require 'active_support/core_ext/hash/indifferent_access'
13+
require 'active_support/core_ext/blank'
1214
end
1315

1416
module Annotate
@@ -24,7 +26,7 @@ module Annotate
2426
:show_indexes, :simple_indexes, :include_version, :exclude_tests,
2527
:exclude_fixtures, :exclude_factories, :ignore_model_sub_dir,
2628
:format_bare, :format_rdoc, :format_markdown, :sort, :force, :trace,
27-
:timestamp, :exclude_serializers
29+
:timestamp, :exclude_serializers, :classified_sort
2830
]
2931
OTHER_OPTIONS=[
3032
:ignore_columns

lib/annotate/annotate_models.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ def get_schema_info(klass, header, options = {})
125125
if options[:ignore_columns]
126126
cols.reject! { |col| col.name.match(/#{options[:ignore_columns]}/) }
127127
end
128+
128129
cols = cols.sort_by(&:name) if(options[:sort])
130+
cols = classified_sort(cols) if(options[:classified_sort])
129131
cols.each do |col|
130132
attrs = []
131133
attrs << "default(#{quote(col.default)})" unless col.default.nil?
@@ -492,5 +494,27 @@ def resolve_filename(filename_template, model_name, table_name)
492494
gsub('%MODEL_NAME%', model_name).
493495
gsub('%TABLE_NAME%', table_name || model_name.pluralize)
494496
end
497+
498+
def classified_sort(cols)
499+
rest_cols = []
500+
timestamps = []
501+
associations = []
502+
id = nil
503+
504+
cols = cols.each do |c|
505+
if c.name.eql?("id")
506+
id = c
507+
elsif (c.name.eql?("created_at") || c.name.eql?("updated_at"))
508+
timestamps << c
509+
elsif c.name[-3,3].eql?("_id")
510+
associations << c
511+
else
512+
rest_cols << c
513+
end
514+
end
515+
[rest_cols, timestamps, associations].each {|a| a.sort_by!(&:name) }
516+
517+
return ([id] << rest_cols << timestamps << associations).flatten
518+
end
495519
end
496520
end

lib/tasks/annotate_models.rake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ task :annotate_models => :environment do
3030
options[:format_markdown] = Annotate.true?(ENV['format_markdown'])
3131
options[:sort] = Annotate.true?(ENV['sort'])
3232
options[:force] = Annotate.true?(ENV['force'])
33+
options[:classified_sort] = Annotate.true?(ENV['classified_sort'])
3334
options[:trace] = Annotate.true?(ENV['trace'])
3435
options[:wrapper_open] = Annotate.fallback(ENV['wrapper_open'], ENV['wrapper'])
3536
options[:wrapper_close] = Annotate.fallback(ENV['wrapper_close'], ENV['wrapper'])

0 commit comments

Comments
 (0)