rails_best_practices 1.20.0 → 1.20.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +3 -1
  3. data/Gemfile.lock +30 -26
  4. data/Guardfile +2 -0
  5. data/Rakefile +2 -0
  6. data/lib/rails_best_practices.rb +1 -2
  7. data/lib/rails_best_practices/analyzer.rb +56 -46
  8. data/lib/rails_best_practices/core/check.rb +39 -32
  9. data/lib/rails_best_practices/core/checks_loader.rb +8 -6
  10. data/lib/rails_best_practices/core/configs.rb +1 -2
  11. data/lib/rails_best_practices/core/controllers.rb +1 -2
  12. data/lib/rails_best_practices/core/error.rb +1 -1
  13. data/lib/rails_best_practices/core/helpers.rb +1 -2
  14. data/lib/rails_best_practices/core/mailers.rb +1 -2
  15. data/lib/rails_best_practices/core/methods.rb +21 -16
  16. data/lib/rails_best_practices/core/model_associations.rb +9 -4
  17. data/lib/rails_best_practices/core/models.rb +1 -2
  18. data/lib/rails_best_practices/core/modules.rb +1 -1
  19. data/lib/rails_best_practices/core/routes.rb +2 -2
  20. data/lib/rails_best_practices/core/runner.rb +29 -33
  21. data/lib/rails_best_practices/lexicals/long_line_check.rb +7 -3
  22. data/lib/rails_best_practices/option_parser.rb +17 -6
  23. data/lib/rails_best_practices/prepares.rb +1 -1
  24. data/lib/rails_best_practices/prepares/controller_prepare.rb +15 -3
  25. data/lib/rails_best_practices/prepares/gemfile_prepare.rb +1 -1
  26. data/lib/rails_best_practices/prepares/helper_prepare.rb +6 -1
  27. data/lib/rails_best_practices/prepares/initializer_prepare.rb +2 -2
  28. data/lib/rails_best_practices/prepares/mailer_prepare.rb +1 -0
  29. data/lib/rails_best_practices/prepares/model_prepare.rb +52 -12
  30. data/lib/rails_best_practices/prepares/route_prepare.rb +16 -10
  31. data/lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb +15 -13
  32. data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +34 -29
  33. data/lib/rails_best_practices/reviews/check_destroy_return_value_review.rb +14 -5
  34. data/lib/rails_best_practices/reviews/check_save_return_value_review.rb +19 -8
  35. data/lib/rails_best_practices/reviews/hash_syntax_review.rb +5 -5
  36. data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +4 -4
  37. data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +7 -8
  38. data/lib/rails_best_practices/reviews/law_of_demeter_review.rb +6 -6
  39. data/lib/rails_best_practices/reviews/move_code_into_controller_review.rb +1 -1
  40. data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +6 -7
  41. data/lib/rails_best_practices/reviews/move_finder_to_named_scope_review.rb +7 -8
  42. data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +12 -10
  43. data/lib/rails_best_practices/reviews/not_use_default_route_review.rb +1 -2
  44. data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +5 -5
  45. data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +5 -2
  46. data/lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb +6 -3
  47. data/lib/rails_best_practices/reviews/remove_unused_methods_in_helpers_review.rb +6 -4
  48. data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +29 -9
  49. data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +3 -3
  50. data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +17 -15
  51. data/lib/rails_best_practices/reviews/simplify_render_in_controllers_review.rb +1 -2
  52. data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +3 -3
  53. data/lib/rails_best_practices/reviews/use_before_filter_review.rb +2 -1
  54. data/lib/rails_best_practices/reviews/use_model_association_review.rb +5 -5
  55. data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +9 -8
  56. data/lib/rails_best_practices/reviews/use_observer_review.rb +9 -9
  57. data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +26 -26
  58. data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +8 -7
  59. data/lib/rails_best_practices/reviews/use_scope_access_review.rb +17 -15
  60. data/lib/rails_best_practices/reviews/use_turbo_sprockets_rails3_review.rb +2 -1
  61. data/lib/rails_best_practices/version.rb +1 -1
  62. data/rails_best_practices.gemspec +35 -36
  63. data/spec/fixtures/lib/rails_best_practices/plugins/reviews/not_use_rails_root_review.rb +1 -2
  64. data/spec/rails_best_practices/analyzer_spec.rb +73 -42
  65. data/spec/rails_best_practices/core/check_spec.rb +5 -5
  66. data/spec/rails_best_practices/core/checks_loader_spec.rb +3 -3
  67. data/spec/rails_best_practices/core/configs_spec.rb +1 -1
  68. data/spec/rails_best_practices/core/controllers_spec.rb +1 -1
  69. data/spec/rails_best_practices/core/error_spec.rb +21 -21
  70. data/spec/rails_best_practices/core/except_methods_spec.rb +7 -7
  71. data/spec/rails_best_practices/core/gems_spec.rb +4 -4
  72. data/spec/rails_best_practices/core/helpers_spec.rb +1 -1
  73. data/spec/rails_best_practices/core/klasses_spec.rb +3 -3
  74. data/spec/rails_best_practices/core/mailers_spec.rb +1 -1
  75. data/spec/rails_best_practices/core/methods_spec.rb +6 -6
  76. data/spec/rails_best_practices/core/model_associations_spec.rb +10 -6
  77. data/spec/rails_best_practices/core/model_attributes_spec.rb +4 -4
  78. data/spec/rails_best_practices/core/models_spec.rb +1 -1
  79. data/spec/rails_best_practices/core/modules_spec.rb +5 -5
  80. data/spec/rails_best_practices/core/routes_spec.rb +5 -5
  81. data/spec/rails_best_practices/core/runner_spec.rb +9 -7
  82. data/spec/rails_best_practices/core_ext/erubis_spec.rb +10 -10
  83. data/spec/rails_best_practices/lexicals/long_line_check_spec.rb +11 -10
  84. data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +6 -6
  85. data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +6 -6
  86. data/spec/rails_best_practices/prepares/config_prepare_spec.rb +2 -2
  87. data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +18 -10
  88. data/spec/rails_best_practices/prepares/gemfile_prepare_spec.rb +2 -2
  89. data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +3 -3
  90. data/spec/rails_best_practices/prepares/initializer_prepare_spec.rb +3 -3
  91. data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +2 -2
  92. data/spec/rails_best_practices/prepares/model_prepare_spec.rb +79 -43
  93. data/spec/rails_best_practices/prepares/route_prepare_spec.rb +138 -77
  94. data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +2 -2
  95. data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +18 -12
  96. data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +28 -22
  97. data/spec/rails_best_practices/reviews/check_destroy_return_value_review_spec.rb +15 -13
  98. data/spec/rails_best_practices/reviews/check_save_return_value_review_spec.rb +31 -21
  99. data/spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb +6 -6
  100. data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +5 -5
  101. data/spec/rails_best_practices/reviews/hash_syntax_review_spec.rb +9 -9
  102. data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +7 -7
  103. data/spec/rails_best_practices/reviews/keep_finders_on_their_own_model_review_spec.rb +9 -9
  104. data/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb +21 -14
  105. data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +6 -6
  106. data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +11 -6
  107. data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +26 -16
  108. data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +7 -7
  109. data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +9 -7
  110. data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +9 -9
  111. data/spec/rails_best_practices/reviews/not_rescue_exception_review_spec.rb +9 -9
  112. data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +5 -5
  113. data/spec/rails_best_practices/reviews/not_use_time_ago_in_words_review_spec.rb +7 -7
  114. data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +7 -7
  115. data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +24 -19
  116. data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +6 -6
  117. data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +44 -31
  118. data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +17 -12
  119. data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +46 -44
  120. data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +10 -8
  121. data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +16 -10
  122. data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +54 -31
  123. data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +9 -9
  124. data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +13 -13
  125. data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +11 -9
  126. data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +7 -7
  127. data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +21 -17
  128. data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +6 -6
  129. data/spec/rails_best_practices/reviews/use_parentheses_in_method_def_review_spec.rb +9 -7
  130. data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +31 -24
  131. data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +15 -11
  132. data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +14 -14
  133. data/spec/rails_best_practices/reviews/use_turbo_sprockets_rails3_review_spec.rb +10 -8
  134. metadata +7 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7b66ea6e0e742d6db68f867fed12f8242aa0d767b7b3fb4728043c97984f455a
