rails_best_practices 1.19.3 → 1.21.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +0 -1
- data/.travis.yml +2 -3
- data/CHANGELOG.md +10 -11
- data/Gemfile +3 -5
- data/Gemfile.lock +125 -0
- data/Guardfile +2 -0
- data/README.md +5 -1
- data/Rakefile +2 -17
- data/assets/result.html.erb +2 -0
- data/lib/rails_best_practices.rb +4 -2
- data/lib/rails_best_practices/analyzer.rb +63 -51
- data/lib/rails_best_practices/cli.rb +22 -0
- data/lib/rails_best_practices/command.rb +1 -131
- data/lib/rails_best_practices/core/check.rb +63 -55
- data/lib/rails_best_practices/core/checks_loader.rb +24 -23
- 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 +27 -21
- data/lib/rails_best_practices/core/model_associations.rb +10 -5
- 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 +87 -72
- data/lib/rails_best_practices/inline_disables.rb +3 -0
- data/lib/rails_best_practices/inline_disables/comment_ripper.rb +19 -0
- data/lib/rails_best_practices/inline_disables/inline_disable.rb +50 -0
- data/lib/rails_best_practices/lexicals/long_line_check.rb +7 -3
- data/lib/rails_best_practices/option_parser.rb +156 -0
- data/lib/rails_best_practices/prepares.rb +1 -1
- data/lib/rails_best_practices/prepares/controller_prepare.rb +23 -17
- data/lib/rails_best_practices/prepares/gemfile_prepare.rb +2 -2
- data/lib/rails_best_practices/prepares/helper_prepare.rb +6 -1
- data/lib/rails_best_practices/prepares/initializer_prepare.rb +3 -3
- data/lib/rails_best_practices/prepares/mailer_prepare.rb +2 -1
- data/lib/rails_best_practices/prepares/model_prepare.rb +63 -23
- data/lib/rails_best_practices/prepares/route_prepare.rb +28 -21
- data/lib/rails_best_practices/prepares/schema_prepare.rb +1 -1
- data/lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb +38 -34
- data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +94 -88
- data/lib/rails_best_practices/reviews/check_destroy_return_value_review.rb +15 -5
- data/lib/rails_best_practices/reviews/check_save_return_value_review.rb +20 -8
- data/lib/rails_best_practices/reviews/default_scope_is_evil_review.rb +1 -1
- data/lib/rails_best_practices/reviews/dry_bundler_in_capistrano_review.rb +1 -1
- data/lib/rails_best_practices/reviews/hash_syntax_review.rb +16 -16
- data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +12 -12
- data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +10 -11
- data/lib/rails_best_practices/reviews/law_of_demeter_review.rb +25 -24
- data/lib/rails_best_practices/reviews/move_code_into_controller_review.rb +4 -4
- data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +9 -10
- data/lib/rails_best_practices/reviews/move_finder_to_named_scope_review.rb +10 -11
- data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +24 -22
- data/lib/rails_best_practices/reviews/not_rescue_exception_review.rb +1 -1
- data/lib/rails_best_practices/reviews/not_use_default_route_review.rb +1 -2
- data/lib/rails_best_practices/reviews/not_use_time_ago_in_words_review.rb +1 -1
- data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +8 -8
- data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +35 -32
- data/lib/rails_best_practices/reviews/remove_empty_helpers_review.rb +4 -4
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb +20 -17
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_helpers_review.rb +12 -10
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +38 -18
- data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +11 -11
- data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +77 -74
- data/lib/rails_best_practices/reviews/review.rb +2 -1
- data/lib/rails_best_practices/reviews/simplify_render_in_controllers_review.rb +2 -3
- data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +12 -12
- data/lib/rails_best_practices/reviews/use_before_filter_review.rb +14 -10
- data/lib/rails_best_practices/reviews/use_model_association_review.rb +15 -15
- data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +24 -22
- data/lib/rails_best_practices/reviews/use_observer_review.rb +28 -28
- data/lib/rails_best_practices/reviews/use_parentheses_in_method_def_review.rb +6 -6
- data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +63 -60
- data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +9 -8
- data/lib/rails_best_practices/reviews/use_scope_access_review.rb +16 -14
- 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 +48 -49
- 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 -18
- 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/inline_disables/inline_disable_spec.rb +62 -0
- data/spec/rails_best_practices/lexicals/long_line_check_spec.rb +32 -31
- 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 +17 -17
- 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 +141 -76
- 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 +13 -13
- 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 +29 -22
- 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 +32 -22
- 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 -17
- 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 +47 -32
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +21 -14
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +57 -53
- 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 +20 -14
- 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 +35 -31
- 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 +10 -8
- 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 +61 -59
- metadata +21 -14
@@ -17,16 +17,16 @@ module RailsBestPractices
|
|
17
17
|
|
18
18
|
# check the body of module node, if it is nil, then it should be removed.
|
19
19
|
add_callback :start_module do |module_node|
|
20
|
-
if
|
20
|
+
if module_node.module_name.to_s != 'ApplicationHelper' && empty_body?(module_node)
|
21
21
|
add_error 'remove empty helpers'
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
25
|
protected
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
def empty_body?(module_node)
|
28
|
+
s(:bodystmt, s(:stmts_add, s(:stmts_new), s(:void_stmt)), nil, nil, nil) == module_node.body
|
29
|
+
end
|
30
30
|
end
|
31
31
|
end
|
32
32
|
end
|
@@ -11,6 +11,7 @@ module RailsBestPractices
|
|
11
11
|
# if they are not defined in routes,
|
12
12
|
# and they are not called in controllers,
|
13
13
|
# then they are the unused methods in controllers.
|
14
|
+
|
14
15
|
class RemoveUnusedMethodsInControllersReview < Review
|
15
16
|
include Classable
|
16
17
|
include Moduleable
|
@@ -71,7 +72,7 @@ module RailsBestPractices
|
|
71
72
|
node.arguments.all.each { |argument| mark_publicize(argument.to_s) }
|
72
73
|
when 'delegate'
|
73
74
|
last_argument = node.arguments.all.last
|
74
|
-
if
|
75
|
+
if last_argument.sexp_type == :bare_assoc_hash && last_argument.hash_value('to').to_s == 'controller'
|
75
76
|
controller_name = current_module_name.sub('Helper', 'Controller')
|
76
77
|
node.arguments.all[0..-2].each { |method| mark_publicize(method.to_s, controller_name) }
|
77
78
|
end
|
@@ -82,7 +83,7 @@ module RailsBestPractices
|
|
82
83
|
|
83
84
|
# mark assignment as used, like current_user = @user
|
84
85
|
add_callback :start_assign do |node|
|
85
|
-
if
|
86
|
+
if node.left_value.sexp_type == :var_field
|
86
87
|
call_method "#{node.left_value}=", current_class_name
|
87
88
|
end
|
88
89
|
end
|
@@ -90,7 +91,7 @@ module RailsBestPractices
|
|
90
91
|
# get all unused methods at the end of review process.
|
91
92
|
add_callback :after_check do
|
92
93
|
@routes.each do |route|
|
93
|
-
if '*'
|
94
|
+
if route.action_name == '*'
|
94
95
|
action_names = @controller_methods.get_methods(route.controller_name_with_namespaces).map(&:method_name)
|
95
96
|
action_names.each { |action_name| call_method(action_name, route.controller_name_with_namespaces) }
|
96
97
|
else
|
@@ -98,27 +99,29 @@ module RailsBestPractices
|
|
98
99
|
end
|
99
100
|
end
|
100
101
|
@controller_methods.get_all_unused_methods.each do |method|
|
101
|
-
|
102
|
-
|
103
|
-
|
102
|
+
next if excepted?(method)
|
103
|
+
|
104
|
+
add_error "remove unused methods (#{method.class_name}##{method.method_name})",
|
105
|
+
method.file,
|
106
|
+
method.line_number
|
104
107
|
end
|
105
108
|
end
|
106
109
|
|
107
110
|
protected
|
108
111
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
+
def methods
|
113
|
+
@controller_methods
|
114
|
+
end
|
112
115
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
116
|
+
def internal_except_methods
|
117
|
+
%w[rescue_action default_url_options].map { |method_name| "*\##{method_name}" } +
|
118
|
+
%w[Devise::OmniauthCallbacksController].map { |controller_name| "#{controller_name}#*" }
|
119
|
+
end
|
117
120
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
121
|
+
def mark_publicize(method_name, class_name = current_class_name)
|
122
|
+
@controller_methods.mark_publicize(class_name, method_name)
|
123
|
+
@controller_methods.mark_parent_class_methods_publicize(class_name, method_name)
|
124
|
+
end
|
122
125
|
end
|
123
126
|
end
|
124
127
|
end
|
@@ -20,27 +20,29 @@ module RailsBestPractices
|
|
20
20
|
def initialize(options = {})
|
21
21
|
super
|
22
22
|
@helper_methods = Prepares.helper_methods
|
23
|
-
self.class.interesting_files Prepares.helpers.map(&:descendants)
|
23
|
+
self.class.interesting_files *Prepares.helpers.map(&:descendants)
|
24
24
|
end
|
25
25
|
|
26
26
|
# get all unused methods at the end of review process
|
27
27
|
add_callback :after_check do
|
28
28
|
@helper_methods.get_all_unused_methods.each do |method|
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
next if excepted?(method)
|
30
|
+
|
31
|
+
add_error "remove unused methods (#{method.class_name}##{method.method_name})",
|
32
|
+
method.file,
|
33
|
+
method.line_number
|
32
34
|
end
|
33
35
|
end
|
34
36
|
|
35
37
|
protected
|
36
38
|
|
37
|
-
|
38
|
-
|
39
|
-
|
39
|
+
def methods
|
40
|
+
@helper_methods
|
41
|
+
end
|
40
42
|
|
41
|
-
|
42
|
-
|
43
|
-
|
43
|
+
def internal_except_methods
|
44
|
+
['*#url_for']
|
45
|
+
end
|
44
46
|
end
|
45
47
|
end
|
46
48
|
end
|
@@ -80,30 +80,50 @@ module RailsBestPractices
|
|
80
80
|
# get all unused methods at the end of review process.
|
81
81
|
add_callback :after_check do
|
82
82
|
@model_methods.get_all_unused_methods.each do |method|
|
83
|
-
|
84
|
-
|
85
|
-
|
83
|
+
next unless !excepted?(method) && method.method_name !~ /=$/
|
84
|
+
|
85
|
+
add_error "remove unused methods (#{method.class_name}##{method.method_name})",
|
86
|
+
method.file,
|
87
|
+
method.line_number
|
86
88
|
end
|
87
89
|
end
|
88
90
|
|
89
91
|
protected
|
90
92
|
|
91
|
-
|
92
|
-
|
93
|
-
|
93
|
+
def methods
|
94
|
+
@model_methods
|
95
|
+
end
|
94
96
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
97
|
+
def internal_except_methods
|
98
|
+
%w[
|
99
|
+
initialize
|
100
|
+
validate
|
101
|
+
validate_each
|
102
|
+
validate_on_create
|
103
|
+
validate_on_update
|
104
|
+
human_attribute_name
|
105
|
+
assign_attributes
|
106
|
+
attributes
|
107
|
+
attribute
|
108
|
+
to_xml
|
109
|
+
to_json
|
110
|
+
as_json
|
111
|
+
to_param
|
112
|
+
before_save
|
113
|
+
before_create
|
114
|
+
before_update
|
115
|
+
before_destroy
|
116
|
+
after_save
|
117
|
+
after_create
|
118
|
+
after_update
|
119
|
+
after_destroy
|
120
|
+
after_find
|
121
|
+
after_initialize
|
122
|
+
method_missing
|
123
|
+
table_name
|
124
|
+
module_prefix
|
125
|
+
].map { |method_name| "*\##{method_name}" }
|
126
|
+
end
|
107
127
|
end
|
108
128
|
end
|
109
129
|
end
|
data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb
CHANGED
@@ -37,7 +37,7 @@ module RailsBestPractices
|
|
37
37
|
node.recursive_children do |child_node|
|
38
38
|
case child_node.sexp_type
|
39
39
|
when :assign
|
40
|
-
if
|
40
|
+
if child_node.receiver[2].to_s == '.'
|
41
41
|
remember_variable_use_count(child_node)
|
42
42
|
end
|
43
43
|
when :call
|
@@ -49,18 +49,18 @@ module RailsBestPractices
|
|
49
49
|
|
50
50
|
private
|
51
51
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
end
|
52
|
+
# check the call node to see if it is with message "save" or "save!",
|
53
|
+
# and the count attribute assignment on the receiver of the call node is greater than @assign_count defined,
|
54
|
+
# then it is a complex creation, should be replaced with factory method.
|
55
|
+
def check_variable_save(node)
|
56
|
+
if ['save', 'save!'].include? node.message.to_s
|
57
|
+
variable = node.receiver.to_s
|
58
|
+
if variable_use_count[variable].to_i > @assigns_count
|
59
|
+
hint = "#{variable} attribute_assignment_count > #{@assigns_count}"
|
60
|
+
add_error "replace complex creation with factory method (#{hint})"
|
62
61
|
end
|
63
62
|
end
|
63
|
+
end
|
64
64
|
end
|
65
65
|
end
|
66
66
|
end
|
@@ -38,23 +38,23 @@ module RailsBestPractices
|
|
38
38
|
|
39
39
|
# check if the generated routes have the corresponding actions in controller for rails routes.
|
40
40
|
add_callback :start_command, :start_command_call do |node|
|
41
|
-
if
|
41
|
+
if node.message.to_s == 'resources'
|
42
42
|
if (mod = module_option(node))
|
43
43
|
@namespaces << mod
|
44
44
|
end
|
45
45
|
check_resources(node)
|
46
46
|
@resource_controllers << node.arguments.all.first.to_s
|
47
|
-
elsif
|
47
|
+
elsif node.message.to_s == 'resource'
|
48
48
|
check_resource(node)
|
49
49
|
@resource_controllers << node.arguments.all.first.to_s
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
53
|
add_callback :end_command do |node|
|
54
|
-
if
|
54
|
+
if node.message.to_s == 'resources'
|
55
55
|
@resource_controllers.pop
|
56
56
|
@namespaces.pop if module_option(node)
|
57
|
-
elsif
|
57
|
+
elsif node.message.to_s == 'resource'
|
58
58
|
@resource_controllers.pop
|
59
59
|
end
|
60
60
|
end
|
@@ -90,103 +90,106 @@ module RailsBestPractices
|
|
90
90
|
end
|
91
91
|
|
92
92
|
def check_method_add_block?(node)
|
93
|
-
|
93
|
+
node[1].sexp_type == :command || (node[1].sexp_type == :command_call && node.receiver.to_s != 'map')
|
94
94
|
end
|
95
95
|
|
96
96
|
private
|
97
97
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
98
|
+
# check resources call, if the routes generated by resources does not exist in the controller.
|
99
|
+
def check_resources(node)
|
100
|
+
_check(node, resources_methods)
|
101
|
+
end
|
102
102
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
103
|
+
# check resource call, if the routes generated by resources does not exist in the controller.
|
104
|
+
def check_resource(node)
|
105
|
+
_check(node, resource_methods)
|
106
|
+
end
|
107
107
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
108
|
+
# get the controller name.
|
109
|
+
def controller_name(node)
|
110
|
+
if option_with_hash(node)
|
111
|
+
option_node = node.arguments.all[1]
|
112
|
+
name =
|
112
113
|
if hash_key_exist?(option_node, 'controller')
|
113
|
-
|
114
|
+
option_node.hash_value('controller').to_s
|
114
115
|
else
|
115
|
-
|
116
|
+
node.arguments.all.first.to_s.gsub('::', '').tableize
|
116
117
|
end
|
117
|
-
|
118
|
-
|
119
|
-
end
|
120
|
-
namespaced_class_name(name)
|
118
|
+
else
|
119
|
+
name = node.arguments.all.first.to_s.gsub('::', '').tableize
|
121
120
|
end
|
121
|
+
namespaced_class_name(name)
|
122
|
+
end
|
122
123
|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
end
|
124
|
+
# get the class name with namespace.
|
125
|
+
def namespaced_class_name(name)
|
126
|
+
class_name = "#{name.split('/').map(&:camelize).join('::')}Controller"
|
127
|
+
if @namespaces.empty?
|
128
|
+
class_name
|
129
|
+
else
|
130
|
+
@namespaces.map { |namespace| "#{namespace.camelize}::" }.join('') + class_name
|
131
131
|
end
|
132
|
+
end
|
132
133
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
134
|
+
def _check(node, methods)
|
135
|
+
controller_name = controller_name(node)
|
136
|
+
return unless Prepares.controllers.include? controller_name
|
137
|
+
|
138
|
+
_methods = _methods(node, methods)
|
139
|
+
unless _methods.all? { |meth| Prepares.controller_methods.has_method?(controller_name, meth) }
|
140
|
+
prepared_method_names = Prepares.controller_methods.get_methods(controller_name).map(&:method_name)
|
141
|
+
only_methods = (_methods & prepared_method_names).map { |meth| ":#{meth}" }
|
142
|
+
routes_message =
|
143
|
+
if only_methods.size > 3
|
144
|
+
"except: [#{(methods.map { |meth| ':' + meth } - only_methods).join(', ')}]"
|
145
|
+
else
|
146
|
+
"only: [#{only_methods.join(', ')}]"
|
147
|
+
end
|
148
|
+
add_error "restrict auto-generated routes #{friendly_route_name(node)} (#{routes_message})"
|
147
149
|
end
|
150
|
+
end
|
148
151
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
else
|
158
|
-
(methods - Array(option_node.hash_value('except').to_object))
|
159
|
-
end
|
152
|
+
def _methods(node, methods)
|
153
|
+
if option_with_hash(node)
|
154
|
+
option_node = node.arguments.all[1]
|
155
|
+
if hash_key_exist?(option_node, 'only')
|
156
|
+
option_node.hash_value('only').to_s == 'none' ? [] : Array(option_node.hash_value('only').to_object)
|
157
|
+
elsif hash_key_exist?(option_node, 'except')
|
158
|
+
if option_node.hash_value('except').to_s == 'all'
|
159
|
+
[]
|
160
160
|
else
|
161
|
-
methods
|
161
|
+
(methods - Array(option_node.hash_value('except').to_object))
|
162
162
|
end
|
163
163
|
else
|
164
164
|
methods
|
165
165
|
end
|
166
|
+
else
|
167
|
+
methods
|
166
168
|
end
|
169
|
+
end
|
167
170
|
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
end
|
171
|
+
def module_option(node)
|
172
|
+
option_node = node.arguments[1].last
|
173
|
+
if option_node && option_node.sexp_type == :bare_assoc_hash && hash_key_exist?(option_node, 'module')
|
174
|
+
option_node.hash_value('module').to_s
|
173
175
|
end
|
176
|
+
end
|
174
177
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
+
def option_with_hash(node)
|
179
|
+
node.arguments.all.size > 1 && node.arguments.all[1].sexp_type == :bare_assoc_hash
|
180
|
+
end
|
178
181
|
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
+
def hash_key_exist?(node, key)
|
183
|
+
node.hash_keys&.include?(key)
|
184
|
+
end
|
182
185
|
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
end
|
186
|
+
def friendly_route_name(node)
|
187
|
+
if @resource_controllers.last == node.arguments.to_s
|
188
|
+
[@namespaces.join('/'), @resource_controllers.join('/')].delete_if(&:blank?).join('/')
|
189
|
+
else
|
190
|
+
[@namespaces.join('/'), @resource_controllers.join('/'), node.arguments.to_s].delete_if(&:blank?).join('/')
|
189
191
|
end
|
192
|
+
end
|
190
193
|
end
|
191
194
|
end
|
192
195
|
end
|