diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index 94f93bfd2..4cd8cd275 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -187,15 +187,7 @@ def schema_default(klass, column) # the type (and length), and any optional attributes def get_schema_info(klass, header, options = {}) info = "# #{header}\n" - info<< "#\n" - if options[:format_markdown] - info<< "# Table name: `#{klass.table_name}`\n" - info<< "#\n" - info<< "# ### Columns\n" - else - info<< "# Table name: #{klass.table_name}\n" - end - info<< "#\n" + info << get_schema_header_text(klass, options) max_size = klass.column_names.map(&:size).max || 0 max_size += options[:format_rdoc] ? 5 : 1 @@ -220,9 +212,9 @@ def get_schema_info(klass, header, options = {}) cols = classified_sort(cols) if options[:classified_sort] cols.each do |col| col_type = (col.type || col.sql_type).to_s - attrs = [] attrs << "default(#{schema_default(klass, col)})" unless col.default.nil? || hide_default?(col_type, options) + attrs << 'unsigned' if col.respond_to?(:unsigned?) && col.unsigned? attrs << 'not null' unless col.null attrs << 'primary key' if klass.primary_key && (klass.primary_key.is_a?(Array) ? klass.primary_key.collect(&:to_sym).include?(col.name.to_sym) : col.name.to_sym == klass.primary_key.to_sym) @@ -280,6 +272,23 @@ def get_schema_info(klass, header, options = {}) info << get_foreign_key_info(klass, options) end + info << get_schema_footer_text(klass, options) + end + + def get_schema_header_text(klass, options = {}) + info = "#\n" + if options[:format_markdown] + info << "# Table name: `#{klass.table_name}`\n" + info << "#\n" + info << "# ### Columns\n" + else + info<< "# Table name: #{klass.table_name}\n" + end + info << "#\n" + end + + def get_schema_footer_text(_klass, options = {}) + info = "" if options[:format_rdoc] info << "#--\n" info << "# #{END_MARK}\n" diff --git a/spec/annotate/annotate_models_spec.rb b/spec/annotate/annotate_models_spec.rb index c2e04e93e..d7e7bfb53 100755 --- a/spec/annotate/annotate_models_spec.rb +++ b/spec/annotate/annotate_models_spec.rb @@ -118,6 +118,7 @@ def mock_column(name, type, options={}) # EOS end + it "should get schema info with enum type " do klass = mock_class(:users, nil, [ mock_column(:id, :integer), @@ -135,6 +136,29 @@ def mock_column(name, type, options={}) EOS end + it "should get schema info with unsigned" do + klass = mock_class(:users, nil, [ + mock_column(:id, :integer), + mock_column(:integer, :integer, :unsigned? => true), + mock_column(:bigint, :bigint, :unsigned? => true), + mock_column(:float, :float, :unsigned? => true), + mock_column(:decimal, :decimal, :unsigned? => true, :precision => 10, :scale => 2), + ]) + + expect(AnnotateModels.get_schema_info(klass, "Schema Info")).to eql(<<-EOS) +# Schema Info +# +# Table name: users +# +# id :integer not null +# integer :integer unsigned, not null +# bigint :bigint unsigned, not null +# float :float unsigned, not null +# decimal :decimal(10, 2) unsigned, not null +# +EOS + end + it "should get schema info for integer and boolean with default" do klass = mock_class(:users, :id, [ mock_column(:id, :integer),