Skip to content

Support database comment for rails 5 #459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2017
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
8 changes: 4 additions & 4 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Lint/UselessAccessModifier:

# Offense count: 17
Metrics/AbcSize:
Max: 146
Max: 159

# Offense count: 3
# Configuration parameters: CountComments.
Expand All @@ -116,7 +116,7 @@ Metrics/BlockNesting:

# Offense count: 8
Metrics/CyclomaticComplexity:
Max: 37
Max: 42

# Offense count: 350
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
Expand All @@ -127,7 +127,7 @@ Metrics/LineLength:
# Offense count: 24
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 71
Max: 79

# Offense count: 1
# Configuration parameters: CountComments.
Expand All @@ -136,7 +136,7 @@ Metrics/ModuleLength:

# Offense count: 7
Metrics/PerceivedComplexity:
Max: 42
Max: 48

# Offense count: 1
Style/AccessorMethodName:
Expand Down
17 changes: 12 additions & 5 deletions lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ def get_schema_info(klass, header, options = {})
info << get_schema_header_text(klass, options)

max_size = klass.column_names.map(&:size).max || 0
with_comment = options[:with_comment] && klass.columns.first.respond_to?(:comment)
max_size = klass.columns.map{|col| col.name.size + col.comment.size }.max || 0 if with_comment
max_size += 2 if with_comment
max_size += options[:format_rdoc] ? 5 : 1
md_names_overhead = 6
md_type_allowance = 18
Expand Down Expand Up @@ -271,15 +274,19 @@ def get_schema_info(klass, header, options = {})
end
end
end

col_name = if with_comment
"#{col.name}(#{col.comment})"
else
col.name
end
if options[:format_rdoc]
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col.name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
elsif options[:format_markdown]
name_remainder = max_size - col.name.length
name_remainder = max_size - col_name.length
type_remainder = (md_type_allowance - 2) - col_type.length
info << (sprintf("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`", col.name, " ", col_type, " ", attrs.join(", ").rstrip)).gsub('``', ' ').rstrip + "\n"
info << (sprintf("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`", col_name, " ", col_type, " ", attrs.join(", ").rstrip)).gsub('``', ' ').rstrip + "\n"
else
info << sprintf("# %-#{max_size}.#{max_size}s:%-#{bare_type_allowance}.#{bare_type_allowance}s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n"
info << sprintf("# %-#{max_size}.#{max_size}s:%-#{bare_type_allowance}.#{bare_type_allowance}s %s", col_name, col_type, attrs.join(", ")).rstrip + "\n"
end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/generators/annotate/templates/auto_annotate_models.rake
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ if Rails.env.development?
'force' => 'false',
'trace' => 'false',
'wrapper_open' => nil,
'wrapper_close' => nil
'wrapper_close' => nil,
'with_comment' => true
)
end

Expand Down
67 changes: 66 additions & 1 deletion spec/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ def self.when_called_with(options = {})
[:notes, :text, { limit: 55 }]
]

it 'should work with options = #{options}' do
it "should work with options = #{options}" do
with_columns = (options.delete(:with_columns) || default_columns).map do |column|
mock_column(column[0], column[1], column[2])
end
Expand Down Expand Up @@ -581,6 +581,71 @@ def self.when_called_with(options = {})
#
EOS
end

describe 'with_comment option' do
mocked_columns_with_comment = [
[:id, :integer, { limit: 8, comment: 'ID' }],
[:active, :boolean, { limit: 1, comment: 'Active' }],
[:name, :string, { limit: 50, comment: 'Name' }],
[:notes, :text, { limit: 55, comment: 'Notes' }]
]

when_called_with with_comment: 'yes',
with_columns: mocked_columns_with_comment, returns:
<<-EOS.strip_heredoc
# Schema Info
#
# Table name: users
#
# id(ID) :integer not null, primary key
# active(Active) :boolean not null
# name(Name) :string(50) not null
# notes(Notes) :text(55) not null
#
EOS

it 'should get schema info as RDoc' do
klass = mock_class(:users,
:id,
[
mock_column(:id, :integer, comment: 'ID'),
mock_column(:name, :string, limit: 50, comment: 'Name')
])
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_rdoc: true, with_comment: true)).to eql(<<-EOS.strip_heredoc)
# #{AnnotateModels::PREFIX}
#
# Table name: users
#
# *id(ID)*:: <tt>integer, not null, primary key</tt>
# *name(Name)*:: <tt>string(50), not null</tt>
#--
# #{AnnotateModels::END_MARK}
#++
EOS
end

it 'should get schema info as Markdown' do
klass = mock_class(:users,
:id,
[
mock_column(:id, :integer, comment: 'ID'),
mock_column(:name, :string, limit: 50, comment: 'Name')
])
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, with_comment: true)).to eql(<<-EOS.strip_heredoc)
# #{AnnotateModels::PREFIX}
#
# Table name: `users`
#
# ### Columns
#
# Name | Type | Attributes
# ----------------- | ------------------ | ---------------------------
# **`id(ID)`** | `integer` | `not null, primary key`
# **`name(Name)`** | `string(50)` | `not null`
#
EOS
end
end
end

describe '#get_model_class' do
Expand Down