rails_best_practices 1.19.3 → 1.21.0

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.
Files changed (153) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +0 -1
  3. data/.travis.yml +2 -3
  4. data/CHANGELOG.md +10 -11
  5. data/Gemfile +3 -5
  6. data/Gemfile.lock +125 -0
  7. data/Guardfile +2 -0
  8. data/README.md +5 -1
  9. data/Rakefile +2 -17
  10. data/assets/result.html.erb +2 -0
  11. data/lib/rails_best_practices.rb +4 -2
  12. data/lib/rails_best_practices/analyzer.rb +63 -51
  13. data/lib/rails_best_practices/cli.rb +22 -0
  14. data/lib/rails_best_practices/command.rb +1 -131
  15. data/lib/rails_best_practices/core/check.rb +63 -55
  16. data/lib/rails_best_practices/core/checks_loader.rb +24 -23
  17. data/lib/rails_best_practices/core/configs.rb +1 -2
  18. data/lib/rails_best_practices/core/controllers.rb +1 -2
  19. data/lib/rails_best_practices/core/error.rb +1 -1
  20. data/lib/rails_best_practices/core/helpers.rb +1 -2
  21. data/lib/rails_best_practices/core/mailers.rb +1 -2
  22. data/lib/rails_best_practices/core/methods.rb +27 -21
  23. data/lib/rails_best_practices/core/model_associations.rb +10 -5
  24. data/lib/rails_best_practices/core/models.rb +1 -2
  25. data/lib/rails_best_practices/core/modules.rb +1 -1
  26. data/lib/rails_best_practices/core/routes.rb +2 -2
  27. data/lib/rails_best_practices/core/runner.rb +87 -72
  28. data/lib/rails_best_practices/inline_disables.rb +3 -0
  29. data/lib/rails_best_practices/inline_disables/comment_ripper.rb +19 -0
  30. data/lib/rails_best_practices/inline_disables/inline_disable.rb +50 -0
  31. data/lib/rails_best_practices/lexicals/long_line_check.rb +7 -3
  32. data/lib/rails_best_practices/option_parser.rb +156 -0
  33. data/lib/rails_best_practices/prepares.rb +1 -1
  34. data/lib/rails_best_practices/prepares/controller_prepare.rb +23 -17
  35. data/lib/rails_best_practices/prepares/gemfile_prepare.rb +2 -2
  36. data/lib/rails_best_practices/prepares/helper_prepare.rb +6 -1
  37. data/lib/rails_best_practices/prepares/initializer_prepare.rb +3 -3
  38. data/lib/rails_best_practices/prepares/mailer_prepare.rb +2 -1
  39. data/lib/rails_best_practices/prepares/model_prepare.rb +63 -23
  40. data/lib/rails_best_practices/prepares/route_prepare.rb +28 -21
  41. data/lib/rails_best_practices/prepares/schema_prepare.rb +1 -1
  42. data/lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb +38 -34
  43. data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +94 -88
  44. data/lib/rails_best_practices/reviews/check_destroy_return_value_review.rb +15 -5
  45. data/lib/rails_best_practices/reviews/check_save_return_value_review.rb +20 -8
  46. data/lib/rails_best_practices/reviews/default_scope_is_evil_review.rb +1 -1
  47. data/lib/rails_best_practices/reviews/dry_bundler_in_capistrano_review.rb +1 -1
  48. data/lib/rails_best_practices/reviews/hash_syntax_review.rb +16 -16
  49. data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +12 -12
  50. data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +10 -11
  51. data/lib/rails_best_practices/reviews/law_of_demeter_review.rb +25 -24
  52. data/lib/rails_best_practices/reviews/move_code_into_controller_review.rb +4 -4
  53. data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +9 -10
  54. data/lib/rails_best_practices/reviews/move_finder_to_named_scope_review.rb +10 -11
  55. data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +24 -22
  56. data/lib/rails_best_practices/reviews/not_rescue_exception_review.rb +1 -1
  57. data/lib/rails_best_practices/reviews/not_use_default_route_review.rb +1 -2
  58. data/lib/rails_best_practices/reviews/not_use_time_ago_in_words_review.rb +1 -1
  59. data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +8 -8
  60. data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +35 -32
  61. data/lib/rails_best_practices/reviews/remove_empty_helpers_review.rb +4 -4
  62. data/lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb +20 -17
  63. data/lib/rails_best_practices/reviews/remove_unused_methods_in_helpers_review.rb +12 -10
  64. data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +38 -18
  65. data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +11 -11
  66. data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +77 -74
  67. data/lib/rails_best_practices/reviews/review.rb +2 -1
  68. data/lib/rails_best_practices/reviews/simplify_render_in_controllers_review.rb +2 -3
  69. data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +12 -12
  70. data/lib/rails_best_practices/reviews/use_before_filter_review.rb +14 -10
  71. data/lib/rails_best_practices/reviews/use_model_association_review.rb +15 -15
  72. data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +24 -22
  73. data/lib/rails_best_practices/reviews/use_observer_review.rb +28 -28
  74. data/lib/rails_best_practices/reviews/use_parentheses_in_method_def_review.rb +6 -6
  75. data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +63 -60
  76. data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +9 -8
  77. data/lib/rails_best_practices/reviews/use_scope_access_review.rb +16 -14
  78. data/lib/rails_best_practices/reviews/use_turbo_sprockets_rails3_review.rb +2 -1
  79. data/lib/rails_best_practices/version.rb +1 -1
  80. data/rails_best_practices.gemspec +48 -49
  81. data/spec/fixtures/lib/rails_best_practices/plugins/reviews/not_use_rails_root_review.rb +1 -2
  82. data/spec/rails_best_practices/analyzer_spec.rb +73 -42
  83. data/spec/rails_best_practices/core/check_spec.rb +5 -5
  84. data/spec/rails_best_practices/core/checks_loader_spec.rb +3 -3
  85. data/spec/rails_best_practices/core/configs_spec.rb +1 -1
  86. data/spec/rails_best_practices/core/controllers_spec.rb +1 -1
  87. data/spec/rails_best_practices/core/error_spec.rb +21 -18
  88. data/spec/rails_best_practices/core/except_methods_spec.rb +7 -7
  89. data/spec/rails_best_practices/core/gems_spec.rb +4 -4
  90. data/spec/rails_best_practices/core/helpers_spec.rb +1 -1
  91. data/spec/rails_best_practices/core/klasses_spec.rb +3 -3
  92. data/spec/rails_best_practices/core/mailers_spec.rb +1 -1
  93. data/spec/rails_best_practices/core/methods_spec.rb +6 -6
  94. data/spec/rails_best_practices/core/model_associations_spec.rb +10 -6
  95. data/spec/rails_best_practices/core/model_attributes_spec.rb +4 -4
  96. data/spec/rails_best_practices/core/models_spec.rb +1 -1
  97. data/spec/rails_best_practices/core/modules_spec.rb +5 -5
  98. data/spec/rails_best_practices/core/routes_spec.rb +5 -5
  99. data/spec/rails_best_practices/core/runner_spec.rb +9 -7
  100. data/spec/rails_best_practices/core_ext/erubis_spec.rb +10 -10
  101. data/spec/rails_best_practices/inline_disables/inline_disable_spec.rb +62 -0
  102. data/spec/rails_best_practices/lexicals/long_line_check_spec.rb +32 -31
  103. data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +6 -6
  104. data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +6 -6
  105. data/spec/rails_best_practices/prepares/config_prepare_spec.rb +2 -2
  106. data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +18 -10
  107. data/spec/rails_best_practices/prepares/gemfile_prepare_spec.rb +17 -17
  108. data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +3 -3
  109. data/spec/rails_best_practices/prepares/initializer_prepare_spec.rb +3 -3
  110. data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +2 -2
  111. data/spec/rails_best_practices/prepares/model_prepare_spec.rb +79 -43
  112. data/spec/rails_best_practices/prepares/route_prepare_spec.rb +141 -76
  113. data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +2 -2
  114. data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +18 -12
  115. data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +28 -22
  116. data/spec/rails_best_practices/reviews/check_destroy_return_value_review_spec.rb +15 -13
  117. data/spec/rails_best_practices/reviews/check_save_return_value_review_spec.rb +31 -21
  118. data/spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb +6 -6
  119. data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +5 -5
  120. data/spec/rails_best_practices/reviews/hash_syntax_review_spec.rb +13 -13
  121. data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +7 -7
  122. data/spec/rails_best_practices/reviews/keep_finders_on_their_own_model_review_spec.rb +9 -9
  123. data/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb +29 -22
  124. data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +6 -6
  125. data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +11 -6
  126. data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +32 -22
  127. data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +7 -7
  128. data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +9 -7
  129. data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +9 -9
  130. data/spec/rails_best_practices/reviews/not_rescue_exception_review_spec.rb +9 -9
  131. data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +5 -5
  132. data/spec/rails_best_practices/reviews/not_use_time_ago_in_words_review_spec.rb +7 -7
  133. data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +7 -7
  134. data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +24 -17
  135. data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +6 -6
  136. data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +47 -32
  137. data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +21 -14
  138. data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +57 -53
  139. data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +10 -8
  140. data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +20 -14
  141. data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +54 -31
  142. data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +9 -9
  143. data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +13 -13
  144. data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +11 -9
  145. data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +7 -7
  146. data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +35 -31
  147. data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +6 -6
  148. data/spec/rails_best_practices/reviews/use_parentheses_in_method_def_review_spec.rb +10 -8
  149. data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +31 -24
  150. data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +15 -11
  151. data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +14 -14
  152. data/spec/rails_best_practices/reviews/use_turbo_sprockets_rails3_review_spec.rb +61 -59
  153. metadata +21 -14
