rails_best_practices 1.20.0 → 1.20.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -12,7 +12,7 @@ module RailsBestPractices
12
12
  interesting_nodes :hash, :bare_assoc_hash
13
13
  interesting_files ALL_FILES
14
14
 
15
- VALID_SYMBOL_KEY = /\A[@$_A-Za-z]([_\w]*[!_=?\w])?\z/
15
+ VALID_SYMBOL_KEY = /\A[@$_A-Za-z]([_\w]*[!_=?\w])?\z/.freeze
16
16
 
17
17
  # check hash node to see if it is ruby 1.8 style.
18
18
  add_callback :start_hash, :start_bare_assoc_hash do |node|
@@ -23,12 +23,12 @@ module RailsBestPractices
23
23
 
24
24
  protected
25
25
 
26
- # check if hash node is empty.
26
+ # check if hash node is empty.
27
27
  def empty_hash?(node)
28
28
  s(:hash, nil) == node || s(:bare_assoc_hash, nil) == node
29
29
  end
30
30
 
31
- # check if hash key/value pairs are ruby 1.8 style.
31
+ # check if hash key/value pairs are ruby 1.8 style.
32
32
  def hash_is_18?(node)
33
33
  pair_nodes = node.sexp_type == :hash ? node[1][1] : node[1]
34
34
  return false if pair_nodes.blank?
@@ -36,8 +36,8 @@ module RailsBestPractices
36
36
  pair_nodes.any? { |pair_node| pair_node[1].sexp_type == :symbol_literal }
37
37
  end
38
38
 
39
- # check if the hash keys are valid to be converted to ruby 1.9
40
- # syntax.
39
+ # check if the hash keys are valid to be converted to ruby 1.9
40
+ # syntax.
41
41
  def valid_keys?(node)
42
42
  node.hash_keys.all? { |key| key =~ VALID_SYMBOL_KEY }
43
43
  end
@@ -55,9 +55,9 @@ module RailsBestPractices
55
55
 
56
56
  private
57
57
 
58
- # check assignment node,
59
- # if the right vavlue is a method_add_arg node with message "new",
60
- # then remember the left value as new variable.
58
+ # check assignment node,
59
+ # if the right vavlue is a method_add_arg node with message "new",
60
+ # then remember the left value as new variable.
61
61
  def remember_new_variable(node)
62
62
  right_value = node.right_value
63
63
  if right_value.sexp_type == :method_add_arg && right_value.message.to_s == 'new'
@@ -65,7 +65,7 @@ module RailsBestPractices
65
65
  end
66
66
  end
67
67
 
68
- # see if the receiver of the call node is included in the @new_varaibles.
68
+ # see if the receiver of the call node is included in the @new_varaibles.
69
69
  def new_record?(node)
70
70
  @new_variables.include? node.receiver.to_s
71
71
  end
@@ -37,15 +37,14 @@ module RailsBestPractices
37
37
 
38
38
  private
39
39
 
40
- # check if the call node is the finder of other model.
41
- #
42
- # the message of the node should be one of find, all, first or last,
43
- # and the receiver of the node should be with message :call (this is the other model),
44
- # and any of its arguments is a hash,
45
- # then it is the finder of other model.
40
+ # check if the call node is the finder of other model.
41
+ #
42
+ # the message of the node should be one of find, all, first or last,
43
+ # and the receiver of the node should be with message :call (this is the other model),
44
+ # and any of its arguments is a hash,
45
+ # then it is the finder of other model.
46
46
  def other_finder?(node)
47
- FINDERS.include?(node[1].message.to_s) &&
48
- node[1].receiver.sexp_type == :call &&
47
+ FINDERS.include?(node[1].message.to_s) && node[1].receiver.sexp_type == :call &&
49
48
  node.arguments.grep_nodes_count(sexp_type: :bare_assoc_hash) > 0
50
49
  end
51
50
  end
@@ -35,11 +35,11 @@ module RailsBestPractices
35
35
 
36
36
  private
37
37
 
