rails_best_practices 1.19.0 → 1.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Guardfile +2 -2
- data/Rakefile +8 -8
- data/bin/rails_best_practices +1 -1
- data/lib/rails_best_practices/analyzer.rb +45 -45
- data/lib/rails_best_practices/command.rb +40 -40
- data/lib/rails_best_practices/core/check.rb +15 -15
- data/lib/rails_best_practices/core/klasses.rb +1 -1
- data/lib/rails_best_practices/core/methods.rb +7 -7
- data/lib/rails_best_practices/core/model_associations.rb +3 -3
- 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 +3 -3
- data/lib/rails_best_practices/core_ext/erubis.rb +4 -4
- data/lib/rails_best_practices/lexicals/remove_tab_check.rb +2 -2
- data/lib/rails_best_practices/lexicals/remove_trailing_whitespace_check.rb +2 -2
- data/lib/rails_best_practices/prepares/config_prepare.rb +1 -1
- data/lib/rails_best_practices/prepares/controller_prepare.rb +8 -8
- data/lib/rails_best_practices/prepares/helper_prepare.rb +1 -1
- data/lib/rails_best_practices/prepares/initializer_prepare.rb +4 -4
- data/lib/rails_best_practices/prepares/mailer_prepare.rb +1 -1
- data/lib/rails_best_practices/prepares/model_prepare.rb +19 -19
- data/lib/rails_best_practices/prepares/route_prepare.rb +34 -34
- data/lib/rails_best_practices/prepares/schema_prepare.rb +1 -1
- data/lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb +2 -2
- data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +7 -7
- data/lib/rails_best_practices/reviews/check_save_return_value_review.rb +1 -1
- data/lib/rails_best_practices/reviews/default_scope_is_evil_review.rb +3 -3
- data/lib/rails_best_practices/reviews/dry_bundler_in_capistrano_review.rb +3 -3
- data/lib/rails_best_practices/reviews/hash_syntax_review.rb +1 -1
- data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +6 -6
- data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +2 -2
- data/lib/rails_best_practices/reviews/law_of_demeter_review.rb +4 -4
- data/lib/rails_best_practices/reviews/move_code_into_controller_review.rb +3 -3
- data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +2 -2
- data/lib/rails_best_practices/reviews/move_code_into_model_review.rb +1 -1
- data/lib/rails_best_practices/reviews/move_finder_to_named_scope_review.rb +2 -2
- data/lib/rails_best_practices/reviews/move_model_logic_into_model_review.rb +1 -1
- data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +4 -4
- data/lib/rails_best_practices/reviews/not_rescue_exception_review.rb +2 -2
- data/lib/rails_best_practices/reviews/not_use_default_route_review.rb +4 -4
- data/lib/rails_best_practices/reviews/not_use_time_ago_in_words_review.rb +3 -3
- data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +2 -2
- data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +9 -9
- data/lib/rails_best_practices/reviews/remove_empty_helpers_review.rb +3 -3
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb +11 -11
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_helpers_review.rb +1 -1
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +8 -7
- 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 -2
- data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +22 -22
- data/lib/rails_best_practices/reviews/review.rb +2 -2
- data/lib/rails_best_practices/reviews/simplify_render_in_controllers_review.rb +3 -3
- data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +4 -4
- data/lib/rails_best_practices/reviews/use_before_filter_review.rb +2 -2
- 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 +2 -2
- data/lib/rails_best_practices/reviews/use_observer_review.rb +3 -3
- data/lib/rails_best_practices/reviews/use_parentheses_in_method_def_review.rb +1 -1
- data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +3 -3
- data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +2 -2
- data/lib/rails_best_practices/reviews/use_scope_access_review.rb +5 -5
- data/lib/rails_best_practices/reviews/use_turbo_sprockets_rails3_review.rb +4 -4
- data/lib/rails_best_practices/version.rb +1 -1
- data/rails_best_practices.gemspec +24 -24
- data/spec/rails_best_practices/analyzer_spec.rb +43 -43
- data/spec/rails_best_practices/core/check_spec.rb +2 -2
- data/spec/rails_best_practices/core/checks_loader_spec.rb +4 -4
- data/spec/rails_best_practices/core/error_spec.rb +16 -16
- data/spec/rails_best_practices/core/except_methods_spec.rb +15 -15
- data/spec/rails_best_practices/core/gems_spec.rb +9 -9
- data/spec/rails_best_practices/core/klasses_spec.rb +17 -17
- data/spec/rails_best_practices/core/methods_spec.rb +28 -28
- data/spec/rails_best_practices/core/model_associations_spec.rb +10 -10
- data/spec/rails_best_practices/core/model_attributes_spec.rb +10 -10
- data/spec/rails_best_practices/core/modules_spec.rb +10 -10
- data/spec/rails_best_practices/core/routes_spec.rb +10 -10
- data/spec/rails_best_practices/core/runner_spec.rb +4 -4
- data/spec/rails_best_practices/core_ext/erubis_spec.rb +7 -7
- data/spec/rails_best_practices/lexicals/long_line_check_spec.rb +7 -7
- 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 +3 -3
- data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +26 -26
- data/spec/rails_best_practices/prepares/gemfile_prepare_spec.rb +3 -3
- data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +6 -6
- data/spec/rails_best_practices/prepares/initializer_prepare_spec.rb +5 -5
- data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +2 -2
- data/spec/rails_best_practices/prepares/model_prepare_spec.rb +108 -108
- data/spec/rails_best_practices/prepares/route_prepare_spec.rb +77 -77
- data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +8 -8
- data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +10 -10
- data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +27 -27
- data/spec/rails_best_practices/reviews/check_destroy_return_value_review_spec.rb +11 -11
- data/spec/rails_best_practices/reviews/check_save_return_value_review_spec.rb +17 -17
- 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 +4 -4
- data/spec/rails_best_practices/reviews/hash_syntax_review_spec.rb +8 -8
- data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +10 -10
- 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 +16 -16
- 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 +4 -4
- data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +13 -13
- 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 +5 -5
- data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +10 -10
- data/spec/rails_best_practices/reviews/not_rescue_exception_review_spec.rb +8 -8
- data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +4 -4
- data/spec/rails_best_practices/reviews/not_use_time_ago_in_words_review_spec.rb +11 -11
- 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 +16 -16
- data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +5 -5
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +33 -33
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +106 -106
- data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +5 -5
- data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +8 -8
- data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +29 -29
- data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +15 -15
- data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +15 -15
- data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +7 -7
- 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 +29 -29
- 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 +5 -5
- data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +28 -28
- data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +9 -9
- data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +23 -23
- data/spec/rails_best_practices/reviews/use_turbo_sprockets_rails3_review_spec.rb +6 -6
- data/spec/spec_helper.rb +1 -1
- metadata +7 -7
@@ -18,7 +18,7 @@ module RailsBestPractices
|
|
18
18
|
|
19
19
|
def initialize(class_name, extend_class_name, modules)
|
20
20
|
@modules = modules.dup
|
21
|
-
base = @modules.map { |modu| "#{modu}::" }.join(
|
21
|
+
base = @modules.map { |modu| "#{modu}::" }.join('')
|
22
22
|
@class_name = base + class_name
|
23
23
|
if extend_class_name
|
24
24
|
@extend_class_name = base + extend_class_name
|
@@ -14,11 +14,11 @@ module RailsBestPractices
|
|
14
14
|
# @param [String] method name
|
15
15
|
# @param [Hash] method meta, file and line, {"file" => "app/models/post.rb", "line_number" => 5}
|
16
16
|
# @param [String] access control, public, protected or private
|
17
|
-
def add_method(class_name, method_name, meta={}, access_control=
|
18
|
-
return if class_name ==
|
17
|
+
def add_method(class_name, method_name, meta={}, access_control='public')
|
18
|
+
return if class_name == ''
|
19
19
|
return if has_method?(class_name, method_name)
|
20
20
|
methods(class_name) << Method.new(class_name, method_name, access_control, meta)
|
21
|
-
if access_control ==
|
21
|
+
if access_control == 'public'
|
22
22
|
@possible_methods[method_name] = false
|
23
23
|
end
|
24
24
|
end
|
@@ -128,7 +128,7 @@ module RailsBestPractices
|
|
128
128
|
else
|
129
129
|
methods.select { |method| !method.used }
|
130
130
|
end
|
131
|
-
}.reject { |method| method.access_control ==
|
131
|
+
}.reject { |method| method.access_control == 'public' && @possible_methods[method.method_name] }
|
132
132
|
end
|
133
133
|
|
134
134
|
private
|
@@ -149,8 +149,8 @@ module RailsBestPractices
|
|
149
149
|
def initialize(class_name, method_name, access_control, meta)
|
150
150
|
@class_name = class_name
|
151
151
|
@method_name = method_name
|
152
|
-
@file = meta[
|
153
|
-
@line_number = meta[
|
152
|
+
@file = meta['file']
|
153
|
+
@line_number = meta['line_number']
|
154
154
|
@access_control = access_control
|
155
155
|
@used = false
|
156
156
|
end
|
@@ -162,7 +162,7 @@ module RailsBestPractices
|
|
162
162
|
|
163
163
|
# Mark the method as public
|
164
164
|
def publicize
|
165
|
-
@access_control =
|
165
|
+
@access_control = 'public'
|
166
166
|
end
|
167
167
|
|
168
168
|
end
|
@@ -14,7 +14,7 @@ module RailsBestPractices
|
|
14
14
|
# @param [String] association class name
|
15
15
|
def add_association(model_name, association_name, association_meta, association_class=nil)
|
16
16
|
@associations[model_name] ||= {}
|
17
|
-
@associations[model_name][association_name] = {
|
17
|
+
@associations[model_name][association_name] = {'meta' => association_meta, 'class_name' => association_class || association_name.classify}
|
18
18
|
end
|
19
19
|
|
20
20
|
# Get a model association.
|
@@ -48,9 +48,9 @@ module RailsBestPractices
|
|
48
48
|
# @param [String] association_name
|
49
49
|
# @return [String] association's class name
|
50
50
|
def get_association_class_name(table_name, association_name)
|
51
|
-
associations = @associations.select { |model, model_associations| model.gsub(
|
51
|
+
associations = @associations.select { |model, model_associations| model.gsub('::', '').tableize == table_name }.values.first and
|
52
52
|
association_meta = associations.select { |name, meta| name == association_name }.values.first and
|
53
|
-
association_meta[
|
53
|
+
association_meta['class_name']
|
54
54
|
end
|
55
55
|
end
|
56
56
|
end
|
@@ -37,9 +37,9 @@ module RailsBestPractices
|
|
37
37
|
|
38
38
|
def controller_name_with_namespaces
|
39
39
|
if controller_name
|
40
|
-
namespaces.map { |namespace| "#{namespace.camelize}::" }.join(
|
40
|
+
namespaces.map { |namespace| "#{namespace.camelize}::" }.join('') + "#{controller_name.camelize}Controller"
|
41
41
|
else
|
42
|
-
namespaces.map
|
42
|
+
namespaces.map(&:camelize).join('::') + 'Controller'
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
@@ -30,7 +30,7 @@ module RailsBestPractices
|
|
30
30
|
#
|
31
31
|
# @return [String] the base path
|
32
32
|
def self.base_path
|
33
|
-
@base_path ||
|
33
|
+
@base_path || '.'
|
34
34
|
end
|
35
35
|
|
36
36
|
# set the configuration path
|
@@ -113,7 +113,7 @@ module RailsBestPractices
|
|
113
113
|
#
|
114
114
|
# @return [Array] all errors from lexicals and reviews
|
115
115
|
def errors
|
116
|
-
@errors ||= (@reviews + @lexicals).collect
|
116
|
+
@errors ||= (@reviews + @lexicals).collect(&:errors).flatten
|
117
117
|
end
|
118
118
|
|
119
119
|
private
|
@@ -159,7 +159,7 @@ module RailsBestPractices
|
|
159
159
|
begin
|
160
160
|
plugins = File.join(Runner.base_path, 'lib', 'rails_best_practices', 'plugins', 'reviews')
|
161
161
|
if File.directory?(plugins)
|
162
|
-
Dir[File.expand_path(File.join(plugins,
|
162
|
+
Dir[File.expand_path(File.join(plugins, '*.rb'))].each do |review|
|
163
163
|
require review
|
164
164
|
end
|
165
165
|
if RailsBestPractices.constants.map(&:to_sym).include? :Plugins
|
@@ -12,22 +12,22 @@ module Erubis
|
|
12
12
|
|
13
13
|
def add_stmt(src, code)
|
14
14
|
src << code
|
15
|
-
src <<
|
15
|
+
src << ';'
|
16
16
|
end
|
17
17
|
|
18
18
|
def add_expr_literal(src, code)
|
19
19
|
src << code
|
20
|
-
src <<
|
20
|
+
src << ';'
|
21
21
|
end
|
22
22
|
|
23
23
|
def add_expr_escaped(src, code)
|
24
24
|
src << code
|
25
|
-
src <<
|
25
|
+
src << ';'
|
26
26
|
end
|
27
27
|
|
28
28
|
def add_expr_debug(src, code)
|
29
29
|
src << code
|
30
|
-
src <<
|
30
|
+
src << ';'
|
31
31
|
end
|
32
32
|
|
33
33
|
def add_postamble(src)
|
@@ -6,7 +6,7 @@ module RailsBestPractices
|
|
6
6
|
# See the best practice details here https://rails-bestpractices.com/posts/2011/07/04/remove-tab/
|
7
7
|
class RemoveTabCheck < Core::Check
|
8
8
|
interesting_files ALL_FILES
|
9
|
-
url
|
9
|
+
url 'https://rails-bestpractices.com/posts/2011/07/04/remove-tab/'
|
10
10
|
|
11
11
|
# check if the content of file contains a tab.
|
12
12
|
#
|
@@ -15,7 +15,7 @@ module RailsBestPractices
|
|
15
15
|
def check(filename, content)
|
16
16
|
if content =~ /\t/m
|
17
17
|
line_no = $`.count("\n") + 1
|
18
|
-
add_error(
|
18
|
+
add_error('remove tab, use spaces instead', filename, line_no)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
@@ -6,7 +6,7 @@ module RailsBestPractices
|
|
6
6
|
# See the best practice details here https://rails-bestpractices.com/posts/2010/12/02/remove-trailing-whitespace/
|
7
7
|
class RemoveTrailingWhitespaceCheck < Core::Check
|
8
8
|
interesting_files ALL_FILES
|
9
|
-
url
|
9
|
+
url 'https://rails-bestpractices.com/posts/2010/12/02/remove-trailing-whitespace/'
|
10
10
|
|
11
11
|
# check if the content of file contain a trailing whitespace.
|
12
12
|
#
|
@@ -15,7 +15,7 @@ module RailsBestPractices
|
|
15
15
|
def check(filename, content)
|
16
16
|
if content =~ / +\n/m
|
17
17
|
line_no = $`.count("\n") + 1
|
18
|
-
add_error(
|
18
|
+
add_error('remove trailing whitespace', filename, line_no)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
@@ -12,7 +12,7 @@ module RailsBestPractices
|
|
12
12
|
|
13
13
|
# check assignments to config
|
14
14
|
add_callback :start_assign do |node|
|
15
|
-
if node.left_value.grep_node(sexp_type: [:vcall, :var_ref], to_s:
|
15
|
+
if node.left_value.grep_node(sexp_type: [:vcall, :var_ref], to_s: 'config').present?
|
16
16
|
@configs[node.left_value.to_s] = node.right_value.to_s
|
17
17
|
end
|
18
18
|
end
|
@@ -30,9 +30,9 @@ module RailsBestPractices
|
|
30
30
|
|
31
31
|
# remember the action names at the end of class node if the controller is a InheritedResources.
|
32
32
|
add_callback :end_class do |node|
|
33
|
-
if @inherited_resources &&
|
33
|
+
if @inherited_resources && 'ApplicationController' != current_class_name
|
34
34
|
@actions.each do |action|
|
35
|
-
@methods.add_method(current_class_name, action, {
|
35
|
+
@methods.add_method(current_class_name, action, {'file' => node.file, 'line_number' => node.line_number})
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
@@ -53,14 +53,14 @@ module RailsBestPractices
|
|
53
53
|
|
54
54
|
# restrict actions for inherited_resources
|
55
55
|
add_callback :start_command do |node|
|
56
|
-
if
|
56
|
+
if 'include' == node.message.to_s
|
57
57
|
@helpers.add_module_descendant(node.arguments.all.first.to_s, current_class_name)
|
58
|
-
elsif @inherited_resources &&
|
59
|
-
if
|
58
|
+
elsif @inherited_resources && 'actions' == node.message.to_s
|
59
|
+
if 'all' == node.arguments.all.first.to_s
|
60
60
|
@actions = DEFAULT_ACTIONS
|
61
61
|
option_argument = node.arguments.all[1]
|
62
|
-
if option_argument && :bare_assoc_hash == option_argument.sexp_type && option_argument.hash_value(
|
63
|
-
@actions -= option_argument.hash_value(
|
62
|
+
if option_argument && :bare_assoc_hash == option_argument.sexp_type && option_argument.hash_value('except')
|
63
|
+
@actions -= option_argument.hash_value('except').to_object
|
64
64
|
end
|
65
65
|
else
|
66
66
|
@actions = node.arguments.all.map(&:to_s)
|
@@ -82,7 +82,7 @@ module RailsBestPractices
|
|
82
82
|
# }
|
83
83
|
add_callback :start_def do |node|
|
84
84
|
method_name = node.method_name.to_s
|
85
|
-
@methods.add_method(current_class_name, method_name, {
|
85
|
+
@methods.add_method(current_class_name, method_name, {'file' => node.file, 'line_number' => node.line_number}, current_access_control)
|
86
86
|
end
|
87
87
|
|
88
88
|
# ask Reviews::RemoveUnusedMoethodsInHelperReview to check the controllers who include helpers.
|
@@ -31,7 +31,7 @@ module RailsBestPractices
|
|
31
31
|
add_callback :start_def do |node|
|
32
32
|
if node.file =~ HELPER_FILES
|
33
33
|
method_name = node.method_name.to_s
|
34
|
-
@methods.add_method(current_module_name, method_name, {
|
34
|
+
@methods.add_method(current_module_name, method_name, {'file' => node.file, 'line_number' => node.line_number}, current_access_control)
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
@@ -13,16 +13,16 @@ module RailsBestPractices
|
|
13
13
|
# check if AR include ActiveModel::ForbiddenAttributesProtection
|
14
14
|
add_callback :start_method_add_arg do |node|
|
15
15
|
if include_forbidden_attributes_protection?(node)
|
16
|
-
@configs[
|
16
|
+
@configs['railsbp.include_forbidden_attributes_protection'] = 'true'
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
20
|
# check if the node is
|
21
21
|
# ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection)
|
22
22
|
def include_forbidden_attributes_protection?(node)
|
23
|
-
|
24
|
-
|
25
|
-
[
|
23
|
+
'ActiveRecord::Base' == node.receiver.to_s &&
|
24
|
+
'send' == node.message.to_s &&
|
25
|
+
['include', 'ActiveModel::ForbiddenAttributesProtection'] == node.arguments.all.map(&:to_s)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
@@ -17,7 +17,7 @@ module RailsBestPractices
|
|
17
17
|
# if it is a subclass of ActionMailer::Base,
|
18
18
|
# then remember its class name.
|
19
19
|
add_callback :start_class do |node|
|
20
|
-
if
|
20
|
+
if 'ActionMailer::Base' == current_extend_class_name
|
21
21
|
@mailers << @klass
|
22
22
|
end
|
23
23
|
end
|
@@ -20,7 +20,7 @@ module RailsBestPractices
|
|
20
20
|
|
21
21
|
# remember the class name.
|
22
22
|
add_callback :start_class do |node|
|
23
|
-
if
|
23
|
+
if 'ActionMailer::Base' != current_extend_class_name
|
24
24
|
@models << @klass
|
25
25
|
end
|
26
26
|
end
|
@@ -39,10 +39,10 @@ module RailsBestPractices
|
|
39
39
|
# }
|
40
40
|
add_callback :start_def do |node|
|
41
41
|
if @klass &&
|
42
|
-
|
42
|
+
'ActionMailer::Base' != current_extend_class_name &&
|
43
43
|
(classable_modules.empty? || klasses.any?)
|
44
44
|
method_name = node.method_name.to_s
|
45
|
-
@methods.add_method(current_class_name, method_name, {
|
45
|
+
@methods.add_method(current_class_name, method_name, {'file' => node.file, 'line_number' => node.line_number}, current_access_control)
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
@@ -59,9 +59,9 @@ module RailsBestPractices
|
|
59
59
|
# }
|
60
60
|
# }
|
61
61
|
add_callback :start_defs do |node|
|
62
|
-
if @klass &&
|
62
|
+
if @klass && 'ActionMailer::Base' != current_extend_class_name
|
63
63
|
method_name = node.method_name.to_s
|
64
|
-
@methods.add_method(current_class_name, method_name, {
|
64
|
+
@methods.add_method(current_class_name, method_name, {'file' => node.file, 'line_number' => node.line_number}, current_access_control)
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
@@ -80,17 +80,17 @@ module RailsBestPractices
|
|
80
80
|
case node.message.to_s
|
81
81
|
when *%w(named_scope scope alias_method)
|
82
82
|
method_name = node.arguments.all.first.to_s
|
83
|
-
@methods.add_method(current_class_name, method_name, {
|
84
|
-
when
|
83
|
+
@methods.add_method(current_class_name, method_name, {'file' => node.file, 'line_number' => node.line_number}, current_access_control)
|
84
|
+
when 'alias_method_chain'
|
85
85
|
method, feature = *node.arguments.all.map(&:to_s)
|
86
|
-
@methods.add_method(current_class_name, "#{method}_with_#{feature}", {
|
87
|
-
@methods.add_method(current_class_name, "#{method}", {
|
88
|
-
when
|
86
|
+
@methods.add_method(current_class_name, "#{method}_with_#{feature}", {'file' => node.file, 'line_number' => node.line_number}, current_access_control)
|
87
|
+
@methods.add_method(current_class_name, "#{method}", {'file' => node.file, 'line_number' => node.line_number}, current_access_control)
|
88
|
+
when 'field'
|
89
89
|
arguments = node.arguments.all
|
90
90
|
attribute_name = arguments.first.to_s
|
91
|
-
attribute_type = arguments.last.hash_value(
|
91
|
+
attribute_type = arguments.last.hash_value('type').present? ? arguments.last.hash_value('type').to_s : 'String'
|
92
92
|
@model_attributes.add_attribute(current_class_name, attribute_name, attribute_type)
|
93
|
-
when
|
93
|
+
when 'key'
|
94
94
|
attribute_name, attribute_type = node.arguments.all.map(&:to_s)
|
95
95
|
@model_attributes.add_attribute(current_class_name, attribute_name, attribute_type)
|
96
96
|
when *ASSOCIATION_METHODS
|
@@ -102,18 +102,18 @@ module RailsBestPractices
|
|
102
102
|
# check alias node to remembr the alias methods.
|
103
103
|
add_callback :start_alias do |node|
|
104
104
|
method_name = node.new_method.to_s
|
105
|
-
@methods.add_method(current_class_name, method_name, {
|
105
|
+
@methods.add_method(current_class_name, method_name, {'file' => node.file, 'line_number' => node.line_number}, current_access_control)
|
106
106
|
end
|
107
107
|
|
108
108
|
# after prepare process, fix incorrect associations' class_name.
|
109
109
|
add_callback :after_check do
|
110
110
|
@model_associations.each do |model, model_associations|
|
111
111
|
model_associations.each do |association_name, association_meta|
|
112
|
-
unless @models.include?(association_meta[
|
112
|
+
unless @models.include?(association_meta['class_name'])
|
113
113
|
if @models.include?("#{model}::#{association_meta['class_name']}")
|
114
|
-
association_meta[
|
115
|
-
elsif @models.include?(model.gsub(/::\w+$/,
|
116
|
-
association_meta[
|
114
|
+
association_meta['class_name'] = "#{model}::#{association_meta['class_name']}"
|
115
|
+
elsif @models.include?(model.gsub(/::\w+$/, ''))
|
116
|
+
association_meta['class_name'] = model.gsub(/::\w+$/, '')
|
117
117
|
end
|
118
118
|
end
|
119
119
|
end
|
@@ -127,8 +127,8 @@ module RailsBestPractices
|
|
127
127
|
association_meta = node.message.to_s
|
128
128
|
association_name = node.arguments.all.first.to_s
|
129
129
|
arguments_node = node.arguments.all.last
|
130
|
-
if arguments_node.hash_value(
|
131
|
-
association_class = arguments_node.hash_value(
|
130
|
+
if arguments_node.hash_value('class_name').present?
|
131
|
+
association_class = arguments_node.hash_value('class_name').to_s
|
132
132
|
end
|
133
133
|
association_class ||= association_name.classify
|
134
134
|
@model_associations.add_association(current_class_name, association_name, association_meta, association_class)
|
@@ -18,11 +18,11 @@ module RailsBestPractices
|
|
18
18
|
# remember route for rails3.
|
19
19
|
add_callback :start_command do |node|
|
20
20
|
case node.message.to_s
|
21
|
-
when
|
21
|
+
when 'resources'
|
22
22
|
add_resources_routes(node)
|
23
|
-
when
|
23
|
+
when 'resource'
|
24
24
|
add_resource_routes(node)
|
25
|
-
when
|
25
|
+
when 'get', 'post', 'put', 'patch', 'delete'
|
26
26
|
first_argument = node.arguments.all.first
|
27
27
|
second_argument = node.arguments.all[1]
|
28
28
|
if @controller_names.last
|
@@ -30,12 +30,12 @@ module RailsBestPractices
|
|
30
30
|
action_names = [first_argument.hash_values.first.to_s]
|
31
31
|
elsif :array == first_argument.sexp_type
|
32
32
|
action_names = first_argument.array_values.map(&:to_s)
|
33
|
-
elsif :bare_assoc_hash == second_argument.try(:sexp_type) && second_argument.hash_value(
|
34
|
-
if :string_literal == second_argument.hash_value(
|
35
|
-
controller_name, action_name = second_argument.hash_value(
|
33
|
+
elsif :bare_assoc_hash == second_argument.try(:sexp_type) && second_argument.hash_value('to').present?
|
34
|
+
if :string_literal == second_argument.hash_value('to').sexp_type
|
35
|
+
controller_name, action_name = second_argument.hash_value('to').to_s.split('#')
|
36
36
|
action_names = [action_name]
|
37
37
|
else
|
38
|
-
action_names = [second_argument.hash_value(
|
38
|
+
action_names = [second_argument.hash_value('to').to_s]
|
39
39
|
end
|
40
40
|
elsif :symbol_literal == first_argument.sexp_type && second_argument.try(:sexp_type) && \
|
41
41
|
:symbol_literal == second_argument.sexp_type
|
@@ -60,11 +60,11 @@ module RailsBestPractices
|
|
60
60
|
@routes.add_route(current_namespaces, controller_name, action_node.to_s)
|
61
61
|
end
|
62
62
|
elsif :bare_assoc_hash == second_argument.try(:sexp_type)
|
63
|
-
if second_argument.hash_value(
|
64
|
-
controller_name, action_name = second_argument.hash_value(
|
63
|
+
if second_argument.hash_value('to').present?
|
64
|
+
controller_name, action_name = second_argument.hash_value('to').to_s.split('#')
|
65
65
|
else
|
66
66
|
controller_name = current_controller_name
|
67
|
-
action_name = second_argument.hash_value(
|
67
|
+
action_name = second_argument.hash_value('action')
|
68
68
|
end
|
69
69
|
@routes.add_route(current_namespaces, controller_name.try(:underscore), action_name)
|
70
70
|
else
|
@@ -72,14 +72,14 @@ module RailsBestPractices
|
|
72
72
|
@routes.add_route(current_namespaces, controller_name.underscore, action_name)
|
73
73
|
end
|
74
74
|
end
|
75
|
-
when
|
75
|
+
when 'match'
|
76
76
|
options = node.arguments.all.last
|
77
77
|
case options.sexp_type
|
78
78
|
when :bare_assoc_hash
|
79
|
-
if options.hash_value(
|
80
|
-
return if :regexp_literal == options.hash_value(
|
81
|
-
controller_name = options.hash_value(
|
82
|
-
action_name = options.hash_value(
|
79
|
+
if options.hash_value('controller').present?
|
80
|
+
return if :regexp_literal == options.hash_value('controller').sexp_type
|
81
|
+
controller_name = options.hash_value('controller').to_s
|
82
|
+
action_name = options.hash_value('action').present? ? options.hash_value('action').to_s : '*'
|
83
83
|
@routes.add_route(current_namespaces, controller_name, action_name)
|
84
84
|
else
|
85
85
|
route_node = options.hash_values.find { |value_node| :string_literal == value_node.sexp_type && value_node.to_s.include?('#') }
|
@@ -95,7 +95,7 @@ module RailsBestPractices
|
|
95
95
|
else
|
96
96
|
# do nothing
|
97
97
|
end
|
98
|
-
when
|
98
|
+
when 'root'
|
99
99
|
options = node.arguments.all.last
|
100
100
|
case options.sexp_type
|
101
101
|
when :bare_assoc_hash
|
@@ -117,18 +117,18 @@ module RailsBestPractices
|
|
117
117
|
# remember route for rails2.
|
118
118
|
add_callback :start_command_call do |node|
|
119
119
|
case node.message.to_s
|
120
|
-
when
|
120
|
+
when 'resources'
|
121
121
|
add_resources_routes(node)
|
122
|
-
when
|
122
|
+
when 'resource'
|
123
123
|
add_resource_routes(node)
|
124
|
-
when
|
124
|
+
when 'namespace'
|
125
125
|
# nothing to do
|
126
126
|
else
|
127
127
|
options = node.arguments.all.last
|
128
|
-
if options.hash_value(
|
129
|
-
@controller_name = [:option, options.hash_value(
|
128
|
+
if options.hash_value('controller').present?
|
129
|
+
@controller_name = [:option, options.hash_value('controller').to_s]
|
130
130
|
end
|
131
|
-
action_name = options.hash_value(
|
131
|
+
action_name = options.hash_value('action').present? ? options.hash_value('action').to_s : '*'
|
132
132
|
@routes.add_route(current_namespaces, current_controller_name, action_name)
|
133
133
|
end
|
134
134
|
end
|
@@ -136,22 +136,22 @@ module RailsBestPractices
|
|
136
136
|
# remember the namespace.
|
137
137
|
add_callback :start_method_add_block do |node|
|
138
138
|
case node.message.to_s
|
139
|
-
when
|
139
|
+
when 'namespace'
|
140
140
|
@namespaces << node.arguments.all.first.to_s
|
141
141
|
@controller_name = nil
|
142
|
-
when
|
143
|
-
if node.arguments.all.last.hash_value(
|
144
|
-
@namespaces << node.arguments.all.last.hash_value(
|
142
|
+
when 'scope'
|
143
|
+
if node.arguments.all.last.hash_value('module').present?
|
144
|
+
@namespaces << node.arguments.all.last.hash_value('module').to_s
|
145
145
|
end
|
146
|
-
if node.arguments.all.last.hash_value(
|
147
|
-
@controller_name = [:scope, node.arguments.all.last.hash_value(
|
146
|
+
if node.arguments.all.last.hash_value('controller').present?
|
147
|
+
@controller_name = [:scope, node.arguments.all.last.hash_value('controller').to_s]
|
148
148
|
else
|
149
149
|
@controller_name = @controller_name.try(:first) == :scope ? @controller_name : nil
|
150
150
|
end
|
151
|
-
when
|
151
|
+
when 'with_options'
|
152
152
|
argument = node.arguments.all.last
|
153
|
-
if :bare_assoc_hash == argument.sexp_type && argument.hash_value(
|
154
|
-
@controller_name = [:with_option, argument.hash_value(
|
153
|
+
if :bare_assoc_hash == argument.sexp_type && argument.hash_value('controller').present?
|
154
|
+
@controller_name = [:with_option, argument.hash_value('controller').to_s]
|
155
155
|
end
|
156
156
|
else
|
157
157
|
# do nothing
|
@@ -161,10 +161,10 @@ module RailsBestPractices
|
|
161
161
|
# end of namespace call.
|
162
162
|
add_callback :end_method_add_block do |node|
|
163
163
|
case node.message.to_s
|
164
|
-
when
|
164
|
+
when 'namespace'
|
165
165
|
@namespaces.pop
|
166
|
-
when
|
167
|
-
if node.arguments.all.last.hash_value(
|
166
|
+
when 'scope'
|
167
|
+
if node.arguments.all.last.hash_value('module').present?
|
168
168
|
@namespaces.pop
|
169
169
|
end
|
170
170
|
else
|