Skip to content

Commit 4383f95

Browse files
committed
chore: auto-fix linting, fix frozen string literal violations, fix env access in specs
1 parent 9b8778a commit 4383f95

29 files changed

+637
-503
lines changed

Guardfile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
# NOTE: The cmd option is now required due to the increasing number of ways
24
# rspec may be run, below are examples of the most common uses.
35
# * bundler: 'bundle exec rspec'
@@ -14,7 +16,9 @@ guard :rspec, cmd: 'bundle exec rspec' do
1416
# Rails example
1517
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
1618
watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
17-
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
19+
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) do |m|
20+
["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"]
21+
end
1822
watch(%r{^spec/support/(.+)\.rb$}) { 'spec' }
1923
watch('config/routes.rb') { 'spec/routing' }
2024
watch('app/controllers/application_controller.rb') { 'spec/controllers' }
@@ -25,5 +29,7 @@ guard :rspec, cmd: 'bundle exec rspec' do
2529

2630
# Turnip features and steps
2731
watch(%r{^spec/acceptance/(.+)\.feature$})
28-
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
32+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
33+
Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance'
34+
end
2935
end

Rakefile

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
# frozen_string_literal: true
2+
13
def exit_exception(e)
2-
$stderr.puts e.message
4+
warn e.message
35
exit e.status_code
46
end
57

@@ -8,7 +10,7 @@ begin
810
require 'bundler'
911
Bundler.setup(:default, :development)
1012
rescue Bundler::BundlerError => e
11-
$stderr.puts 'Run `bundle install` to install missing gems'
13+
warn 'Run `bundle install` to install missing gems'
1214
exit_exception(e)
1315
end
1416

@@ -28,7 +30,7 @@ require 'mg'
2830
begin
2931
MG.new('annotate.gemspec')
3032
rescue Exception
31-
$stderr.puts("WARNING: Couldn't read gemspec. As such, a number of tasks may be unavailable to you until you run 'rake gem:gemspec' to correct the issue.")
33+
warn("WARNING: Couldn't read gemspec. As such, a number of tasks may be unavailable to you until you run 'rake gem:gemspec' to correct the issue.")
3234
# Gemspec is probably in a broken state, so let's give ourselves a chance to
3335
# build a new one...
3436
end
@@ -54,9 +56,7 @@ namespace :gem do
5456

5557
Bundler.load.dependencies_for(*RUNTIME_GROUPS).each do |dep|
5658
runtime_resolved = Bundler.definition.specs_for(RUNTIME_GROUPS).find { |spec| spec.name == dep.name }
57-
unless runtime_resolved.nil?
58-
gem.add_dependency(dep.name, dep.requirement)
59-
end
59+
gem.add_dependency(dep.name, dep.requirement) unless runtime_resolved.nil?
6060
end
6161

