Skip to content

Commit

Permalink
Merge pull request #209 from vasilakisfil/classified_sort
Browse files Browse the repository at this point in the history
Add option for classified annotation (id first, timestamps/assotiactions last)
  • Loading branch information
ctran committed Mar 4, 2015
2 parents 6464fc0 + 34c963e commit d260a19
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
5 changes: 5 additions & 0 deletions bin/annotate
Expand Up @@ -134,6 +134,11 @@ OptionParser.new do |opts|
ENV['sort'] = "yes"
end

opts.on('--classified-sort',
"Sort columns alphabetically, but first goes id, then the rest columns, then the timestamp columns and then the association columns") do |dir|
ENV['classified_sort'] = "yes"
end

opts.on('-R', '--require path',
"Additional file to require before loading models, may be used multiple times") do |path|
if !ENV['require'].blank?
Expand Down
4 changes: 3 additions & 1 deletion lib/annotate.rb
Expand Up @@ -6,9 +6,11 @@
begin
# ActiveSupport 3.x...
require 'active_support/hash_with_indifferent_access'
require 'active_support/core_ext/object/blank'
rescue Exception => e
# ActiveSupport 2.x...
require 'active_support/core_ext/hash/indifferent_access'
require 'active_support/core_ext/blank'
end

module Annotate
Expand All @@ -24,7 +26,7 @@ module Annotate
:show_indexes, :simple_indexes, :include_version, :exclude_tests,
:exclude_fixtures, :exclude_factories, :ignore_model_sub_dir,
:format_bare, :format_rdoc, :format_markdown, :sort, :force, :trace,
:timestamp, :exclude_serializers
:timestamp, :exclude_serializers, :classified_sort
]
OTHER_OPTIONS=[
:ignore_columns
Expand Down
24 changes: 24 additions & 0 deletions lib/annotate/annotate_models.rb
Expand Up @@ -125,7 +125,9 @@ def get_schema_info(klass, header, options = {})
if options[:ignore_columns]
cols.reject! { |col| col.name.match(/#{options[:ignore_columns]}/) }
end

cols = cols.sort_by(&:name) if(options[:sort])
cols = classified_sort(cols) if(options[:classified_sort])
cols.each do |col|
attrs = []
attrs << "default(#{quote(col.default)})" unless col.default.nil?
Expand Down Expand Up @@ -492,5 +494,27 @@ def resolve_filename(filename_template, model_name, table_name)
gsub('%MODEL_NAME%', model_name).
gsub('%TABLE_NAME%', table_name || model_name.pluralize)
end

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

cols = 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) }

return ([id] << rest_cols << timestamps << associations).flatten
end
end
end
1 change: 1 addition & 0 deletions lib/tasks/annotate_models.rake
Expand Up @@ -30,6 +30,7 @@ task :annotate_models => :environment do
options[:format_markdown] = Annotate.true?(ENV['format_markdown'])
options[:sort] = Annotate.true?(ENV['sort'])
options[:force] = Annotate.true?(ENV['force'])
options[:classified_sort] = Annotate.true?(ENV['classified_sort'])
options[:trace] = Annotate.true?(ENV['trace'])
options[:wrapper_open] = Annotate.fallback(ENV['wrapper_open'], ENV['wrapper'])
options[:wrapper_close] = Annotate.fallback(ENV['wrapper_close'], ENV['wrapper'])
Expand Down

0 comments on commit d260a19

Please sign in to comment.