4
- data.tar.gz: 6f8ff9290459f8436908396fec8612d6050c7f5b278697475c4601e8931c0c19
3
+ metadata.gz: e7a2b67944c27b7a11e48f2c490a4ea1080fc4df39ad1d59b20cdeaedfb4b083
4
+ data.tar.gz: d96f6d7da9909bac895162f70ae1247a98a8df39da7fd2ce17365e959568c4f6
5
5
  SHA512:
6
- metadata.gz: '03850b2873726ec5f3928bc1a846320927c56793c71221c715bbbc630cd1f10333b52ac9a713a78db3cbf1db6e2f25a34493d06bcce76d184f2447f5d888deb1'
7
- data.tar.gz: 72586f6144df24dd9fd11861c443554f3903bc0cd8401bd08b1e6a89ff356d545b475b8e465b7e95e1803dd198517695fb91d81d6a746df9654e51525e638365
6
+ metadata.gz: 244acdd4067af5f87364236d4fe5bc40d5a4d68b98d1c6786aaf0e53c7766626ef670c4aaa61d793a47bba643fddcd2cbd545a4697dff8d0a3d6f7238912c2b5
7
+ data.tar.gz: 9b4ca4c95172317d9017691399fcb9211295bad98959477fa8fa24a054a7a6283dc7cdbabd926e925ccf0f3ea9bfe1dde3e594cc42a2db7bd3b37758cb7bdc6e
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
  gemspec
