Commit d260a199 by Cuong Tran

Merge pull request #209 from vasilakisfil/classified_sort

Add option for classified annotation (id first, timestamps/assotiactions last)
parents 6464fc08 34c963e5
...@@ -134,6 +134,11 @@ OptionParser.new do |opts| ...@@ -134,6 +134,11 @@ OptionParser.new do |opts|
ENV['sort'] = "yes" ENV['sort'] = "yes"
end 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', opts.on('-R', '--require path',
"Additional file to require before loading models, may be used multiple times") do |path| "Additional file to require before loading models, may be used multiple times") do |path|
if !ENV['require'].blank? if !ENV['require'].blank?
......
...@@ -6,9 +6,11 @@ require 'annotate/annotate_routes' ...@@ -6,9 +6,11 @@ require 'annotate/annotate_routes'
begin begin
# ActiveSupport 3.x... # ActiveSupport 3.x...
require 'active_support/hash_with_indifferent_access' require 'active_support/hash_with_indifferent_access'
require 'active_support/core_ext/object/blank'
rescue Exception => e rescue Exception => e
# ActiveSupport 2.x... # ActiveSupport 2.x...
require 'active_support/core_ext/hash/indifferent_access' require 'active_support/core_ext/hash/indifferent_access'
require 'active_support/core_ext/blank'
end end
module Annotate module Annotate
...@@ -24,7 +26,7 @@ module Annotate ...@@ -24,7 +26,7 @@ module Annotate
:show_indexes, :simple_indexes, :include_version, :exclude_tests, :show_indexes, :simple_indexes, :include_version, :exclude_tests,
:exclude_fixtures, :exclude_factories, :ignore_model_sub_dir, :exclude_fixtures, :exclude_factories, :ignore_model_sub_dir,
:format_bare, :format_rdoc, :format_markdown, :sort, :force, :trace, :format_bare, :format_rdoc, :format_markdown, :sort, :force, :trace,
:timestamp, :exclude_serializers :timestamp, :exclude_serializers, :classified_sort
] ]
OTHER_OPTIONS=[ OTHER_OPTIONS=[
:ignore_columns :ignore_columns
......
...@@ -125,7 +125,9 @@ module AnnotateModels ...@@ -125,7 +125,9 @@ module AnnotateModels
if options[:ignore_columns] if options[:ignore_columns]
cols.reject! { |col| col.name.match(/#{options[:ignore_columns]}/) } cols.reject! { |col| col.name.match(/#{options[:ignore_columns]}/) }
end end
cols = cols.sort_by(&:name) if(options[:sort]) cols = cols.sort_by(&:name) if(options[:sort])
cols = classified_sort(cols) if(options[:classified_sort])
cols.each do |col| cols.each do |col|
attrs = [] attrs = []
attrs << "default(#{quote(col.default)})" unless col.default.nil? attrs << "default(#{quote(col.default)})" unless col.default.nil?
...@@ -492,5 +494,27 @@ module AnnotateModels ...@@ -492,5 +494,27 @@ module AnnotateModels
gsub('%MODEL_NAME%', model_name). gsub('%MODEL_NAME%', model_name).
gsub('%TABLE_NAME%', table_name || model_name.pluralize) gsub('%TABLE_NAME%', table_name || model_name.pluralize)
end 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
end end
...@@ -30,6 +30,7 @@ task :annotate_models => :environment do ...@@ -30,6 +30,7 @@ task :annotate_models => :environment do
options[:format_markdown] = Annotate.true?(ENV['format_markdown']) options[:format_markdown] = Annotate.true?(ENV['format_markdown'])
options[:sort] = Annotate.true?(ENV['sort']) options[:sort] = Annotate.true?(ENV['sort'])
options[:force] = Annotate.true?(ENV['force']) options[:force] = Annotate.true?(ENV['force'])
options[:classified_sort] = Annotate.true?(ENV['classified_sort'])
options[:trace] = Annotate.true?(ENV['trace']) options[:trace] = Annotate.true?(ENV['trace'])
options[:wrapper_open] = Annotate.fallback(ENV['wrapper_open'], ENV['wrapper']) options[:wrapper_open] = Annotate.fallback(ENV['wrapper_open'], ENV['wrapper'])
options[:wrapper_close] = Annotate.fallback(ENV['wrapper_close'], ENV['wrapper']) options[:wrapper_close] = Annotate.fallback(ENV['wrapper_close'], ENV['wrapper'])
......
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