rails_best_practices 1.19.0 → 1.19.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 (133) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/Guardfile +2 -2
  4. data/Rakefile +8 -8
  5. data/bin/rails_best_practices +1 -1
  6. data/lib/rails_best_practices/analyzer.rb +45 -45
  7. data/lib/rails_best_practices/command.rb +40 -40
  8. data/lib/rails_best_practices/core/check.rb +15 -15
  9. data/lib/rails_best_practices/core/klasses.rb +1 -1
  10. data/lib/rails_best_practices/core/methods.rb +7 -7
  11. data/lib/rails_best_practices/core/model_associations.rb +3 -3
  12. data/lib/rails_best_practices/core/modules.rb +1 -1
  13. data/lib/rails_best_practices/core/routes.rb +2 -2
  14. data/lib/rails_best_practices/core/runner.rb +3 -3
  15. data/lib/rails_best_practices/core_ext/erubis.rb +4 -4
  16. data/lib/rails_best_practices/lexicals/remove_tab_check.rb +2 -2
  17. data/lib/rails_best_practices/lexicals/remove_trailing_whitespace_check.rb +2 -2
  18. data/lib/rails_best_practices/prepares/config_prepare.rb +1 -1
  19. data/lib/rails_best_practices/prepares/controller_prepare.rb +8 -8
  20. data/lib/rails_best_practices/prepares/helper_prepare.rb +1 -1
  21. data/lib/rails_best_practices/prepares/initializer_prepare.rb +4 -4
  22. data/lib/rails_best_practices/prepares/mailer_prepare.rb +1 -1
  23. data/lib/rails_best_practices/prepares/model_prepare.rb +19 -19
  24. data/lib/rails_best_practices/prepares/route_prepare.rb +34 -34
  25. data/lib/rails_best_practices/prepares/schema_prepare.rb +1 -1
  26. data/lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb +2 -2
  27. data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +7 -7
  28. data/lib/rails_best_practices/reviews/check_save_return_value_review.rb +1 -1
  29. data/lib/rails_best_practices/reviews/default_scope_is_evil_review.rb +3 -3
  30. data/lib/rails_best_practices/reviews/dry_bundler_in_capistrano_review.rb +3 -3
  31. data/lib/rails_best_practices/reviews/hash_syntax_review.rb +1 -1
  32. data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +6 -6
  33. data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +2 -2
  34. data/lib/rails_best_practices/reviews/law_of_demeter_review.rb +4 -4
  35. data/lib/rails_best_practices/reviews/move_code_into_controller_review.rb +3 -3
  36. data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +2 -2
  37. data/lib/rails_best_practices/reviews/move_code_into_model_review.rb +1 -1
  38. data/lib/rails_best_practices/reviews/move_finder_to_named_scope_review.rb +2 -2
  39. data/lib/rails_best_practices/reviews/move_model_logic_into_model_review.rb +1 -1
  40. data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +4 -4
  41. data/lib/rails_best_practices/reviews/not_rescue_exception_review.rb +2 -2
  42. data/lib/rails_best_practices/reviews/not_use_default_route_review.rb +4 -4
  43. data/lib/rails_best_practices/reviews/not_use_time_ago_in_words_review.rb +3 -3
  44. data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +2 -2
  45. data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +9 -9
  46. data/lib/rails_best_practices/reviews/remove_empty_helpers_review.rb +3 -3
  47. data/lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb +11 -11
  48. data/lib/rails_best_practices/reviews/remove_unused_methods_in_helpers_review.rb +1 -1
  49. data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +8 -7
  50. data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +2 -2
  51. data/lib/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review.rb +2 -2
  52. data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +22 -22
  53. data/lib/rails_best_practices/reviews/review.rb +2 -2
  54. data/lib/rails_best_practices/reviews/simplify_render_in_controllers_review.rb +3 -3
  55. data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +4 -4
  56. data/lib/rails_best_practices/reviews/use_before_filter_review.rb +2 -2
  57. data/lib/rails_best_practices/reviews/use_model_association_review.rb +2 -2
  58. data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +2 -2
  59. data/lib/rails_best_practices/reviews/use_observer_review.rb +3 -3
  60. data/lib/rails_best_practices/reviews/use_parentheses_in_method_def_review.rb +1 -1
  61. data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +3 -3
  62. data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +2 -2
  63. data/lib/rails_best_practices/reviews/use_scope_access_review.rb +5 -5
  64. data/lib/rails_best_practices/reviews/use_turbo_sprockets_rails3_review.rb +4 -4
  65. data/lib/rails_best_practices/version.rb +1 -1
  66. data/rails_best_practices.gemspec +24 -24
  67. data/spec/rails_best_practices/analyzer_spec.rb +43 -43
  68. data/spec/rails_best_practices/core/check_spec.rb +2 -2
  69. data/spec/rails_best_practices/core/checks_loader_spec.rb +4 -4
  70. data/spec/rails_best_practices/core/error_spec.rb +16 -16
  71. data/spec/rails_best_practices/core/except_methods_spec.rb +15 -15
  72. data/spec/rails_best_practices/core/gems_spec.rb +9 -9
  73. data/spec/rails_best_practices/core/klasses_spec.rb +17 -17
  74. data/spec/rails_best_practices/core/methods_spec.rb +28 -28
  75. data/spec/rails_best_practices/core/model_associations_spec.rb +10 -10
  76. data/spec/rails_best_practices/core/model_attributes_spec.rb +10 -10
  77. data/spec/rails_best_practices/core/modules_spec.rb +10 -10
  78. data/spec/rails_best_practices/core/routes_spec.rb +10 -10
  79. data/spec/rails_best_practices/core/runner_spec.rb +4 -4
  80. data/spec/rails_best_practices/core_ext/erubis_spec.rb +7 -7
  81. data/spec/rails_best_practices/lexicals/long_line_check_spec.rb +7 -7
  82. data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +6 -6
  83. data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +6 -6
  84. data/spec/rails_best_practices/prepares/config_prepare_spec.rb +3 -3
  85. data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +26 -26
  86. data/spec/rails_best_practices/prepares/gemfile_prepare_spec.rb +3 -3
  87. data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +6 -6
  88. data/spec/rails_best_practices/prepares/initializer_prepare_spec.rb +5 -5
  89. data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +2 -2
  90. data/spec/rails_best_practices/prepares/model_prepare_spec.rb +108 -108
  91. data/spec/rails_best_practices/prepares/route_prepare_spec.rb +77 -77
  92. data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +8 -8
  93. data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +10 -10
  94. data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +27 -27
  95. data/spec/rails_best_practices/reviews/check_destroy_return_value_review_spec.rb +11 -11
  96. data/spec/rails_best_practices/reviews/check_save_return_value_review_spec.rb +17 -17
  97. data/spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb +6 -6
  98. data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +4 -4
  99. data/spec/rails_best_practices/reviews/hash_syntax_review_spec.rb +8 -8
  100. data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +10 -10
  101. data/spec/rails_best_practices/reviews/keep_finders_on_their_own_model_review_spec.rb +9 -9
  102. data/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb +16 -16
  103. data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +6 -6
  104. data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +4 -4
  105. data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +13 -13
  106. data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +7 -7
  107. data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +5 -5
  108. data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +10 -10
  109. data/spec/rails_best_practices/reviews/not_rescue_exception_review_spec.rb +8 -8
  110. data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +4 -4
  111. data/spec/rails_best_practices/reviews/not_use_time_ago_in_words_review_spec.rb +11 -11
  112. data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +7 -7
  113. data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +16 -16
  114. data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +5 -5
  115. data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +33 -33
  116. data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +7 -7
  117. data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +106 -106
  118. data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +5 -5
  119. data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +8 -8
  120. data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +29 -29
  121. data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +15 -15
  122. data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +15 -15
  123. data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +7 -7
  124. data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +7 -7
  125. data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +29 -29
  126. data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +6 -6
  127. data/spec/rails_best_practices/reviews/use_parentheses_in_method_def_review_spec.rb +5 -5
  128. data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +28 -28
  129. data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +9 -9
  130. data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +23 -23
  131. data/spec/rails_best_practices/reviews/use_turbo_sprockets_rails3_review_spec.rb +6 -6
  132. data/spec/spec_helper.rb +1 -1
  133. metadata +7 -7
