rails_best_practices 1.19.1 → 1.19.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/Guardfile +1 -1
  4. data/README.md +1 -0
  5. data/Rakefile +2 -2
  6. data/bin/rails_best_practices +2 -1
  7. data/lib/rails_best_practices.rb +2 -1
  8. data/lib/rails_best_practices/analyzer.rb +13 -12
  9. data/lib/rails_best_practices/colorize.rb +2 -0
  10. data/lib/rails_best_practices/command.rb +6 -5
  11. data/lib/rails_best_practices/core.rb +2 -1
  12. data/lib/rails_best_practices/core/check.rb +19 -17
  13. data/lib/rails_best_practices/core/checks_loader.rb +3 -2
  14. data/lib/rails_best_practices/core/configs.rb +2 -1
  15. data/lib/rails_best_practices/core/controllers.rb +4 -1
  16. data/lib/rails_best_practices/core/error.rb +3 -2
  17. data/lib/rails_best_practices/core/gems.rb +4 -3
  18. data/lib/rails_best_practices/core/helpers.rb +4 -1
  19. data/lib/rails_best_practices/core/klasses.rb +3 -2
  20. data/lib/rails_best_practices/core/mailers.rb +2 -1
  21. data/lib/rails_best_practices/core/methods.rb +9 -10
  22. data/lib/rails_best_practices/core/model_associations.rb +6 -5
  23. data/lib/rails_best_practices/core/model_attributes.rb +2 -1
  24. data/lib/rails_best_practices/core/models.rb +2 -1
  25. data/lib/rails_best_practices/core/modules.rb +2 -1
  26. data/lib/rails_best_practices/core/routes.rb +2 -1
  27. data/lib/rails_best_practices/core/runner.rb +7 -6
  28. data/lib/rails_best_practices/core_ext/erubis.rb +4 -5
  29. data/lib/rails_best_practices/lexicals.rb +2 -1
  30. data/lib/rails_best_practices/lexicals/long_line_check.rb +2 -1
  31. data/lib/rails_best_practices/lexicals/remove_tab_check.rb +2 -1
  32. data/lib/rails_best_practices/lexicals/remove_trailing_whitespace_check.rb +2 -1
  33. data/lib/rails_best_practices/prepares.rb +2 -1
  34. data/lib/rails_best_practices/prepares/config_prepare.rb +3 -2
  35. data/lib/rails_best_practices/prepares/controller_prepare.rb +9 -8
  36. data/lib/rails_best_practices/prepares/gemfile_prepare.rb +2 -1
  37. data/lib/rails_best_practices/prepares/helper_prepare.rb +4 -3
  38. data/lib/rails_best_practices/prepares/initializer_prepare.rb +2 -1
  39. data/lib/rails_best_practices/prepares/mailer_prepare.rb +3 -2
  40. data/lib/rails_best_practices/prepares/model_prepare.rb +12 -12
  41. data/lib/rails_best_practices/prepares/route_prepare.rb +8 -7
  42. data/lib/rails_best_practices/prepares/schema_prepare.rb +3 -2
  43. data/lib/rails_best_practices/reviews.rb +2 -1
  44. data/lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb +7 -4
  45. data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +8 -7
  46. data/lib/rails_best_practices/reviews/check_destroy_return_value_review.rb +4 -3
  47. data/lib/rails_best_practices/reviews/check_save_return_value_review.rb +5 -4
  48. data/lib/rails_best_practices/reviews/default_scope_is_evil_review.rb +2 -1
  49. data/lib/rails_best_practices/reviews/dry_bundler_in_capistrano_review.rb +2 -1
  50. data/lib/rails_best_practices/reviews/hash_syntax_review.rb +2 -1
  51. data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +3 -2
  52. data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +3 -2
  53. data/lib/rails_best_practices/reviews/law_of_demeter_review.rb +3 -2
  54. data/lib/rails_best_practices/reviews/move_code_into_controller_review.rb +3 -2
  55. data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +2 -1
  56. data/lib/rails_best_practices/reviews/move_code_into_model_review.rb +3 -2
  57. data/lib/rails_best_practices/reviews/move_finder_to_named_scope_review.rb +3 -2
  58. data/lib/rails_best_practices/reviews/move_model_logic_into_model_review.rb +3 -2
  59. data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +5 -4
  60. data/lib/rails_best_practices/reviews/not_rescue_exception_review.rb +2 -1
  61. data/lib/rails_best_practices/reviews/not_use_default_route_review.rb +2 -1
  62. data/lib/rails_best_practices/reviews/not_use_time_ago_in_words_review.rb +2 -1
  63. data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +3 -2
  64. data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +4 -3
  65. data/lib/rails_best_practices/reviews/remove_empty_helpers_review.rb +2 -1
  66. data/lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb +11 -10
  67. data/lib/rails_best_practices/reviews/remove_unused_methods_in_helpers_review.rb +4 -3
  68. data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +6 -5
  69. data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +2 -2
  70. data/lib/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review.rb +2 -1
  71. data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +8 -8
  72. data/lib/rails_best_practices/reviews/review.rb +4 -3
  73. data/lib/rails_best_practices/reviews/simplify_render_in_controllers_review.rb +2 -1
  74. data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +3 -2
  75. data/lib/rails_best_practices/reviews/use_before_filter_review.rb +5 -4
  76. data/lib/rails_best_practices/reviews/use_model_association_review.rb +2 -2
  77. data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +5 -5
  78. data/lib/rails_best_practices/reviews/use_observer_review.rb +2 -1
  79. data/lib/rails_best_practices/reviews/use_parentheses_in_method_def_review.rb +2 -1
  80. data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +4 -3
  81. data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +4 -3
  82. data/lib/rails_best_practices/reviews/use_scope_access_review.rb +2 -1
  83. data/lib/rails_best_practices/reviews/use_turbo_sprockets_rails3_review.rb +2 -1
  84. data/lib/rails_best_practices/version.rb +3 -2
  85. data/rails_best_practices.gemspec +11 -10
  86. data/spec/fixtures/lib/rails_best_practices/plugins/reviews/not_use_rails_root_review.rb +3 -2
  87. data/spec/rails_best_practices/analyzer_spec.rb +6 -8
  88. data/spec/rails_best_practices/core/check_spec.rb +2 -0
  89. data/spec/rails_best_practices/core/checks_loader_spec.rb +2 -0
  90. data/spec/rails_best_practices/core/configs_spec.rb +2 -0
  91. data/spec/rails_best_practices/core/controllers_spec.rb +2 -0
  92. data/spec/rails_best_practices/core/error_spec.rb +2 -0
  93. data/spec/rails_best_practices/core/except_methods_spec.rb +2 -0
  94. data/spec/rails_best_practices/core/gems_spec.rb +2 -0
  95. data/spec/rails_best_practices/core/helpers_spec.rb +2 -0
  96. data/spec/rails_best_practices/core/klasses_spec.rb +2 -0
  97. data/spec/rails_best_practices/core/mailers_spec.rb +2 -0
  98. data/spec/rails_best_practices/core/methods_spec.rb +6 -4
  99. data/spec/rails_best_practices/core/model_associations_spec.rb +4 -2
  100. data/spec/rails_best_practices/core/model_attributes_spec.rb +2 -0
  101. data/spec/rails_best_practices/core/models_spec.rb +2 -0
  102. data/spec/rails_best_practices/core/modules_spec.rb +2 -0
  103. data/spec/rails_best_practices/core/routes_spec.rb +4 -2
  104. data/spec/rails_best_practices/core/runner_spec.rb +2 -0
  105. data/spec/rails_best_practices/core_ext/erubis_spec.rb +3 -1
  106. data/spec/rails_best_practices/lexicals/long_line_check_spec.rb +8 -7
  107. data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +8 -6
  108. data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +9 -7
  109. data/spec/rails_best_practices/prepares/config_prepare_spec.rb +3 -1
  110. data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +23 -21
  111. data/spec/rails_best_practices/prepares/gemfile_prepare_spec.rb +3 -1
  112. data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +7 -5
  113. data/spec/rails_best_practices/prepares/initializer_prepare_spec.rb +4 -2
  114. data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +3 -1
  115. data/spec/rails_best_practices/prepares/model_prepare_spec.rb +56 -54
  116. data/spec/rails_best_practices/prepares/route_prepare_spec.rb +39 -37
  117. data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +3 -1
  118. data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +2 -0
  119. data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +7 -5
  120. data/spec/rails_best_practices/reviews/check_destroy_return_value_review_spec.rb +12 -10
  121. data/spec/rails_best_practices/reviews/check_save_return_value_review_spec.rb +20 -18
  122. data/spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb +2 -0
  123. data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +2 -0
  124. data/spec/rails_best_practices/reviews/hash_syntax_review_spec.rb +9 -7
  125. data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +2 -0
  126. data/spec/rails_best_practices/reviews/keep_finders_on_their_own_model_review_spec.rb +2 -0
  127. data/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb +2 -0
  128. data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +2 -0
  129. data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +2 -0
  130. data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +11 -9
  131. data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +2 -0
  132. data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +2 -0
  133. data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +2 -0
  134. data/spec/rails_best_practices/reviews/not_rescue_exception_review_spec.rb +9 -7
  135. data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +2 -0
  136. data/spec/rails_best_practices/reviews/not_use_time_ago_in_words_review_spec.rb +7 -5
  137. data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +2 -0
  138. data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +21 -19
  139. data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +6 -4
  140. data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +33 -31
  141. data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +13 -11
  142. data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +65 -63
  143. data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +2 -0
  144. data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +2 -0
  145. data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +24 -22
  146. data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +9 -7
  147. data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +13 -11
  148. data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +3 -1
  149. data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +2 -0
  150. data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +2 -0
  151. data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +7 -5
  152. data/spec/rails_best_practices/reviews/use_parentheses_in_method_def_review_spec.rb +2 -0
  153. data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +3 -1
  154. data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +9 -7
  155. data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +2 -0
  156. data/spec/rails_best_practices/reviews/use_turbo_sprockets_rails3_review_spec.rb +2 -0
  157. data/spec/spec_helper.rb +2 -0
  158. metadata +26 -26
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Core
4
5
  # Mailer classes.
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Core
4
5
  # Method container.