38
- # check if the call node can use delegate to avoid violating law of demeter.
39
- #
40
- # if the receiver of receiver of the call node matchs any in model names,
41
- # and the message of receiver of the call node matchs any in association names,
42
- # then it needs delegate.
38
+ # check if the call node can use delegate to avoid violating law of demeter.
39
+ #
40
+ # if the receiver of receiver of the call node matchs any in model names,
41
+ # and the message of receiver of the call node matchs any in association names,
42
+ # then it needs delegate.
43
43
  def need_delegate?(node)
44
44
  return unless variable(node)
45
45
 
@@ -55,7 +55,7 @@ module RailsBestPractices
55
55
  if association_name =~ /able$/
56
56
  models.each do |class_name|
57
57
  if model_associations.is_association?(class_name, association_name.sub(/able$/, '')) ||
58
- model_associations.is_association?(class_name, association_name.sub(/able$/, 's'))
58
+ model_associations.is_association?(class_name, association_name.sub(/able$/, 's'))
59
59
  return true if model_attributes.is_attribute?(class_name, attribute_name)
60
60
  end
61
61
  end
@@ -37,7 +37,7 @@ module RailsBestPractices
37
37
 
38
38
  private
39
39
 
40
- # check if the node is a finder call node.
40
+ # check if the node is a finder call node.
41
41
  def finder?(node)
42
42
  node.receiver.const? && FINDERS.include?(node.message.to_s)
43
43
  end
@@ -38,14 +38,13 @@ module RailsBestPractices
38
38
 
39
39
  private
40
40
 
41
- # check if the arguments of options_for_select are complex.
42
- #
43
- # if the first argument is an array,
44
- # and the size of array is greater than @array_count you defined,
45
- # then it is complext.
41
+ # check if the arguments of options_for_select are complex.
42
+ #
43
+ # if the first argument is an array,
44
+ # and the size of array is greater than @array_count you defined,
45
+ # then it is complext.
46
46
  def complex_select_options?(node)
47
- node[1].message.to_s == 'options_for_select' &&
48
- node.arguments.all.first.sexp_type == :array &&
47
+ node[1].message.to_s == 'options_for_select' && node.arguments.all.first.sexp_type == :array &&
49
48
  node.arguments.all.first.array_size > @array_count
50
49
  end
51
50
  end
@@ -29,15 +29,14 @@ module RailsBestPractices
29
29
 
30
30
  private
31
31
 
32
- # check if the method_add_arg node is a finder.
33
- #
34
- # if the receiver of method_add_arg node is a constant,
35
- # and the message of call method_add_arg is one of find, all, first or last,
36
- # and any of its arguments is a hash,
37
- # then it is a finder.
32
+ # check if the method_add_arg node is a finder.
33
+ #
34
+ # if the receiver of method_add_arg node is a constant,
35
+ # and the message of call method_add_arg is one of find, all, first or last,
36
+ # and any of its arguments is a hash,
37
+ # then it is a finder.
38
38
  def finder?(node)
39
- FINDERS.include?(node[1].message.to_s) &&
40
- node[1].sexp_type == :call &&
39
+ FINDERS.include?(node[1].message.to_s) && node[1].sexp_type == :call &&
41
40
  node.arguments.grep_nodes_count(sexp_type: :bare_assoc_hash) > 0
42
41
  end
43
42
  end
@@ -51,17 +51,18 @@ module RailsBestPractices
51
51
 
52
52
  private
53
53
 
54
- # check nested route.
55
- #
56
- # if the receiver of the method_add_block is with message "resources" or "resource",
57
- # then increment the @counter, recursively check the block body, and decrement the @counter.
58
- #
59
- # if the node type is command_call or command,
60
- # and its message is resources or resource,
61
- # then check if @counter is greater than or equal to @nested_count,
62
- # if so, it is the needless deep nesting.
54
+ # check nested route.
55
+ #
56
+ # if the receiver of the method_add_block is with message "resources" or "resource",
57
+ # then increment the @counter, recursively check the block body, and decrement the @counter.
58
+ #
59
+ # if the node type is command_call or command,
60
+ # and its message is resources or resource,
61
+ # then check if @counter is greater than or equal to @nested_count,
62
+ # if so, it is the needless deep nesting.
63
63
  def recursively_check(node)