@@ -12,12 +12,12 @@ module RailsBestPractices
12
12
  class RemoveEmptyHelpersReview < Review
13
13
  interesting_nodes :module
14
14
  interesting_files HELPER_FILES
15
- url "https://rails-bestpractices.com/posts/2011/04/09/remove-empty-helpers/"
15
+ url 'https://rails-bestpractices.com/posts/2011/04/09/remove-empty-helpers/'
16
16
 
17
17
  # check the body of module node, if it is nil, then it should be removed.
18
18
  add_callback :start_module do |module_node|
19
- if "ApplicationHelper" != module_node.module_name.to_s && empty_body?(module_node)
20
- add_error "remove empty helpers"
19
+ if 'ApplicationHelper' != module_node.module_name.to_s && empty_body?(module_node)
20
+ add_error 'remove empty helpers'
21
21
  end
22
22
  end
23
23
 
@@ -46,28 +46,28 @@ module RailsBestPractices
46
46
  # mark corresponding action as used for cells' render and render_call.
47
47
  add_callback :start_command, :start_method_add_arg do |node|
48
48
  case node.message.to_s
49
- when "render_cell"
49
+ when 'render_cell'
50
50
  controller_name, action_name, _ = *node.arguments.all.map(&:to_s)
51
51
  call_method(action_name, "#{controller_name}_cell".classify)
