Skip to content

Commit 74a2075

Browse files
committed
Merge branch 'develop' into tmr08c-fix-multi-line-comments
2 parents 84c21eb + da1e605 commit 74a2075

13 files changed

+235
-182
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
bundle install --jobs=4 --retry=3
3939
4040
- name: Run Tests
41-
run: bundle exec rspec
41+
run: INTEGRATION_TESTS=1 bundle exec rspec
4242

4343
- name: Rubocop
4444
run: bundle exec rubocop

.rubocop_todo.yml

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2020-03-01 03:15:48 +0900 using RuboCop version 0.68.1.
3+
# on 2020-04-03 00:51:53 +0900 using RuboCop version 0.68.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -163,12 +163,11 @@ Lint/InheritException:
163163
Exclude:
164164
- 'lib/annotate/annotate_models.rb'
165165

166-
# Offense count: 2
166+
# Offense count: 1
167167
# Configuration parameters: MaximumRangeSize.
168168
Lint/MissingCopEnableDirective:
169169
Exclude:
170170
- 'lib/annotate/annotate_models.rb'
171-
- 'lib/annotate/annotate_routes.rb'
172171

173172
# Offense count: 2
174173
Lint/RescueException:
@@ -213,7 +212,7 @@ Naming/AccessorMethodName:
213212
Exclude:
214213
- 'lib/annotate.rb'
215214

216-
# Offense count: 103
215+
# Offense count: 102
217216
# Configuration parameters: Blacklist.
218217
# Blacklist: (?-mix:(^|\s)(EO[A-Z]{1}|END)(\s|$))
219218
Naming/HeredocDelimiterNaming:
@@ -235,12 +234,13 @@ Naming/UncommunicativeMethodParamName:
235234
Exclude:
236235
- 'Rakefile'
237236

238-
# Offense count: 1
237+
# Offense count: 2
239238
# Configuration parameters: EnforcedStyle.
240239
# SupportedStyles: inline, group
241240
Style/AccessModifierDeclarations:
242241
Exclude:
243242
- 'lib/annotate/annotate_models.rb'
243+
- 'lib/annotate/annotate_routes/header_generator.rb'
244244

245245
# Offense count: 1
246246
Style/CaseEquality:
@@ -266,7 +266,7 @@ Style/Dir:
266266
Exclude:
267267
- 'bin/annotate'
268268

269-
# Offense count: 9
269+
# Offense count: 10
270270
Style/Documentation:
271271
Exclude:
272272
- 'spec/**/*'
@@ -275,6 +275,7 @@ Style/Documentation:
275275
- 'lib/annotate/active_record_patch.rb'
276276
- 'lib/annotate/annotate_models.rb'
277277
- 'lib/annotate/annotate_routes.rb'
278+
- 'lib/annotate/annotate_routes/header_generator.rb'
278279
- 'lib/annotate/annotate_routes/helpers.rb'
279280
- 'lib/annotate/version.rb'
280281
- 'lib/generators/annotate/install_generator.rb'
@@ -293,14 +294,13 @@ Style/ExpandPathArguments:
293294
Exclude:
294295
- 'annotate.gemspec'
295296

296-
# Offense count: 10
297+
# Offense count: 9
297298
# Cop supports --auto-correct.
298299
# Configuration parameters: EnforcedStyle.
299300
# SupportedStyles: format, sprintf, percent
300301
Style/FormatString:
301302
Exclude:
302303
- 'lib/annotate/annotate_models.rb'
303-
- 'lib/annotate/annotate_routes.rb'
304304

305305
# Offense count: 23
306306
# Configuration parameters: EnforcedStyle.
@@ -309,7 +309,7 @@ Style/FormatStringToken:
309309
Exclude:
310310
- 'lib/annotate/annotate_models.rb'
311311