3
5
 
@@ -6,4 +8,4 @@ gem 'coveralls', require: false
6
8
  gem 'guard'
7
9
  gem 'guard-rspec'
8
10
  gem 'pry'
9
- gem 'rspec', '~> 2.14.0'
11
+ gem 'rspec'
data/Gemfile.lock CHANGED
@@ -1,9 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_best_practices (1.20.0)
4
+ rails_best_practices (1.20.1)
5
5
  activesupport
6
- code_analyzer (>= 0.5.1)
6
+ code_analyzer (>= 0.5.2)
7
7
  erubis
8
8
  i18n
9
9
  json
@@ -13,17 +13,17 @@ PATH
13
13
  GEM
14
14
  remote: https://rubygems.org/
15
15
  specs:
16
- activesupport (6.0.2.1)
16
+ activesupport (6.1.3.2)
17
17
  concurrent-ruby (~> 1.0, >= 1.0.2)
18
- i18n (>= 0.7, < 2)
19
- minitest (~> 5.1)
20
- tzinfo (~> 1.1)
21
- zeitwerk (~> 2.2)
18
+ i18n (>= 1.6, < 2)
19
+ minitest (>= 5.1)
20
+ tzinfo (~> 2.0)
21
+ zeitwerk (~> 2.3)
22
22
  awesome_print (1.8.0)
23
- code_analyzer (0.5.1)
23
+ code_analyzer (0.5.2)
24
24
  sexp_processor
25
25
  coderay (1.1.2)
26
- concurrent-ruby (1.1.5)
26
+ concurrent-ruby (1.1.8)
27
27
  coveralls (0.8.23)
28
28
  json (>= 1.8, < 3)
29
29
  simplecov (~> 0.16.1)
@@ -50,7 +50,7 @@ GEM
50
50
  haml (5.1.2)
51
51
  temple (>= 0.8.0)
52
52
  tilt
53
- i18n (1.7.0)
53
+ i18n (1.8.10)
54
54
  concurrent-ruby (~> 1.0)
