Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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"
Expand Down
24 changes: 24 additions & 0 deletions spec/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down