@@ -14,7 +15,7 @@ module RailsBestPractices
14
15
  # @param [String] method name
15
16
  # @param [Hash] method meta, file and line, {"file" => "app/models/post.rb", "line_number" => 5}
16
17
  # @param [String] access control, public, protected or private
17
- def add_method(class_name, method_name, meta={}, access_control='public')
18
+ def add_method(class_name, method_name, meta = {}, access_control = 'public')
18
19
  return if class_name == ''
19
20
  return if has_method?(class_name, method_name)
20
21
  methods(class_name) << Method.new(class_name, method_name, access_control, meta)
@@ -28,7 +29,7 @@ module RailsBestPractices
28
29
  # @param [String] class name
29
30
  # @param [String] access control
30
31
  # @return [Array] all methods of a class for such access control, if access control is nil, return all public/protected/private methods
31
- def get_methods(class_name, access_control=nil)
32
+ def get_methods(class_name, access_control = nil)
32
33
  if access_control
33
34
  methods(class_name).select { |method| method.access_control == access_control }
34
35
  else
@@ -42,7 +43,7 @@ module RailsBestPractices
42
43
  # @param [String] method name
43
44
  # @param [String] access control
44
45
  # @return [Boolean] has a method or not
45
- def has_method?(class_name, method_name, access_control=nil)
46
+ def has_method?(class_name, method_name, access_control = nil)
46
47
  if access_control
