rails_best_practices 1.20.0 → 1.20.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -1
- data/Gemfile.lock +30 -26
- data/Guardfile +2 -0
- data/Rakefile +2 -0
- data/lib/rails_best_practices.rb +1 -2
- data/lib/rails_best_practices/analyzer.rb +56 -46
- data/lib/rails_best_practices/core/check.rb +39 -32
- data/lib/rails_best_practices/core/checks_loader.rb +8 -6
- data/lib/rails_best_practices/core/configs.rb +1 -2
- data/lib/rails_best_practices/core/controllers.rb +1 -2
- data/lib/rails_best_practices/core/error.rb +1 -1
- data/lib/rails_best_practices/core/helpers.rb +1 -2
- data/lib/rails_best_practices/core/mailers.rb +1 -2
- data/lib/rails_best_practices/core/methods.rb +21 -16
- data/lib/rails_best_practices/core/model_associations.rb +9 -4
- data/lib/rails_best_practices/core/models.rb +1 -2
- data/lib/rails_best_practices/core/modules.rb +1 -1
- data/lib/rails_best_practices/core/routes.rb +2 -2
- data/lib/rails_best_practices/core/runner.rb +29 -33
- data/lib/rails_best_practices/lexicals/long_line_check.rb +7 -3
- data/lib/rails_best_practices/option_parser.rb +17 -6
- data/lib/rails_best_practices/prepares.rb +1 -1
- data/lib/rails_best_practices/prepares/controller_prepare.rb +15 -3
- data/lib/rails_best_practices/prepares/gemfile_prepare.rb +1 -1
- data/lib/rails_best_practices/prepares/helper_prepare.rb +6 -1
- data/lib/rails_best_practices/prepares/initializer_prepare.rb +2 -2
- data/lib/rails_best_practices/prepares/mailer_prepare.rb +1 -0
- data/lib/rails_best_practices/prepares/model_prepare.rb +52 -12
- data/lib/rails_best_practices/prepares/route_prepare.rb +16 -10
- data/lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb +15 -13
- data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +34 -29
- data/lib/rails_best_practices/reviews/check_destroy_return_value_review.rb +14 -5
- data/lib/rails_best_practices/reviews/check_save_return_value_review.rb +19 -8
- data/lib/rails_best_practices/reviews/hash_syntax_review.rb +5 -5
- data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +4 -4
- data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +7 -8
- data/lib/rails_best_practices/reviews/law_of_demeter_review.rb +6 -6
- data/lib/rails_best_practices/reviews/move_code_into_controller_review.rb +1 -1
- data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +6 -7
- data/lib/rails_best_practices/reviews/move_finder_to_named_scope_review.rb +7 -8
- data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +12 -10
- data/lib/rails_best_practices/reviews/not_use_default_route_review.rb +1 -2
- data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +5 -5
- data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +5 -2
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb +6 -3
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_helpers_review.rb +6 -4
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +29 -9
- data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +3 -3
- data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +17 -15
- data/lib/rails_best_practices/reviews/simplify_render_in_controllers_review.rb +1 -2
- data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +3 -3
- data/lib/rails_best_practices/reviews/use_before_filter_review.rb +2 -1
- data/lib/rails_best_practices/reviews/use_model_association_review.rb +5 -5
- data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +9 -8
- data/lib/rails_best_practices/reviews/use_observer_review.rb +9 -9
- data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +26 -26
- data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +8 -7
- data/lib/rails_best_practices/reviews/use_scope_access_review.rb +17 -15
- data/lib/rails_best_practices/reviews/use_turbo_sprockets_rails3_review.rb +2 -1
- data/lib/rails_best_practices/version.rb +1 -1
- data/rails_best_practices.gemspec +35 -36
- data/spec/fixtures/lib/rails_best_practices/plugins/reviews/not_use_rails_root_review.rb +1 -2
- data/spec/rails_best_practices/analyzer_spec.rb +73 -42
- data/spec/rails_best_practices/core/check_spec.rb +5 -5
- data/spec/rails_best_practices/core/checks_loader_spec.rb +3 -3
- data/spec/rails_best_practices/core/configs_spec.rb +1 -1
- data/spec/rails_best_practices/core/controllers_spec.rb +1 -1
- data/spec/rails_best_practices/core/error_spec.rb +21 -21
- data/spec/rails_best_practices/core/except_methods_spec.rb +7 -7
- data/spec/rails_best_practices/core/gems_spec.rb +4 -4
- data/spec/rails_best_practices/core/helpers_spec.rb +1 -1
- data/spec/rails_best_practices/core/klasses_spec.rb +3 -3
- data/spec/rails_best_practices/core/mailers_spec.rb +1 -1
- data/spec/rails_best_practices/core/methods_spec.rb +6 -6
- data/spec/rails_best_practices/core/model_associations_spec.rb +10 -6
- data/spec/rails_best_practices/core/model_attributes_spec.rb +4 -4
- data/spec/rails_best_practices/core/models_spec.rb +1 -1
- data/spec/rails_best_practices/core/modules_spec.rb +5 -5
- data/spec/rails_best_practices/core/routes_spec.rb +5 -5
- data/spec/rails_best_practices/core/runner_spec.rb +9 -7
- data/spec/rails_best_practices/core_ext/erubis_spec.rb +10 -10
- data/spec/rails_best_practices/lexicals/long_line_check_spec.rb +11 -10
- data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +6 -6
- data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +6 -6
- data/spec/rails_best_practices/prepares/config_prepare_spec.rb +2 -2
- data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +18 -10
- data/spec/rails_best_practices/prepares/gemfile_prepare_spec.rb +2 -2
- data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +3 -3
- data/spec/rails_best_practices/prepares/initializer_prepare_spec.rb +3 -3
- data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +2 -2
- data/spec/rails_best_practices/prepares/model_prepare_spec.rb +79 -43
- data/spec/rails_best_practices/prepares/route_prepare_spec.rb +138 -77
- data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +2 -2
- data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +18 -12
- data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +28 -22
- data/spec/rails_best_practices/reviews/check_destroy_return_value_review_spec.rb +15 -13
- data/spec/rails_best_practices/reviews/check_save_return_value_review_spec.rb +31 -21
- data/spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +5 -5
- data/spec/rails_best_practices/reviews/hash_syntax_review_spec.rb +9 -9
- data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/keep_finders_on_their_own_model_review_spec.rb +9 -9
- data/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb +21 -14
- data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +11 -6
- data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +26 -16
- data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +9 -7
- data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +9 -9
- data/spec/rails_best_practices/reviews/not_rescue_exception_review_spec.rb +9 -9
- data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +5 -5
- data/spec/rails_best_practices/reviews/not_use_time_ago_in_words_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +24 -19
- data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +44 -31
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +17 -12
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +46 -44
- data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +10 -8
- data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +16 -10
- data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +54 -31
- data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +9 -9
- data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +13 -13
- data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +11 -9
- data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +21 -17
- data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/use_parentheses_in_method_def_review_spec.rb +9 -7
- data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +31 -24
- data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +15 -11
- data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +14 -14
- data/spec/rails_best_practices/reviews/use_turbo_sprockets_rails3_review_spec.rb +10 -8
- metadata +7 -7
@@ -20,7 +20,7 @@ module RailsBestPractices
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def short_filename
|
23
|
-
File.expand_path(filename)[File.expand_path(Core::Runner.base_path).size..-1].sub(
|
23
|
+
File.expand_path(filename)[File.expand_path(Core::Runner.base_path).size..-1].sub(%r{^/}, '')
|
24
24
|
end
|
25
25
|
|
26
26
|
def first_line_number
|
@@ -46,7 +46,9 @@ module RailsBestPractices
|
|
46
46
|
# @return [Boolean] has a method or not
|
47
47
|
def has_method?(class_name, method_name, access_control = nil)
|
48
48
|
if access_control
|
49
|
-
!!methods(class_name).find
|
49
|
+
!!methods(class_name).find do |method|
|
50
|
+
method.method_name == method_name && method.access_control == access_control
|
51
|
+
end
|
50
52
|
else
|
51
53
|
!!methods(class_name).find { |method| method.method_name == method_name }
|
52
54
|
end
|
@@ -58,10 +60,10 @@ module RailsBestPractices
|
|
58
60
|
# @param [String] method name
|
59
61
|
def mark_parent_class_method_used(class_name, method_name)
|
60
62
|
klass = Prepares.klasses.find { |klass| klass.to_s == class_name }
|
61
|
-
if klass
|
63
|
+
if klass&.extend_class_name
|
62
64
|
mark_parent_class_method_used(klass.extend_class_name, method_name)
|
63
65
|
method = get_method(klass.extend_class_name, method_name)
|
64
|
-
method
|
66
|
+
method&.mark_used
|
65
67
|
end
|
66
68
|
end
|
67
69
|
|
@@ -73,7 +75,7 @@ module RailsBestPractices
|
|
73
75
|
Prepares.klasses.select { |klass| klass.extend_class_name == class_name }.each do |klass|
|
74
76
|
mark_subclasses_method_used(klass.to_s, method_name)
|
75
77
|
method = get_method(klass.to_s, method_name)
|
76
|
-
method
|
78
|
+
method&.mark_used
|
77
79
|
end
|
78
80
|
end
|
79
81
|
|
@@ -83,7 +85,7 @@ module RailsBestPractices
|
|
83
85
|
# @param [String] method name
|
84
86
|
def mark_publicize(class_name, method_name)
|
85
87
|
method = get_method(class_name, method_name)
|
86
|
-
method
|
88
|
+
method&.publicize
|
87
89
|
end
|
88
90
|
|
89
91
|
# Mark parent classs' method as public.
|
@@ -92,7 +94,7 @@ module RailsBestPractices
|
|
92
94
|
# @param [String] method name
|
93
95
|
def mark_parent_class_methods_publicize(class_name, method_name)
|
94
96
|
klass = Prepares.klasses.find { |klass| klass.to_s == class_name }
|
95
|
-
if klass
|
97
|
+
if klass&.extend_class_name
|
96
98
|
mark_parent_class_methods_publicize(klass.extend_class_name, method_name)
|
97
99
|
mark_publicize(class_name, method_name)
|
98
100
|
end
|
@@ -113,7 +115,9 @@ module RailsBestPractices
|
|
113
115
|
# @return [Method] Method object
|
114
116
|
def get_method(class_name, method_name, access_control = nil)
|
115
117
|
if access_control
|
116
|
-
methods(class_name).find
|
118
|
+
methods(class_name).find do |method|
|
119
|
+
method.method_name == method_name && method.access_control == access_control
|
120
|
+
end
|
117
121
|
else
|
118
122
|
methods(class_name).find { |method| method.method_name == method_name }
|
119
123
|
end
|
@@ -125,20 +129,21 @@ module RailsBestPractices
|
|
125
129
|
# @return [Array] array of Method
|
126
130
|
def get_all_unused_methods(access_control = nil)
|
127
131
|
@methods.inject([]) do |unused_methods, (_class_name, methods)|
|
128
|
-
unused_methods +=
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
132
|
+
unused_methods +=
|
133
|
+
if access_control
|
134
|
+
methods.select { |method| method.access_control == access_control && !method.used }
|
135
|
+
else
|
136
|
+
methods.reject(&:used)
|
137
|
+
end
|
133
138
|
end.reject { |method| method.access_control == 'public' && @possible_methods[method.method_name] }
|
134
139
|
end
|
135
140
|
|
136
141
|
private
|
137
142
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
143
|
+
# Methods of a class.
|
144
|
+
#
|
145
|
+
# @param [String] class name
|
146
|
+
# @return [Array] array of methods
|
142
147
|
def methods(class_name)
|
143
148
|
@methods[class_name] ||= []
|
144
149
|
end
|
@@ -15,7 +15,9 @@ module RailsBestPractices
|
|
15
15
|
# @param [String] association class name
|
16
16
|
def add_association(model_name, association_name, association_meta, association_class = nil)
|
17
17
|
@associations[model_name] ||= {}
|
18
|
-
@associations[model_name][association_name] = {
|
18
|
+
@associations[model_name][association_name] = {
|
19
|
+
'meta' => association_meta, 'class_name' => association_class || association_name.classify
|
20
|
+
}
|
19
21
|
end
|
20
22
|
|
21
23
|
# Get a model association.
|
@@ -25,7 +27,7 @@ module RailsBestPractices
|
|
25
27
|
# @return [Hash] {"meta" => association_meta, "class_name" => association_class}
|
26
28
|
def get_association(model_name, association_name)
|
27
29
|
associations = @associations[model_name]
|
28
|
-
associations
|
30
|
+
associations && associations[association_name]
|
29
31
|
end
|
30
32
|
|
31
33
|
# If it is a model's association.
|
@@ -49,8 +51,11 @@ module RailsBestPractices
|
|
49
51
|
# @param [String] association_name
|
50
52
|
# @return [String] association's class name
|
51
53
|
def get_association_class_name(table_name, association_name)
|
52
|
-
|
53
|
-
|
54
|
+
(
|
55
|
+
associations =
|
56
|
+
@associations.select { |model, _model_associations| model.gsub('::', '').tableize == table_name }.values
|
57
|
+
.first
|
58
|
+
) && (association_meta = associations.select { |name, _meta| name == association_name }.values.first) &&
|
54
59
|
association_meta['class_name']
|
55
60
|
end
|
56
61
|
end
|
@@ -24,8 +24,8 @@ module RailsBestPractices
|
|
24
24
|
# mappings can be specified by e.g.
|
25
25
|
# post 'some/:pattern' => 'controller#action'
|
26
26
|
if action_name.is_a?(String) && action_name =~ /\A(\w+)#(\w+)\z/
|
27
|
-
controller_name =
|
28
|
-
action_name =
|
27
|
+
controller_name = Regexp.last_match(1)
|
28
|
+
action_name = Regexp.last_match(2)
|
29
29
|
end
|
30
30
|
|
31
31
|
if controller_name
|
@@ -20,33 +20,23 @@ module RailsBestPractices
|
|
20
20
|
class Runner
|
21
21
|
attr_reader :checks
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
# @param [String] path the base path
|
26
|
-
def self.base_path=(path)
|
27
|
-
@base_path = path
|
28
|
-
end
|
23
|
+
class << self
|
24
|
+
attr_writer :base_path, :config_path
|
29
25
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
# set the configuration path
|
38
|
-
#
|
39
|
-
# @param path [String] path to rbc config file
|
40
|
-
def self.config_path=(path)
|
41
|
-
@config_path = path
|
42
|
-
end
|
26
|
+
# get the base path, by default, the base path is current path.
|
27
|
+
#
|
28
|
+
# @return [String] the base path
|
29
|
+
def base_path
|
30
|
+
@base_path || '.'
|
31
|
+
end
|
43
32
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
33
|
+
# get the configuration path, if will default to config/rails_best_practices.yml
|
34
|
+
#
|
35
|
+
# @return [String] the config path
|
36
|
+
def config_path
|
37
|
+
custom_config = @config_path || File.join(Runner.base_path, 'config/rails_best_practices.yml')
|
38
|
+
File.exist?(custom_config) ? custom_config : RailsBestPractices::Analyzer::DEFAULT_CONFIG
|
39
|
+
end
|
50
40
|
end
|
51
41
|
|
52
42
|
# initialize the runner.
|
@@ -66,8 +56,14 @@ module RailsBestPractices
|
|
66
56
|
load_plugin_reviews if reviews.empty?
|
67
57
|
|
68
58
|
@lexical_checker ||= CodeAnalyzer::CheckingVisitor::Plain.new(checkers: @lexicals)
|
69
|
-
@plain_prepare_checker ||=
|
70
|
-
|
59
|
+
@plain_prepare_checker ||=
|
60
|
+
CodeAnalyzer::CheckingVisitor::Plain.new(
|
61
|
+
checkers: @prepares.select { |checker| checker.is_a? Prepares::GemfilePrepare }
|
62
|
+
)
|
63
|
+
@default_prepare_checker ||=
|
64
|
+
CodeAnalyzer::CheckingVisitor::Default.new(
|
65
|
+
checkers: @prepares.reject { |checker| checker.is_a? Prepares::GemfilePrepare }
|
66
|
+
)
|
71
67
|
@review_checker ||= CodeAnalyzer::CheckingVisitor::Default.new(checkers: @reviews)
|
72
68
|
end
|
73
69
|
|
@@ -119,10 +115,10 @@ module RailsBestPractices
|
|
119
115
|
|
120
116
|
private
|
121
117
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
118
|
+
# parse html template code, erb, haml and slim.
|
119
|
+
#
|
120
|
+
# @param [String] filename is the filename of the erb, haml or slim code.
|
121
|
+
# @param [String] content is the source code of erb, haml or slim file.
|
126
122
|
def parse_html_template(filename, content)
|
127
123
|
if filename =~ /.*\.erb$|.*\.rhtml$/
|
128
124
|
content = Erubis::OnlyRuby.new(content).src
|
@@ -150,12 +146,12 @@ module RailsBestPractices
|
|
150
146
|
content
|
151
147
|
end
|
152
148
|
|
153
|
-
|
149
|
+
# load all prepares.
|
154
150
|
def load_prepares
|
155
151
|
Prepares.constants.map { |prepare| Prepares.const_get(prepare).new }
|
156
152
|
end
|
157
153
|
|
158
|
-
|
154
|
+
# load all plugin reviews.
|
159
155
|
def load_plugin_reviews
|
160
156
|
plugins = File.join(Runner.base_path, 'lib', 'rails_best_practices', 'plugins', 'reviews')
|
161
157
|
if File.directory?(plugins)
|
@@ -22,9 +22,13 @@ module RailsBestPractices
|
|
22
22
|
content.each_line do |line|
|
23
23
|
line_no += 1
|
24
24
|
actual_line_length = line.sub(/\s+$/, '').length
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
next unless actual_line_length > @max_line_length
|
26
|
+
|
27
|
+
add_error(
|
28
|
+
"line is longer than #{@max_line_length} characters (#{actual_line_length} characters)",
|
29
|
+
filename,
|
30
|
+
line_no
|
31
|
+
)
|
28
32
|
end
|
29
33
|
end
|
30
34
|
end
|
@@ -7,7 +7,7 @@ module RailsBestPractices
|
|
7
7
|
# Usage: rails_best_practices [options] path
|
8
8
|
# -d, --debug debug mode
|
9
9
|
# --silent silent mode
|
10
|
-
# -f, --format FORMAT output format (text, html,
|
10
|
+
# -f, --format FORMAT output format (text, html, yaml, json, xml)
|
11
11
|
# --output-file FILE output html file for the analyzing result
|
12
12
|
# --without-color only output plain text without color
|
13
13
|
# --with-textmate open file by textmate in html format
|
@@ -41,7 +41,7 @@ module RailsBestPractices
|
|
41
41
|
options['debug'] = true
|
42
42
|
end
|
43
43
|
|
44
|
-
opts.on('-f', '--format FORMAT', 'output format (text, html,
|
44
|
+
opts.on('-f', '--format FORMAT', 'output format (text, html, yaml, json, xml)') do |format|
|
45
45
|
options['format'] = format
|
46
46
|
end
|
47
47
|
|
@@ -111,7 +111,12 @@ module RailsBestPractices
|
|
111
111
|
exit
|
112
112
|
end
|
113
113
|
|
114
|
-
opts.on(
|
114
|
+
opts.on(
|
115
|
+
'-x',
|
116
|
+
'--exclude PATTERNS',
|
117
|
+
"Don't analyze files matching a pattern",
|
118
|
+
'(comma-separated regexp list)'
|
119
|
+
) do |list|
|
115
120
|
begin
|
116
121
|
options['exclude'] = list.split(',').map { |x| Regexp.new x }
|
117
122
|
rescue RegexpError => e
|
@@ -119,11 +124,16 @@ module RailsBestPractices
|
|
119
124
|
end
|
120
125
|
end
|
121
126
|
|
122
|
-
opts.on(
|
127
|
+
opts.on(
|
128
|
+
'-o',
|
129
|
+
'--only PATTERNS',
|
130
|
+
'Analyze files only matching a pattern',
|
131
|
+
'(comma-separated regexp list)'
|
132
|
+
) do |list|
|
123
133
|
begin
|
124
134
|
options['only'] = list.split(',').map { |x| Regexp.new x }
|
125
135
|
rescue RegexpError => e
|
126
|
-
raise OptionParser::InvalidArgument e.message
|
136
|
+
raise OptionParser::InvalidArgument, e.message
|
127
137
|
end
|
128
138
|
end
|
129
139
|
|
@@ -133,7 +143,8 @@ module RailsBestPractices
|
|
133
143
|
|
134
144
|
opts.on(
|
135
145
|
'-c',
|
136
|
-
'--config CONFIG_PATH',
|
146
|
+
'--config CONFIG_PATH',
|
147
|
+
'configuration file location (defaults to config/rails_best_practices.yml)'
|
137
148
|
) do |config_path|
|
138
149
|
options['config'] = config_path
|
139
150
|
end
|
@@ -3,6 +3,7 @@
|
|
3
3
|
module RailsBestPractices
|
4
4
|
module Prepares
|
5
5
|
# Remember controllers and controller methods
|
6
|
+
|
6
7
|
class ControllerPrepare < Core::Check
|
7
8
|
include Core::Check::Classable
|
8
9
|
include Core::Check::InheritedResourcesable
|
@@ -32,7 +33,11 @@ module RailsBestPractices
|
|
32
33
|
add_callback :end_class do |node|
|
33
34
|
if @inherited_resources && @current_controller_name != 'ApplicationController'
|
34
35
|
@actions.each do |action|
|
35
|
-
@methods.add_method(
|
36
|
+
@methods.add_method(
|
37
|
+
@current_controller_name,
|
38
|
+
action,
|
39
|
+
'file' => node.file, 'line_number' => node.line_number
|
40
|
+
)
|
36
41
|
end
|
37
42
|
end
|
38
43
|
end
|
@@ -78,14 +83,21 @@ module RailsBestPractices
|
|
78
83
|
# }
|
79
84
|
add_callback :start_def do |node|
|
80
85
|
method_name = node.method_name.to_s
|
81
|
-
@methods.add_method(
|
86
|
+
@methods.add_method(
|
87
|
+
current_class_name,
|
88
|
+
method_name,
|
89
|
+
{ 'file' => node.file, 'line_number' => node.line_number },
|
90
|
+
current_access_control
|
91
|
+
)
|
82
92
|
end
|
83
93
|
|
84
94
|
# ask Reviews::RemoveUnusedMoethodsInHelperReview to check the controllers who include helpers.
|
85
95
|
add_callback :after_check do
|
86
96
|
descendants = @helpers.map(&:descendants).flatten
|
87
97
|
if descendants.present?
|
88
|
-
Reviews::RemoveUnusedMethodsInHelpersReview.interesting_files *descendants.map { |descendant|
|
98
|
+
Reviews::RemoveUnusedMethodsInHelpersReview.interesting_files *descendants.map { |descendant|
|
99
|
+
/#{descendant.underscore}/
|
100
|
+
}
|
89
101
|
end
|
90
102
|
end
|
91
103
|
end
|
@@ -32,7 +32,12 @@ module RailsBestPractices
|
|
32
32
|
add_callback :start_def do |node|
|
33
33
|
if node.file =~ HELPER_FILES
|
34
34
|
method_name = node.method_name.to_s
|
35
|
-
@methods.add_method(
|
35
|
+
@methods.add_method(
|
36
|
+
current_module_name,
|
37
|
+
method_name,
|
38
|
+
{ 'file' => node.file, 'line_number' => node.line_number },
|
39
|
+
current_access_control
|
40
|
+
)
|
36
41
|
end
|
37
42
|
end
|
38
43
|
end
|