52
- when "render"
52
+ when 'render'
53
53
  first_argument = node.arguments.all.first
54
- if first_argument.present? && first_argument.hash_value("state").present?
55
- action_name = first_argument.hash_value("state").to_s
54
+ if first_argument.present? && first_argument.hash_value('state').present?
55
+ action_name = first_argument.hash_value('state').to_s
56
56
  call_method(action_name, current_class_name)
57
57
  end
58
- when "around_filter"
58
+ when 'around_filter'
59
59
  node.arguments.all.each { |argument| mark_used(argument) }
60
- when "layout"
60
+ when 'layout'
61
61
  first_argument = node.arguments.all.first
62
62
  if first_argument.sexp_type == :symbol_literal
63
63
  mark_used(first_argument)
64
64
  end
65
- when "helper_method"
65
+ when 'helper_method'
66
66
  node.arguments.all.each { |argument| mark_publicize(argument.to_s) }
67
- when "delegate"
67
+ when 'delegate'
68
68
  last_argument = node.arguments.all.last
69
- if :bare_assoc_hash == last_argument.sexp_type && "controller" == last_argument.hash_value("to").to_s
70
- controller_name = current_module_name.sub("Helper", "Controller")
69
+ if :bare_assoc_hash == last_argument.sexp_type && 'controller' == last_argument.hash_value('to').to_s
70
+ controller_name = current_module_name.sub('Helper', 'Controller')
71
71
  node.arguments.all[0..-2].each { |method| mark_publicize(method.to_s, controller_name) }
72
72
  end
73
73
  else
@@ -85,7 +85,7 @@ module RailsBestPractices
85
85
  # get all unused methods at the end of review process.
86
86
  add_callback :after_check do
87
87
  @routes.each do |route|
88
- if "*" == route.action_name
88
+ if '*' == route.action_name
89
89
  action_names = @controller_methods.get_methods(route.controller_name_with_namespaces).map(&:method_name)
90
90
  action_names.each { |action_name| call_method(action_name, route.controller_name_with_namespaces) }
91
91
  else
@@ -38,7 +38,7 @@ module RailsBestPractices
38
38
  end
39
39
 
40
40
  def internal_except_methods
41
- ["*#url_for"]
41
+ ['*#url_for']
42
42
  end
43
43
  end
44
44
  end
@@ -32,12 +32,12 @@ module RailsBestPractices
32
32
  add_callback :start_command do |node|