55
55
  json (2.3.0)
56
56
  listen (3.2.1)
@@ -58,7 +58,7 @@ GEM
58
58
  rb-inotify (~> 0.9, >= 0.9.10)
59
59
  lumberjack (1.0.13)
60
60
  method_source (0.9.2)
61
- minitest (5.13.0)
61
+ minitest (5.14.4)
62
62
  nenv (0.3.0)
63
63
  notiffany (0.1.3)
64
64
  nenv (~> 0.1)
@@ -71,16 +71,21 @@ GEM
71
71
  rb-inotify (0.10.1)
72
72
  ffi (~> 1.0)
73
73
  require_all (3.0.0)
74
- rspec (2.14.1)
75
- rspec-core (~> 2.14.0)
76
- rspec-expectations (~> 2.14.0)
77
- rspec-mocks (~> 2.14.0)
78
- rspec-core (2.14.8)
79
- rspec-expectations (2.14.5)
80
- diff-lcs (>= 1.1.3, < 2.0)
81
- rspec-mocks (2.14.6)
82
- ruby-progressbar (1.10.1)
83
- sexp_processor (4.13.0)
74
+ rspec (3.9.0)
75
+ rspec-core (~> 3.9.0)
76
+ rspec-expectations (~> 3.9.0)
77
+ rspec-mocks (~> 3.9.0)
78
+ rspec-core (3.9.1)
79
+ rspec-support (~> 3.9.1)
80
+ rspec-expectations (3.9.1)
81
+ diff-lcs (>= 1.2.0, < 2.0)
82
+ rspec-support (~> 3.9.0)
83
+ rspec-mocks (3.9.1)
84
+ diff-lcs (>= 1.2.0, < 2.0)
85
+ rspec-support (~> 3.9.0)
86
+ rspec-support (3.9.2)
87
+ ruby-progressbar (1.11.0)
88
+ sexp_processor (4.15.2)
84
89
  shellany (0.0.1)
85
90
  simplecov (0.16.1)
86
91
  docile (~> 1.1)
@@ -94,12 +99,11 @@ GEM
94
99
  term-ansicolor (1.7.1)
95
100
  tins (~> 1.0)
96
101
  thor (1.0.1)
97
- thread_safe (0.3.6)
98
102
  tilt (2.0.10)
99
103
  tins (1.22.2)
100
- tzinfo (1.2.6)
101
- thread_safe (~> 0.1)
102
- zeitwerk (2.2.2)
104
+ tzinfo (2.0.4)
105
+ concurrent-ruby (~> 1.0)
106
+ zeitwerk (2.4.2)
103
107
 
104
108
  PLATFORMS
105
109
  ruby
@@ -114,7 +118,7 @@ DEPENDENCIES
114
118
  pry
115
119
  rails_best_practices!
116
120
  rake
117
- rspec (~> 2.14.0)
121
+ rspec
118
122
  slim
119
123
 
120
124
  BUNDLED WITH
data/Guardfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # A sample Guardfile
2
4
  # More info at https://github.com/guard/guard#readme
3
5
 
data/Rakefile CHANGED
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/gem_tasks'
@@ -11,5 +11,4 @@ require 'rails_best_practices/reviews'
11
11
  require 'rails_best_practices/option_parser'
12
12
  require 'rails_best_practices/cli'
13
13
 
14
- module RailsBestPractices
15
- end
14
+ module RailsBestPractices; end
@@ -104,30 +104,31 @@ module RailsBestPractices
104
104
  #
105
105
  # @return [Array] all files for parsing
106
106
  def parse_files
107
- @parse_files ||= begin
108
- files = expand_dirs_to_files(@path)
109
- files = file_sort(files)
107
+ @parse_files ||=
108
+ begin
109
+ files = expand_dirs_to_files(@path)
110
+ files = file_sort(files)
110
111
 