64
64
  shallow = @shallow_nodes.include? node
65
+
65
66
  if %i[command_call command].include?(node[1].sexp_type) && %w[resources resource].include?(node[1].message.to_s)
66
67
  hash_node = node[1].arguments.grep_node(sexp_type: :bare_assoc_hash)
67
68
  shallow ||= (hash_node && hash_node.hash_value('shallow').to_s == 'true')
@@ -72,7 +73,8 @@ module RailsBestPractices
72
73
  end
73
74
  @counter -= 1
74
75
  elsif %i[command_call command].include?(node.sexp_type) && %w[resources resource].include?(node.message.to_s)
75
- add_error "needless deep nesting (nested_count > #{@nested_count})", @file, node.line_number if @counter >= @nested_count && !@shallow_nodes.include?(node)
76
+ add_error "needless deep nesting (nested_count > #{@nested_count})", @file, node.line_number if @counter >=
77
+ @nested_count && !@shallow_nodes.include?(node)
76
78
  end
77
79
  end
78
80
  end
@@ -24,8 +24,7 @@ module RailsBestPractices
24
24
 
25
25
  # check all command nodes
26
26
  add_callback :start_command do |node|
27
- if node.message.to_s == 'match' &&
28
- node.arguments.all.first.to_s == ':controller(/:action(/:id(.:format)))'
27
+ if node.message.to_s == 'match' && node.arguments.all.first.to_s == ':controller(/:action(/:id(.:format)))'
29
28
  add_error 'not use default route'
30
29
  end
31
30
  end
@@ -41,11 +41,11 @@ module RailsBestPractices
41
41
 
42
42
  private
43
43
 
44
- # check method_add_block node to calculate the count of member and collection custom routes.
45
- #
46
- # if its receiver is with message "resources",
47
- # then calculate the count of call nodes, whose message is get, post, update or delete,
48
- # it is just the count of member and collection custom routes.
44
+ # check method_add_block node to calculate the count of member and collection custom routes.
45
+ #
46
+ # if its receiver is with message "resources",
47
+ # then calculate the count of call nodes, whose message is get, post, update or delete,
48
+ # it is just the count of member and collection custom routes.
49
49
  def member_and_collection_count_for_rails3(node)
50
50
  node[1].message.to_s == 'resources' ? node.grep_nodes_count(sexp_type: :command, message: VERBS) : 0
51
51
  end
@@ -11,6 +11,7 @@ module RailsBestPractices
11
11
  # Review process:
12
12
  # check nodes to see if there is a command with message attr_accessible or attr_protected,
13
13
  # or include ActiveModel::ForbiddenAttributesProtection.
14
+
14
15
  class ProtectMassAssignmentReview < Review
15
16
  interesting_files MODEL_FILES
16
17
  interesting_nodes :class, :command, :var_ref, :vcall, :fcall
@@ -73,13 +74,15 @@ module RailsBestPractices
73
74
  end
74
75
 
75
76
  def check_rails_builtin(node)
76
- if @whitelist_attributes || [node.to_s, node.message.to_s].any? { |str| %w[attr_accessible attr_protected].include? str }
77
+ if @whitelist_attributes ||
78
+ [node.to_s, node.message.to_s].any? { |str| %w[attr_accessible attr_protected].include? str }
77
79
  @mass_assignement = false
78
80
  end
79
81
  end
80
82
 
81
83
  def check_strong_parameters(command_node)
82
- if command_node.message.to_s == 'include' && command_node.arguments.all.first.to_s == 'ActiveModel::ForbiddenAttributesProtection'
84
+ if command_node.message.to_s == 'include' &&
85
+ command_node.arguments.all.first.to_s == 'ActiveModel::ForbiddenAttributesProtection'
83
86
  @mass_assignement = false
