From 34c963e5d82bdc1ba43213daec0eda6284c72bed Mon Sep 17 00:00:00 2001 From: Jason Cox Date: Tue, 26 Aug 2014 18:47:33 +1000 Subject: [PATCH] Add option for classified annotation (id first, timestamps/assotiactions last) Add option for classified annotation (id first, timestamps/assotiactions last) require activesupport core extension .blank? Add option for classified annotation (id first, timestamps/assotiactions last) Fix bug Restore gemspec file --- bin/annotate | 5 +++++ lib/annotate.rb | 4 +++- lib/annotate/annotate_models.rb | 24 ++++++++++++++++++++++++ lib/tasks/annotate_models.rake | 1 + 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/bin/annotate b/bin/annotate index 62eb31c0a..2b9fd7c9d 100755 --- a/bin/annotate +++ b/bin/annotate @@ -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? diff --git a/lib/annotate.rb b/lib/annotate.rb index 3c4607326..17ea8b710 100755 --- a/lib/annotate.rb +++ b/lib/annotate.rb @@ -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 @@ -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 diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index 709b5f95f..0b5131a2d 100755 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -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? @@ -491,5 +493,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 diff --git a/lib/tasks/annotate_models.rake b/lib/tasks/annotate_models.rake index d788946d2..9c0a1980a 100644 --- a/lib/tasks/annotate_models.rake +++ b/lib/tasks/annotate_models.rake @@ -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'])