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
@@ -5,9 +5,9 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Reviews
|
7
7
|
describe RemoveEmptyHelpersReview do
|
8
|
-
let(:runner) { Core::Runner.new(reviews:
|
8
|
+
let(:runner) { Core::Runner.new(reviews: described_class.new) }
|
9
9
|
|
10
|
-
it '
|
10
|
+
it 'removes empty helpers' do
|
11
11
|
content = <<-EOF
|
12
12
|
module PostsHelper
|
13
13
|
end
|
@@ -17,7 +17,7 @@ module RailsBestPractices
|
|
17
17
|
expect(runner.errors[0].to_s).to eq('app/helpers/posts_helper.rb:1 - remove empty helpers')
|
18
18
|
end
|
19
19
|
|
20
|
-
it '
|
20
|
+
it 'does not remove empty helpers' do
|
21
21
|
content = <<-EOF
|
22
22
|
module PostsHelper
|
23
23
|
def post_link(post)
|
@@ -29,7 +29,7 @@ module RailsBestPractices
|
|
29
29
|
expect(runner.errors.size).to eq(0)
|
30
30
|
end
|
31
31
|
|
32
|
-
it '
|
32
|
+
it 'does not remove empty application_helper' do
|
33
33
|
content = <<-EOF
|
34
34
|
module ApplicationHelper
|
35
35
|
end
|
@@ -38,8 +38,8 @@ module RailsBestPractices
|
|
38
38
|
expect(runner.errors.size).to eq(0)
|
39
39
|
end
|
40
40
|
|
41
|
-
it '
|
42
|
-
runner = Core::Runner.new(reviews:
|
41
|
+
it 'does not check ignored files' do
|
42
|
+
runner = Core::Runner.new(reviews: described_class.new(ignored_files: /posts_helper/))
|
43
43
|
content = <<-EOF
|
44
44
|
module PostsHelper
|
45
45
|
end
|
@@ -5,15 +5,15 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Reviews
|
7
7
|
describe RemoveUnusedMethodsInControllersReview do
|
8
|
-
let(:runner)
|
8
|
+
let(:runner) do
|
9
9
|
Core::Runner.new(
|
10
10
|
prepares: [Prepares::ControllerPrepare.new, Prepares::RoutePrepare.new],
|
11
|
-
reviews:
|
11
|
+
reviews: described_class.new('except_methods' => ['ExceptableController#*'])
|
12
12
|
)
|
13
|
-
|
13
|
+
end
|
14
14
|
|
15
15
|
context 'private/protected' do
|
16
|
-
it '
|
16
|
+
it 'removes unused methods' do
|
17
17
|
content = <<-EOF
|
18
18
|
RailsBestPracticesCom::Application.routes.draw do
|
19
19
|
resources :posts do
|
@@ -40,11 +40,15 @@ module RailsBestPractices
|
|
40
40
|
runner.review('app/controllers/posts_controller.rb', content)
|
41
41
|
runner.after_review
|
42
42
|
expect(runner.errors.size).to eq(2)
|
43
|
-
expect(runner.errors[0].to_s).to eq(
|
44
|
-
|
43
|
+
expect(runner.errors[0].to_s).to eq(
|
44
|
+
'app/controllers/posts_controller.rb:6 - remove unused methods (PostsController#load_post)'
|
45
|
+
)
|
46
|
+
expect(runner.errors[1].to_s).to eq(
|
47
|
+
'app/controllers/posts_controller.rb:8 - remove unused methods (PostsController#load_user)'
|
48
|
+
)
|
45
49
|
end
|
46
50
|
|
47
|
-
it '
|
51
|
+
it 'does not remove unused methods for before_filter' do
|
48
52
|
content = <<-EOF
|
49
53
|
RailsBestPracticesCom::Application.routes.draw do
|
50
54
|
resources :posts
|
@@ -66,7 +70,7 @@ module RailsBestPractices
|
|
66
70
|
expect(runner.errors.size).to eq(0)
|
67
71
|
end
|
68
72
|
|
69
|
-
it '
|
73
|
+
it 'does not remove unused methods for around_filter' do
|
70
74
|
content = <<-EOF
|
71
75
|
RailsBestPracticesCom::Application.routes.draw do
|
72
76
|
resources :posts
|
@@ -91,7 +95,7 @@ module RailsBestPractices
|
|
91
95
|
expect(runner.errors.size).to eq(0)
|
92
96
|
end
|
93
97
|
|
94
|
-
it '
|
98
|
+
it 'does not remove unused methods for around_action (new syntax)' do
|
95
99
|
content = <<-EOF
|
96
100
|
class PostsController < ActiveRecord::Base
|
97
101
|
around_action :use_time_zone
|
@@ -109,7 +113,7 @@ module RailsBestPractices
|
|
109
113
|
expect(runner.errors.size).to eq(0)
|
110
114
|
end
|
111
115
|
|
112
|
-
it '
|
116
|
+
it 'does not remove unused methods for layout' do
|
113
117
|
content = <<-EOF
|
114
118
|
RailsBestPracticesCom::Application.routes.draw do
|
115
119
|
resources :posts
|
@@ -131,7 +135,7 @@ module RailsBestPractices
|
|
131
135
|
expect(runner.errors.size).to eq(0)
|
132
136
|
end
|
133
137
|
|
134
|
-
it '
|
138
|
+
it 'does not remove inherited_resources methods' do
|
135
139
|
content = <<-EOF
|
136
140
|
RailsBestPracticesCom::Application.routes.draw do
|
137
141
|
resources :posts
|
@@ -155,7 +159,7 @@ module RailsBestPractices
|
|
155
159
|
end
|
156
160
|
|
157
161
|
context 'public' do
|
158
|
-
it '
|
162
|
+
it 'removes unused methods' do
|
159
163
|
content = <<-EOF
|
160
164
|
RailsBestPracticesCom::Application.routes.draw do
|
161
165
|
resources :posts
|
@@ -172,10 +176,12 @@ module RailsBestPractices
|
|
172
176
|
runner.review('app/controllers/posts_controller.rb', content)
|
173
177
|
runner.after_review
|
174
178
|
expect(runner.errors.size).to eq(1)
|
175
|
-
expect(runner.errors[0].to_s).to eq(
|
179
|
+
expect(runner.errors[0].to_s).to eq(
|
180
|
+
'app/controllers/posts_controller.rb:3 - remove unused methods (PostsController#list)'
|
181
|
+
)
|
176
182
|
end
|
177
183
|
|
178
|
-
it '
|
184
|
+
it 'does not remove inline routes' do
|
179
185
|
content = <<-EOF
|
180
186
|
RailsBestPracticesCom::Application.routes.draw do
|
181
187
|
resources :posts, only: :none do
|
@@ -196,7 +202,7 @@ module RailsBestPractices
|
|
196
202
|
expect(runner.errors.size).to eq(0)
|
197
203
|
end
|
198
204
|
|
199
|
-
it '
|
205
|
+
it 'does not remove unused methods if all actions are used in route' do
|
200
206
|
content = <<-EOF
|
201
207
|
ActionController::Routing::Routes.draw do |map|
|
202
208
|
map.connect 'internal/:action/*whatever', controller: "internal"
|
@@ -216,7 +222,7 @@ module RailsBestPractices
|
|
216
222
|
expect(runner.errors.size).to eq(0)
|
217
223
|
end
|
218
224
|
|
219
|
-
it '
|
225
|
+
it 'does not remove unused methods if they are except_methods' do
|
220
226
|
content = <<-EOF
|
221
227
|
class ExceptableController < ApplicationController
|
222
228
|
def list; end
|
@@ -230,7 +236,7 @@ module RailsBestPractices
|
|
230
236
|
end
|
231
237
|
|
232
238
|
context 'assignment' do
|
233
|
-
it '
|
239
|
+
it 'does not remove unused methods if call in base class' do
|
234
240
|
content = <<-EOF
|
235
241
|
RailsBestPracticesCom::Application.routes.draw do
|
236
242
|
resources :user, only: :show do; end
|
@@ -259,7 +265,7 @@ module RailsBestPractices
|
|
259
265
|
end
|
260
266
|
|
261
267
|
context 'helper_method' do
|
262
|
-
it '
|
268
|
+
it 'removes unused methods if helper method is not called' do
|
263
269
|
content = <<-EOF
|
264
270
|
class PostsController < ApplicationController
|
265
271
|
helper_method :helper_post
|
@@ -271,10 +277,12 @@ module RailsBestPractices
|
|
271
277
|
runner.review('app/controllers/posts_controller.rb', content)
|
272
278
|
runner.after_review
|
273
279
|
expect(runner.errors.size).to eq(1)
|
274
|
-
expect(runner.errors[0].to_s).to eq(
|
280
|
+
expect(runner.errors[0].to_s).to eq(
|
281
|
+
'app/controllers/posts_controller.rb:4 - remove unused methods (PostsController#helper_post)'
|
282
|
+
)
|
275
283
|
end
|
276
284
|
|
277
|
-
it '
|
285
|
+
it 'does not remove unused methods if call helper method in views' do
|
278
286
|
content = <<-EOF
|
279
287
|
class PostsController < ApplicationController
|
280
288
|
helper_method :helper_post
|
@@ -292,7 +300,7 @@ module RailsBestPractices
|
|
292
300
|
expect(runner.errors.size).to eq(0)
|
293
301
|
end
|
294
302
|
|
295
|
-
it '
|
303
|
+
it 'does not remove unused methods if call helper method in helpers' do
|
296
304
|
content = <<-EOF
|
297
305
|
class PostsController < ApplicationController
|
298
306
|
helper_method :helper_post
|
@@ -316,7 +324,7 @@ module RailsBestPractices
|
|
316
324
|
end
|
317
325
|
|
318
326
|
context 'delegate to: :controller' do
|
319
|
-
it '
|
327
|
+
it 'removes unused methods if delegate method is not called' do
|
320
328
|
content = <<-EOF
|
321
329
|
class PostsController < ApplicationController
|
322
330
|
protected
|
@@ -333,10 +341,12 @@ module RailsBestPractices
|
|
333
341
|
runner.review('app/helpers/posts_helper.rb', content)
|
334
342
|
runner.after_review
|
335
343
|
expect(runner.errors.size).to eq(1)
|
336
|
-
expect(runner.errors[0].to_s).to eq(
|
344
|
+
expect(runner.errors[0].to_s).to eq(
|
345
|
+
'app/controllers/posts_controller.rb:3 - remove unused methods (PostsController#helper_post)'
|
346
|
+
)
|
337
347
|
end
|
338
348
|
|
339
|
-
it '
|
349
|
+
it 'removes unused methods if delegate method is called' do
|
340
350
|
content = <<-EOF
|
341
351
|
class PostsController < ApplicationController
|
342
352
|
protected
|
@@ -361,7 +371,7 @@ module RailsBestPractices
|
|
361
371
|
end
|
362
372
|
|
363
373
|
context 'cells' do
|
364
|
-
it '
|
374
|
+
it 'removes unused methods' do
|
365
375
|
content = <<-EOF
|
366
376
|
class PostsCell < Cell::Rails
|
367
377
|
def list; end
|
@@ -374,7 +384,7 @@ module RailsBestPractices
|
|
374
384
|
expect(runner.errors[0].to_s).to eq('app/cells/posts_cell.rb:2 - remove unused methods (PostsCell#list)')
|
375
385
|
end
|
376
386
|
|
377
|
-
it '
|
387
|
+
it 'does not remove unused methods if render_cell' do
|
378
388
|
content = <<-EOF
|
379
389
|
class PostsCell < Cell::Rails
|
380
390
|
def list; end
|
@@ -392,7 +402,7 @@ module RailsBestPractices
|
|
392
402
|
expect(runner.errors.size).to eq(0)
|
393
403
|
end
|
394
404
|
|
395
|
-
it '
|
405
|
+
it 'does not remove unused methods if render with state' do
|
396
406
|
content = <<-EOF
|
397
407
|
class PostsCell < Cell::Rails
|
398
408
|
def list
|
@@ -415,7 +425,7 @@ module RailsBestPractices
|
|
415
425
|
end
|
416
426
|
end
|
417
427
|
|
418
|
-
it '
|
428
|
+
it 'does not remove unused methods' do
|
419
429
|
route_content = <<-EOF
|
420
430
|
RailsBestPracticesCom::Application.routes.draw do
|
421
431
|
namespace :admin do
|
@@ -452,9 +462,12 @@ module RailsBestPractices
|
|
452
462
|
expect(runner.errors.size).to eq(0)
|
453
463
|
end
|
454
464
|
|
455
|
-
it '
|
456
|
-
runner =
|
457
|
-
|
465
|
+
it 'does not check ignored files' do
|
466
|
+
runner =
|
467
|
+
Core::Runner.new(
|
468
|
+
prepares: [Prepares::ControllerPrepare.new, Prepares::RoutePrepare.new],
|
469
|
+
reviews: described_class.new(ignored_files: /posts_controller/, except_methods: [])
|
470
|
+
)
|
458
471
|
content = <<-EOF
|
459
472
|
RailsBestPracticesCom::Application.routes.draw do
|
460
473
|
resources :posts do
|
@@ -5,14 +5,14 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Reviews
|
7
7
|
describe RemoveUnusedMethodsInHelpersReview do
|
8
|
-
let(:runner)
|
8
|
+
let(:runner) do
|
9
9
|
Core::Runner.new(
|
10
10
|
prepares: [Prepares::ControllerPrepare.new, Prepares::HelperPrepare.new],
|
11
|
-
reviews:
|
11
|
+
reviews: described_class.new(except_methods: [])
|
12
12
|
)
|
13
|
-
|
13
|
+
end
|
14
14
|
|
15
|
-
it '
|
15
|
+
it 'removes unused methods' do
|
16
16
|
content = <<-EOF
|
17
17
|
module PostsHelper
|
18
18
|
def unused; end
|
@@ -22,10 +22,12 @@ module RailsBestPractices
|
|
22
22
|
runner.review('app/helpers/posts_helper.rb', content)
|
23
23
|
runner.after_review
|
24
24
|
expect(runner.errors.size).to eq(1)
|
25
|
-
expect(runner.errors[0].to_s).to eq(
|
25
|
+
expect(runner.errors[0].to_s).to eq(
|
26
|
+
'app/helpers/posts_helper.rb:2 - remove unused methods (PostsHelper#unused)'
|
27
|
+
)
|
26
28
|
end
|
27
29
|
|
28
|
-
it '
|
30
|
+
it 'does not remove unused methods if called on views' do
|
29
31
|
content = <<-EOF
|
30
32
|
module PostsHelper
|
31
33
|
def used?(post); end
|
@@ -42,7 +44,7 @@ module RailsBestPractices
|
|
42
44
|
expect(runner.errors.size).to eq(0)
|
43
45
|
end
|
44
46
|
|
45
|
-
it '
|
47
|
+
it 'does not remove unused methods if called on helpers' do
|
46
48
|
content = <<-EOF
|
47
49
|
module PostsHelper
|
48
50
|
def used?(post)
|
@@ -63,7 +65,7 @@ module RailsBestPractices
|
|
63
65
|
expect(runner.errors.size).to eq(0)
|
64
66
|
end
|
65
67
|
|
66
|
-
it '
|
68
|
+
it 'does not remove unused methods if called on controllers' do
|
67
69
|
helper_content = <<-EOF
|
68
70
|
module PostsHelper
|
69
71
|
def used?(post); end
|
@@ -88,7 +90,7 @@ module RailsBestPractices
|
|
88
90
|
expect(runner.errors.size).to eq(0)
|
89
91
|
end
|
90
92
|
|
91
|
-
it '
|
93
|
+
it 'does not remove unused methods if called in descendant controllers' do
|
92
94
|
application_helper_content = <<-EOF
|
93
95
|
module ApplicationHelper
|
94
96
|
def admin?; end
|
@@ -118,9 +120,12 @@ module RailsBestPractices
|
|
118
120
|
expect(runner.errors.size).to eq(0)
|
119
121
|
end
|
120
122
|
|
121
|
-
it '
|
122
|
-
runner =
|
123
|
-
|
123
|
+
it 'does not check ignored files' do
|
124
|
+
runner =
|
125
|
+
Core::Runner.new(
|
126
|
+
prepares: [Prepares::ControllerPrepare.new, Prepares::HelperPrepare.new],
|
127
|
+
reviews: described_class.new(ignored_files: /posts_helper/, except_methods: [])
|
128
|
+
)
|
124
129
|
|
125
130
|
content = <<-EOF
|
126
131
|
module PostsHelper
|
@@ -5,15 +5,14 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Reviews
|
7
7
|
describe RemoveUnusedMethodsInModelsReview do
|
8
|
-
let(:runner)
|
8
|
+
let(:runner) do
|
9
9
|
Core::Runner.new(
|
10
|
-
prepares: Prepares::ModelPrepare.new,
|
11
|
-
reviews: RemoveUnusedMethodsInModelsReview.new('except_methods' => ['*#set_cache'])
|
10
|
+
prepares: Prepares::ModelPrepare.new, reviews: described_class.new('except_methods' => ['*#set_cache'])
|
12
11
|
)
|
13
|
-
|
12
|
+
end
|
14
13
|
|
15
14
|
context 'private' do
|
16
|
-
it '
|
15
|
+
it 'removes unused methods' do
|
17
16
|
content = <<-EOF
|
18
17
|
class Post < ActiveRecord::Base
|
19
18
|
def find; end
|
@@ -36,7 +35,7 @@ module RailsBestPractices
|
|
36
35
|
expect(runner.errors[0].to_s).to eq('app/models/post.rb:4 - remove unused methods (Post#find_by_sql)')
|
37
36
|
end
|
38
37
|
|
39
|
-
it '
|
38
|
+
it 'does not remove unused methods with except_methods' do
|
40
39
|
content = <<-EOF
|
41
40
|
class Post < ActiveRecord::Base
|
42
41
|
def set_cache; end
|
@@ -48,7 +47,7 @@ module RailsBestPractices
|
|
48
47
|
expect(runner.errors.size).to eq(0)
|
49
48
|
end
|
50
49
|
|
51
|
-
it '
|
50
|
+
it 'does not remove unused methods with var_ref' do
|
52
51
|
content = <<-EOF
|
53
52
|
class Post < ActiveRecord::Base
|
54
53
|
def find
|
@@ -72,7 +71,7 @@ module RailsBestPractices
|
|
72
71
|
expect(runner.errors.size).to eq(0)
|
73
72
|
end
|
74
73
|
|
75
|
-
it '
|
74
|
+
it 'does not remove unused methods with callback' do
|
76
75
|
content = <<-EOF
|
77
76
|
class Post < ActiveRecord::Base
|
78
77
|
after_save :expire_cache
|
@@ -86,7 +85,7 @@ module RailsBestPractices
|
|
86
85
|
expect(runner.errors.size).to eq(0)
|
87
86
|
end
|
88
87
|
|
89
|
-
it '
|
88
|
+
it 'does not remove unused method with command' do
|
90
89
|
content = <<-EOF
|
91
90
|
class Post < ActiveRecord::Base
|
92
91
|
def fetch
|
@@ -110,7 +109,7 @@ module RailsBestPractices
|
|
110
109
|
expect(runner.errors.size).to eq(0)
|
111
110
|
end
|
112
111
|
|
113
|
-
it '
|
112
|
+
it 'does not remove unused method with call' do
|
114
113
|
content = <<-EOF
|
115
114
|
class Post < ActiveRecord::Base
|
116
115
|
def conditions
|
@@ -134,7 +133,7 @@ module RailsBestPractices
|
|
134
133
|
expect(runner.errors.size).to eq(0)
|
135
134
|
end
|
136
135
|
|
137
|
-
it '
|
136
|
+
it 'does not remove unused method with message' do
|
138
137
|
content = <<-EOF
|
139
138
|
class Post < ActiveRecord::Base
|
140
139
|
def save
|
@@ -160,7 +159,7 @@ module RailsBestPractices
|
|
160
159
|
expect(runner.errors.size).to eq(0)
|
161
160
|
end
|
162
161
|
|
163
|
-
it '
|
162
|
+
it 'does not remove unused method with validation condition' do
|
164
163
|
content = <<-EOF
|
165
164
|
class Post < ActiveRecord::Base
|
166
165
|
validates_uniqueness_of :login, if: :email_blank?
|
@@ -174,7 +173,7 @@ module RailsBestPractices
|
|
174
173
|
expect(runner.errors.size).to eq(0)
|
175
174
|
end
|
176
175
|
|
177
|
-
it '
|
176
|
+
it 'does not remove unused method with aasm' do
|
178
177
|
content = <<-EOF
|
179
178
|
class Post < ActiveRecord::Base
|
180
179
|
aasm_state :accepted, enter: [:update_datetime]
|
@@ -188,7 +187,7 @@ module RailsBestPractices
|
|
188
187
|
expect(runner.errors.size).to eq(0)
|
189
188
|
end
|
190
189
|
|
191
|
-
it '
|
190
|
+
it 'does not remove unused method with initialize' do
|
192
191
|
content = <<-EOF
|
193
192
|
class Post < ActiveRecord::Base
|
194
193
|
private
|
@@ -203,7 +202,7 @@ module RailsBestPractices
|
|
203
202
|
end
|
204
203
|
|
205
204
|
context 'public' do
|
206
|
-
it '
|
205
|
+
it 'removes unused methods' do
|
207
206
|
content = <<-EOF
|
208
207
|
class Post < ActiveRecord::Base
|
209
208
|
def fetch; end
|
@@ -215,7 +214,7 @@ module RailsBestPractices
|
|
215
214
|
expect(runner.errors[0].to_s).to eq('app/models/post.rb:2 - remove unused methods (Post#fetch)')
|
216
215
|
end
|
217
216
|
|
218
|
-
it '
|
217
|
+
it 'does not remove unused methods' do
|
219
218
|
content = <<-EOF
|
220
219
|
class Post < ActiveRecord::Base
|
221
220
|
def fetch; end
|
@@ -234,7 +233,7 @@ module RailsBestPractices
|
|
234
233
|
expect(runner.errors.size).to eq(0)
|
235
234
|
end
|
236
235
|
|
237
|
-
it '
|
236
|
+
it 'does not remove unused methods for attribute assignment' do
|
238
237
|
content = <<-EOF
|
239
238
|
class Post < ActiveRecord::Base
|
240
239
|
def user=(user); end
|
@@ -246,7 +245,7 @@ module RailsBestPractices
|
|
246
245
|
expect(runner.errors.size).to eq(0)
|
247
246
|
end
|
248
247
|
|
249
|
-
it '
|
248
|
+
it 'does not remove unused methods for try' do
|
250
249
|
content = <<-EOF
|
251
250
|
class Post < ActiveRecord::Base
|
252
251
|
def find(user_id); end
|
@@ -266,7 +265,7 @@ module RailsBestPractices
|
|
266
265
|
expect(runner.errors.size).to eq(0)
|
267
266
|
end
|
268
267
|
|
269
|
-
it '
|
268
|
+
it 'does not remove unused methods for send' do
|
270
269
|
content = <<-EOF
|
271
270
|
class Post < ActiveRecord::Base
|
272
271
|
def find(user_id); end
|
@@ -286,7 +285,7 @@ module RailsBestPractices
|
|
286
285
|
expect(runner.errors.size).to eq(0)
|
287
286
|
end
|
288
287
|
|
289
|
-
it '
|
288
|
+
it 'removes unused methods for send string_embexpre' do
|
290
289
|
content = <<-EOF
|
291
290
|
class Post < ActiveRecord::Base
|
292
291
|
def find_first; end
|
@@ -307,7 +306,7 @@ module RailsBestPractices
|
|
307
306
|
expect(runner.errors.size).to eq(1)
|
308
307
|
end
|
309
308
|
|
310
|
-
it '
|
309
|
+
it 'removes unused methods for send variable' do
|
311
310
|
content = <<-EOF
|
312
311
|
class Post < ActiveRecord::Base
|
313
312
|
def first; end
|
@@ -330,7 +329,7 @@ module RailsBestPractices
|
|
330
329
|
end
|
331
330
|
|
332
331
|
context 'protected' do
|
333
|
-
it '
|
332
|
+
it 'does not remove unused methods' do
|
334
333
|
content = <<-EOF
|
335
334
|
class Post < ActiveRecord::Base
|
336
335
|
protected
|
@@ -352,7 +351,7 @@ module RailsBestPractices
|
|
352
351
|
expect(runner.errors[0].to_s).to eq('app/models/post.rb:3 - remove unused methods (Post#test)')
|
353
352
|
end
|
354
353
|
|
355
|
-
it '
|
354
|
+
it 'does not remove unused methods' do
|
356
355
|
post_content = <<-EOF
|
357
356
|
class Post < ActiveRecord::Base
|
358
357
|
protected
|
@@ -384,7 +383,7 @@ module RailsBestPractices
|
|
384
383
|
end
|
385
384
|
|
386
385
|
context 'named_scope' do
|
387
|
-
it '
|
386
|
+
it 'does not remove unused named_scope' do
|
388
387
|
content = <<-EOF
|
389
388
|
class Post < ActiveRecord::Base
|
390
389
|
named_scope :active, conditions: {active: true}
|
@@ -404,7 +403,7 @@ module RailsBestPractices
|
|
404
403
|
expect(runner.errors.size).to eq(0)
|
405
404
|
end
|
406
405
|
|
407
|
-
it '
|
406
|
+
it 'removes unused named_scope' do
|
408
407
|
content = <<-EOF
|
409
408
|
class Post < ActiveRecord::Base
|
410
409
|
named_scope :active, conditions: {active: true}
|
@@ -419,7 +418,7 @@ module RailsBestPractices
|
|
419
418
|
end
|
420
419
|
|
421
420
|
context 'scope' do
|
422
|
-
it '
|
421
|
+
it 'does not remove unused scope' do
|
423
422
|
content = <<-EOF
|
424
423
|
class Post < ActiveRecord::Base
|
425
424
|
scope :active, where(active: true)
|
@@ -439,7 +438,7 @@ module RailsBestPractices
|
|
439
438
|
expect(runner.errors.size).to eq(0)
|
440
439
|
end
|
441
440
|
|
442
|
-
it '
|
441
|
+
it 'removes unused named_scope' do
|
443
442
|
content = <<-EOF
|
444
443
|
class Post < ActiveRecord::Base
|
445
444
|
scope :active, where(active: true)
|
@@ -454,7 +453,7 @@ module RailsBestPractices
|
|
454
453
|
end
|
455
454
|
|
456
455
|
context 'alias' do
|
457
|
-
it '
|
456
|
+
it 'does not remove unused method with alias' do
|
458
457
|
content = <<-EOF
|
459
458
|
class Post < ActiveRecord::Base
|
460
459
|
def old; end
|
@@ -475,7 +474,7 @@ module RailsBestPractices
|
|
475
474
|
expect(runner.errors.size).to eq(0)
|
476
475
|
end
|
477
476
|
|
478
|
-
it '
|
477
|
+
it 'does not remove unused method with symbol alias' do
|
479
478
|
content = <<-EOF
|
480
479
|
class Post < ActiveRecord::Base
|
481
480
|
def old; end
|
@@ -496,7 +495,7 @@ module RailsBestPractices
|
|
496
495
|
expect(runner.errors.size).to eq(0)
|
497
496
|
end
|
498
497
|
|
499
|
-
it '
|
498
|
+
it 'does not remove unused method with alias_method' do
|
500
499
|
content = <<-EOF
|
501
500
|
class Post < ActiveRecord::Base
|
502
501
|
def old; end
|
@@ -517,7 +516,7 @@ module RailsBestPractices
|
|
517
516
|
expect(runner.errors.size).to eq(0)
|
518
517
|
end
|
519
518
|
|
520
|
-
it '
|
519
|
+
it 'does not remove unused method with alias_method_chain' do
|
521
520
|
content = <<-EOF
|
522
521
|
class Post < ActiveRecord::Base
|
523
522
|
def method_with_feature; end
|
@@ -540,7 +539,7 @@ module RailsBestPractices
|
|
540
539
|
end
|
541
540
|
|
542
541
|
context 'methods hash' do
|
543
|
-
it '
|
542
|
+
it 'does not remove unused method with methods hash' do
|
544
543
|
content = <<-EOF
|
545
544
|
class Post < ActiveRecord::Base
|
546
545
|
def to_xml(options = {})
|
@@ -558,7 +557,7 @@ module RailsBestPractices
|
|
558
557
|
end
|
559
558
|
|
560
559
|
context 'callbacks' do
|
561
|
-
it '
|
560
|
+
it 'does not remove unused method' do
|
562
561
|
content = <<-EOF
|
563
562
|
class Post < ActiveRecord::Base
|
564
563
|
before_save :init_columns
|
@@ -577,7 +576,7 @@ module RailsBestPractices
|
|
577
576
|
end
|
578
577
|
|
579
578
|
context 'validates' do
|
580
|
-
it '
|
579
|
+
it 'does not remove unused method' do
|
581
580
|
content = <<-EOF
|
582
581
|
class Post < ActiveRecord::Base
|
583
582
|
validate :valid_birth_date
|
@@ -592,7 +591,7 @@ module RailsBestPractices
|
|
592
591
|
expect(runner.errors.size).to eq(0)
|
593
592
|
end
|
594
593
|
|
595
|
-
it '
|
594
|
+
it 'does not remove unused method for validate_on_create and validate_on_update' do
|
596
595
|
content = <<-EOF
|
597
596
|
class Post < ActiveRecord::Base
|
598
597
|
validate_on_create :valid_email
|
@@ -609,7 +608,7 @@ module RailsBestPractices
|
|
609
608
|
expect(runner.errors.size).to eq(0)
|
610
609
|
end
|
611
610
|
|
612
|
-
it '
|
611
|
+
it 'does not remove unused methods for to_param' do
|
613
612
|
content = <<-EOF
|
614
613
|
class Post < ActiveRecord::Base
|
615
614
|
def to_param
|
@@ -625,7 +624,7 @@ module RailsBestPractices
|
|
625
624
|
end
|
626
625
|
|
627
626
|
context 'helper method' do
|
628
|
-
it '
|
627
|
+
it 'does not remove unused method for coommand_call collection_select' do
|
629
628
|
content = <<-EOF
|
630
629
|
class Category < ActiveRecord::Base
|
631
630
|
def indented_name; end
|
@@ -641,7 +640,7 @@ module RailsBestPractices
|
|
641
640
|
expect(runner.errors.size).to eq(0)
|
642
641
|
end
|
643
642
|
|
644
|
-
it '
|
643
|
+
it 'does not remove unused method for command collection_select' do
|
645
644
|
content = <<-EOF
|
646
645
|
class Category < ActiveRecord::Base
|
647
646
|
def indented_name; end
|
@@ -657,7 +656,7 @@ module RailsBestPractices
|
|
657
656
|
expect(runner.errors.size).to eq(0)
|
658
657
|
end
|
659
658
|
|
660
|
-
it '
|
659
|
+
it 'does not remove unused method for options_from_collection_for_select' do
|
661
660
|
content = <<-EOF
|
662
661
|
class Category < ActiveRecord::Base
|
663
662
|
def indented_name; end
|
@@ -674,7 +673,7 @@ module RailsBestPractices
|
|
674
673
|
end
|
675
674
|
end
|
676
675
|
|
677
|
-
it '
|
676
|
+
it 'does not remove unused methods for rabl view' do
|
678
677
|
content = <<-EOF
|
679
678
|
class User
|
680
679
|
def first_name; end
|
@@ -693,7 +692,7 @@ module RailsBestPractices
|
|
693
692
|
expect(runner.errors.size).to eq(0)
|
694
693
|
end
|
695
694
|
|
696
|
-
it '
|
695
|
+
it 'does not skip :call as call message' do
|
697
696
|
content = <<-EOF
|
698
697
|
module DateRange
|
699
698
|
RANGES = lambda {
|
@@ -708,9 +707,12 @@ module RailsBestPractices
|
|
708
707
|
runner.review('app/mixins/date_range.rb', content)
|
709
708
|
end
|
710
709
|
|
711
|
-
it '
|
712
|
-
runner =
|
713
|
-
|
710
|
+
it 'does not check ignored files' do
|
711
|
+
runner =
|
712
|
+
Core::Runner.new(
|
713
|
+
prepares: Prepares::ModelPrepare.new,
|
714
|
+
reviews: described_class.new(except_methods: [], ignored_files: /post/)
|
715
|
+
)
|
714
716
|
|
715
717
|
content = <<-EOF
|
716
718
|
class Post < ActiveRecord::Base
|