@@ -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
- if actual_line_length > @max_line_length
26
- add_error("line is longer than #{@max_line_length} characters (#{actual_line_length} characters)", filename, line_no)
27
- end
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
@@ -0,0 +1,156 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'optparse'
4
+
5
+ module RailsBestPractices
6
+ class OptionParser
7
+ # Usage: rails_best_practices [options] path
8
+ # -d, --debug debug mode
9
+ # --silent silent mode
10
+ # -f, --format FORMAT output format (text, html, yaml, json, xml)
11
+ # --output-file FILE output html file for the analyzing result
12
+ # --without-color only output plain text without color
13
+ # --with-textmate open file by textmate in html format
14
+ # --with-vscode open file by vscode in html format
15
+ # --with-sublime open file by sublime in html format (requires subl-handler)
16
+ # --with-mvim open file by mvim in html format
17
+ # --with-github GITHUB_NAME open file on github in html format, GITHUB_NAME is like railsbp/rails-bestpractices.com
18
+ # --with-git display git commit and username, only support html format
19
+ # --with-hg display hg commit and username, only support html format
20
+ # --template TEMPLATE customize erb template
21
+ # --vendor include vendor files
22
+ # --spec include spec files
23
+ # --test include test files
24
+ # --features include features files
25
+ # -x, --exclude PATTERNS don't analyze files matching a pattern
26
+ # (comma-separated regexp list)
27
+ # -o, --only PATTERNS analyze files only matching a pattern
28
+ # (comma-separated regexp list)
29
+ # -g, --generate generate configuration yaml
30
+ # -v, --version show this version
31
+ # -h, --help show this message
32
+
33
+ def self.parse!(argv = ARGV)
34
+ options = {}
35
+ OptParse.new do |opts|
36
+ opts.default_argv = argv
37
+
38
+ opts.banner = 'Usage: rails_best_practices [options] path'
39
+
40
+ opts.on('-d', '--debug', 'Debug mode') do
41
+ options['debug'] = true
42
+ end
43
+
44
+ opts.on('-f', '--format FORMAT', 'output format (text, html, yaml, json, xml)') do |format|
45
+ options['format'] = format
46
+ end
47
+
48
+ opts.on('--without-color', 'only output plain text without color') do
49
+ options['without-color'] = true
50
+ end
51
+
52
+ opts.on('--with-textmate', 'open file by textmate in html format') do
53
+ options['with-textmate'] = true
54
+ end
55
+
56
+ opts.on('--with-vscode', 'open file by vscode in html format') do
57
+ options['with-vscode'] = true
58
+ end
59
+
60
+ opts.on('--with-sublime', 'open file by sublime in html format') do
61
+ options['with-sublime'] = true
62
+ end
63
+
64
+ opts.on('--with-mvim', 'open file by mvim in html format') do
65
+ options['with-mvim'] = true
66
+ end
67
+
68
+ opts.on('--with-github GITHUB_NAME', 'open file on github in html format') do |github_name|
69
+ options['with-github'] = true
70
+ options['github-name'] = github_name
71
+ end
72
+
73
+ opts.on('--last-commit-id COMMIT_ID', 'last commit id') do |commit_id|
74
+ options['last-commit-id'] = commit_id
75
+ end
76
+
77
+ opts.on('--with-hg', 'display hg commit and username, only support html format') do
78
+ options['with-hg'] = true
79
+ end
80
+
81
+ opts.on('--with-git', 'display git commit and username, only support html format') do
82
+ options['with-git'] = true
83
+ end
84
+
85
+ opts.on('--template TEMPLATE', 'customize erb template') do |template|
86
+ options['template'] = template
87
+ end
88
+
89
+ opts.on('--output-file OUTPUT_FILE', 'output html file for the analyzing result') do |output_file|
90
+ options['output-file'] = output_file
91
+ end
92
+
93
+ opts.on('--silent', 'silent mode') do
94
+ options['silent'] = true
95
+ end
96
+
97
+ %w[vendor spec test features].each do |pattern|
98
+ opts.on("--#{pattern}", "include #{pattern} files") do
99
+ options[pattern] = true
100
+ end
101
+ end
102
+
103
+ opts.on_tail('-v', '--version', 'Show this version') do
104
+ require 'rails_best_practices/version'
105
+ puts RailsBestPractices::VERSION
106
+ exit
107
+ end
108
+
109
+ opts.on_tail('-h', '--help', 'Show this message') do
110
+ puts opts
111
+ exit
112
+ end
113
+
114
+ opts.on(
115
+ '-x',
116
+ '--exclude PATTERNS',
117
+ "Don't analyze files matching a pattern",
118
+ '(comma-separated regexp list)'
119
+ ) do |list|
120
+ begin
121
+ options['exclude'] = list.split(',').map { |x| Regexp.new x }
122
+ rescue RegexpError => e
123
+ raise OptionParser::InvalidArgument, e.message
124
+ end
125
+ end
126
+
127
+ opts.on(
128
+ '-o',
129
+ '--only PATTERNS',
130
+ 'Analyze files only matching a pattern',
131
+ '(comma-separated regexp list)'
132
+ ) do |list|
133
+ begin
134
+ options['only'] = list.split(',').map { |x| Regexp.new x }
135
+ rescue RegexpError => e
136
+ raise OptionParser::InvalidArgument, e.message
137
+ end
138
+ end
139
+
140
+ opts.on('-g', '--generate', 'Generate configuration yaml') do
141
+ options['generate'] = true
142
+ end
143
+
144
+ opts.on(
145
+ '-c',
146
+ '--config CONFIG_PATH',
147
+ 'configuration file location (defaults to config/rails_best_practices.yml)'
148
+ ) do |config_path|
149
+ options['config'] = config_path
150
+ end
151
+ opts.parse!
152
+ end
153
+ options
154
+ end
155
+ end
156
+ end
@@ -4,7 +4,7 @@ require_rel 'prepares'
4
4
 