6262
gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
@@ -73,7 +73,7 @@ namespace :gem do
7373
fn =~ /^pkg/ ||
7474
fn =~ /^spec/ ||
7575
fn =~ /^doc/ ||
76-
fn =~ /^vendor\/cache/
76+
fn =~ %r{^vendor/cache}
7777
end.sort
7878
end
7979
File.open('annotate.gemspec', 'wb') do |fh|
@@ -135,21 +135,22 @@ namespace :integration do
135135
target_dir = File.expand_path(ENV['TARGET']) if ENV['TARGET']
136136
raise 'Must specify TARGET=x, where x is an integration test scenario!' unless target_dir && Dir.exist?(target_dir)
137137
raise 'TARGET directory must be within spec/integration/!' unless target_dir.start_with?(integration_dir)
138+
138139
candidates = {}
139140
FileList[
140141
"#{target_dir}/.rvmrc",
141142
"#{target_dir}/**/*"
142143
].select { |fname| !(File.symlink?(fname) || File.directory?(fname)) }
143144
.map { |fname| fname.sub(integration_dir, '') }
144145
.reject do |fname|
145-
fname =~ /\/\.gitkeep$/ ||
146-
fname =~ /\/app\/models\// ||
147-
fname =~ /\/routes\.rb$/ ||
148-
fname =~ /\/fixtures\// ||
149-
fname =~ /\/factories\// ||
146+
fname =~ %r{/\.gitkeep$} ||
147+
fname =~ %r{/app/models/} ||
148+
fname =~ %r{/routes\.rb$} ||
149+
fname =~ %r{/fixtures/} ||
150+
fname =~ %r{/factories/} ||
150151
fname =~ /\.sqlite3$/ ||
151-
(fname =~ /\/test\// && fname !~ /_helper\.rb$/) ||
152-
(fname =~ /\/spec\// && fname !~ /_helper\.rb$/)
152+
(fname =~ %r{/test/} && fname !~ /_helper\.rb$/) ||
153+
(fname =~ %r{/spec/} && fname !~ /_helper\.rb$/)
153154
end
154155
.map { |fname| "#{integration_dir}#{fname}" }
155156
.each do |fname|
@@ -164,9 +165,11 @@ namespace :integration do
164165

165166
candidates.each_key do |digest|
166167
next unless fixtures.key?(digest)
168+
167169
candidates[digest].each do |fname|
168170
# Double-check contents in case of hash collision...
169171
next unless FileUtils.identical?(fname, fixtures[digest])
172+
170173
destination_dir = Pathname.new(File.dirname(fname))
171174
relative_target = Pathname.new(fixtures[digest]).relative_path_from(destination_dir)
172175
Dir.chdir(destination_dir) do

annotate.gemspec

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# -*- encoding: utf-8 -*-
2-
lib = File.expand_path('../lib', __FILE__)
1+
# frozen_string_literal: true
2+
3+
lib = File.expand_path('lib', __dir__)
34
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
45
require 'annotate/version'
56

@@ -18,15 +19,14 @@ Gem::Specification.new do |s|
1819
s.homepage = 'https://github.com/ctran/annotate_models'
1920
s.licenses = ['Ruby']
2021
s.require_paths = ['lib']
21-
s.rubygems_version = '2.1.11'
2222
s.summary = 'Annotates Rails Models, routes, fixtures, and others based on the database schema.'
2323

24-
s.specification_version = 4 if s.respond_to? :specification_version
25-
s.add_runtime_dependency(%q<rake>, '>= 10.4', '< 14.0')
26-
s.add_runtime_dependency(%q<activerecord>, ['>= 3.2', '< 8.0'])
24+
s.add_dependency('activerecord', ['>= 8.0.0', '< 9'])
25+
s.add_dependency('rake', '>= 10.4', '< 14.0')
2726

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

bin/annotate

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
23

3-
unless File.exist?('./Rakefile') || File.exist?('./Gemfile')
4-
abort 'Please run annotate from the root of the project.'
5-
end
4+
abort 'Please run annotate from the root of the project.' unless File.exist?('./Rakefile') || File.exist?('./Gemfile')
65

76
require 'rubygems'
87
begin
@@ -11,7 +10,7 @@ begin
1110
rescue StandardError
1211
end
1312

14-
here = File.expand_path(File.dirname __FILE__)
13+
here = __dir__
1514
$LOAD_PATH << "#{here}/../lib"
1615

1716
require 'annotate'
@@ -24,7 +23,7 @@ options_result = Annotate::Parser.parse(ARGV)
2423
exit if options_result[:exit]
2524

2625
options = Annotate.setup_options(
27-
is_rake: ENV['is_rake'] && !ENV['is_rake'].empty?
26+
is_rake: ENV.fetch('is_rake', nil) && !ENV['is_rake'].empty?
2827
)
2928
Annotate.eager_load(options) if Annotate::Helpers.include_models?
3029

lib/annotate.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
$LOAD_PATH.unshift(File.dirname(__FILE__))
24
require 'annotate/version'
35
require 'annotate/annotate_models'
@@ -21,6 +23,7 @@ module Annotate
2123
#
2224
def self.set_defaults(options = {})
2325
return if @has_set_defaults
26+
2427
@has_set_defaults = true
2528

2629
options = ActiveSupport::HashWithIndifferentAccess.new(options)
@@ -44,20 +47,23 @@ def self.set_defaults(options = {})
4447
#
4548
def self.setup_options(options = {})
4649
Constants::POSITION_OPTIONS.each do |key|
47-
options[key] = Annotate::Helpers.fallback(ENV[key.to_s], ENV['position'], 'before')
50+
options[key] = Annotate::Helpers.fallback(ENV.fetch(key.to_s, nil), ENV.fetch('position', nil), 'before')
4851
end
4952
Constants::FLAG_OPTIONS.each do |key|
50-
options[key] = Annotate::Helpers.true?(ENV[key.to_s])
53+
options[key] = Annotate::Helpers.true?(ENV.fetch(key.to_s, nil))
5154
end
5255
Constants::OTHER_OPTIONS.each do |key|
53-
options[key] = !ENV[key.to_s].blank? ? ENV[key.to_s] : nil
56+
options[key] = ENV[key.to_s].blank? ? nil : ENV.fetch(key.to_s, nil)
5457
end
5558
Constants::PATH_OPTIONS.each do |key|
56-
options[key] = !ENV[key.to_s].blank? ? ENV[key.to_s].split(',') : []
59+
options[key] = ENV[key.to_s].blank? ? [] : ENV[key.to_s].split(',')
5760
end
5861

5962
options[:additional_file_patterns] ||= []
60-
options[:additional_file_patterns] = options[:additional_file_patterns].split(',') if options[:additional_file_patterns].is_a?(String)
63+
if options[:additional_file_patterns].is_a?(String)
64+
options[:additional_file_patterns] =
65+
options[:additional_file_patterns].split(',')
66+
end
6167
options[:model_dir] = ['app/models'] if options[:model_dir].empty?
6268

6369
options[:wrapper_open] ||= options[:wrapper]
@@ -111,15 +117,15 @@ def self.bootstrap_rake
111117
require 'rake/dsl_definition'
112118
rescue StandardError => e
113119
# We might just be on an old version of Rake...
114-
$stderr.puts e.message
120+
warn e.message
115121
exit e.status_code
116122
end
117123
require 'rake'
118124

119125
load './Rakefile' if File.exist?('./Rakefile')
120126
begin
121127
Rake::Task[:environment].invoke
122-
rescue
128+
rescue StandardError
123129
nil
124130
end
125131
unless defined?(Rails)

lib/annotate/active_record_patch.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
# monkey patches
24

35
module ::ActiveRecord

0 commit comments

Comments
 (0)