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 Prepares
4
5
  # Remembber routes.
@@ -6,8 +7,8 @@ module RailsBestPractices
6
7
  interesting_nodes :command, :command_call, :method_add_block, :do_block, :brace_block
7
8
  interesting_files ROUTE_FILES
8
9
 
9
- RESOURCES_ACTIONS = %w(index show new create edit update destroy)
10
- RESOURCE_ACTIONS = %w(show new create edit update destroy)
10
+ RESOURCES_ACTIONS = %w[index show new create edit update destroy].freeze
11
+ RESOURCE_ACTIONS = %w[show new create edit update destroy].freeze
11
12
 
12
13
  def initialize
13
14
  @routes = Prepares.routes
@@ -40,7 +41,7 @@ module RailsBestPractices
40
41
  elsif :symbol_literal == first_argument.sexp_type && second_argument.try(:sexp_type) && \
41
42
  :symbol_literal == second_argument.sexp_type
42
43
  action_names = node.arguments.all.select \
43
- {|arg| :symbol_literal == arg.sexp_type }.map(&:to_s)
44
+ { |arg| :symbol_literal == arg.sexp_type }.map(&:to_s)
44
45
  else
45
46
  action_names = [first_argument.to_s]
46
47
  end
@@ -173,16 +174,16 @@ module RailsBestPractices
173
174
  end
174
175
 
175
176
  # remember current controller name, used for nested resources.
176
- add_callback :start_do_block, :start_brace_block do |node|
177
+ add_callback :start_do_block, :start_brace_block do |_node|
177
178
  @controller_names << @controller_name.try(:last)
178
179
  end
179
180
 
180
181
  # remove current controller name, and use upper lever resource name.
181
- add_callback :end_do_block, :end_brace_block do |node|
182
+ add_callback :end_do_block, :end_brace_block do |_node|
182
183
  @controller_names.pop
183
184
  end
184
185
 
185
- [:resources, :resource].each do |route_name|
186
+ %i[resources resource].each do |route_name|
186
187
  class_eval <<-EOF
187
188
  def add_#{route_name}_routes(node)
188
189
  resource_names = node.arguments.all.select { |argument| :symbol_literal == argument.sexp_type }
@@ -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 model attributes.
@@ -7,7 +8,7 @@ module RailsBestPractices
7
8
  interesting_files SCHEMA_FILE
8
9
 
9
10
  # all attribute types
10
- ATTRIBUTE_TYPES = %w(integer float boolean string text date time datetime binary)
11
+ ATTRIBUTE_TYPES = %w[integer float boolean string text date time datetime binary].freeze
11
12
 
12
13
  def initialize
13
14
  @model_attributes = Prepares.model_attributes
@@ -1,2 +1,3 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require_rel 'reviews'
@@ -1,4 +1,7 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
3
+ require_rel 'review'
4
+
2
5
  module RailsBestPractices
3
6
  module Reviews
4
7
  # Make sure to add a model virual attribute to simplify model creation.
@@ -51,7 +54,7 @@ module RailsBestPractices
51
54
  return unless :field == left_value.sexp_type && :call == right_value.sexp_type
52
55
  aref_node = right_value.grep_node(sexp_type: :aref)
53
56
  if aref_node
54
- assignments(left_value.receiver.to_s) << {message: left_value.message.to_s, arguments: aref_node.to_s}
57
+ assignments(left_value.receiver.to_s) << { message: left_value.message.to_s, arguments: aref_node.to_s }
55
58
  end
56
59
  end
57
60
 
@@ -62,7 +65,7 @@ module RailsBestPractices
62
65
  def call_assignment(node)
63
66
  if ['save', 'save!'].include? node.message.to_s
64
67
  receiver = node.receiver.to_s
65
- add_error "add model virtual attribute (for #{receiver})" if params_dup?(assignments(receiver).collect {|h| h[:arguments]})
68
+ add_error "add model virtual attribute (for #{receiver})" if params_dup?(assignments(receiver).collect { |h| h[:arguments] })
66
69
  end
67
70
  end
68
71
 
@@ -81,7 +84,7 @@ module RailsBestPractices
81
84
  #
82
85
  # @return [Enumerable] the duplicate entries.
83
86
  def dups(nodes)