84
87
  end
85
88
  end
@@ -11,6 +11,7 @@ module RailsBestPractices
11
11
  # if they are not defined in routes,
12
12
  # and they are not called in controllers,
13
13
  # then they are the unused methods in controllers.
14
+
14
15
  class RemoveUnusedMethodsInControllersReview < Review
15
16
  include Classable
16
17
  include Moduleable
@@ -98,9 +99,11 @@ module RailsBestPractices
98
99
  end
99
100
  end
100
101
  @controller_methods.get_all_unused_methods.each do |method|
101
- unless excepted?(method)
102
- add_error "remove unused methods (#{method.class_name}##{method.method_name})", method.file, method.line_number
103
- end
102
+ next if excepted?(method)
103
+
104
+ add_error "remove unused methods (#{method.class_name}##{method.method_name})",
105
+ method.file,
106
+ method.line_number
104
107
  end
105
108
  end
106
109
 
@@ -20,15 +20,17 @@ module RailsBestPractices
20
20
  def initialize(options = {})
21
21
  super
22
22
  @helper_methods = Prepares.helper_methods
23
- self.class.interesting_files Prepares.helpers.map(&:descendants)
23
+ self.class.interesting_files *Prepares.helpers.map(&:descendants)
24
24
  end
25
25
 
26
26
  # get all unused methods at the end of review process
27
27
  add_callback :after_check do
28
28
  @helper_methods.get_all_unused_methods.each do |method|
29
- unless excepted?(method)
30
- add_error "remove unused methods (#{method.class_name}##{method.method_name})", method.file, method.line_number
31
- end
29
+ next if excepted?(method)
30
+
31
+ add_error "remove unused methods (#{method.class_name}##{method.method_name})",
32
+ method.file,
33
+ method.line_number
32
34
  end
33
35
  end
34
36
 
@@ -80,9 +80,11 @@ module RailsBestPractices
80
80
  # get all unused methods at the end of review process.
81
81
  add_callback :after_check do
82
82
  @model_methods.get_all_unused_methods.each do |method|
83
- if !excepted?(method) && method.method_name !~ /=$/
84
- add_error "remove unused methods (#{method.class_name}##{method.method_name})", method.file, method.line_number
85
- end
83
+ next unless !excepted?(method) && method.method_name !~ /=$/
84
+
85
+ add_error "remove unused methods (#{method.class_name}##{method.method_name})",
86
+ method.file,
87
+ method.line_number
86
88
  end
87
89
  end
88
90
 
@@ -95,13 +97,31 @@ module RailsBestPractices
95
97
  def internal_except_methods
96
98
  %w[
97
99
  initialize
98
- validate validate_each validate_on_create validate_on_update
99
- human_attribute_name assign_attributes attributes attribute
100
- to_xml to_json as_json to_param
101
- before_save before_create before_update before_destroy after_save after_create
102
- after_update after_destroy after_find after_initialize
100
+ validate
101
+ validate_each
102
+ validate_on_create
103
+ validate_on_update
104
+ human_attribute_name
105
+ assign_attributes
106
+ attributes
107
+ attribute
108
+ to_xml
109
+ to_json
110
+ as_json
111
+ to_param
112
+ before_save
113
+ before_create
114
+ before_update
115
+ before_destroy
116
+ after_save
117
+ after_create
118
+ after_update
119
+ after_destroy
120
+ after_find
121
+ after_initialize
103
122
  method_missing
104
- table_name module_prefix
123
+ table_name
124
+ module_prefix
105
125
  ].map { |method_name| "*\##{method_name}" }
106
126
  end
107
127
  end
@@ -49,9 +49,9 @@ module RailsBestPractices
49
49
 
50
50
  private
51
51
 
