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 Reviews
|
4
5
|
# Find out unused methods in controllers.
|
@@ -20,9 +21,9 @@ module RailsBestPractices
|
|
20
21
|
interesting_nodes :class, :command, :method_add_arg, :assign
|
21
22
|
interesting_files CONTROLLER_FILES, VIEW_FILES, HELPER_FILES
|
22
23
|
|
23
|
-
INHERITED_RESOURCES_METHODS = %w
|
24
|
+
INHERITED_RESOURCES_METHODS = %w[resource collection begin_of_association_chain build_resource].freeze
|
24
25
|
|
25
|
-
def initialize(options={})
|
26
|
+
def initialize(options = {})
|
26
27
|
super
|
27
28
|
@controller_methods = Prepares.controller_methods
|
28
29
|
@routes = Prepares.routes
|
@@ -30,7 +31,7 @@ module RailsBestPractices
|
|
30
31
|
end
|
31
32
|
|
32
33
|
# mark custom inherited_resources methods as used.
|
33
|
-
add_callback :end_class do |
|
34
|
+
add_callback :end_class do |_node|
|
34
35
|
if @inherited_resources
|
35
36
|
INHERITED_RESOURCES_METHODS.each do |method|
|
36
37
|
call_method(method)
|
@@ -40,14 +41,14 @@ module RailsBestPractices
|
|
40
41
|
|
41
42
|
# skip render and around_filter nodes for start_command callbacks.
|
42
43
|
def skip_command_callback_nodes
|
43
|
-
%w
|
44
|
+
%w[render_cell render around_filter]
|
44
45
|
end
|
45
46
|
|
46
47
|
# mark corresponding action as used for cells' render and render_call.
|
47
48
|
add_callback :start_command, :start_method_add_arg do |node|
|
48
49
|
case node.message.to_s
|
49
50
|
when 'render_cell'
|
50
|
-
controller_name, action_name,
|
51
|
+
controller_name, action_name, = *node.arguments.all.map(&:to_s)
|
51
52
|
call_method(action_name, "#{controller_name}_cell".classify)
|
52
53
|
when 'render'
|
53
54
|
first_argument = node.arguments.all.first
|
@@ -93,7 +94,7 @@ module RailsBestPractices
|
|
93
94
|
end
|
94
95
|
end
|
95
96
|
@controller_methods.get_all_unused_methods.each do |method|
|
96
|
-
|
97
|
+
unless excepted?(method)
|
97
98
|
add_error "remove unused methods (#{method.class_name}##{method.method_name})", method.file, method.line_number
|
98
99
|
end
|
99
100
|
end
|
@@ -106,11 +107,11 @@ module RailsBestPractices
|
|
106
107
|
end
|
107
108
|
|
108
109
|
def internal_except_methods
|
109
|
-
%w
|
110
|
-
%w
|
110
|
+
%w[rescue_action default_url_options].map { |method_name| "*\##{method_name}" } +
|
111
|
+
%w[Devise::OmniauthCallbacksController].map { |controller_name| "#{controller_name}#*" }
|
111
112
|
end
|
112
113
|
|
113
|
-
def mark_publicize(method_name, class_name=current_class_name)
|
114
|
+
def mark_publicize(method_name, class_name = current_class_name)
|
114
115
|
@controller_methods.mark_publicize(class_name, method_name)
|
115
116
|
@controller_methods.mark_parent_class_methods_publicize(class_name, method_name)
|
116
117
|
end
|
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module RailsBestPractices
|
3
4
|
module Reviews
|
4
5
|
# Find out unused methods in helpers.
|
@@ -16,7 +17,7 @@ module RailsBestPractices
|
|
16
17
|
|
17
18
|
interesting_files HELPER_FILES, VIEW_FILES, CONTROLLER_FILES
|
18
19
|
|
19
|
-
def initialize(options={})
|
20
|
+
def initialize(options = {})
|
20
21
|
super
|
21
22
|
@helper_methods = Prepares.helper_methods
|
22
23
|
self.class.interesting_files Prepares.helpers.map(&:descendants)
|
@@ -25,7 +26,7 @@ module RailsBestPractices
|
|
25
26
|
# get all unused methods at the end of review process
|
26
27
|
add_callback :after_check do
|
27
28
|
@helper_methods.get_all_unused_methods.each do |method|
|
28
|
-
|
29
|
+
unless excepted?(method)
|
29
30
|
add_error "remove unused methods (#{method.class_name}##{method.method_name})", method.file, method.line_number
|
30
31
|
end
|
31
32
|
end
|
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module RailsBestPractices
|
3
4
|
module Reviews
|
4
5
|
# Find out unused methods in models.
|
@@ -17,14 +18,14 @@ module RailsBestPractices
|
|
17
18
|
interesting_nodes :command, :command_call, :method_add_arg
|
18
19
|
interesting_files ALL_FILES
|
19
20
|
|
20
|
-
def initialize(options={})
|
21
|
+
def initialize(options = {})
|
21
22
|
super
|
22
23
|
@model_methods = Prepares.model_methods
|
23
24
|
end
|
24
25
|
|
25
26
|
# skip scope and validate nodes for start_command callbacks.
|
26
27
|
def skip_command_callback_nodes
|
27
|
-
%w
|
28
|
+
%w[named_scope scope validate validate_on_create validate_on_update]
|
28
29
|
end
|
29
30
|
|
30
31
|
# mark validate methods as used.
|
@@ -92,7 +93,7 @@ module RailsBestPractices
|
|
92
93
|
end
|
93
94
|
|
94
95
|
def internal_except_methods
|
95
|
-
%w
|
96
|
+
%w[
|
96
97
|
initialize
|
97
98
|
validate validate_each validate_on_create validate_on_update
|
98
99
|
human_attribute_name assign_attributes attributes attribute
|
@@ -101,7 +102,7 @@ module RailsBestPractices
|
|
101
102
|
after_update after_destroy after_find after_initialize
|
102
103
|
method_missing
|
103
104
|
table_name module_prefix
|
104
|
-
|
105
|
+
].map { |method_name| "*\##{method_name}" }
|
105
106
|
end
|
106
107
|
end
|
107
108
|
end
|
data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb
CHANGED
@@ -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 creation should not exist in
|
@@ -41,7 +42,6 @@ module RailsBestPractices
|
|
41
42
|
end
|
42
43
|
when :call
|
43
44
|
check_variable_save(child_node)
|
44
|
-
else
|
45
45
|
end
|
46
46
|
end
|
47
47
|
reset_variable_use_count
|
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module RailsBestPractices
|
3
4
|
module Reviews
|
4
5
|
# Review a route file to make sure all auto-generated routes have corresponding actions in controller.
|
@@ -19,9 +20,9 @@ module RailsBestPractices
|
|
19
20
|
|
20
21
|
def resource_methods
|
21
22
|
if Prepares.configs['config.api_only']
|
22
|
-
%w
|
23
|
+
%w[show create update destroy]
|
23
24
|
else
|
24
|
-
%w
|
25
|
+
%w[show new create edit update destroy]
|
25
26
|
end
|
26
27
|
end
|
27
28
|
|
@@ -29,7 +30,7 @@ module RailsBestPractices
|
|
29
30
|
resource_methods + ['index']
|
30
31
|
end
|
31
32
|
|
32
|
-
def initialize(options={})
|
33
|
+
def initialize(options = {})
|
33
34
|
super(options)
|
34
35
|
@namespaces = []
|
35
36
|
@resource_controllers = []
|
@@ -69,7 +70,6 @@ module RailsBestPractices
|
|
69
70
|
if check_method_add_block?(node) && (mod = module_option(node))
|
70
71
|
@namespaces << mod
|
71
72
|
end
|
72
|
-
else
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
@@ -109,7 +109,7 @@ 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')
|
112
|
+
if hash_key_exist?(option_node, 'controller')
|
113
113
|
name = option_node.hash_value('controller').to_s
|
114
114
|
else
|
115
115
|
name = node.arguments.all.first.to_s.gsub('::', '').tableize
|
@@ -122,7 +122,7 @@ module RailsBestPractices
|
|
122
122
|
|
123
123
|
# get the class name with namespace.
|
124
124
|
def namespaced_class_name(name)
|
125
|
-
class_name = "#{name.split(
|
125
|
+
class_name = "#{name.split('/').map(&:camelize).join('::')}Controller"
|
126
126
|
if @namespaces.empty?
|
127
127
|
class_name
|
128
128
|
else
|
@@ -138,7 +138,7 @@ module RailsBestPractices
|
|
138
138
|
prepared_method_names = Prepares.controller_methods.get_methods(controller_name).map(&:method_name)
|
139
139
|
only_methods = (_methods & prepared_method_names).map { |meth| ":#{meth}" }
|
140
140
|
routes_message = if only_methods.size > 3
|
141
|
-
"except: [#{(methods.map { |meth|
|
141
|
+
"except: [#{(methods.map { |meth| ':' + meth } - only_methods).join(', ')}]"
|
142
142
|
else
|
143
143
|
"only: [#{only_methods.join(', ')}]"
|
144
144
|
end
|
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module RailsBestPractices
|
3
4
|
module Reviews
|
4
5
|
# A Review class that takes charge of reviewing one rails best practice.
|
@@ -31,10 +32,10 @@ module RailsBestPractices
|
|
31
32
|
|
32
33
|
# find variable in the call or field node.
|
33
34
|
def variable(node)
|
34
|
-
while [
|
35
|
+
while %i[call field method_add_arg method_add_block].include?(node.receiver.sexp_type)
|
35
36
|
node = node.receiver
|
36
37
|
end
|
37
|
-
return if [
|
38
|
+
return if %i[fcall hash].include?(node.receiver.sexp_type)
|
38
39
|
node.receiver
|
39
40
|
end
|
40
41
|
|
@@ -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 using simplified syntax for render.
|
@@ -15,7 +16,7 @@ module RailsBestPractices
|
|
15
16
|
interesting_files VIEW_FILES
|
16
17
|
url 'https://rails-bestpractices.com/posts/2010/12/04/simplify-render-in-views/'
|
17
18
|
|
18
|
-
VALID_KEYS = %w
|
19
|
+
VALID_KEYS = %w[object collection locals].freeze
|
19
20
|
|
20
21
|
# check command node in view file,
|
21
22
|
# if its message is render and the arguments contain a key partial,
|
@@ -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 to use before_filter to remove duplicated first code
|
@@ -30,12 +31,12 @@ module RailsBestPractices
|
|
30
31
|
@first_sentences = {}
|
31
32
|
|
32
33
|
node.body.statements.each do |statement_node|
|
33
|
-
var_ref_or_vcall_included = [
|
34
|
-
private_or_protected_included = [
|
34
|
+
var_ref_or_vcall_included = %i[var_ref vcall].include?(statement_node.sexp_type)
|
35
|
+
private_or_protected_included = %w[protected private].include?(statement_node.to_s)
|
35
36
|
break if var_ref_or_vcall_included && private_or_protected_included
|
36
37
|
remember_first_sentence(statement_node) if :def == statement_node.sexp_type
|
37
38
|
end
|
38
|
-
@first_sentences.each do |
|
39
|
+
@first_sentences.each do |_first_sentence, def_nodes|
|
39
40
|
if def_nodes.size > @customize_count
|
40
41
|
add_error "use before_filter for #{def_nodes.map { |node| node.method_name.to_s }.join(',')}",
|
41
42
|
node.file,
|
@@ -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 to use model association instead of foreign key id assignment.
|
@@ -35,7 +36,6 @@ module RailsBestPractices
|
|
35
36
|
attribute_assignment(child)
|
36
37
|
when :call
|
37
38
|
call_assignment(child)
|
38
|
-
else
|
39
39
|
end
|
40
40
|
end
|
41
41
|
@assignments = nil
|
data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module RailsBestPractices
|
3
4
|
module Reviews
|
4
5
|
# Make sure to use multipart/alternative as content_type of email.
|
@@ -23,7 +24,7 @@ module RailsBestPractices
|
|
23
24
|
# check def node and find if the corresponding views exist or not?
|
24
25
|
add_callback :start_def do |node|
|
25
26
|
name = node.method_name.to_s
|
26
|
-
|
27
|
+
unless rails3_canonical_mailer_views?(name)
|
27
28
|
add_error('use multipart/alternative as content_type of email')
|
28
29
|
end
|
29
30
|
end
|
@@ -33,14 +34,13 @@ module RailsBestPractices
|
|
33
34
|
# check if rails's syntax mailer views are canonical.
|
34
35
|
#
|
35
36
|
# @param [String] name method name in action_mailer
|
36
|
-
def rails_canonical_mailer_views?(name)
|
37
|
-
end
|
37
|
+
def rails_canonical_mailer_views?(name); end
|
38
38
|
|
39
39
|
# check if rails3's syntax mailer views are canonical.
|
40
40
|
#
|
41
41
|
# @param [String] name method name in action_mailer
|
42
42
|
def rails3_canonical_mailer_views?(name)
|
43
|
-
return true if mailer_files(name).
|
43
|
+
return true if mailer_files(name).empty?
|
44
44
|
return true if mailer_files(name).none? { |filename| filename.index 'html' }
|
45
45
|
mailer_files(name).any? { |filename| filename.index 'html' } &&
|
46
46
|
mailer_files(name).any? { |filename| filename.index 'text' }
|
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module RailsBestPractices
|
3
4
|
module Reviews
|
4
5
|
# Make sure to use query attribute instead of nil?, blank? and present?.
|
@@ -18,7 +19,7 @@ module RailsBestPractices
|
|
18
19
|
interesting_files ALL_FILES
|
19
20
|
url 'https://rails-bestpractices.com/posts/2010/10/03/use-query-attribute/'
|
20
21
|
|
21
|
-
QUERY_METHODS = %w
|
22
|
+
QUERY_METHODS = %w[nil? blank? present?].freeze
|
22
23
|
|
23
24
|
# check if node to see whose conditional statement nodes contain nodes that can use query attribute instead.
|
24
25
|
#
|
@@ -102,7 +103,7 @@ module RailsBestPractices
|
|
102
103
|
def model_attribute?(variable_node, message)
|
103
104
|
class_name = variable_node.to_s.sub(/^@/, '').classify
|
104
105
|
attribute_type = model_attributes.get_attribute_type(class_name, message)
|
105
|
-
attribute_type &&
|
106
|
+
attribute_type && !%w[integer float].include?(attribute_type)
|
106
107
|
end
|
107
108
|
|
108
109
|
# check if the node is with node type :binary, node message :== and node argument is empty string.
|
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module RailsBestPractices
|
3
4
|
module Reviews
|
4
5
|
# Review a migration file to make sure to use say or say_with_time for customized data changes
|
@@ -18,7 +19,7 @@ module RailsBestPractices
|
|
18
19
|
interesting_files MIGRATION_FILES
|
19
20
|
url 'https://rails-bestpractices.com/posts/2010/08/19/use-say-and-say_with_time-in-migrations-to-make-a-useful-migration-log/'
|
20
21
|
|
21
|
-
WITH_SAY_METHODS = %w
|
22
|
+
WITH_SAY_METHODS = %w[say say_with_time].freeze
|
22
23
|
|
23
24
|
# check a class method define node to see if there are method calls that need to be wrapped by say
|
24
25
|
# or say_with_time.
|
@@ -28,7 +29,7 @@ module RailsBestPractices
|
|
28
29
|
# then such method call should be wrapped by say or say_with_time
|
29
30
|
add_callback :start_defs do |node|
|
30
31
|
node.body.statements.each do |child_node|
|
31
|
-
next if child_node.grep_nodes_count(sexp_type: [
|
32
|
+
next if child_node.grep_nodes_count(sexp_type: %i[fcall command], message: WITH_SAY_METHODS) > 0
|
32
33
|
|
33
34
|
receiver_node = if :method_add_block == child_node.sexp_type
|
34
35
|
child_node[1]
|
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require File.expand_path('../lib/rails_best_practices/version', __FILE__)
|
3
4
|
|
4
5
|
Gem::Specification.new do |s|
|
@@ -20,24 +21,24 @@ Gem::Specification.new do |s|
|
|
20
21
|
s.add_dependency('code_analyzer', '>= 0.4.8')
|
21
22
|
s.add_dependency('erubis')
|
22
23
|
s.add_dependency('i18n')
|
23
|
-
s.add_dependency('require_all', '~> 1.5.0')
|
24
|
-
s.add_dependency('ruby-progressbar')
|
25
24
|
s.add_dependency('json')
|
25
|
+
s.add_dependency('require_all', '~> 2.0')
|
26
|
+
s.add_dependency('ruby-progressbar')
|
26
27
|
|
28
|
+
s.add_development_dependency('awesome_print')
|
29
|
+
s.add_development_dependency('bundler')
|
30
|
+
s.add_development_dependency('haml')
|
27
31
|
s.add_development_dependency('rake', '< 11.0')
|
28
32
|
s.add_development_dependency('rspec')
|
29
|
-
s.add_development_dependency('haml')
|
30
33
|
s.add_development_dependency('slim')
|
31
|
-
s.add_development_dependency('bundler')
|
32
|
-
s.add_development_dependency('awesome_print')
|
33
34
|
|
34
35
|
s.files = `git ls-files`.split("\n")
|
35
36
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
36
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
37
|
-
s.require_paths = [
|
37
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
38
|
+
s.require_paths = %w[lib assets]
|
38
39
|
|
39
40
|
s.post_install_message = <<-POST_INSTALL_MESSAGE
|
40
|
-
#{
|
41
|
+
#{'*' * 80}
|
41
42
|
|
42
43
|
rails_best_practices is a code metric tool to check the quality of rails codes.
|
43
44
|
|
@@ -53,6 +54,6 @@ Gem::Specification.new do |s|
|
|
53
54
|
|
54
55
|
Richard Huang (flyerhzm@gmail.com)
|
55
56
|
|
56
|
-
#{
|
57
|
+
#{'*' * 80}
|
57
58
|
POST_INSTALL_MESSAGE
|
58
59
|
end
|