33
33
  arguments = node.arguments.all
34
34
  case node.message.to_s
35
- when "validate", "validate_on_create", "validate_on_update"
35
+ when 'validate', 'validate_on_create', 'validate_on_update'
36
36
  arguments.each { |argument| mark_used(argument) }
37
- when "collection_select"
37
+ when 'collection_select'
38
38
  mark_used(arguments[3])
39
39
  mark_used(arguments[4])
40
- when "grouped_collection_select"
40
+ when 'grouped_collection_select'
41
41
  mark_used(arguments[3])
42
42
  mark_used(arguments[4])
43
43
  mark_used(arguments[5])
@@ -49,10 +49,10 @@ module RailsBestPractices
49
49
  add_callback :start_command_call do |node|
50
50
  arguments = node.arguments.all
51
51
  case node.message.to_s
52
- when "collection_select"
52
+ when 'collection_select'
53
53
  mark_used(arguments[2])
54
54
  mark_used(arguments[3])
55
- when "grouped_collection_select"
55
+ when 'grouped_collection_select'
56
56
  mark_used(arguments[2])
57
57
  mark_used(arguments[3])
58
58
  mark_used(arguments[4])
@@ -65,10 +65,10 @@ module RailsBestPractices
65
65
  add_callback :start_method_add_arg do |node|
66
66
  arguments = node.arguments.all
67
67
  case node.message.to_s
68
- when "options_from_collection_for_select"
68
+ when 'options_from_collection_for_select'
69
69
  mark_used(arguments[1])
70
70
  mark_used(arguments[2])
71
- when "option_groups_from_collection_for_select"
71
+ when 'option_groups_from_collection_for_select'
72
72
  mark_used(arguments[1])
73
73
  mark_used(arguments[2])
74
74
  mark_used(arguments[3])
@@ -100,6 +100,7 @@ module RailsBestPractices
100
100
  before_save before_create before_update before_destroy after_save after_create
101
101
  after_update after_destroy after_find after_initialize
102
102
  method_missing
103
+ table_name module_prefix
103
104
  ).map { |method_name| "*\##{method_name}" }
104
105
  end
105
106
  end
@@ -17,7 +17,7 @@ module RailsBestPractices
17
17
  class ReplaceComplexCreationWithFactoryMethodReview < Review
18
18
  interesting_nodes :def
19
19
  interesting_files CONTROLLER_FILES
20
- url "https://rails-bestpractices.com/posts/2010/07/21/replace-complex-creation-with-factory-method/"
20
+ url 'https://rails-bestpractices.com/posts/2010/07/21/replace-complex-creation-with-factory-method/'
21
21
 
22
22
  def initialize(options = {})
23
23
  super(options)
@@ -53,7 +53,7 @@ module RailsBestPractices
53
53
  # and the count attribute assignment on the receiver of the call node is greater than @assign_count defined,
54
54
  # then it is a complex creation, should be replaced with factory method.
55
55
  def check_variable_save(node)
56
- if ["save", "save!"].include? node.message.to_s
56
+ if ['save', 'save!'].include? node.message.to_s
57
57
  variable = node.receiver.to_s
58
58
  if variable_use_count[variable].to_i > @assigns_count
59
59
  hint = "#{variable} attribute_assignment_count > #{@assigns_count}"
@@ -13,13 +13,13 @@ module RailsBestPractices
13
13
  class ReplaceInstanceVariableWithLocalVariableReview < Review
14
14
  interesting_nodes :var_ref, :vcall
15
15
  interesting_files PARTIAL_VIEW_FILES
16
- url "https://rails-bestpractices.com/posts/2010/07/24/replace-instance-variable-with-local-variable/"
16
+ url 'https://rails-bestpractices.com/posts/2010/07/24/replace-instance-variable-with-local-variable/'
17
17
 
18
18
  # check ivar node in partial view file,
19
19
  # it is an instance variable, and should be replaced with local variable.
20
20
  add_callback :start_var_ref, :start_vcall do |node|
21
21
  if node.to_s.start_with?('@')