312-
# Offense count: 27
312+
# Offense count: 28
313313
# Cop supports --auto-correct.
314314
# Configuration parameters: EnforcedStyle.
315315
# SupportedStyles: when_needed, always, never
@@ -338,7 +338,7 @@ Style/IfUnlessModifier:
338338
- 'Rakefile'
339339
- 'bin/annotate'
340340
- 'lib/annotate/annotate_models.rb'
341-
- 'lib/annotate/annotate_routes.rb'
341+
- 'lib/annotate/annotate_routes/header_generator.rb'
342342

343343
# Offense count: 1
344344
# Cop supports --auto-correct.
@@ -432,15 +432,14 @@ Style/RedundantSelf:
432432
Exclude:
433433
- 'lib/tasks/annotate_models_migrate.rake'
434434

435-
# Offense count: 13
435+
# Offense count: 12
436436
# Cop supports --auto-correct.
437437
# Configuration parameters: EnforcedStyle, AllowInnerSlashes.
438438
# SupportedStyles: slashes, percent_r, mixed
439439
Style/RegexpLiteral:
440440
Exclude:
441441
- 'Rakefile'
442442
- 'lib/annotate/annotate_models.rb'
443-
- 'lib/annotate/annotate_routes.rb'
444443

445444
# Offense count: 1
446445
# Cop supports --auto-correct.
@@ -521,7 +520,7 @@ Style/UnneededPercentQ:
521520
Exclude:
522521
- 'annotate.gemspec'
523522

524-
# Offense count: 377
523+
# Offense count: 375
525524
# Cop supports --auto-correct.
526525
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
527526
# URISchemes: http, https

.travis.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@ rvm:
33
- 2.4.9
44
- 2.5.7
55
- 2.6.5
6-
- ruby-head
76

87
env:
9-
- RAILS_ENV=development RACK_ENV=development
10-
11-
matrix:
12-
allow_failures:
13-
- rvm: ruby-head
8+
- RAILS_ENV=development RACK_ENV=development INTEGRATION_TESTS=1
149

1510
addons:
1611
apt_packages:
@@ -30,7 +25,7 @@ jobs:
3025
include:
3126
- stage: gem release
3227
if: tag =~ ^v
33-
rvm: 2.6.0
28+
rvm: 2.6.5
3429
script: echo "Deploying to rubygems.org ..."
3530
deploy:
3631
provider: rubygems

Gemfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,4 @@ end
3838
group :test do
3939
gem 'files', require: false
4040
gem 'git', require: false
41-
gem 'wrong', require: false
4241
end