5
5
  module RailsBestPractices
6
6
  module Prepares
7
- class <<self
7
+ class << self
8
8
  def klasses
9
9
  models + mailers + controllers
10
10
  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
@@ -25,43 +26,41 @@ module RailsBestPractices
25
26
  add_callback :start_class do |_node|
26
27
  @controllers << @klass
27
28
  @current_controller_name = @klass.to_s
28
- if @inherited_resources
29
- @actions = DEFAULT_ACTIONS
30
- end
29
+ @actions = DEFAULT_ACTIONS if @inherited_resources
31
30
  end
32
31
 
33
32
  # remember the action names at the end of class node if the controller is a InheritedResources.
34
33
  add_callback :end_class do |node|
35
- if @inherited_resources && 'ApplicationController' != @current_controller_name
34
+ if @inherited_resources && @current_controller_name != 'ApplicationController'
36
35
  @actions.each do |action|
37
- @methods.add_method(@current_controller_name, action, 'file' => node.file, 'line_number' => node.line_number)
36
+ @methods.add_method(
37
+ @current_controller_name,
38
+ action,
39
+ 'file' => node.file, 'line_number' => node.line_number
40
+ )
38
41
  end
39
42
  end
40
43
  end
41
44
 
42
45
  # check if there is a DSL call inherit_resources.