22
- add_error "replace instance variable with local variable"
22
+ add_error 'replace instance variable with local variable'
23
23
  end
24
24
  end
25
25
  end
@@ -15,7 +15,7 @@ module RailsBestPractices
15
15
  class RestrictAutoGeneratedRoutesReview < Review
16
16
  interesting_nodes :command, :command_call, :method_add_block
17
17
  interesting_files ROUTE_FILES
18
- url "https://rails-bestpractices.com/posts/2011/08/19/restrict-auto-generated-routes/"
18
+ url 'https://rails-bestpractices.com/posts/2011/08/19/restrict-auto-generated-routes/'
19
19
 
20
20
  def resource_methods
21
21
  if Prepares.configs['config.api_only']
@@ -37,23 +37,23 @@ module RailsBestPractices
37
37
 
38
38
  # check if the generated routes have the corresponding actions in controller for rails routes.
39
39
  add_callback :start_command, :start_command_call do |node|
40
- if "resources" == node.message.to_s
40
+ if 'resources' == node.message.to_s
41
41
  if (mod = module_option(node))
42
42
  @namespaces << mod
43
43
  end
44
44
  check_resources(node)
45
45
  @resource_controllers << node.arguments.all.first.to_s
46
- elsif "resource" == node.message.to_s
46
+ elsif 'resource' == node.message.to_s
47
47
  check_resource(node)
48
48
  @resource_controllers << node.arguments.all.first.to_s
49
49
  end
50
50
  end
51
51
 
52
52
  add_callback :end_command do |node|
53
- if "resources" == node.message.to_s
53
+ if 'resources' == node.message.to_s
54
54
  @resource_controllers.pop
55
55
  @namespaces.pop if module_option(node)
56
- elsif "resource" == node.message.to_s
56
+ elsif 'resource' == node.message.to_s
57
57
  @resource_controllers.pop
58
58
  end
59
59
  end
@@ -61,9 +61,9 @@ module RailsBestPractices
61
61
  # remember the namespace.
62
62
  add_callback :start_method_add_block do |node|
63
63
  case node.message.to_s
64
- when "namespace"
64
+ when 'namespace'
65
65
  @namespaces << node.arguments.all.first.to_s if check_method_add_block?(node)
66
- when "resources", "resource"
66
+ when 'resources', 'resource'
67
67
  @resource_controllers << node.arguments.all.first.to_s if check_method_add_block?(node)
68
68
  when 'scope'
69
69
  if check_method_add_block?(node) && (mod = module_option(node))
@@ -77,9 +77,9 @@ module RailsBestPractices
77
77
  add_callback :end_method_add_block do |node|
78
78
  if check_method_add_block?(node)
79
79
  case node.message.to_s
80
- when "namespace"
80
+ when 'namespace'
81
81
  @namespaces.pop
82
- when "resources", "resource"
82
+ when 'resources', 'resource'
83
83
  @resource_controllers.pop
84
84
  when 'scope'
85
85
  if check_method_add_block?(node) && module_option(node)
@@ -90,7 +90,7 @@ module RailsBestPractices
90
90
  end
91
91
 
92
92
  def check_method_add_block?(node)
93
- :command == node[1].sexp_type || (:command_call == node[1].sexp_type && "map" != node.receiver.to_s)
93
+ :command == node[1].sexp_type || (:command_call == node[1].sexp_type && 'map' != node.receiver.to_s)
94
94
  end
95
95
 
96
96
  private
@@ -109,13 +109,13 @@ module RailsBestPractices
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
112
+ if hash_key_exist?(option_node,'controller')
113
+ name = option_node.hash_value('controller').to_s
114
114
  else
115
- name = node.arguments.all.first.to_s.gsub("::", "").tableize
115
+ name = node.arguments.all.first.to_s.gsub('::', '').tableize
116
116
  end
117
117
  else
118
- name = node.arguments.all.first.to_s.gsub("::", "").tableize
118
+ name = node.arguments.all.first.to_s.gsub('::', '').tableize
119
119
  end
120
120
  namespaced_class_name(name)
121
121
  end