lib/annotate/annotate_models.rb

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -249,49 +249,7 @@ def get_schema_info(klass, header, options = {})
249249
cols = columns(klass, options)
250250
cols.each do |col|
251251
col_type = get_col_type(col)
252-
attrs = []
253-
attrs << "default(#{schema_default(klass, col)})" unless col.default.nil? || hide_default?(col_type, options)
254-
attrs << 'unsigned' if col.respond_to?(:unsigned?) && col.unsigned?
255-
attrs << 'not null' unless col.null
256-
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)
257-
258-
if col_type == 'decimal'
259-
col_type << "(#{col.precision}, #{col.scale})"
260-
elsif !%w[spatial geometry geography].include?(col_type)
261-
if col.limit && !options[:format_yard]
262-
if col.limit.is_a? Array
263-
attrs << "(#{col.limit.join(', ')})"
264-
else
265-
col_type << "(#{col.limit})" unless hide_limit?(col_type, options)
266-
end
267-
end
268-
end
269-
270-
# Check out if we got an array column
271-
attrs << 'is an Array' if col.respond_to?(:array) && col.array
272-
273-
# Check out if we got a geometric column
274-
# and print the type and SRID
275-
if col.respond_to?(:geometry_type)
276-
attrs << "#{col.geometry_type}, #{col.srid}"
277-
elsif col.respond_to?(:geometric_type) && col.geometric_type.present?
278-
attrs << "#{col.geometric_type.to_s.downcase}, #{col.srid}"
279-
end
280-
281-
# Check if the column has indices and print "indexed" if true
282-
# If the index includes another column, print it too.
283-
if options[:simple_indexes] && klass.table_exists?# Check out if this column is indexed
284-
indices = retrieve_indexes_from_table(klass)
285-
if indices = indices.select { |ind| ind.columns.include? col.name }
286-
indices.sort_by(&:name).each do |ind|
287-
next if ind.columns.is_a?(String)
288-
ind = ind.columns.reject! { |i| i == col.name }
289-
attrs << (ind.empty? ? "indexed" : "indexed => [#{ind.join(", ")}]")
290-
end
291-
end
292-
end
293-
294-
# Generate the column name optionally including column comments
252+
attrs = get_attributes(col, col_type, klass, options)
295253
col_name = if with_comments?(klass, options) && col.comment
296254
"#{col.name}(#{col.comment.gsub(/\n/, "\\n")})"
297255
else
@@ -812,7 +770,7 @@ def annotate_model_file(annotated, file, header, options)
812770
begin
813771
return false if /#{SKIP_ANNOTATION_PREFIX}.*/ =~ (File.exist?(file) ? File.read(file) : '')
814772
klass = get_model_class(file)
815-
do_annotate = klass &&
773+
do_annotate = klass.is_a?(Class) &&
816774
klass < ActiveRecord::Base &&
817775
(!options[:exclude_sti_subclasses] || !(klass.superclass < ActiveRecord::Base && klass.table_name == klass.superclass.table_name)) &&
818776
!klass.abstract_class? &&
@@ -999,6 +957,55 @@ def ignored_translation_table_colums(klass)
999957
foreign_column_name
1000958
]
1001959
end
960+
961+
##
962+
# Get the list of attributes that should be included in the annotation for
963+
# a given column.
964+
def get_attributes(column, column_type, klass, options)
965+
attrs = []
966+
attrs << "default(#{schema_default(klass, column)})" unless column.default.nil? || hide_default?(column_type, options)
967+
attrs << 'unsigned' if column.respond_to?(:unsigned?) && column.unsigned?
968+
attrs << 'not null' unless column.null
969+
attrs << 'primary key' if klass.primary_key && (klass.primary_key.is_a?(Array) ? klass.primary_key.collect(&:to_sym).include?(column.name.to_sym) : column.name.to_sym == klass.primary_key.to_sym)
970+
971+
if column_type == 'decimal'
972+
column_type << "(#{column.precision}, #{column.scale})"
973+
elsif !%w[spatial geometry geography].include?(column_type)
974+
if column.limit && !options[:format_yard]
975+
if column.limit.is_a? Array
976+
attrs << "(#{column.limit.join(', ')})"
977+
else
978+
column_type << "(#{column.limit})" unless hide_limit?(column_type, options)
979+
end
980+
end
981+
end
982+
983+
# Check out if we got an array columnumn
984+
attrs << 'is an Array' if column.respond_to?(:array) && column.array
985+
986+
# Check out if we got a geometric columnumn
987+
# and print the type and SRID
988+
if column.respond_to?(:geometry_type)
989+
attrs << "#{column.geometry_type}, #{column.srid}"
990+
elsif column.respond_to?(:geometric_type) && column.geometric_type.present?
991+
attrs << "#{column.geometric_type.to_s.downcase}, #{column.srid}"
992+
end
993+
994+
# Check if the column has indices and print "indexed" if true
995+
# If the index includes another column, print it too.
996+
if options[:simple_indexes] && klass.table_exists?# Check out if this column is indexed
997+
indices = retrieve_indexes_from_table(klass)
998+
if indices = indices.select { |ind| ind.columns.include? column.name }
999+
indices.sort_by(&:name).each do |ind|
1000+
next if ind.columns.is_a?(String)
1001+
ind = ind.columns.reject! { |i| i == column.name }
1002+
attrs << (ind.empty? ? "indexed" : "indexed => [#{ind.join(", ")}]")
1003+
end
1004+
end
1005+
end
1006+
1007+
attrs
1008+
end
10021009
end
10031010

10041011
class BadModelFileError < LoadError