111
- if @options['only'].present?
112
- files = file_accept(files, @options['only'])
113
- end
112
+ if @options['only'].present?
113
+ files = file_accept(files, @options['only'])
114
+ end
114
115
 
115
- # By default, tmp, vender, spec, test, features are ignored.
116
- %w[vendor spec test features tmp].each do |dir|
117
- files = file_ignore(files, File.join(@path, dir)) unless @options[dir]
118
- end
116
+ # By default, tmp, vender, spec, test, features are ignored.
117
+ %w[vendor spec test features tmp].each do |dir|
118
+ files = file_ignore(files, File.join(@path, dir)) unless @options[dir]
119
+ end
119
120
 
120
- # Exclude files based on exclude regexes if the option is set.
121
- @options['exclude'].each do |pattern|
122
- files = file_ignore(files, pattern)
123
- end
121
+ # Exclude files based on exclude regexes if the option is set.
122
+ @options['exclude'].each do |pattern|
123
+ files = file_ignore(files, pattern)
124
+ end
124
125
 
125
- %w[Capfile Gemfile Gemfile.lock].each do |file|
126
- files.unshift File.join(@path, file)
127
- end
126
+ %w[Capfile Gemfile Gemfile.lock].each do |file|
127
+ files.unshift File.join(@path, file)
128
+ end
128
129
 
129
- files.compact
130
- end
130
+ files.compact
131
+ end
131
132
  end
132
133
 
133
134
  # expand all files with extenstion rb, erb, haml, slim, builder and rxml under the dirs
@@ -158,7 +159,10 @@ module RailsBestPractices
158
159
  models = files.find_all { |file| file =~ Core::Check::MODEL_FILES }
159
160
  mailers = files.find_all { |file| file =~ Core::Check::MAILER_FILES }
160
161
  helpers = files.find_all { |file| file =~ Core::Check::HELPER_FILES }
161
- others = files.find_all { |file| file !~ Core::Check::MAILER_FILES && file !~ Core::Check::MODEL_FILES && file !~ Core::Check::HELPER_FILES }
162
+ others =
163
+ files.find_all do |file|
164
+ file !~ Core::Check::MAILER_FILES && file !~ Core::Check::MODEL_FILES && file !~ Core::Check::HELPER_FILES
165
+ end
162
166
  models + mailers + helpers + others
163
167
  end
164
168
 
@@ -194,7 +198,10 @@ module RailsBestPractices
194
198
  def load_hg_info
195
199
  hg_progressbar = ProgressBar.create(title: 'Hg Info', total: errors.size) if display_bar?
196
200
  errors.each do |error|
197
- hg_info = `cd #{@runner.class.base_path} && hg blame -lvcu #{error.filename[@runner.class.base_path.size..-1].gsub(/^\//, '')} | sed -n /:#{error.line_number.split(',').first}:/p`
201
+ info_command = "cd #{@runner.class.base_path}"
202
+ info_command += " && hg blame -lvcu #{error.filename[@runner.class.base_path.size..-1].gsub(%r{^/}, '')}"
203
+ info_command += " | sed -n /:#{error.line_number.split(',').first}:/p"
204
+ hg_info = system(info_command)
198
205
  unless hg_info == ''
199
206
  hg_commit_username = hg_info.split(':')[0].strip
200
207
  error.hg_username = hg_commit_username.split(/\ /)[0..-2].join(' ')
@@ -208,9 +215,11 @@ module RailsBestPractices
208
215
  # load git commit and git username info.
209
216
  def load_git_info
210
217
  git_progressbar = ProgressBar.create(title: 'Git Info', total: errors.size) if display_bar?
211
- start = @runner.class.base_path =~ /\/$/ ? @runner.class.base_path.size : @runner.class.base_path.size + 1
218
+ start = @runner.class.base_path =~ %r{/$} ? @runner.class.base_path.size : @runner.class.base_path.size + 1
212
219
  errors.each do |error|