@@ -126,7 +126,7 @@ module RailsBestPractices
126
126
  if @namespaces.empty?
127
127
  class_name
128
128
  else
129
- @namespaces.map { |namespace| "#{namespace.camelize}::" }.join("") + class_name
129
+ @namespaces.map { |namespace| "#{namespace.camelize}::" }.join('') + class_name
130
130
  end
131
131
  end
132
132
 
@@ -149,13 +149,13 @@ module RailsBestPractices
149
149
  def _methods(node, methods)
150
150
  if option_with_hash(node)
151
151
  option_node = node.arguments.all[1]
152
- if hash_key_exist?(option_node, "only")
153
- option_node.hash_value("only").to_s == "none" ? [] : Array(option_node.hash_value("only").to_object)
154
- elsif hash_key_exist?(option_node, "except")
155
- if option_node.hash_value("except").to_s == "all"
152
+ if hash_key_exist?(option_node, 'only')
153
+ option_node.hash_value('only').to_s == 'none' ? [] : Array(option_node.hash_value('only').to_object)
154
+ elsif hash_key_exist?(option_node, 'except')
155
+ if option_node.hash_value('except').to_s == 'all'
156
156
  []
157
157
  else
158
- (methods - Array(option_node.hash_value("except").to_object))
158
+ (methods - Array(option_node.hash_value('except').to_object))
159
159
  end
160
160
  else
161
161
  methods
@@ -182,9 +182,9 @@ module RailsBestPractices
182
182
 
183
183
  def friendly_route_name(node)
184
184
  if @resource_controllers.last == node.arguments.to_s
185
- [@namespaces.join("/"), @resource_controllers.join("/")].delete_if(&:blank?).join("/")
185
+ [@namespaces.join('/'), @resource_controllers.join('/')].delete_if(&:blank?).join('/')
186
186
  else
187
- [@namespaces.join("/"), @resource_controllers.join("/"), node.arguments.to_s].delete_if(&:blank?).join("/")
187
+ [@namespaces.join('/'), @resource_controllers.join('/'), node.arguments.to_s].delete_if(&:blank?).join('/')
188
188
  end
189
189
  end
190
190
  end
@@ -4,7 +4,7 @@ module RailsBestPractices
4
4
  # A Review class that takes charge of reviewing one rails best practice.
5
5
  class Review < Core::Check
6
6
  # default url.
7
- url "#"
7
+ url '#'
8
8
 
9
9
  # remember use count for the variable in the call or assign node.
10
10
  #
@@ -12,7 +12,7 @@ module RailsBestPractices
12
12
  # then save it to as key in @variable_use_count hash, and add the call count (hash value).
13
13
  def remember_variable_use_count(node)
14
14
  variable_node = variable(node)
15
- if variable_node && "self" != variable_node.to_s && @last_variable_node != variable_node
15
+ if variable_node && 'self' != variable_node.to_s && @last_variable_node != variable_node
16
16
  @last_variable_node = variable_node
17
17
  variable_use_count[variable_node.to_s] ||= 0
18
18
  variable_use_count[variable_node.to_s] += 1
@@ -14,16 +14,16 @@ module RailsBestPractices
14
14
  class SimplifyRenderInControllersReview < Review
15
15
  interesting_nodes :command
16
16
  interesting_files CONTROLLER_FILES
17
- url "https://rails-bestpractices.com/posts/2010/12/12/simplify-render-in-controllers/"
17
+ url 'https://rails-bestpractices.com/posts/2010/12/12/simplify-render-in-controllers/'
18
18
 
19
19
  # check command node in the controller file,
20
20
  # if its message is render and the arguments contain a key action, template or file,
21
21
  # then it should be replaced by simplified syntax.
22
22
  add_callback :start_command do |node|
23
- if "render" == node.message.to_s
23
+ if 'render' == node.message.to_s
24
24
  keys = node.arguments.all.first.hash_keys
25
25
  if keys && keys.size == 1 &&
26
- (keys.include?("action") || keys.include?("template") || keys.include?("file"))
26
+ (keys.include?('action') || keys.include?('template') || keys.include?('file'))
27
27
  add_error 'simplify render in controllers'