43
46
  add_callback :start_var_ref do |_node|
44
- if @inherited_resources
45
- @actions = DEFAULT_ACTIONS
46
- end
47
+ @actions = DEFAULT_ACTIONS if @inherited_resources
47
48
  end
48
49
 
49
50
  # check if there is a DSL call inherit_resources.
50
51
  add_callback :start_vcall do |_node|
51
- if @inherited_resources
52
- @actions = DEFAULT_ACTIONS
53
- end
52
+ @actions = DEFAULT_ACTIONS if @inherited_resources
54
53
  end
55
54
 
56
55
  # restrict actions for inherited_resources
57
56
  add_callback :start_command do |node|
58
- if 'include' == node.message.to_s
57
+ if node.message.to_s == 'include'
59
58
  @helpers.add_module_descendant(node.arguments.all.first.to_s, current_class_name)
60
- elsif @inherited_resources && 'actions' == node.message.to_s
61
- if 'all' == node.arguments.all.first.to_s
59
+ elsif @inherited_resources && node.message.to_s == 'actions'
60
+ if node.arguments.all.first.to_s == 'all'
62
61
  @actions = DEFAULT_ACTIONS
63
62
  option_argument = node.arguments.all[1]
64
- if option_argument && :bare_assoc_hash == option_argument.sexp_type && option_argument.hash_value('except')
63
+ if option_argument && option_argument.sexp_type == :bare_assoc_hash && option_argument.hash_value('except')
65
64
  @actions -= option_argument.hash_value('except').to_object