213
- git_info = `cd #{@runner.class.base_path} && git blame -L #{error.line_number.split(',').first},+1 #{error.filename[start..-1]}`
220
+ info_command = "cd #{@runner.class.base_path}"
221
+ info_command += " && git blame -L #{error.line_number.split(',').first},+1 #{error.filename[start..-1]}"
222
+ git_info = system(info_command)
214
223
  unless git_info == ''
215
224
  git_commit, git_username = git_info.split(/\d{4}-\d{2}-\d{2}/).first.split('(')
216
225
  error.git_commit = git_commit.split(' ').first.strip
@@ -224,7 +233,10 @@ module RailsBestPractices
224
233
  # output errors with html format.
225
234
  def output_html_errors
226
235
  require 'erubis'
227
- template = @options['template'] ? File.read(File.expand_path(@options['template'])) : File.read(File.join(File.dirname(__FILE__), '..', '..', 'assets', 'result.html.erb'))
236
+ template =
237
+ @options['template'] ?
238
+ File.read(File.expand_path(@options['template'])) :
239
+ File.read(File.join(File.dirname(__FILE__), '..', '..', 'assets', 'result.html.erb'))
228
240
 
229
241
  if @options['with-github']
230
242
  last_commit_id = @options['last-commit-id'] || `cd #{@runner.class.base_path} && git rev-parse HEAD`.chomp
@@ -235,27 +247,28 @@ module RailsBestPractices
235
247
  File.open(@options['output-file'], 'w+') do |file|
236
248
  eruby = Erubis::Eruby.new(template)
237
249
  file.puts eruby.evaluate(
238
- errors: errors,
239
- error_types: error_types,
240
- textmate: @options['with-textmate'],
241
- vscode: @options['with-vscode'],
242
- sublime: @options['with-sublime'],
243
- mvim: @options['with-mvim'],
244
- github: @options['with-github'],
245
- github_name: @options['github-name'],
246
- last_commit_id: last_commit_id,
247
- git: @options['with-git'],
248
- hg: @options['with-hg']
249
- )
250
+ errors: errors,
251
+ error_types: error_types,
252
+ textmate: @options['with-textmate'],
253
+ vscode: @options['with-vscode'],
254
+ sublime: @options['with-sublime'],
255
+ mvim: @options['with-mvim'],
256
+ github: @options['with-github'],
257
+ github_name: @options['github-name'],
258
+ last_commit_id: last_commit_id,
259
+ git: @options['with-git'],
260
+ hg: @options['with-hg']
261
+ )
250
262
  end
251
263
  end
252
264
 
253
265
  def output_xml_errors
254
266
  require 'rexml/document'
255
267
 
256
- document = REXML::Document.new.tap do |d|
257
- d << REXML::XMLDecl.new
258
- end
268
+ document =
269
+ REXML::Document.new.tap do |d|
270
+ d << REXML::XMLDecl.new
271
+ end
259
272
 
260
273
  checkstyle = REXML::Element.new('checkstyle', document)
261
274
 
@@ -289,13 +302,10 @@ module RailsBestPractices
289
302
 
290
303
  # output errors with json format.
291
304
  def output_json_errors
292
- errors_as_hashes = errors.map do |err|
293
- {
294
- filename: err.filename,
295
- line_number: err.line_number,
296
- message: err.message
297
- }
298
- end
305
+ errors_as_hashes =
306
+ errors.map do |err|
307
+ { filename: err.filename, line_number: err.line_number, message: err.message }
308
+ end
299
309
 
300
310
  File.open(@options['output-file'], 'w+') do |file|
301
311
  file.write JSON.dump(errors_as_hashes)
@@ -3,24 +3,25 @@
3
3
  module RailsBestPractices
4
4
  module Core
5
5
  # A Check class that takes charge of checking the sexp.
6
+
6
7
  class Check < CodeAnalyzer::Checker