28
28
  end
29
29
  end
@@ -13,7 +13,7 @@ module RailsBestPractices
13
13
  class SimplifyRenderInViewsReview < Review
14
14
  interesting_nodes :command
15
15
  interesting_files VIEW_FILES
16
- url "https://rails-bestpractices.com/posts/2010/12/04/simplify-render-in-views/"
16
+ url 'https://rails-bestpractices.com/posts/2010/12/04/simplify-render-in-views/'
17
17
 
18
18
  VALID_KEYS = %w(object collection locals)
19
19
 
@@ -21,7 +21,7 @@ module RailsBestPractices
21
21
  # if its message is render and the arguments contain a key partial,
22
22
  # then it should be replaced by simplified syntax.
23
23
  add_callback :start_command do |node|
24
- if "render" == node.message.to_s
24
+ if 'render' == node.message.to_s
25
25
  hash_node = node.arguments.all.first
26
26
  if hash_node && :bare_assoc_hash == hash_node.sexp_type &&
27
27
  include_partial?(hash_node) && valid_hash?(hash_node)
@@ -33,12 +33,12 @@ module RailsBestPractices
33
33
  protected
34
34
 
35
35
  def include_partial?(hash_node)
36
- hash_node.hash_keys.include?("partial") && !hash_node.hash_value("partial").to_s.include?('/')
36
+ hash_node.hash_keys.include?('partial') && !hash_node.hash_value('partial').to_s.include?('/')
37
37
  end
38
38
 
39
39
  def valid_hash?(hash_node)
40
40
  keys = hash_node.hash_keys
41
- keys.delete("partial")
41
+ keys.delete('partial')
42
42
  (keys - VALID_KEYS).empty?
43
43
  end
44
44
  end
@@ -14,7 +14,7 @@ module RailsBestPractices
14
14
  class UseBeforeFilterReview < Review
15
15
  interesting_nodes :class
16
16
  interesting_files CONTROLLER_FILES
17
- url "https://rails-bestpractices.com/posts/2010/07/24/use-before_filter/"
17
+ url 'https://rails-bestpractices.com/posts/2010/07/24/use-before_filter/'
18
18
 
19
19
  def initialize(options = {})
20
20
  super()
@@ -31,7 +31,7 @@ module RailsBestPractices
31
31
 
32
32
  node.body.statements.each do |statement_node|
33
33
  var_ref_or_vcall_included = [:var_ref, :vcall].include?(statement_node.sexp_type)
34
- private_or_protected_included = ["protected", "private"].include?(statement_node.to_s)
34
+ private_or_protected_included = ['protected', 'private'].include?(statement_node.to_s)
35
35
  break if var_ref_or_vcall_included && private_or_protected_included
36
36
  remember_first_sentence(statement_node) if :def == statement_node.sexp_type
37
37
  end
@@ -16,7 +16,7 @@ module RailsBestPractices
16
16
  class UseModelAssociationReview < Review
17
17
  interesting_nodes :def
18
18
  interesting_files CONTROLLER_FILES
19
- url "https://rails-bestpractices.com/posts/2010/07/19/use-model-association/"
19
+ url 'https://rails-bestpractices.com/posts/2010/07/19/use-model-association/'
20
20
 
21
21
  # check method define nodes to see if there are some attribute assignments that can use model association instead.
22
22
  #
@@ -56,7 +56,7 @@ module RailsBestPractices
56
56
  # if the receiver of call node exists in @assignments,
57
57
  # then the attribute assignment should be replaced by using model association.
58
58
  def call_assignment(node)
59
- if ["save", "save!"].include? node.message.to_s
59
+ if ['save', 'save!'].include? node.message.to_s
60
60
  receiver = node.receiver.to_s
61
61
  add_error "use model association (for #{receiver})" if @assignments[receiver]
62
62
  end
@@ -13,7 +13,7 @@ module RailsBestPractices
13
13
  class UseMultipartAlternativeAsContentTypeOfEmailReview < Review