lib/annotate/annotate_routes.rb

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# rubocop:disable Metrics/ModuleLength
2-
31
# == Annotate Routes
42
#
53
# Based on:
@@ -21,18 +19,15 @@
2119
#
2220

2321
require_relative './annotate_routes/helpers'
22+
require_relative './annotate_routes/header_generator'
2423

2524
module AnnotateRoutes
26-
PREFIX = '== Route Map'.freeze
27-
PREFIX_MD = '## Route Map'.freeze
28-
HEADER_ROW = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action'].freeze
29-
3025
class << self
3126
def do_annotations(options = {})
3227
if routes_file_exist?
3328
existing_text = File.read(routes_file)
3429
content, header_position = Helpers.strip_annotations(existing_text)
35-
new_content = annotate_routes(header(options), content, header_position, options)
30+
new_content = annotate_routes(HeaderGenerator.generate(options), content, header_position, options)
3631
new_text = new_content.join("\n")
3732

3833
if rewrite_contents(existing_text, new_text)
@@ -71,49 +66,6 @@ def routes_file
7166
@routes_rb ||= File.join('config', 'routes.rb')
7267
end
7368

74-
def header(options = {})
75-
routes_map = app_routes_map(options)
76-
77-
magic_comments_map, routes_map = Helpers.extract_magic_comments_from_array(routes_map)
78-
79-
out = []
80-
81-
magic_comments_map.each do |magic_comment|
82-
out << magic_comment
83-
end
84-
out << '' if magic_comments_map.any?
85-
86-
out << comment(options[:wrapper_open]) if options[:wrapper_open]
87-
88-
out << comment(options[:format_markdown] ? PREFIX_MD : PREFIX) + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '')
89-
out << comment
90-
return out if routes_map.size.zero?
91-
92-
maxs = [HEADER_ROW.map(&:size)] + routes_map[1..-1].map { |line| line.split.map(&:size) }
93-
94-
if options[:format_markdown]
95-
max = maxs.map(&:max).compact.max
96-
97-
out << comment(content(HEADER_ROW, maxs, options))
98-
out << comment(content(['-' * max, '-' * max, '-' * max, '-' * max], maxs, options))
99-
else
100-
out << comment(content(routes_map[0], maxs, options))
101-
end
102-
103-
out += routes_map[1..-1].map { |line| comment(content(options[:format_markdown] ? line.split(' ') : line, maxs, options)) }
104-
out << comment(options[:wrapper_close]) if options[:wrapper_close]
105-
106-
out
107-
end
108-
109-
def comment(row = '')
110-
if row == ''
111-
'#'
112-
else
113-
"# #{row}"
114-
end
115-
end
116-
11769
def strip_on_removal(content, header_position)
11870
if header_position == :before
11971
content.shift while content.first == ''
@@ -162,32 +114,5 @@ def annotate_routes(header, content, header_position, options = {})
162114

163115
new_content
164116
end
165-
166-
def app_routes_map(options)
167-
routes_map = `rake routes`.chomp("\n").split(/\n/, -1)
168-
169-
# In old versions of Rake, the first line of output was the cwd. Not so
170-
# much in newer ones. We ditch that line if it exists, and if not, we
171-
# keep the line around.
172-
routes_map.shift if routes_map.first =~ /^\(in \//
173-
174-
# Skip routes which match given regex
175-
# Note: it matches the complete line (route_name, path, controller/action)
176-
if options[:ignore_routes]
177-
routes_map.reject! { |line| line =~ /#{options[:ignore_routes]}/ }
178-
end
179-
180-
routes_map
181-
end
182-
183-
def content(line, maxs, options = {})
184-
return line.rstrip unless options[:format_markdown]
185-
186-
line.each_with_index.map do |elem, index|
187-
min_length = maxs.map { |arr| arr[index] }.max || 0
188-
189-
sprintf("%-#{min_length}.#{min_length}s", elem.tr('|', '-'))
190-
end.join(' | ')
191-
end
192117
end
193118
end

0 commit comments

Comments
 (0)