7
- ALL_FILES = /.*/
8
- CONTROLLER_FILES = /app\/(controllers|cells)\/.*\.rb$/
9
- MIGRATION_FILES = /db\/migrate\/.*\.rb$/
10
- MODEL_FILES = /app\/models\/.*\.rb$/
11
- MAILER_FILES = /app\/models\/.*mailer\.rb$|app\/mailers\/.*\.rb/
12
- VIEW_FILES = /app\/(views|cells)\/.*\.(erb|haml|slim|builder|rxml)$/
13
- PARTIAL_VIEW_FILES = /app\/(views|cells)\/.*\/_.*\.(erb|haml|slim|builder|rxml)$/
14
- ROUTE_FILES = /config\/routes.*\.rb/
15
- SCHEMA_FILE = /db\/schema\.rb/
16
- HELPER_FILES = /app\/helpers\/.*\.rb$/
17
- DEPLOY_FILES = /config\/deploy.*\.rb/
18
- CONFIG_FILES = /config\/(application|environment|environments\/.*)\.rb/
19
- INITIALIZER_FILES = /config\/initializers\/.*\.rb/
20
- CAPFILE = /Capfile/
21
- GEMFILE_LOCK = /Gemfile\.lock/
22
-
23
- SKIP_FILES = /db\/schema.rb/
8
+ ALL_FILES = /.*/.freeze
9
+ CONTROLLER_FILES = %r{app/(controllers|cells)/.*\.rb$}.freeze
10
+ MIGRATION_FILES = %r{db/migrate/.*\.rb$}.freeze
11
+ MODEL_FILES = %r{app/models/.*\.rb$}.freeze
12
+ MAILER_FILES = %r{app/models/.*mailer\.rb$|app/mailers/.*\.rb}.freeze
13
+ VIEW_FILES = %r{app/(views|cells)/.*\.(erb|haml|slim|builder|rxml)$}.freeze
14
+ PARTIAL_VIEW_FILES = %r{app/(views|cells)/.*/_.*\.(erb|haml|slim|builder|rxml)$}.freeze
15
+ ROUTE_FILES = %r{config/routes.*\.rb}.freeze
16
+ SCHEMA_FILE = %r{db/schema\.rb}.freeze
17
+ HELPER_FILES = %r{app/helpers/.*\.rb$}.freeze
18
+ DEPLOY_FILES = %r{config/deploy.*\.rb}.freeze
19
+ CONFIG_FILES = %r{config/(application|environment|environments/.*)\.rb}.freeze
20
+ INITIALIZER_FILES = %r{config/initializers/.*\.rb}.freeze
21
+ CAPFILE = /Capfile/.freeze
22
+ GEMFILE_LOCK = /Gemfile\.lock/.freeze
23
+
24
+ SKIP_FILES = %r{db/schema.rb}.freeze
24
25
 
25
26
  def initialize(options = {})
26
27
  options.each do |key, value|
@@ -33,7 +34,7 @@ module RailsBestPractices
33
34
  # @param [String] the file name of node.
34
35
  # @return [Boolean] true if the check will need to parse the file.
35
36
  def parse_file?(node_file)
36
- is_interesting_file?(node_file) and !is_ignored?(node_file)
37
+ node_file.is_a?(String) && is_interesting_file?(node_file) && !is_ignored?(node_file)
37
38
  end
38
39
 
39
40
  def is_interesting_file?(node_file)
@@ -60,13 +61,10 @@ module RailsBestPractices
60
61
  # @param [String] filename, is the filename of source code
61
62
  # @param [Integer] line_number, is the line number of the source code which is reviewing
62
63
  def add_error(message, filename = @node.file, line_number = @node.line_number)
63
- errors << RailsBestPractices::Core::Error.new(
64
- filename: filename,
65
- line_number: line_number,
66
- message: message,
67
- type: self.class.to_s,
68
- url: url
69
- )
64
+ errors <<
65
+ RailsBestPractices::Core::Error.new(
66
+ filename: filename, line_number: line_number, message: message, type: self.class.to_s, url: url
67
+ )
70
68
  end
71
69
 
72
70
  # errors that violate the rails best practices.
