Skip to content

Commit 2407ee4

Browse files
committed
Merge remote-tracking branch 'upstream/pr/982' into community-maintained
2 parents 2ea4402 + ac92cb2 commit 2407ee4

File tree

16 files changed

+117
-120
lines changed

16 files changed

+117
-120
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
strategy:
1515
fail-fast: false
1616
matrix:
17-
ruby: ['2.7.6']
17+
ruby: ['2.7', '3.0', '3.1', '3.2']
1818

1919
steps:
2020
- name: Checkout

.rubocop.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ AllCops:
1212
- 'tmp/**/*'
1313
- 'spec/integration/**/*'
1414
NewCops: enable
15+
TargetRubyVersion: 2.7
16+
17+
Lint/FormatParameterMismatch:
18+
Enabled: false
1519

1620
Metrics/BlockLength:
1721
Exclude:
1822
- 'spec/**/*.rb'
23+
24+
Metrics/ClassLength:
25+
Exclude:
26+
- 'lib/annotate/annotate_models.rb'

Gemfile

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ source 'https://rubygems.org'
22

33
ruby '>= 2.7.6'
44

5-
gem 'activerecord', '>= 4.2.5', '< 6', require: false
6-
gem 'rake', '>= 13.0.6'
5+
gem 'activerecord', '>= 4.2.5', '< 8', require: false
6+
gem 'rake', require: false
77

88
group :development do
99
gem 'bump'
1010
gem 'mg', require: false
11-
gem 'travis', require: false
1211
platforms :mri, :mingw do
1312
gem 'yard', require: false
1413
end
@@ -19,17 +18,13 @@ group :development, :test do
1918
gem 'guard-rspec', require: false
2019
gem 'rspec', require: false
2120

22-
gem 'rubocop', '~> 1.12.0', require: false
21+
gem 'rubocop', '~> 1.59.0', require: false
2322
gem 'rubocop-rake', require: false
24-
gem 'rubocop-rspec', '~> 2.2.0', require: false
23+
gem 'rubocop-rspec', '~> 2.25.0', require: false
2524
gem 'simplecov', require: false
2625
gem 'terminal-notifier-guard', require: false
2726

28-
gem 'codeclimate-test-reporter'
29-
gem 'coveralls'
30-
3127
gem 'overcommit'
32-
gem 'ruby_dep', '1.5.0'
3328

3429
platforms :mri, :mingw do
3530
gem 'pry', require: false
@@ -38,6 +33,5 @@ group :development, :test do
3833
end
3934

4035
group :test do
41-
gem 'files', require: false
4236
gem 'git', require: false
4337
end

annotate.gemspec

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ Gem::Specification.new do |s|
1818
s.homepage = 'https://github.com/ctran/annotate_models'
1919
s.licenses = ['Ruby']
2020
s.require_paths = ['lib']
21-
s.rubygems_version = '2.1.11'
2221
s.summary = 'Annotates Rails Models, routes, fixtures, and others based on the database schema.'
2322

24-
s.specification_version = 4 if s.respond_to? :specification_version
2523
s.add_runtime_dependency(%q<rake>, '>= 10.4', '< 14.0')
2624
s.add_runtime_dependency(%q<activerecord>, ['>= 3.2', '< 8.0'])
2725

2826
s.metadata = {
2927
"bug_tracker_uri" => "https://github.com/ctran/annotate_models/issues/",
30-
"source_code_uri" => "https://github.com/ctran/annotate_models.git"
28+
"source_code_uri" => "https://github.com/ctran/annotate_models.git",
29+
'rubygems_mfa_required' => 'true'
3130
}
3231
end

bin/annotate

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ options_result = Annotate::Parser.parse(ARGV)
2424
exit if options_result[:exit]
2525

2626
options = Annotate.setup_options(
27-
is_rake: ENV['is_rake'] && !ENV['is_rake'].empty?
27+
is_rake: ENV.fetch('is_rake', nil) && !ENV['is_rake'].empty?
2828
)
2929
Annotate.eager_load(options) if Annotate::Helpers.include_models?
3030