47
48
  !!methods(class_name).find { |method| method.method_name == method_name && method.access_control == access_control }
48
49
  else
@@ -109,7 +110,7 @@ module RailsBestPractices
109
110
  # @param [String] method name
110
111
  # @param [String] access control
111
112
  # @return [Method] Method object
112
- def get_method(class_name, method_name, access_control=nil)
113
+ def get_method(class_name, method_name, access_control = nil)
113
114
  if access_control
114
115
  methods(class_name).find { |method| method.method_name == method_name && method.access_control == access_control }
115
116
  else
@@ -121,12 +122,12 @@ module RailsBestPractices
121
122
  #
122
123
  # @param [String] access control
123
124
  # @return [Array] array of Method
124
- def get_all_unused_methods(access_control=nil)
125
- @methods.inject([]) { |unused_methods, (class_name, methods)|
125
+ def get_all_unused_methods(access_control = nil)
126
+ @methods.inject([]) { |unused_methods, (_class_name, methods)|
126
127
  unused_methods += if access_control
127
128
  methods.select { |method| method.access_control == access_control && !method.used }
128
129
  else
129
- methods.select { |method| !method.used }
130
+ methods.reject { |method| method.used }
130
131
  end
131
132
  }.reject { |method| method.access_control == 'public' && @possible_methods[method.method_name] }
132
133
  end
@@ -164,8 +165,6 @@ module RailsBestPractices
164
165
  def publicize
165
166
  @access_control = 'public'
166
167
  end
167
-
168
168
  end
169
169
  end
170
170
  end
171
-
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Core
4
5
  # Model associations container.
@@ -12,9 +13,9 @@ module RailsBestPractices
12
13
  # @param [String] association name
13
14
  # @param [String] association meta, has_many, has_one, belongs_to and has_and_belongs_to_many
14
15
  # @param [String] association class name
15
- def add_association(model_name, association_name, association_meta, association_class=nil)
16
+ def add_association(model_name, association_name, association_meta, association_class = nil)
16
17
  @associations[model_name] ||= {}
17
- @associations[model_name][association_name] = {'meta' => association_meta, 'class_name' => association_class || association_name.classify}
18
+ @associations[model_name][association_name] = { 'meta' => association_meta, 'class_name' => association_class || association_name.classify }
18
19
  end
19
20
 
20
21
  # Get a model association.
@@ -48,8 +49,8 @@ module RailsBestPractices
48
49
  # @param [String] association_name
49
50
  # @return [String] association's class name
50
51
  def get_association_class_name(table_name, association_name)
51
- associations = @associations.select { |model, model_associations| model.gsub('::', '').tableize == table_name }.values.first and
52
- association_meta = associations.select { |name, meta| name == association_name }.values.first and
52
+ associations = @associations.select { |model, _model_associations| model.gsub('::', '').tableize == table_name }.values.first and
53
+ association_meta = associations.select { |name, _meta| name == association_name }.values.first and
53
54
  association_meta['class_name']
54
55
  end
55
56
  end
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Core
4
5
  # Model attributes container.
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Core
4
5
  # Model classes.
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Core
4
5
  # Module container
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Core
4
5
  class Routes < Array
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'yaml'
3
4
  require 'active_support/core_ext/object/blank'
4
5
  begin
@@ -45,13 +46,13 @@ module RailsBestPractices
45
46
  # @return [String] the config path
46
47
  def self.config_path
47
48
  custom_config = @config_path || File.join(Runner.base_path, 'config/rails_best_practices.yml')
48
- File.exists?(custom_config) ? custom_config : RailsBestPractices::Analyzer::DEFAULT_CONFIG
49
+ File.exist?(custom_config) ? custom_config : RailsBestPractices::Analyzer::DEFAULT_CONFIG
49
50
  end
50
51
 
51
52
  # initialize the runner.
52
53
  #
53
54
  # @param [Hash] options pass the prepares and reviews.
54
- def initialize(options={})
55
+ def initialize(options = {})
55
56
  @config = self.class.config_path
56
57
 
57
58
  lexicals = Array(options[:lexicals])
@@ -66,7 +67,7 @@ module RailsBestPractices
66
67
 
67
68
  @lexical_checker ||= CodeAnalyzer::CheckingVisitor::Plain.new(checkers: @lexicals)
68
69
  @plain_prepare_checker ||= CodeAnalyzer::CheckingVisitor::Plain.new(checkers: @prepares.select { |checker| checker.is_a? Prepares::GemfilePrepare })
69
- @default_prepare_checker ||= CodeAnalyzer::CheckingVisitor::Default.new(checkers: @prepares.select { |checker| !checker.is_a? Prepares::GemfilePrepare })
70
+ @default_prepare_checker ||= CodeAnalyzer::CheckingVisitor::Default.new(checkers: @prepares.reject { |checker| checker.is_a? Prepares::GemfilePrepare })
70
71
  @review_checker ||= CodeAnalyzer::CheckingVisitor::Default.new(checkers: @reviews)
71
72
  end
72
73
 
@@ -156,7 +157,7 @@ module RailsBestPractices
156
157
 
157
158
  # load all plugin reviews.
158
159
  def load_plugin_reviews
159
- begin
160
+
160
161
  plugins = File.join(Runner.base_path, 'lib', 'rails_best_practices', 'plugins', 'reviews')
161
162
  if File.directory?(plugins)
162
163
  Dir[File.expand_path(File.join(plugins, '*.rb'))].each do |review|
@@ -168,7 +169,7 @@ module RailsBestPractices
168
169
  end
169
170
  end
170
171
  end
171
- end
172
+
172
173
  end
173
174
  end
174
175
  end
@@ -1,10 +1,10 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'erubis'
3
4
 
4
5
  module Erubis
5
6
  class OnlyRuby < Eruby
6
- def add_preamble(src)
7
- end
7
+ def add_preamble(src); end
8
8
 
9
9
  def add_text(src, text)
10
10
  src << text.gsub(/[^\s;]/, '')
@@ -30,7 +30,6 @@ module Erubis
30
30
  src << ';'
31
31
  end
32
32
 
33
- def add_postamble(src)
34
- end
33
+ def add_postamble(src); end
35
34
  end
36
35
  end
@@ -1,2 +1,3 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require_rel 'lexicals'
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Lexicals
4
5
  # Keep lines fewer than 80 characters.
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Lexicals
4
5
  # Make sure there are no tabs in files.
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Lexicals
4
5
  # Make sure there are no trailing whitespace in codes.
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require_rel 'prepares'
3
4
 
4
5
  module RailsBestPractices
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Prepares
4
5
  # Remember all configs
@@ -12,7 +13,7 @@ module RailsBestPractices
12
13
 
13
14
  # check assignments to config
14
15
  add_callback :start_assign do |node|
15
- if node.left_value.grep_node(sexp_type: [:vcall, :var_ref], to_s: 'config').present?
16
+ if node.left_value.grep_node(sexp_type: %i[vcall var_ref], to_s: 'config').present?
16
17
  @configs[node.left_value.to_s] = node.right_value.to_s
17
18
  end
18
19
  end
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Prepares
4
5
  # Remember controllers and controller methods
@@ -10,7 +11,7 @@ module RailsBestPractices
10
11
  interesting_nodes :class, :var_ref, :vcall, :command, :def
11
12
  interesting_files CONTROLLER_FILES
12
13
 
13
- DEFAULT_ACTIONS = %w(index show new create edit update destroy)
14
+ DEFAULT_ACTIONS = %w[index show new create edit update destroy].freeze
14
15
 
15
16
  def initialize
16
17
  @controllers = Prepares.controllers
@@ -21,7 +22,7 @@ module RailsBestPractices
21
22
 
22
23
  # check class node to remember the class name.
23
24
  # also check if the controller is inherit from InheritedResources::Base.
24
- add_callback :start_class do |node|
25
+ add_callback :start_class do |_node|
25
26
  @controllers << @klass
26
27
  if @inherited_resources
27
28
  @actions = DEFAULT_ACTIONS
@@ -32,20 +33,20 @@ module RailsBestPractices
32
33
  add_callback :end_class do |node|
33
34
  if @inherited_resources && 'ApplicationController' != current_class_name
34
35
  @actions.each do |action|
35
- @methods.add_method(current_class_name, action, {'file' => node.file, 'line_number' => node.line_number})
36
+ @methods.add_method(current_class_name, action, 'file' => node.file, 'line_number' => node.line_number)
36
37
  end
37
38
  end
38
39
  end
39
40
 
40
41
  # check if there is a DSL call inherit_resources.
41
- add_callback :start_var_ref do |node|
42
+ add_callback :start_var_ref do |_node|
42
43
  if @inherited_resources
43
44
  @actions = DEFAULT_ACTIONS
44
45
  end
45
46
  end
46
47
 
47
48
  # check if there is a DSL call inherit_resources.
48
- add_callback :start_vcall do |node|
49
+ add_callback :start_vcall do |_node|
49
50
  if @inherited_resources
50
51
  @actions = DEFAULT_ACTIONS
51
52
  end
@@ -82,14 +83,14 @@ module RailsBestPractices
82
83
  # }
83
84
  add_callback :start_def do |node|
84
85
  method_name = node.method_name.to_s
85
- @methods.add_method(current_class_name, method_name, {'file' => node.file, 'line_number' => node.line_number}, current_access_control)
86
+ @methods.add_method(current_class_name, method_name, { 'file' => node.file, 'line_number' => node.line_number }, current_access_control)
86
87
  end
87
88
 
88
89
  # ask Reviews::RemoveUnusedMoethodsInHelperReview to check the controllers who include helpers.
89
90
  add_callback :after_check do
90
91
  descendants = @helpers.map(&:descendants).flatten
91
92
  if descendants.present?
92
- Reviews::RemoveUnusedMethodsInHelpersReview.interesting_files *descendants.map { |descendant| %r|#{descendant.underscore}| }
93
+ Reviews::RemoveUnusedMethodsInHelpersReview.interesting_files *descendants.map { |descendant| /#{descendant.underscore}/ }
93
94
  end
94
95
  end
95
96
  end
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Prepares
4
5
  # Remember all gems in Gemfile
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Prepares
4
5
  # Remember helper methods.
@@ -15,7 +16,7 @@ module RailsBestPractices
15
16
  end
16
17
 
17
18
  # check module node to remember the module name.
18
- add_callback :start_module do |node|
19
+ add_callback :start_module do |_node|
19
20
  @helpers << Core::Mod.new(current_module_name, [])
20
21
  end
21
22
 
@@ -31,7 +32,7 @@ module RailsBestPractices
31
32
  add_callback :start_def do |node|
32
33
  if node.file =~ HELPER_FILES
33
34
  method_name = node.method_name.to_s
34
- @methods.add_method(current_module_name, method_name, {'file' => node.file, 'line_number' => node.line_number}, current_access_control)
35
+ @methods.add_method(current_module_name, method_name, { 'file' => node.file, 'line_number' => node.line_number }, current_access_control)
35
36
  end
36
37
  end
37
38
  end
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Prepares
4
5
  # Check all initializers
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Prepares
4
5
  # Remember the mailer names.
@@ -16,7 +17,7 @@ module RailsBestPractices
16
17
  #
17
18
  # if it is a subclass of ActionMailer::Base,
18
19
  # then remember its class name.
19
- add_callback :start_class do |node|
20
+ add_callback :start_class do |_node|
20
21
  if 'ActionMailer::Base' == current_extend_class_name
21
22
  @mailers << @klass
22
23
  end
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Prepares
4
5
  # Remember models and model associations.
@@ -9,7 +10,7 @@ module RailsBestPractices
9
10
  interesting_nodes :class, :def, :defs, :command, :alias
10
11
  interesting_files MODEL_FILES
11
12
 
12
- ASSOCIATION_METHODS = %w(belongs_to has_one has_many has_and_belongs_to_many embeds_many embeds_one embedded_in many one)
13
+ ASSOCIATION_METHODS = %w[belongs_to has_one has_many has_and_belongs_to_many embeds_many embeds_one embedded_in many one].freeze
13
14
 
14
15
  def initialize
15
16
  @models = Prepares.models
@@ -19,7 +20,7 @@ module RailsBestPractices
19
20
  end
20
21
 
21
22
  # remember the class name.
22
- add_callback :start_class do |node|
23
+ add_callback :start_class do |_node|
23
24
  if 'ActionMailer::Base' != current_extend_class_name
24
25
  @models << @klass
25
26
  end
@@ -42,7 +43,7 @@ module RailsBestPractices
42
43
  'ActionMailer::Base' != current_extend_class_name &&
43
44
  (classable_modules.empty? || klasses.any?)
44
45
  method_name = node.method_name.to_s
45
- @methods.add_method(current_class_name, method_name, {'file' => node.file, 'line_number' => node.line_number}, current_access_control)
46
+ @methods.add_method(current_class_name, method_name, { 'file' => node.file, 'line_number' => node.line_number }, current_access_control)
46
47
  end
47
48
  end
48
49
 
@@ -61,7 +62,7 @@ module RailsBestPractices
61
62
  add_callback :start_defs do |node|
62
63
  if @klass && 'ActionMailer::Base' != current_extend_class_name
63
64
  method_name = node.method_name.to_s
64
- @methods.add_method(current_class_name, method_name, {'file' => node.file, 'line_number' => node.line_number}, current_access_control)
65
+ @methods.add_method(current_class_name, method_name, { 'file' => node.file, 'line_number' => node.line_number }, current_access_control)
65
66
  end
66
67
  end
67
68
 
@@ -78,13 +79,13 @@ module RailsBestPractices
78
79
  # }
79
80
  add_callback :start_command do |node|
80
81
  case node.message.to_s
81
- when *%w(named_scope scope alias_method)
82
+ when 'named_scope', 'scope', 'alias_method'
82
83
  method_name = node.arguments.all.first.to_s
83
- @methods.add_method(current_class_name, method_name, {'file' => node.file, 'line_number' => node.line_number}, current_access_control)
84
+ @methods.add_method(current_class_name, method_name, { 'file' => node.file, 'line_number' => node.line_number }, current_access_control)
84
85
  when 'alias_method_chain'
85
86
  method, feature = *node.arguments.all.map(&:to_s)
86
- @methods.add_method(current_class_name, "#{method}_with_#{feature}", {'file' => node.file, 'line_number' => node.line_number}, current_access_control)
87
- @methods.add_method(current_class_name, "#{method}", {'file' => node.file, 'line_number' => node.line_number}, current_access_control)
87
+ @methods.add_method(current_class_name, "#{method}_with_#{feature}", { 'file' => node.file, 'line_number' => node.line_number }, current_access_control)
88
+ @methods.add_method(current_class_name, method.to_s, { 'file' => node.file, 'line_number' => node.line_number }, current_access_control)
88
89
  when 'field'
89
90
  arguments = node.arguments.all
90
91
  attribute_name = arguments.first.to_s
@@ -95,20 +96,19 @@ module RailsBestPractices
95
96
  @model_attributes.add_attribute(current_class_name, attribute_name, attribute_type)
96
97
  when *ASSOCIATION_METHODS
97
98
  remember_association(node)
98
- else
99
99
  end
100
100
  end
101
101
 
102
102
  # check alias node to remembr the alias methods.
103
103
  add_callback :start_alias do |node|
104
104
  method_name = node.new_method.to_s
105
- @methods.add_method(current_class_name, method_name, {'file' => node.file, 'line_number' => node.line_number}, current_access_control)
105
+ @methods.add_method(current_class_name, method_name, { 'file' => node.file, 'line_number' => node.line_number }, current_access_control)
106
106
  end
107
107
 
108
108
  # after prepare process, fix incorrect associations' class_name.
109
109
  add_callback :after_check do
110
110
  @model_associations.each do |model, model_associations|
111
- model_associations.each do |association_name, association_meta|
111
+ model_associations.each do |_association_name, association_meta|
112
112
  unless @models.include?(association_meta['class_name'])
113
113
  if @models.include?("#{model}::#{association_meta['class_name']}")
114
114
  association_meta['class_name'] = "#{model}::#{association_meta['class_name']}"