52
- # check the call node to see if it is with message "save" or "save!",
53
- # and the count attribute assignment on the receiver of the call node is greater than @assign_count defined,
54
- # then it is a complex creation, should be replaced with factory method.
52
+ # check the call node to see if it is with message "save" or "save!",
53
+ # and the count attribute assignment on the receiver of the call node is greater than @assign_count defined,
54
+ # then it is a complex creation, should be replaced with factory method.
55
55
  def check_variable_save(node)
56
56
  if ['save', 'save!'].include? node.message.to_s
57
57
  variable = node.receiver.to_s
@@ -95,32 +95,33 @@ module RailsBestPractices
95
95
 
96
96
  private
97
97
 
98
- # check resources call, if the routes generated by resources does not exist in the controller.
98
+ # check resources call, if the routes generated by resources does not exist in the controller.
99
99
  def check_resources(node)
100
100
  _check(node, resources_methods)
101
101
  end
102
102
 
103
- # check resource call, if the routes generated by resources does not exist in the controller.
103
+ # check resource call, if the routes generated by resources does not exist in the controller.
104
104
  def check_resource(node)
105
105
  _check(node, resource_methods)
106
106
  end
107
107
 
108
- # get the controller name.
108
+ # get the controller name.
109
109
  def controller_name(node)
110
110
  if option_with_hash(node)
111
111
  option_node = node.arguments.all[1]
112
- if hash_key_exist?(option_node, 'controller')
113
- name = option_node.hash_value('controller').to_s
114
- else
115
- name = node.arguments.all.first.to_s.gsub('::', '').tableize
116
- end
112
+ name =
113
+ if hash_key_exist?(option_node, 'controller')
114
+ option_node.hash_value('controller').to_s
115
+ else
116
+ node.arguments.all.first.to_s.gsub('::', '').tableize
117
+ end
117
118
  else
118
119
  name = node.arguments.all.first.to_s.gsub('::', '').tableize
119
120
  end
120
121
  namespaced_class_name(name)
121
122
  end
122
123
 
123
- # get the class name with namespace.
124
+ # get the class name with namespace.
124
125
  def namespaced_class_name(name)
125
126
  class_name = "#{name.split('/').map(&:camelize).join('::')}Controller"
126
127
  if @namespaces.empty?
@@ -138,11 +139,12 @@ module RailsBestPractices
138
139
  unless _methods.all? { |meth| Prepares.controller_methods.has_method?(controller_name, meth) }
139
140
  prepared_method_names = Prepares.controller_methods.get_methods(controller_name).map(&:method_name)
140
141
  only_methods = (_methods & prepared_method_names).map { |meth| ":#{meth}" }
141
- routes_message = if only_methods.size > 3
142
- "except: [#{(methods.map { |meth| ':' + meth } - only_methods).join(', ')}]"
143
- else
144
- "only: [#{only_methods.join(', ')}]"
145
- end
142
+ routes_message =
143
+ if only_methods.size > 3
144
+ "except: [#{(methods.map { |meth| ':' + meth } - only_methods).join(', ')}]"
145
+ else
146
+ "only: [#{only_methods.join(', ')}]"
147
+ end
146
148
  add_error "restrict auto-generated routes #{friendly_route_name(node)} (#{routes_message})"
147
149
  end
148
150
  end
@@ -178,7 +180,7 @@ module RailsBestPractices
178
180
  end
179
181
 
180
182
  def hash_key_exist?(node, key)
181
- node.hash_keys && node.hash_keys.include?(key)
183
+ node.hash_keys&.include?(key)
182
184
  end
183
185
 
184
186
  def friendly_route_name(node)
@@ -23,8 +23,7 @@ module RailsBestPractices
23
23
  add_callback :start_command do |node|
24
24
  if node.message.to_s == 'render'
25
25
  keys = node.arguments.all.first.hash_keys
26
- if keys && keys.size == 1 &&
27
- (keys.include?('action') || keys.include?('template') || keys.include?('file'))
26
+ if keys && keys.size == 1 && (keys.include?('action') || keys.include?('template') || keys.include?('file'))
28
27
  add_error 'simplify render in controllers'
29
28
  end
30
29
  end