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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Guardfile +1 -1
- data/README.md +1 -0
- data/Rakefile +2 -2
- data/bin/rails_best_practices +2 -1
- data/lib/rails_best_practices.rb +2 -1
- data/lib/rails_best_practices/analyzer.rb +13 -12
- data/lib/rails_best_practices/colorize.rb +2 -0
- data/lib/rails_best_practices/command.rb +6 -5
- data/lib/rails_best_practices/core.rb +2 -1
- data/lib/rails_best_practices/core/check.rb +19 -17
- data/lib/rails_best_practices/core/checks_loader.rb +3 -2
- data/lib/rails_best_practices/core/configs.rb +2 -1
- data/lib/rails_best_practices/core/controllers.rb +4 -1
- data/lib/rails_best_practices/core/error.rb +3 -2
- data/lib/rails_best_practices/core/gems.rb +4 -3
- data/lib/rails_best_practices/core/helpers.rb +4 -1
- data/lib/rails_best_practices/core/klasses.rb +3 -2
- data/lib/rails_best_practices/core/mailers.rb +2 -1
- data/lib/rails_best_practices/core/methods.rb +9 -10
- data/lib/rails_best_practices/core/model_associations.rb +6 -5
- data/lib/rails_best_practices/core/model_attributes.rb +2 -1
- data/lib/rails_best_practices/core/models.rb +2 -1
- data/lib/rails_best_practices/core/modules.rb +2 -1
- data/lib/rails_best_practices/core/routes.rb +2 -1
- data/lib/rails_best_practices/core/runner.rb +7 -6
- data/lib/rails_best_practices/core_ext/erubis.rb +4 -5
- data/lib/rails_best_practices/lexicals.rb +2 -1
- data/lib/rails_best_practices/lexicals/long_line_check.rb +2 -1
- data/lib/rails_best_practices/lexicals/remove_tab_check.rb +2 -1
- data/lib/rails_best_practices/lexicals/remove_trailing_whitespace_check.rb +2 -1
- data/lib/rails_best_practices/prepares.rb +2 -1
- data/lib/rails_best_practices/prepares/config_prepare.rb +3 -2
- data/lib/rails_best_practices/prepares/controller_prepare.rb +9 -8
- data/lib/rails_best_practices/prepares/gemfile_prepare.rb +2 -1
- data/lib/rails_best_practices/prepares/helper_prepare.rb +4 -3
- data/lib/rails_best_practices/prepares/initializer_prepare.rb +2 -1
- data/lib/rails_best_practices/prepares/mailer_prepare.rb +3 -2
- data/lib/rails_best_practices/prepares/model_prepare.rb +12 -12
- data/lib/rails_best_practices/prepares/route_prepare.rb +8 -7
- data/lib/rails_best_practices/prepares/schema_prepare.rb +3 -2
- data/lib/rails_best_practices/reviews.rb +2 -1
- data/lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb +7 -4
- data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +8 -7
- data/lib/rails_best_practices/reviews/check_destroy_return_value_review.rb +4 -3
- data/lib/rails_best_practices/reviews/check_save_return_value_review.rb +5 -4
- data/lib/rails_best_practices/reviews/default_scope_is_evil_review.rb +2 -1
- data/lib/rails_best_practices/reviews/dry_bundler_in_capistrano_review.rb +2 -1
- data/lib/rails_best_practices/reviews/hash_syntax_review.rb +2 -1
- data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +3 -2
- data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +3 -2
- data/lib/rails_best_practices/reviews/law_of_demeter_review.rb +3 -2
- data/lib/rails_best_practices/reviews/move_code_into_controller_review.rb +3 -2
- data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +2 -1
- data/lib/rails_best_practices/reviews/move_code_into_model_review.rb +3 -2
- data/lib/rails_best_practices/reviews/move_finder_to_named_scope_review.rb +3 -2
- data/lib/rails_best_practices/reviews/move_model_logic_into_model_review.rb +3 -2
- data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +5 -4
- data/lib/rails_best_practices/reviews/not_rescue_exception_review.rb +2 -1
- data/lib/rails_best_practices/reviews/not_use_default_route_review.rb +2 -1
- data/lib/rails_best_practices/reviews/not_use_time_ago_in_words_review.rb +2 -1
- data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +3 -2
- data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +4 -3
- data/lib/rails_best_practices/reviews/remove_empty_helpers_review.rb +2 -1
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb +11 -10
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_helpers_review.rb +4 -3
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +6 -5
- data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +2 -2
- data/lib/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review.rb +2 -1
- data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +8 -8
- data/lib/rails_best_practices/reviews/review.rb +4 -3
- data/lib/rails_best_practices/reviews/simplify_render_in_controllers_review.rb +2 -1
- data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +3 -2
- data/lib/rails_best_practices/reviews/use_before_filter_review.rb +5 -4
- data/lib/rails_best_practices/reviews/use_model_association_review.rb +2 -2
- data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +5 -5
- data/lib/rails_best_practices/reviews/use_observer_review.rb +2 -1
- data/lib/rails_best_practices/reviews/use_parentheses_in_method_def_review.rb +2 -1
- data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +4 -3
- data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +4 -3
- data/lib/rails_best_practices/reviews/use_scope_access_review.rb +2 -1
- data/lib/rails_best_practices/reviews/use_turbo_sprockets_rails3_review.rb +2 -1
- data/lib/rails_best_practices/version.rb +3 -2
- data/rails_best_practices.gemspec +11 -10
- data/spec/fixtures/lib/rails_best_practices/plugins/reviews/not_use_rails_root_review.rb +3 -2
- data/spec/rails_best_practices/analyzer_spec.rb +6 -8
- data/spec/rails_best_practices/core/check_spec.rb +2 -0
- data/spec/rails_best_practices/core/checks_loader_spec.rb +2 -0
- data/spec/rails_best_practices/core/configs_spec.rb +2 -0
- data/spec/rails_best_practices/core/controllers_spec.rb +2 -0
- data/spec/rails_best_practices/core/error_spec.rb +2 -0
- data/spec/rails_best_practices/core/except_methods_spec.rb +2 -0
- data/spec/rails_best_practices/core/gems_spec.rb +2 -0
- data/spec/rails_best_practices/core/helpers_spec.rb +2 -0
- data/spec/rails_best_practices/core/klasses_spec.rb +2 -0
- data/spec/rails_best_practices/core/mailers_spec.rb +2 -0
- data/spec/rails_best_practices/core/methods_spec.rb +6 -4
- data/spec/rails_best_practices/core/model_associations_spec.rb +4 -2
- data/spec/rails_best_practices/core/model_attributes_spec.rb +2 -0
- data/spec/rails_best_practices/core/models_spec.rb +2 -0
- data/spec/rails_best_practices/core/modules_spec.rb +2 -0
- data/spec/rails_best_practices/core/routes_spec.rb +4 -2
- data/spec/rails_best_practices/core/runner_spec.rb +2 -0
- data/spec/rails_best_practices/core_ext/erubis_spec.rb +3 -1
- data/spec/rails_best_practices/lexicals/long_line_check_spec.rb +8 -7
- data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +8 -6
- data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +9 -7
- data/spec/rails_best_practices/prepares/config_prepare_spec.rb +3 -1
- data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +23 -21
- data/spec/rails_best_practices/prepares/gemfile_prepare_spec.rb +3 -1
- data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +7 -5
- data/spec/rails_best_practices/prepares/initializer_prepare_spec.rb +4 -2
- data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +3 -1
- data/spec/rails_best_practices/prepares/model_prepare_spec.rb +56 -54
- data/spec/rails_best_practices/prepares/route_prepare_spec.rb +39 -37
- data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +3 -1
- data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +7 -5
- data/spec/rails_best_practices/reviews/check_destroy_return_value_review_spec.rb +12 -10
- data/spec/rails_best_practices/reviews/check_save_return_value_review_spec.rb +20 -18
- data/spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/hash_syntax_review_spec.rb +9 -7
- data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/keep_finders_on_their_own_model_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +11 -9
- data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/not_rescue_exception_review_spec.rb +9 -7
- data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/not_use_time_ago_in_words_review_spec.rb +7 -5
- data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +21 -19
- data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +6 -4
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +33 -31
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +13 -11
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +65 -63
- data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +24 -22
- data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +9 -7
- data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +13 -11
- data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +3 -1
- data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +7 -5
- data/spec/rails_best_practices/reviews/use_parentheses_in_method_def_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +3 -1
- data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +9 -7
- data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/use_turbo_sprockets_rails3_review_spec.rb +2 -0
- data/spec/spec_helper.rb +2 -0
- metadata +26 -26
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
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
|
10
|
-
RESOURCE_ACTIONS = %w
|
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 |
|
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 |
|
182
|
+
add_callback :end_do_block, :end_brace_block do |_node|
|
182
183
|
@controller_names.pop
|
183
184
|
end
|
184
185
|
|
185
|
-
[
|
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
|
-
#
|
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
|
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,4 +1,7 @@
|
|
1
|
-
#
|
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{|
|
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
|
-
#
|
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
|
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?
|
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 { |
|
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 { |
|
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 ||
|
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
|
-
#
|
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
|
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?
|
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
|
-
#
|
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
|
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?
|
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 [
|
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
|
-
#
|
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
|
-
#
|
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
|
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
|
-
#
|
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
|
22
|
+
ASSOCIATION_METHODS = %w[belongs_to has_one].freeze
|
22
23
|
|
23
24
|
# check the call node,
|
24
25
|
#
|
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
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
|
18
|
+
FINDERS = %w[find all first last].freeze
|
18
19
|
|
19
20
|
# check method_add_arg nodes.
|
20
21
|
#
|
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
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
|
-
#
|
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
|
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
|
-
#
|
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: [
|
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
|
-
#
|
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 [
|
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
|
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 [
|
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
|
-
#
|
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
|
23
|
+
VERBS = %w[get post update delete].freeze
|
23
24
|
|
24
25
|
def initialize(options = {})
|
25
26
|
super(options)
|
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
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 |
|
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
|
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
|