lib/annotate.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ def self.set_defaults(options = {})
4444
#
4545
def self.setup_options(options = {})
4646
Constants::POSITION_OPTIONS.each do |key|
47-
options[key] = Annotate::Helpers.fallback(ENV[key.to_s], ENV['position'], 'before')
47+
options[key] = Annotate::Helpers.fallback(ENV.fetch(key.to_s, nil), ENV.fetch('position', nil), 'before')
4848
end
4949
Constants::FLAG_OPTIONS.each do |key|
50-
options[key] = Annotate::Helpers.true?(ENV[key.to_s])
50+
options[key] = Annotate::Helpers.true?(ENV.fetch(key.to_s, nil))
5151
end
5252
Constants::OTHER_OPTIONS.each do |key|
53-
options[key] = !ENV[key.to_s].blank? ? ENV[key.to_s] : nil
53+
options[key] = !ENV[key.to_s].blank? ? ENV.fetch(key.to_s, nil) : nil
5454
end
5555
Constants::PATH_OPTIONS.each do |key|
5656
options[key] = !ENV[key.to_s].blank? ? ENV[key.to_s].split(',') : []

lib/annotate/annotate_models.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module AnnotateModels
3939
}
4040
}.freeze
4141

42-
MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*(?:\n|r\n))|(^# coding:.*(?:\n|\r\n))|(^# -\*- coding:.*(?:\n|\r\n))|(^# -\*- encoding\s?:.*(?:\n|\r\n))|(^#\s*frozen_string_literal:.+(?:\n|\r\n))|(^# -\*- frozen_string_literal\s*:.+-\*-(?:\n|\r\n))/).freeze
42+
MAGIC_COMMENT_MATCHER = /(^#\s*encoding:.*(?:\n|r\n))|(^# coding:.*(?:\n|\r\n))|(^# -\*- coding:.*(?:\n|\r\n))|(^# -\*- encoding\s?:.*(?:\n|\r\n))|(^#\s*frozen_string_literal:.+(?:\n|\r\n))|(^# -\*- frozen_string_literal\s*:.+-\*-(?:\n|\r\n))/.freeze
4343

4444
class << self
4545
def annotate_pattern(options = {})
@@ -155,8 +155,8 @@ def get_schema_info(klass, header, options = {}) # rubocop:disable Metrics/Metho
155155
with_comments_column = with_comments_column?(klass, options)
156156

157157
# Precalculate Values
158-
cols_meta = cols.map do |col|
159-
col_comment = with_comments || with_comments_column ? col.comment&.gsub(/\n/, "\\n") : nil
158+
cols_meta = cols.to_h do |col|
159+
col_comment = with_comments || with_comments_column ? col.comment&.gsub("\n", "\\n") : nil
160160
col_type = get_col_type(col)
161161
attrs = get_attributes(col, col_type, klass, options)
162162
col_name = if with_comments && col_comment
@@ -166,7 +166,7 @@ def get_schema_info(klass, header, options = {}) # rubocop:disable Metrics/Metho
166166
end
167167
simple_formatted_attrs = attrs.join(", ")
168168
[col.name, { col_type: col_type, attrs: attrs, col_name: col_name, simple_formatted_attrs: simple_formatted_attrs, col_comment: col_comment }]
169-
end.to_h
169+
end
170170

171171
# Output annotation
172172
bare_max_attrs_length = cols_meta.map { |_, m| m[:simple_formatted_attrs].length }.max
@@ -179,15 +179,15 @@ def get_schema_info(klass, header, options = {}) # rubocop:disable Metrics/Metho
179179
col_comment = cols_meta[col.name][:col_comment]
180180

181181
if options[:format_rdoc]
182-
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
182+
info << (sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n")
183183
elsif options[:format_yard]
184-
info << sprintf("# @!attribute #{col_name}") + "\n"
184+
info << (sprintf("# @!attribute #{col_name}") + "\n")
185185
ruby_class = col.respond_to?(:array) && col.array ? "Array<#{map_col_type_to_ruby_classes(col_type)}>": map_col_type_to_ruby_classes(col_type)
186-
info << sprintf("# @return [#{ruby_class}]") + "\n"
186+
info << (sprintf("# @return [#{ruby_class}]") + "\n")
187187
elsif options[:format_markdown]
188188
name_remainder = max_size - col_name.length - non_ascii_length(col_name)
189189
type_remainder = (md_type_allowance - 2) - col_type.length
190-
info << (sprintf("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`", col_name, " ", col_type, " ", attrs.join(", ").rstrip)).gsub('``', ' ').rstrip + "\n"
190+
info << ((sprintf("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`", col_name, " ", col_type, " ", attrs.join(", ").rstrip)).gsub('``', ' ').rstrip + "\n")
191191
elsif with_comments_column
192192
info << format_default(col_name, max_size, col_type, bare_type_allowance, simple_formatted_attrs, bare_max_attrs_length, col_comment)
193193
else
@@ -704,7 +704,7 @@ def parse_options(options = {})
704704
end
705705

706706
def split_model_dir(option_value)
707-
option_value = option_value.is_a?(Array) ? option_value : option_value.split(',')
707+
option_value = option_value.split(',') unless option_value.is_a?(Array)
708708
option_value.map(&:strip).reject(&:empty?)
709709
end
710710

lib/annotate/annotate_routes.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
# Released under the same license as Ruby. No Support. No Warranty.
1919
#
2020

21-
require_relative './annotate_routes/helpers'
22-
require_relative './annotate_routes/header_generator'
21+
require_relative 'annotate_routes/helpers'
22+
require_relative 'annotate_routes/header_generator'
2323

2424
module AnnotateRoutes
2525
class << self
@@ -95,7 +95,7 @@ def rewrite_contents(existing_text, new_text, frozen)
9595
def annotate_routes(header, content, header_position, options = {})
9696
magic_comments_map, content = Helpers.extract_magic_comments_from_array(content)
9797
if %w(before top).include?(options[:position_in_routes])
98-
header = header << '' if content.first != ''
98+
header <<= '' if content.first != ''
9999
magic_comments_map << '' if magic_comments_map.any?
100100
new_content = magic_comments_map + header + content
101101
else

lib/annotate/annotate_routes/header_generator.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require_relative './helpers'
1+
require_relative 'helpers'
22

33
module AnnotateRoutes
44
class HeaderGenerator
@@ -16,7 +16,7 @@ def generate(options = {})
1616
private
1717

1818
def routes_map(options)
19-
result = `rake routes`.chomp("\n").split(/\n/, -1)
19+
result = `rake routes`.chomp("\n").split("\n", -1)
2020

2121
# In old versions of Rake, the first line of output was the cwd. Not so
2222
# much in newer ones. We ditch that line if it exists, and if not, we
@@ -29,7 +29,7 @@ def routes_map(options)
2929
# Skip routes which match given regex
3030
# Note: it matches the complete line (route_name, path, controller/action)
3131
if regexp_for_ignoring_routes
32-
result.reject { |line| line =~ regexp_for_ignoring_routes }
32+
result.grep_v(regexp_for_ignoring_routes)
3333
else
3434
result
3535
end
@@ -53,9 +53,9 @@ def generate
5353

5454
out << comment(options[:wrapper_open]) if options[:wrapper_open]
5555

56-
out << comment(markdown? ? PREFIX_MD : PREFIX) + timestamp_if_required
56+
out << (comment(markdown? ? PREFIX_MD : PREFIX) + timestamp_if_required)
5757
out << comment
58-
return out if contents_without_magic_comments.size.zero?
58+
return out if contents_without_magic_comments.empty?
5959

6060
maxs = [HEADER_ROW.map(&:size)] + contents_without_magic_comments[1..-1].map { |line| line.split.map(&:size) }
6161

lib/annotate/annotate_routes/helpers.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module AnnotateRoutes
22
module Helpers
3-
MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze
3+
MAGIC_COMMENT_MATCHER = /(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/.freeze
44

55
class << self
66
# TODO: write the method doc using ruby rdoc formats
@@ -15,7 +15,7 @@ def strip_annotations(content)
1515
mode = :content
1616
header_position = 0
1717

18-
content.split(/\n/, -1).each_with_index do |line, line_number|
18+
content.split("\n", -1).each_with_index do |line, line_number|
1919
if mode == :header && line !~ /\s*#/
2020
mode = :content
2121
real_content << line unless line.blank?

0 commit comments

Comments
 (0)