rails_best_practices 1.19.2 → 1.20.1
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 +7 -7
- data/Gemfile +3 -5
- data/Gemfile.lock +125 -0
- data/Guardfile +2 -0
- data/README.md +6 -6
- data/Rakefile +2 -17
- data/assets/result.html.erb +2 -0
- data/lib/rails_best_practices.rb +3 -2
- data/lib/rails_best_practices/analyzer.rb +61 -49
- 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 +64 -56
- 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 +67 -73
- 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 +24 -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 -89
- 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 +26 -19
- 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 +18 -15
- 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 +68 -66
- 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 +38 -43
- 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/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 +64 -31
- 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 +16 -18
@@ -5,10 +5,14 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Reviews
|
7
7
|
describe ProtectMassAssignmentReview do
|
8
|
-
let(:runner)
|
9
|
-
|
8
|
+
let(:runner) do
|
9
|
+
Core::Runner.new(
|
10
|
+
prepares: [Prepares::GemfilePrepare.new, Prepares::ConfigPrepare.new, Prepares::InitializerPrepare.new],
|
11
|
+
reviews: described_class.new
|
12
|
+
)
|
13
|
+
end
|
10
14
|
|
11
|
-
it '
|
15
|
+
it 'protects mass assignment' do
|
12
16
|
content = <<-EOF
|
13
17
|
class User < ActiveRecord::Base
|
14
18
|
end
|
@@ -18,7 +22,7 @@ module RailsBestPractices
|
|
18
22
|
expect(runner.errors[0].to_s).to eq('app/models/user.rb:1 - protect mass assignment')
|
19
23
|
end
|
20
24
|
|
21
|
-
it '
|
25
|
+
it 'does not protect mass assignment if attr_accessible is used with arguments and user set config.active_record.whitelist_attributes' do
|
22
26
|
content = <<-EOF
|
23
27
|
module RailsBestPracticesCom
|
24
28
|
class Application < Rails::Application
|
@@ -36,7 +40,7 @@ module RailsBestPractices
|
|
36
40
|
expect(runner.errors.size).to eq(0)
|
37
41
|
end
|
38
42
|
|
39
|
-
it '
|
43
|
+
it 'does not protect mass assignment if attr_accessible is used without arguments and user set config.active_record.whitelist_attributes' do
|
40
44
|
content = <<-EOF
|
41
45
|
module RailsBestPracticesCom
|
42
46
|
class Application < Rails::Application
|
@@ -54,7 +58,7 @@ module RailsBestPractices
|
|
54
58
|
expect(runner.errors.size).to eq(0)
|
55
59
|
end
|
56
60
|
|
57
|
-
it '
|
61
|
+
it 'does not protect mass assignment with attr_protected if user set config.active_record.whitelist_attributes' do
|
58
62
|
content = <<-EOF
|
59
63
|
module RailsBestPracticesCom
|
60
64
|
class Application < Rails::Application
|
@@ -72,7 +76,7 @@ module RailsBestPractices
|
|
72
76
|
expect(runner.errors.size).to eq(0)
|
73
77
|
end
|
74
78
|
|
75
|
-
it '
|
79
|
+
it 'does not protect mass assignment if using devise' do
|
76
80
|
content = <<-EOF
|
77
81
|
class User < ActiveRecord::Base
|
78
82
|
devise :database_authenticatable, :registerable, :confirmable, :recoverable, stretches: 20
|
@@ -82,7 +86,7 @@ module RailsBestPractices
|
|
82
86
|
expect(runner.errors.size).to eq(0)
|
83
87
|
end
|
84
88
|
|
85
|
-
it '
|
89
|
+
it 'does not protect mass assignment if using authlogic with configuration' do
|
86
90
|
content = <<-EOF
|
87
91
|
class User < ActiveRecord::Base
|
88
92
|
acts_as_authentic do |c|
|
@@ -94,7 +98,7 @@ module RailsBestPractices
|
|
94
98
|
expect(runner.errors.size).to eq(0)
|
95
99
|
end
|
96
100
|
|
97
|
-
it '
|
101
|
+
it 'does not protect mass assignment if using authlogic without configuration' do
|
98
102
|
content = <<-EOF
|
99
103
|
class User < ActiveRecord::Base
|
100
104
|
acts_as_authentic
|
@@ -104,7 +108,7 @@ module RailsBestPractices
|
|
104
108
|
expect(runner.errors.size).to eq(0)
|
105
109
|
end
|
106
110
|
|
107
|
-
it '
|
111
|
+
it 'does not protect mass assignment if checking non ActiveRecord::Base inherited model' do
|
108
112
|
content = <<-EOF
|
109
113
|
class User < Person
|
110
114
|
end
|
@@ -114,7 +118,7 @@ module RailsBestPractices
|
|
114
118
|
end
|
115
119
|
|
116
120
|
context 'strong_parameters' do
|
117
|
-
it '
|
121
|
+
it 'does not protect mass assignment for strong_parameters' do
|
118
122
|
content = <<-EOF
|
119
123
|
class User < ActiveRecord::Base
|
120
124
|
include ActiveModel::ForbiddenAttributesProtection
|
@@ -124,7 +128,7 @@ module RailsBestPractices
|
|
124
128
|
expect(runner.errors.size).to eq(0)
|
125
129
|
end
|
126
130
|
|
127
|
-
it '
|
131
|
+
it 'does not protect mass assignment for strong_parameters' do
|
128
132
|
content = <<-EOF
|
129
133
|
class AR
|
130
134
|
ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection)
|
@@ -141,7 +145,7 @@ module RailsBestPractices
|
|
141
145
|
end
|
142
146
|
|
143
147
|
context 'activerecord 4' do
|
144
|
-
it '
|
148
|
+
it 'does not protect mass assignment for activerecord 4' do
|
145
149
|
content = <<-EOF
|
146
150
|
GEM
|
147
151
|
remote: https://rubygems.org
|
@@ -157,7 +161,7 @@ module RailsBestPractices
|
|
157
161
|
expect(runner.errors.size).to eq(0)
|
158
162
|
end
|
159
163
|
|
160
|
-
it '
|
164
|
+
it 'protects mass assignment for activerecord 3' do
|
161
165
|
content = <<-EOF
|
162
166
|
GEM
|
163
167
|
remote: https://rubygems.org
|
@@ -174,9 +178,12 @@ module RailsBestPractices
|
|
174
178
|
end
|
175
179
|
end
|
176
180
|
|
177
|
-
it '
|
178
|
-
runner =
|
179
|
-
|
181
|
+
it 'does not check ignored files' do
|
182
|
+
runner =
|
183
|
+
Core::Runner.new(
|
184
|
+
prepares: [Prepares::GemfilePrepare.new, Prepares::ConfigPrepare.new, Prepares::InitializerPrepare.new],
|
185
|
+
reviews: described_class.new(ignored_files: %r{app/models/user\.rb})
|
186
|
+
)
|
180
187
|
content = <<-EOF
|
181
188
|
class User < ActiveRecord::Base
|
182
189
|
end
|
@@ -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,13 +5,15 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Reviews
|
7
7
|
describe RemoveUnusedMethodsInControllersReview do
|
8
|
-
let(:runner)
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
let(:runner) do
|
9
|
+
Core::Runner.new(
|
10
|
+
prepares: [Prepares::ControllerPrepare.new, Prepares::RoutePrepare.new],
|
11
|
+
reviews: described_class.new('except_methods' => ['ExceptableController#*'])
|
12
|
+
)
|
13
|
+
end
|
12
14
|
|
13
15
|
context 'private/protected' do
|
14
|
-
it '
|
16
|
+
it 'removes unused methods' do
|
15
17
|
content = <<-EOF
|
16
18
|
RailsBestPracticesCom::Application.routes.draw do
|
17
19
|
resources :posts do
|
@@ -38,11 +40,15 @@ module RailsBestPractices
|
|
38
40
|
runner.review('app/controllers/posts_controller.rb', content)
|
39
41
|
runner.after_review
|
40
42
|
expect(runner.errors.size).to eq(2)
|
41
|
-
expect(runner.errors[0].to_s).to eq(
|
42
|
-
|
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
|
+
)
|
43
49
|
end
|
44
50
|
|
45
|
-
it '
|
51
|
+
it 'does not remove unused methods for before_filter' do
|
46
52
|
content = <<-EOF
|
47
53
|
RailsBestPracticesCom::Application.routes.draw do
|
48
54
|
resources :posts
|
@@ -64,7 +70,7 @@ module RailsBestPractices
|
|
64
70
|
expect(runner.errors.size).to eq(0)
|
65
71
|
end
|
66
72
|
|
67
|
-
it '
|
73
|
+
it 'does not remove unused methods for around_filter' do
|
68
74
|
content = <<-EOF
|
69
75
|
RailsBestPracticesCom::Application.routes.draw do
|
70
76
|
resources :posts
|
@@ -89,7 +95,25 @@ module RailsBestPractices
|
|
89
95
|
expect(runner.errors.size).to eq(0)
|
90
96
|
end
|
91
97
|
|
92
|
-
it '
|
98
|
+
it 'does not remove unused methods for around_action (new syntax)' do
|
99
|
+
content = <<-EOF
|
100
|
+
class PostsController < ActiveRecord::Base
|
101
|
+
around_action :use_time_zone
|
102
|
+
|
103
|
+
protected
|
104
|
+
|
105
|
+
def use_time_zone(&block)
|
106
|
+
Time.use_zone(current_user.time_zone, &block)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
EOF
|
110
|
+
runner.prepare('app/controllers/posts_controller.rb', content)
|
111
|
+
runner.review('app/controllers/posts_controller.rb', content)
|
112
|
+
runner.after_review
|
113
|
+
expect(runner.errors.size).to eq(0)
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'does not remove unused methods for layout' do
|
93
117
|
content = <<-EOF
|
94
118
|
RailsBestPracticesCom::Application.routes.draw do
|
95
119
|
resources :posts
|
@@ -111,7 +135,7 @@ module RailsBestPractices
|
|
111
135
|
expect(runner.errors.size).to eq(0)
|
112
136
|
end
|
113
137
|
|
114
|
-
it '
|
138
|
+
it 'does not remove inherited_resources methods' do
|
115
139
|
content = <<-EOF
|
116
140
|
RailsBestPracticesCom::Application.routes.draw do
|
117
141
|
resources :posts
|
@@ -135,7 +159,7 @@ module RailsBestPractices
|
|
135
159
|
end
|
136
160
|
|
137
161
|
context 'public' do
|
138
|
-
it '
|
162
|
+
it 'removes unused methods' do
|
139
163
|
content = <<-EOF
|
140
164
|
RailsBestPracticesCom::Application.routes.draw do
|
141
165
|
resources :posts
|
@@ -152,10 +176,12 @@ module RailsBestPractices
|
|
152
176
|
runner.review('app/controllers/posts_controller.rb', content)
|
153
177
|
runner.after_review
|
154
178
|
expect(runner.errors.size).to eq(1)
|
155
|
-
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
|
+
)
|
156
182
|
end
|
157
183
|
|
158
|
-
it '
|
184
|
+
it 'does not remove inline routes' do
|
159
185
|
content = <<-EOF
|
160
186
|
RailsBestPracticesCom::Application.routes.draw do
|
161
187
|
resources :posts, only: :none do
|
@@ -176,7 +202,7 @@ module RailsBestPractices
|
|
176
202
|
expect(runner.errors.size).to eq(0)
|
177
203
|
end
|
178
204
|
|
179
|
-
it '
|
205
|
+
it 'does not remove unused methods if all actions are used in route' do
|
180
206
|
content = <<-EOF
|
181
207
|
ActionController::Routing::Routes.draw do |map|
|
182
208
|
map.connect 'internal/:action/*whatever', controller: "internal"
|
@@ -196,7 +222,7 @@ module RailsBestPractices
|
|
196
222
|
expect(runner.errors.size).to eq(0)
|
197
223
|
end
|
198
224
|
|
199
|
-
it '
|
225
|
+
it 'does not remove unused methods if they are except_methods' do
|
200
226
|
content = <<-EOF
|
201
227
|
class ExceptableController < ApplicationController
|
202
228
|
def list; end
|
@@ -210,7 +236,7 @@ module RailsBestPractices
|
|
210
236
|
end
|
211
237
|
|
212
238
|
context 'assignment' do
|
213
|
-
it '
|
239
|
+
it 'does not remove unused methods if call in base class' do
|
214
240
|
content = <<-EOF
|
215
241
|
RailsBestPracticesCom::Application.routes.draw do
|
216
242
|
resources :user, only: :show do; end
|
@@ -239,7 +265,7 @@ module RailsBestPractices
|
|
239
265
|
end
|
240
266
|
|
241
267
|
context 'helper_method' do
|
242
|
-
it '
|
268
|
+
it 'removes unused methods if helper method is not called' do
|
243
269
|
content = <<-EOF
|
244
270
|
class PostsController < ApplicationController
|
245
271
|
helper_method :helper_post
|
@@ -251,10 +277,12 @@ module RailsBestPractices
|
|
251
277
|
runner.review('app/controllers/posts_controller.rb', content)
|
252
278
|
runner.after_review
|
253
279
|
expect(runner.errors.size).to eq(1)
|
254
|
-
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
|
+
)
|
255
283
|
end
|
256
284
|
|
257
|
-
it '
|
285
|
+
it 'does not remove unused methods if call helper method in views' do
|
258
286
|
content = <<-EOF
|
259
287
|
class PostsController < ApplicationController
|
260
288
|
helper_method :helper_post
|
@@ -272,7 +300,7 @@ module RailsBestPractices
|
|
272
300
|
expect(runner.errors.size).to eq(0)
|
273
301
|
end
|
274
302
|
|
275
|
-
it '
|
303
|
+
it 'does not remove unused methods if call helper method in helpers' do
|
276
304
|
content = <<-EOF
|
277
305
|
class PostsController < ApplicationController
|
278
306
|
helper_method :helper_post
|
@@ -296,7 +324,7 @@ module RailsBestPractices
|
|
296
324
|
end
|
297
325
|
|
298
326
|
context 'delegate to: :controller' do
|
299
|
-
it '
|
327
|
+
it 'removes unused methods if delegate method is not called' do
|
300
328
|
content = <<-EOF
|
301
329
|
class PostsController < ApplicationController
|
302
330
|
protected
|
@@ -313,10 +341,12 @@ module RailsBestPractices
|
|
313
341
|
runner.review('app/helpers/posts_helper.rb', content)
|
314
342
|
runner.after_review
|
315
343
|
expect(runner.errors.size).to eq(1)
|
316
|
-
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
|
+
)
|
317
347
|
end
|
318
348
|
|
319
|
-
it '
|
349
|
+
it 'removes unused methods if delegate method is called' do
|
320
350
|
content = <<-EOF
|
321
351
|
class PostsController < ApplicationController
|
322
352
|
protected
|
@@ -341,7 +371,7 @@ module RailsBestPractices
|
|
341
371
|
end
|
342
372
|
|
343
373
|
context 'cells' do
|
344
|
-
it '
|
374
|
+
it 'removes unused methods' do
|
345
375
|
content = <<-EOF
|
346
376
|
class PostsCell < Cell::Rails
|
347
377
|
def list; end
|
@@ -354,7 +384,7 @@ module RailsBestPractices
|
|
354
384
|
expect(runner.errors[0].to_s).to eq('app/cells/posts_cell.rb:2 - remove unused methods (PostsCell#list)')
|
355
385
|
end
|
356
386
|
|
357
|
-
it '
|
387
|
+
it 'does not remove unused methods if render_cell' do
|
358
388
|
content = <<-EOF
|
359
389
|
class PostsCell < Cell::Rails
|
360
390
|
def list; end
|
@@ -372,7 +402,7 @@ module RailsBestPractices
|
|
372
402
|
expect(runner.errors.size).to eq(0)
|
373
403
|
end
|
374
404
|
|
375
|
-
it '
|
405
|
+
it 'does not remove unused methods if render with state' do
|
376
406
|
content = <<-EOF
|
377
407
|
class PostsCell < Cell::Rails
|
378
408
|
def list
|
@@ -395,7 +425,7 @@ module RailsBestPractices
|
|
395
425
|
end
|
396
426
|
end
|
397
427
|
|
398
|
-
it '
|
428
|
+
it 'does not remove unused methods' do
|
399
429
|
route_content = <<-EOF
|
400
430
|
RailsBestPracticesCom::Application.routes.draw do
|
401
431
|
namespace :admin do
|
@@ -432,9 +462,12 @@ module RailsBestPractices
|
|
432
462
|
expect(runner.errors.size).to eq(0)
|
433
463
|
end
|
434
464
|
|
435
|
-
it '
|
436
|
-
runner =
|
437
|
-
|
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
|
+
)
|
438
471
|
content = <<-EOF
|
439
472
|
RailsBestPracticesCom::Application.routes.draw do
|
440
473
|
resources :posts do
|
@@ -5,12 +5,14 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Reviews
|
7
7
|
describe RemoveUnusedMethodsInHelpersReview do
|
8
|
-
let(:runner)
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
let(:runner) do
|
9
|
+
Core::Runner.new(
|
10
|
+
prepares: [Prepares::ControllerPrepare.new, Prepares::HelperPrepare.new],
|
11
|
+
reviews: described_class.new(except_methods: [])
|
12
|
+
)
|
13
|
+
end
|
12
14
|
|
13
|
-
it '
|
15
|
+
it 'removes unused methods' do
|
14
16
|
content = <<-EOF
|
15
17
|
module PostsHelper
|
16
18
|
def unused; end
|
@@ -20,10 +22,12 @@ module RailsBestPractices
|
|
20
22
|
runner.review('app/helpers/posts_helper.rb', content)
|
21
23
|
runner.after_review
|
22
24
|
expect(runner.errors.size).to eq(1)
|
23
|
-
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
|
+
)
|
24
28
|
end
|
25
29
|
|
26
|
-
it '
|
30
|
+
it 'does not remove unused methods if called on views' do
|
27
31
|
content = <<-EOF
|
28
32
|
module PostsHelper
|
29
33
|
def used?(post); end
|
@@ -40,7 +44,7 @@ module RailsBestPractices
|
|
40
44
|
expect(runner.errors.size).to eq(0)
|
41
45
|
end
|
42
46
|
|
43
|
-
it '
|
47
|
+
it 'does not remove unused methods if called on helpers' do
|
44
48
|
content = <<-EOF
|
45
49
|
module PostsHelper
|
46
50
|
def used?(post)
|
@@ -61,7 +65,7 @@ module RailsBestPractices
|
|
61
65
|
expect(runner.errors.size).to eq(0)
|
62
66
|
end
|
63
67
|
|
64
|
-
it '
|
68
|
+
it 'does not remove unused methods if called on controllers' do
|
65
69
|
helper_content = <<-EOF
|
66
70
|
module PostsHelper
|
67
71
|
def used?(post); end
|
@@ -86,7 +90,7 @@ module RailsBestPractices
|
|
86
90
|
expect(runner.errors.size).to eq(0)
|
87
91
|
end
|
88
92
|
|
89
|
-
it '
|
93
|
+
it 'does not remove unused methods if called in descendant controllers' do
|
90
94
|
application_helper_content = <<-EOF
|
91
95
|
module ApplicationHelper
|
92
96
|
def admin?; end
|
@@ -116,9 +120,12 @@ module RailsBestPractices
|
|
116
120
|
expect(runner.errors.size).to eq(0)
|
117
121
|
end
|
118
122
|
|
119
|
-
|
120
|
-
runner =
|
121
|
-
|
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
|
+
)
|
122
129
|
|
123
130
|
content = <<-EOF
|
124
131
|
module PostsHelper
|
@@ -129,7 +136,7 @@ module RailsBestPractices
|
|
129
136
|
runner.review('app/helpers/posts_helper.rb', content)
|
130
137
|
runner.after_review
|
131
138
|
expect(runner.errors.size).to eq(0)
|
132
|
-
|
139
|
+
end
|
133
140
|
end
|
134
141
|
end
|
135
142
|
end
|