14
14
  interesting_nodes :class, :def
15
15
  interesting_files MAILER_FILES
16
- url "https://rails-bestpractices.com/posts/2010/08/05/use-multipart-alternative-as-content_type-of-email/"
16
+ url 'https://rails-bestpractices.com/posts/2010/08/05/use-multipart-alternative-as-content_type-of-email/'
17
17
 
18
18
  # check class node to remember the ActionMailer class name.
19
19
  add_callback :start_class do |node|
@@ -24,7 +24,7 @@ module RailsBestPractices
24
24
  add_callback :start_def do |node|
25
25
  name = node.method_name.to_s
26
26
  if !rails3_canonical_mailer_views?(name)
27
- add_error("use multipart/alternative as content_type of email")
27
+ add_error('use multipart/alternative as content_type of email')
28
28
  end
29
29
  end
30
30
 
@@ -20,7 +20,7 @@ module RailsBestPractices
20
20
  class UseObserverReview < Review
21
21
  interesting_nodes :def, :command
22
22
  interesting_files MODEL_FILES
23
- url "https://rails-bestpractices.com/posts/2010/07/24/use-observer/"
23
+ url 'https://rails-bestpractices.com/posts/2010/07/24/use-observer/'
24
24
 
25
25
  def initialize(options = {})
26
26
  super
@@ -42,7 +42,7 @@ module RailsBestPractices
42
42
  # then it should be replaced by using observer.
43
43
  add_callback :start_def do |node|
44
44
  if callback_method?(node) && deliver_mailer?(node)
45
- add_error "use observer"
45
+ add_error 'use observer'
46
46
  end
47
47
  end
48
48
 
@@ -71,7 +71,7 @@ module RailsBestPractices
71
71
  # then the call node is actionmailer deliver call.
72
72
  def deliver_mailer?(node)
73
73
  node.grep_nodes(sexp_type: :call) do |child_node|
74
- if "deliver" == child_node.message.to_s
74
+ if 'deliver' == child_node.message.to_s
75
75
  if :method_add_arg == child_node.receiver.sexp_type &&
76
76
  mailers.include?(child_node.receiver[1].receiver.to_s)
77
77
  return true
@@ -14,7 +14,7 @@ module RailsBestPractices
14
14
  # check def node to see if parameters are wrapped by parentheses.
15
15
  add_callback :start_def do |node|
16
16
  if no_parentheses_around_parameters?(node) && has_parameters?(node)
17
- add_error("use parentheses around parameters in method definitions")
17
+ add_error('use parentheses around parameters in method definitions')
18
18
  end
19
19
  end
20
20
 
@@ -16,7 +16,7 @@ module RailsBestPractices
16
16
  class UseQueryAttributeReview < Review
17
17
  interesting_nodes :if, :unless, :elsif, :ifop, :if_mod, :unless_mod
18
18
  interesting_files ALL_FILES
19
- url "https://rails-bestpractices.com/posts/2010/10/03/use-query-attribute/"
19
+ url 'https://rails-bestpractices.com/posts/2010/10/03/use-query-attribute/'
20
20
 
21
21
  QUERY_METHODS = %w(nil? blank? present?)
22
22
 
@@ -102,13 +102,13 @@ module RailsBestPractices
102
102
  def model_attribute?(variable_node, message)
103
103
  class_name = variable_node.to_s.sub(/^@/, '').classify
104
104
  attribute_type = model_attributes.get_attribute_type(class_name, message)
105
- attribute_type && !["integer", "float"].include?(attribute_type)
105
+ attribute_type && !['integer', 'float'].include?(attribute_type)
106
106
  end
107
107
 
108
108
  # check if the node is with node type :binary, node message :== and node argument is empty string.
109
109
  def compare_with_empty_string?(node)
110
110
  :binary == node.sexp_type &&
111
- ["==", "!="].include?(node.message.to_s) &&
111
+ ['==', '!='].include?(node.message.to_s) &&
112
112
  s(:string_literal, s(:string_content)) == node.argument
113
113
  end
114
114
  end