84
- nodes.inject({}) {|h,v| h[v]=h[v].to_i+1; h}.reject{|k,v| v==1}.keys
87
+ nodes.inject({}) { |h, v| h[v] = h[v].to_i + 1; h }.reject { |_k, v| v == 1 }.keys
85
88
  end
86
89
  end
87
90
  end
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Reviews
4
5
  # Review db/schema.rb file to make sure every reference key has a database index.
@@ -27,7 +28,7 @@ module RailsBestPractices
27
28
  interesting_files SCHEMA_FILE
28
29
  url 'https://rails-bestpractices.com/posts/2010/07/24/always-add-db-index/'
29
30
 
30
- def initialize(options={})
31
+ def initialize(options = {})
31
32
  super(options)
32
33
  @index_columns = {}
33
34
  @foreign_keys = {}
@@ -39,7 +40,7 @@ module RailsBestPractices
39
40
  # if the message of command_call node is "create_table", then remember the table name.
40
41
  # if the message of command_call node is "add_index", then remember it as index columns.
41
42
  add_callback :start_command_call do |node|
42
- if %w(integer string).include? node.message.to_s
43
+ if %w[integer string].include? node.message.to_s
43
44
  remember_foreign_key_columns(node)
44
45
  elsif 'index' == node.message.to_s
45
46
  remember_index_columns_inside_table(node)
@@ -134,7 +135,7 @@ module RailsBestPractices
134
135
 
135
136
  if foreign_id_column
136
137
  index_node = node.arguments.all.last.hash_value('index')
137
- if index_node.present? and 'false' != index_node.to_s
138
+ if index_node.present? && ('false' != index_node.to_s)
138
139
  @index_columns[table_name] ||= []
139
140
  @index_columns[table_name] << foreign_id_column
140
141
  end
@@ -155,7 +156,7 @@ module RailsBestPractices
155
156
 
156
157
  # remove the non foreign keys with only _type column.
157
158
  def remove_only_type_foreign_keys