@@ -99,9 +97,9 @@ module RailsBestPractices
99
97
  end
100
98
  end
101
99
 
102
- class <<self
100
+ class << self
103
101
  def url(url = nil)
104
- url ? @url = url : @url
102
+ url ? @url = url : @url
105
103
  end
106
104
 
107
105
  def debug?
@@ -114,6 +112,7 @@ module RailsBestPractices
114
112
  end
115
113
 
116
114
  # Helper to parse the class name.
115
+
117
116
  module Classable
118
117
  def self.included(base)
119
118
  base.class_eval do
@@ -165,6 +164,7 @@ module RailsBestPractices
165
164
  end
166
165
 
167
166
  # Helper to parse the module name.
167
+
168
168
  module Moduleable
169
169
  def self.included(base)
170
170
  base.class_eval do
@@ -197,7 +197,15 @@ module RailsBestPractices
197
197
  module Callable
198
198
  def self.included(base)
199
199
  base.class_eval do
200
- interesting_nodes :call, :fcall, :var_ref, :vcall, :command_call, :command, :alias, :bare_assoc_hash, :method_add_arg
200
+ interesting_nodes :call,
201
+ :fcall,
202
+ :var_ref,
203
+ :vcall,
204
+ :command_call,
205
+ :command,
206
+ :alias,
207
+ :bare_assoc_hash,
208
+ :method_add_arg
201
209
 
202
210
  # remembe the message of call node.
203
211
  add_callback :start_call do |node|
@@ -371,10 +379,8 @@ module RailsBestPractices
371
379
  class_name = '.*' if class_name == '*'
372
380
  class_expression = Regexp.new class_name
373
381
 
374
- class_names = Prepares.klasses
375
- .select { |klass| klass.class_name == method.class_name }
376
- .map(&:extend_class_name)
377
- .compact
382
+ class_names =
383
+ Prepares.klasses.select { |klass| klass.class_name == method.class_name }.map(&:extend_class_name).compact
378
384
 
379
385
  class_names.unshift method.class_name
380
386
  matched = class_names.any? { |name| name =~ class_expression }
@@ -385,6 +391,7 @@ module RailsBestPractices
385
391
  end
386
392
 
387
393
  # Helper to parse the access control.
394
+
388
395
  module Accessable
389
396
  def self.included(base)
390
397
  base.class_eval do
@@ -14,17 +14,19 @@ module RailsBestPractices
14
14
 
15
15
  # load all reviews according to configuration.
16
16
  def load_reviews
17
- load_checks_from_config { |check_name| RailsBestPractices::Reviews.const_get(check_name.gsub(/Check$/, 'Review')) }
17
+ load_checks_from_config do |check_name|
18
+ RailsBestPractices::Reviews.const_get(check_name.gsub(/Check$/, 'Review'))
19
+ end
18
20
  end
19
21
 
20
22
  private
21
23
 
22
- # read the checks from yaml config.
24
+ # read the checks from yaml config.
23
25
  def checks_from_config
24
26
  @checks ||= YAML.load_file @config
25
27
  end
26
28
 
27
- # load all checks from the configuration
29
+ # load all checks from the configuration
28
30
  def load_checks_from_config(&block)
29
31
  checks_from_config.each_with_object([]) do |check, active_checks|
30
32
  check_instance = instantiate_check(block, *check)
@@ -32,13 +34,13 @@ module RailsBestPractices
32
34
  end
33
35
  end
34
36
 
35
- # instantiates a check
37
+ # instantiates a check
36
38
  def instantiate_check(block, check_name, options)
37
39
  check_class = load_check_class(check_name, &block)
38
- check_class.new(options || {}) unless check_class.nil?
40
+ check_class&.new(options || {})
39
41
  end
40
42
 
41
- # loads the class for a check by calling the given block
43
+ # loads the class for a check by calling the given block
42
44
  def load_check_class(check_name)
43
45
  yield(check_name)
44
46
  rescue NameError