66
65
  end
67
66
  else
@@ -84,14 +83,21 @@ module RailsBestPractices
84
83
  # }
85
84
  add_callback :start_def do |node|
86
85
  method_name = node.method_name.to_s
87
- @methods.add_method(current_class_name, method_name, { 'file' => node.file, 'line_number' => node.line_number }, current_access_control)
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
+ )
88
92
  end
89
93
 
90
94
  # ask Reviews::RemoveUnusedMoethodsInHelperReview to check the controllers who include helpers.
91
95
  add_callback :after_check do
92
96
  descendants = @helpers.map(&:descendants).flatten
93
97
  if descendants.present?
94
- Reviews::RemoveUnusedMethodsInHelpersReview.interesting_files *descendants.map { |descendant| /#{descendant.underscore}/ }
98
+ Reviews::RemoveUnusedMethodsInHelpersReview.interesting_files *descendants.map { |descendant|
99
+ /#{descendant.underscore}/
100
+ }
95
101
  end
96
102
  end
97
103
  end
@@ -10,10 +10,10 @@ module RailsBestPractices
10
10
  @gems = Prepares.gems
11
11
  end
12
12
 
13
- def check(filename, content)
13
+ def check(_filename, content)
14
14
  content.split("\n").each do |line|
15
15
  if line =~ /([^ ]+) \((\d.*)\)/
16
- @gems << Core::Gem.new($1, $2)
16
+ @gems << Core::Gem.new(Regexp.last_match(1), Regexp.last_match(2))
17
17
  end
18
18
  end
19
19
  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(current_module_name, method_name, { 'file' => node.file, 'line_number' => node.line_number }, current_access_control)
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
@@ -3,6 +3,7 @@
3
3
  module RailsBestPractices
4
4
  module Prepares
5
5
  # Check all initializers
6
+
6
7
  class InitializerPrepare < Core::Check
7
8
  interesting_nodes :method_add_arg, :class
8
9
  interesting_files INITIALIZER_FILES
@@ -21,9 +22,8 @@ module RailsBestPractices
21
22
  # check if the node is
22
23
  # ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection)
23
24
  def include_forbidden_attributes_protection?(node)
24
- 'ActiveRecord::Base' == node.receiver.to_s &&
25
- 'send' == node.message.to_s &&
26
- ['include', 'ActiveModel::ForbiddenAttributesProtection'] == node.arguments.all.map(&:to_s)
25
+ node.receiver.to_s == 'ActiveRecord::Base' && node.message.to_s == 'send' &&
26
+ node.arguments.all.map(&:to_s) == ['include', 'ActiveModel::ForbiddenAttributesProtection']
27
27
  end
28
28
  end
29
29
  end
@@ -3,6 +3,7 @@
3
3
  module RailsBestPractices
4
4
  module Prepares
5
5
  # Remember the mailer names.
6
+
6
7
  class MailerPrepare < Core::Check
7
8
  include Core::Check::Classable
8
9
 
@@ -18,7 +19,7 @@ module RailsBestPractices
18
19
  # if it is a subclass of ActionMailer::Base,
19
20
  # then remember its class name.
20
21
  add_callback :start_class do |_node|
21
- if 'ActionMailer::Base' == current_extend_class_name
22
+ if current_extend_class_name == 'ActionMailer::Base'
22
23
  @mailers << @klass
23
24
  end
24
25
  end
@@ -3,6 +3,7 @@
3
3
  module RailsBestPractices
4
4
  module Prepares
5
5
  # Remember models and model associations.
6
+
6
7
  class ModelPrepare < Core::Check
7
8
  include Core::Check::Classable
8
9
  include Core::Check::Accessable
@@ -10,7 +11,17 @@ module RailsBestPractices
10
11
  interesting_nodes :class, :def, :defs, :command, :alias
11
12
  interesting_files MODEL_FILES
12
13
 
13
- ASSOCIATION_METHODS = %w[belongs_to has_one has_many has_and_belongs_to_many embeds_many embeds_one embedded_in many one].freeze
14
+ ASSOCIATION_METHODS = %w[
15
+ belongs_to
16
+ has_one
17
+ has_many
18
+ has_and_belongs_to_many
19
+ embeds_many
20
+ embeds_one
21
+ embedded_in
22
+ many
23
+ one
24
+ ].freeze
14
25
 
15
26
  def initialize
16
27
  @models = Prepares.models
@@ -21,7 +32,7 @@ module RailsBestPractices
21
32
 
22
33
  # remember the class name.
23
34
  add_callback :start_class do |_node|
24
- if 'ActionMailer::Base' != current_extend_class_name
35
+ if current_extend_class_name != 'ActionMailer::Base'
25
36
  @models << @klass
26
37
  end
27
38
  end
@@ -39,11 +50,14 @@ module RailsBestPractices
39
50
  # }
40
51
  # }
41
52
  add_callback :start_def do |node|
42
- if @klass &&
43
- 'ActionMailer::Base' != current_extend_class_name &&
44
- (classable_modules.empty? || klasses.any?)
53
+ if @klass && current_extend_class_name != 'ActionMailer::Base' && (classable_modules.empty? || klasses.any?)
45
54
  method_name = node.method_name.to_s
46
- @methods.add_method(current_class_name, method_name, { 'file' => node.file, 'line_number' => node.line_number }, current_access_control)
55
+ @methods.add_method(
56
+ current_class_name,
57
+ method_name,
58
+ { 'file' => node.file, 'line_number' => node.line_number },
59
+ current_access_control
60
+ )
47
61
  end
48
62
  end
49
63
 
@@ -60,9 +74,14 @@ module RailsBestPractices
60
74
  # }
61
75
  # }
62
76
  add_callback :start_defs do |node|
63
- if @klass && 'ActionMailer::Base' != current_extend_class_name
77
+ if @klass && current_extend_class_name != 'ActionMailer::Base'
64
78
  method_name = node.method_name.to_s
65
- @methods.add_method(current_class_name, method_name, { 'file' => node.file, 'line_number' => node.line_number }, current_access_control)
79
+ @methods.add_method(
80
+ current_class_name,
81
+ method_name,
82
+ { 'file' => node.file, 'line_number' => node.line_number },
83
+ current_access_control
84
+ )
66
85
  end
67
86
  end
68
87
 
@@ -81,15 +100,31 @@ module RailsBestPractices
81
100
  case node.message.to_s
82
101
  when 'named_scope', 'scope', 'alias_method'
83
102
  method_name = node.arguments.all.first.to_s
84
- @methods.add_method(current_class_name, method_name, { 'file' => node.file, 'line_number' => node.line_number }, current_access_control)
103
+ @methods.add_method(
104
+ current_class_name,
105
+ method_name,
106
+ { 'file' => node.file, 'line_number' => node.line_number },
107
+ current_access_control
108
+ )
85
109
  when 'alias_method_chain'
86
110
  method, feature = *node.arguments.all.map(&:to_s)
87
- @methods.add_method(current_class_name, "#{method}_with_#{feature}", { 'file' => node.file, 'line_number' => node.line_number }, current_access_control)
88
- @methods.add_method(current_class_name, method.to_s, { 'file' => node.file, 'line_number' => node.line_number }, current_access_control)
111
+ @methods.add_method(
112
+ current_class_name,
113
+ "#{method}_with_#{feature}",
114
+ { 'file' => node.file, 'line_number' => node.line_number },
115
+ current_access_control
116
+ )
117
+ @methods.add_method(
118
+ current_class_name,
119
+ method.to_s,
120
+ { 'file' => node.file, 'line_number' => node.line_number },
121
+ current_access_control
122
+ )
89
123
  when 'field'
90
124
  arguments = node.arguments.all
91
125
  attribute_name = arguments.first.to_s
92
- attribute_type = arguments.last.hash_value('type').present? ? arguments.last.hash_value('type').to_s : 'String'
126
+ attribute_type =
127
+ arguments.last.hash_value('type').present? ? arguments.last.hash_value('type').to_s : 'String'
93
128
  @model_attributes.add_attribute(current_class_name, attribute_name, attribute_type)
94
129
  when 'key'
95
130
  attribute_name, attribute_type = node.arguments.all.map(&:to_s)
@@ -102,7 +137,12 @@ module RailsBestPractices
102
137
  # check alias node to remembr the alias methods.
103
138
  add_callback :start_alias do |node|
104
139
  method_name = node.new_method.to_s
105
- @methods.add_method(current_class_name, method_name, { 'file' => node.file, 'line_number' => node.line_number }, current_access_control)
140
+ @methods.add_method(
141
+ current_class_name,
142
+ method_name,
143
+ { 'file' => node.file, 'line_number' => node.line_number },
144
+ current_access_control
145
+ )
106
146
  end
107
147
 
108
148
  # after prepare process, fix incorrect associations' class_name.
@@ -122,17 +162,17 @@ module RailsBestPractices
122
162
 
123
163
  private
124
164
 
125
- # remember associations, with class to association names.
126
- def remember_association(node)
127
- association_meta = node.message.to_s
128
- association_name = node.arguments.all.first.to_s
129
- arguments_node = node.arguments.all.last
130
- if arguments_node.hash_value('class_name').present?
131
- association_class = arguments_node.hash_value('class_name').to_s
132
- end
133
- association_class ||= association_name.classify
134
- @model_associations.add_association(current_class_name, association_name, association_meta, association_class)
165
+ # remember associations, with class to association names.
166
+ def remember_association(node)
167
+ association_meta = node.message.to_s
168
+ association_name = node.arguments.all.first.to_s
169
+ arguments_node = node.arguments.all.last
170
+ if arguments_node.hash_value('class_name').present?
171
+ association_class = arguments_node.hash_value('class_name').to_s
135
172
  end
173
+ association_class ||= association_name.classify
174
+ @model_associations.add_association(current_class_name, association_name, association_meta, association_class)
175
+ end
136
176
  end
137
177
  end
138
178
  end