158
- @foreign_keys.each { |table, foreign_keys|
159
+ @foreign_keys.each { |_table, foreign_keys|
159
160
  foreign_keys.delete_if { |key| key =~ /_type$/ }
160
161
  }
161
162
  end
@@ -163,7 +164,7 @@ module RailsBestPractices
163
164
  # combine polymorphic foreign keys, e.g.
164
165
  # [tagger_id], [tagger_type] => [tagger_id, tagger_type]
165
166
  def combine_polymorphic_foreign_keys
166
- @index_columns.each { |table, foreign_keys|
167
+ @index_columns.each { |_table, foreign_keys|
167
168
  foreign_id_keys = foreign_keys.select { |key| key.size == 1 && key.first =~ /_id/ }
168
169
  foreign_type_keys = foreign_keys.select { |key| key.size == 1 && key.first =~ /_type/ }
169
170
  foreign_id_keys.each do |id_key|
@@ -179,7 +180,7 @@ module RailsBestPractices
179
180
  # check if the table's column is indexed.
180
181
  def not_indexed?(table, column)
181
182
  index_columns = @index_columns[table]
182
- !index_columns || !index_columns.any? { |e| greater_than_or_equal(Array(e), Array(column)) }
183
+ !index_columns || index_columns.none? { |e| greater_than_or_equal(Array(e), Array(column)) }
183
184
  end
184
185
 
185
186
  # check if more_array is greater than less_array or equal to less_array.
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Reviews
4
5
  # Review all code to make sure we either check the return value of "destroy"
@@ -22,7 +23,7 @@ module RailsBestPractices
22
23
 
23
24
  add_callback :start_binary do |node|
24
25
  # Consider anything used in an expression like "A or B" as used
25
- if %w(&& || and or).include?(node[2].to_s)
26
+ if %w[&& || and or].include?(node[2].to_s)
26
27
  all_conditions = node.all_conditions
27
28
  # if our current binary is a subset of the @used_return_value_of
28
29
  # then don't overwrite it
@@ -33,7 +34,7 @@ module RailsBestPractices
33
34
  end
34
35
  end
35
36
 
36
- def return_value_is_used? node
37
+ def return_value_is_used?(node)
37
38
  return false unless @used_return_value_of
38
39
  node == @used_return_value_of or @used_return_value_of.include?(node)
39
40
  end
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Reviews
4
5
  # Review all code to make sure we either check the return value of "save", "update_attributes"
@@ -27,7 +28,7 @@ module RailsBestPractices
27
28
 
28
29
  add_callback :start_binary do |node|
29
30
  # Consider anything used in an expression like "A or B" as used
30
- if %w(&& || and or).include?(node[2].to_s)
31
+ if %w[&& || and or].include?(node[2].to_s)
31
32
  all_conditions = node.all_conditions
32
33
  # if our current binary is a subset of the @used_return_value_of
33
34
  # then don't overwrite it
@@ -38,7 +39,7 @@ module RailsBestPractices
38
39
  end
39
40
  end
40
41
 
41
- def return_value_is_used? node
42
+ def return_value_is_used?(node)
42
43
  return false unless @used_return_value_of
43
44
  node == @used_return_value_of or @used_return_value_of.include?(node)
44
45
  end
@@ -50,7 +51,7 @@ module RailsBestPractices
50
51
  add_callback :start_call, :start_command_call, :start_method_add_arg do |node|
51
52
  unless @already_checked == node
52
53
  message = node.message.to_s
53
- if ['save', 'update_attributes'].include? message
54
+ if %w[save update_attributes].include? message
54
55
  unless return_value_is_used? node
55
56
  add_error "check '#{message}' return value or use '#{message}!'"
56
57
  end
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Reviews
4
5
  # Review model files to make sure not use default_scope
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Reviews
4
5
  # Review config/deploy.rb file to make sure using the bundler's capistrano recipe.
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Reviews
4
5
  # Check ruby 1.8 style hash and suggest to change hash syntax to 1.9.
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Reviews
4
5
  # Make sure not to insert data in migration, move them to seed file.
@@ -23,7 +24,7 @@ module RailsBestPractices
23
24
  interesting_files MIGRATION_FILES
24
25
  url 'https://rails-bestpractices.com/posts/2010/07/24/isolating-seed-data/'
25
26
 
26
- def initialize(options={})
27
+ def initialize(options = {})
27
28
  super(options)
28
29
  @new_variables = []
29
30
  end
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Reviews
4
5
  # Review model files to make sure finders are on their own model.
@@ -19,7 +20,7 @@ module RailsBestPractices
19
20
  interesting_files MODEL_FILES
20
21
  url 'https://rails-bestpractices.com/posts/2010/07/23/keep-finders-on-their-own-model/'
21
22
 
22
- FINDERS = %w(find all first last)
23
+ FINDERS = %w[find all first last].freeze
23
24
 
24
25
  # check all the call nodes to see if there is a finder for other model.
25
26
  #
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Reviews
4
5
  # Review to make sure not to avoid the law of demeter.
@@ -18,7 +19,7 @@ module RailsBestPractices
18
19
  interesting_files ALL_FILES
19
20
  url 'https://rails-bestpractices.com/posts/2010/07/24/the-law-of-demeter/'
20
21
 
21
- ASSOCIATION_METHODS = %w(belongs_to has_one)
22
+ ASSOCIATION_METHODS = %w[belongs_to has_one].freeze
22
23
 
23
24
  # check the call node,
24
25
  #
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Reviews
4
5
  # Review a view file to make sure there is no finder, finder should be moved to controller.
@@ -14,7 +15,7 @@ module RailsBestPractices
14
15
  interesting_files VIEW_FILES
15
16
  url 'https://rails-bestpractices.com/posts/2010/07/24/move-code-into-controller/'
16
17
 
17
- FINDERS = %w(find all first last)
18
+ FINDERS = %w[find all first last].freeze
18
19
 
19
20
  # check method_add_arg nodes.
20
21
  #
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Reviews
4
5
  # Review a view file to make sure there is no complex options_for_select message call.
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Reviews
4
5
  # Review a view file to make sure there is no complex logic call for model.
@@ -15,7 +16,7 @@ module RailsBestPractices
15
16
  interesting_files VIEW_FILES
16
17
  url 'https://rails-bestpractices.com/posts/2010/07/24/move-code-into-model/'
17
18
 
18
- def initialize(options={})
19
+ def initialize(options = {})
19
20
  super(options)
20
21
  @use_count = options['use_count'] || 2
21
22
  end
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Reviews
4
5
  # Review a controller file to make sure there are no complex finder.
@@ -17,7 +18,7 @@ module RailsBestPractices
17
18
  interesting_files CONTROLLER_FILES
18
19
  url 'https://rails-bestpractices.com/posts/2010/07/14/move-finder-to-named_scope/'
19
20
 
20
- FINDERS = %w(find all first last)
21
+ FINDERS = %w[find all first last].freeze
21
22
 
22
23
  # check method_add_ag node if its message is one of find, all, first or last,
23
24
  # and it has a hash argument,
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Reviews
4
5
  # Review a controller file to make sure that complex model logic should not exist in controller, should be moved into a model.
@@ -29,7 +30,7 @@ module RailsBestPractices
29
30
  # and the receiver is a variable,
30
31
  # then these method calls and attribute assignments should be moved into model.
31
32
  add_callback :start_def do |node|
32
- node.grep_nodes(sexp_type: [:call, :assign]) do |child_node|
33
+ node.grep_nodes(sexp_type: %i[call assign]) do |child_node|
33
34
  remember_variable_use_count(child_node)
34
35
  end
35
36
 
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Reviews
4
5
  # Review config/routes.rb file to make sure not to use too deep nesting routes.
@@ -61,16 +62,16 @@ module RailsBestPractices
61
62
  # if so, it is the needless deep nesting.
62
63
  def recursively_check(node)
63
64
  shallow = @shallow_nodes.include? node
64
- if [:command_call, :command].include?(node[1].sexp_type) && ['resources', 'resource'].include?(node[1].message.to_s)
65
+ if %i[command_call command].include?(node[1].sexp_type) && %w[resources resource].include?(node[1].message.to_s)
65
66
  hash_node = node[1].arguments.grep_node(sexp_type: :bare_assoc_hash)
66
- shallow = (hash_node && 'true' == hash_node.hash_value('shallow').to_s) unless shallow
67
+ shallow ||= (hash_node && 'true' == hash_node.hash_value('shallow').to_s)
67
68
  @counter += 1
68
69
  node.block_node.statements.each do |stmt_node|
69
70
  @shallow_nodes << stmt_node if shallow
70
71
  recursively_check(stmt_node)
71
72
  end
72
73
  @counter -= 1
73
- elsif [:command_call, :command].include?(node.sexp_type) && ['resources', 'resource'].include?(node.message.to_s)
74
+ elsif %i[command_call command].include?(node.sexp_type) && %w[resources resource].include?(node.message.to_s)
74
75
  add_error "needless deep nesting (nested_count > #{@nested_count})", @file, node.line_number if @counter >= @nested_count && !@shallow_nodes.include?(node)
75
76
  end
76
77
  end
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Reviews
4
5
  # Review all code to make sure we don't rescue Exception
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Reviews
4
5
  # Review config/routes file to make sure not use default route that rails generated.
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Reviews
4
5
  # Review view and helper files to make sure not use time_ago_in_words or distance_of_time_in_words_to_now.
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Reviews
4
5
  # Review config/routes.rb file to make sure there are no overuse route customizations.
@@ -19,7 +20,7 @@ module RailsBestPractices
19
20
  interesting_files ROUTE_FILES
20
21
  url 'https://rails-bestpractices.com/posts/2010/07/22/overuse-route-customizations/'
21
22
 
22
- VERBS = %w(get post update delete)
23
+ VERBS = %w[get post update delete].freeze
23
24
 
24
25
  def initialize(options = {})
25
26
  super(options)
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module RailsBestPractices
3
4
  module Reviews
4
5
  # Review model files to make sure to use attr_accessible, attr_protected or strong_parameters to protect mass assignment.
@@ -16,7 +17,7 @@ module RailsBestPractices
16
17
  url 'https://rails-bestpractices.com/posts/2012/03/06/protect-mass-assignment/'
17
18
 
18
19
  # we treat it as mass assignment by default.
19
- add_callback :start_class do |node|
20
+ add_callback :start_class do |_node|
20
21
  @mass_assignement = true
21
22
  check_activerecord_version
22
23
  check_whitelist_attributes_config
@@ -72,7 +73,7 @@ module RailsBestPractices
72
73
  end
73
74
 
74
75
  def check_rails_builtin(node)
75
- if @whitelist_attributes || [node.to_s, node.message.to_s].any? { |str| %w(attr_accessible attr_protected).include? str }
76
+ if @whitelist_attributes || [node.to_s, node.message.to_s].any? { |str| %w[attr_accessible attr_protected].include? str }
76
77
  @mass_assignement = false
77
78
  end
78
79
  end