rails_best_practices 1.14.4 → 1.15.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/.travis.yml +1 -0
- data/README.md +26 -6
- data/lib/rails_best_practices/core/check.rb +15 -2
- data/lib/rails_best_practices/core/klasses.rb +13 -3
- data/lib/rails_best_practices/core/model_associations.rb +1 -1
- data/lib/rails_best_practices/core/runner.rb +2 -0
- data/lib/rails_best_practices/lexicals/long_line_check.rb +1 -1
- data/lib/rails_best_practices/prepares/route_prepare.rb +17 -1
- data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +2 -2
- data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +2 -2
- data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +1 -1
- data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +1 -1
- data/lib/rails_best_practices/reviews/move_code_into_model_review.rb +1 -1
- data/lib/rails_best_practices/reviews/move_model_logic_into_model_review.rb +1 -1
- data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +1 -1
- data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +1 -1
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +1 -2
- data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +1 -1
- data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +2 -1
- data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +15 -26
- data/lib/rails_best_practices/reviews/use_turbo_sprockets_rails3_review.rb +4 -2
- data/lib/rails_best_practices/version.rb +1 -1
- data/spec/rails_best_practices/analyzer_spec.rb +11 -11
- data/spec/rails_best_practices/core/check_spec.rb +1 -1
- data/spec/rails_best_practices/core/error_spec.rb +6 -6
- data/spec/rails_best_practices/core/klasses_spec.rb +26 -2
- data/spec/rails_best_practices/core/methods_spec.rb +18 -18
- data/spec/rails_best_practices/core/model_associations_spec.rb +6 -6
- data/spec/rails_best_practices/core/model_attributes_spec.rb +6 -6
- data/spec/rails_best_practices/core/modules_spec.rb +3 -3
- data/spec/rails_best_practices/core/routes_spec.rb +4 -4
- data/spec/rails_best_practices/core/runner_spec.rb +1 -1
- data/spec/rails_best_practices/lexicals/long_line_check_spec.rb +18 -6
- data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +18 -5
- data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +17 -5
- data/spec/rails_best_practices/prepares/config_prepare_spec.rb +1 -1
- data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +13 -13
- data/spec/rails_best_practices/prepares/gemfile_prepare_spec.rb +1 -1
- data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +2 -2
- data/spec/rails_best_practices/prepares/initializer_prepare_spec.rb +2 -2
- data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +1 -1
- data/spec/rails_best_practices/prepares/model_prepare_spec.rb +44 -44
- data/spec/rails_best_practices/prepares/route_prepare_spec.rb +102 -78
- data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +25 -9
- data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +43 -23
- data/spec/rails_best_practices/reviews/check_save_return_value_spec.rb +34 -20
- data/spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb +16 -5
- data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +25 -3
- data/spec/rails_best_practices/reviews/hash_syntax_review_spec.rb +18 -7
- data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +30 -7
- data/spec/rails_best_practices/reviews/keep_finders_on_their_own_model_review_spec.rb +24 -8
- data/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb +24 -12
- data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +17 -5
- data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +14 -3
- data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +24 -13
- data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +25 -6
- data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +26 -4
- data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +31 -18
- data/spec/rails_best_practices/reviews/not_rescue_exception_spec.rb +23 -10
- data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +34 -7
- data/spec/rails_best_practices/reviews/not_use_times_ago_in_words_review_spec.rb +19 -8
- data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +33 -19
- data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +25 -13
- data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +14 -4
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +91 -23
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +20 -5
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +66 -41
- data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +28 -4
- data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +16 -7
- data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +45 -32
- data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +20 -9
- data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +23 -14
- data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +34 -7
- data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +21 -6
- data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +124 -79
- data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +12 -12
- data/spec/rails_best_practices/reviews/use_parenthesis_in_method_def_spec.rb +16 -4
- data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +39 -27
- data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +22 -8
- data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +39 -21
- data/spec/rails_best_practices/reviews/use_turbo_sprockets_rails3_review_spec.rb +66 -7
- metadata +36 -37
- data/.ruby-gemset +0 -1
@@ -11,8 +11,8 @@ module RailsBestPractices
|
|
11
11
|
<%= time_ago_in_words(post.created_at) %>
|
12
12
|
EOF
|
13
13
|
runner.review('app/views/posts/show.html.erb', content)
|
14
|
-
runner.
|
15
|
-
runner.errors[0].to_s.
|
14
|
+
expect(runner.errors.size).to eq(1)
|
15
|
+
expect(runner.errors[0].to_s).to eq("app/views/posts/show.html.erb:1 - not use time_ago_in_words")
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should not use in helpers" do
|
@@ -22,8 +22,8 @@ module RailsBestPractices
|
|
22
22
|
end
|
23
23
|
EOF
|
24
24
|
runner.review('app/helpers/posts_helper.rb', content)
|
25
|
-
runner.
|
26
|
-
runner.errors[0].to_s.
|
25
|
+
expect(runner.errors.size).to eq(1)
|
26
|
+
expect(runner.errors[0].to_s).to eq("app/helpers/posts_helper.rb:2 - not use time_ago_in_words")
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
@@ -33,8 +33,8 @@ module RailsBestPractices
|
|
33
33
|
<%= distance_of_time_in_words_to_now(post.created_at) %>
|
34
34
|
EOF
|
35
35
|
runner.review('app/views/posts/show.html.erb', content)
|
36
|
-
runner.
|
37
|
-
runner.errors[0].to_s.
|
36
|
+
expect(runner.errors.size).to eq(1)
|
37
|
+
expect(runner.errors[0].to_s).to eq("app/views/posts/show.html.erb:1 - not use time_ago_in_words")
|
38
38
|
end
|
39
39
|
|
40
40
|
it "should not use in helpers" do
|
@@ -44,10 +44,21 @@ module RailsBestPractices
|
|
44
44
|
end
|
45
45
|
EOF
|
46
46
|
runner.review('app/helpers/posts_helper.rb', content)
|
47
|
-
runner.
|
48
|
-
runner.errors[0].to_s.
|
47
|
+
expect(runner.errors.size).to eq(1)
|
48
|
+
expect(runner.errors[0].to_s).to eq("app/helpers/posts_helper.rb:2 - not use time_ago_in_words")
|
49
49
|
end
|
50
50
|
end
|
51
|
+
|
52
|
+
it "should not check ignored files" do
|
53
|
+
runner = Core::Runner.new(reviews: NotUseTimeAgoInWordsReview.new(ignored_files: /posts_helper/))
|
54
|
+
content =<<-EOF
|
55
|
+
def timeago
|
56
|
+
content_tag(:p, time_ago_in_words(post.created_at))
|
57
|
+
end
|
58
|
+
EOF
|
59
|
+
runner.review('app/helpers/posts_helper.rb', content)
|
60
|
+
expect(runner.errors.size).to eq(0)
|
61
|
+
end
|
51
62
|
end
|
52
63
|
end
|
53
64
|
end
|
@@ -16,8 +16,8 @@ module RailsBestPractices
|
|
16
16
|
end
|
17
17
|
EOF
|
18
18
|
runner.review('config/routes.rb', content)
|
19
|
-
runner.
|
20
|
-
runner.errors[0].to_s.
|
19
|
+
expect(runner.errors.size).to eq(1)
|
20
|
+
expect(runner.errors[0].to_s).to eq("config/routes.rb:2 - overuse route customizations (customize_count > 3)")
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should overuse route customizations with member" do
|
@@ -30,8 +30,8 @@ module RailsBestPractices
|
|
30
30
|
end
|
31
31
|
EOF
|
32
32
|
runner.review('config/routes.rb', content)
|
33
|
-
runner.
|
34
|
-
runner.errors[0].to_s.
|
33
|
+
expect(runner.errors.size).to eq(1)
|
34
|
+
expect(runner.errors[0].to_s).to eq("config/routes.rb:2 - overuse route customizations (customize_count > 3)")
|
35
35
|
end
|
36
36
|
|
37
37
|
it "should overuse route customizations with collection" do
|
@@ -44,8 +44,8 @@ module RailsBestPractices
|
|
44
44
|
end
|
45
45
|
EOF
|
46
46
|
runner.review('config/routes.rb', content)
|
47
|
-
runner.
|
48
|
-
runner.errors[0].to_s.
|
47
|
+
expect(runner.errors.size).to eq(1)
|
48
|
+
expect(runner.errors[0].to_s).to eq("config/routes.rb:2 - overuse route customizations (customize_count > 3)")
|
49
49
|
end
|
50
50
|
|
51
51
|
it "should overuse route customizations with hash member and collection" do
|
@@ -60,8 +60,8 @@ module RailsBestPractices
|
|
60
60
|
end
|
61
61
|
EOF
|
62
62
|
runner.review('config/routes.rb', content)
|
63
|
-
runner.
|
64
|
-
runner.errors[0].to_s.
|
63
|
+
expect(runner.errors.size).to eq(1)
|
64
|
+
expect(runner.errors[0].to_s).to eq("config/routes.rb:3 - overuse route customizations (customize_count > 3)")
|
65
65
|
end
|
66
66
|
|
67
67
|
it "should overuse route customizations with array member and collection" do
|
@@ -74,8 +74,8 @@ module RailsBestPractices
|
|
74
74
|
end
|
75
75
|
EOF
|
76
76
|
runner.review('config/routes.rb', content)
|
77
|
-
runner.
|
78
|
-
runner.errors[0].to_s.
|
77
|
+
expect(runner.errors.size).to eq(1)
|
78
|
+
expect(runner.errors[0].to_s).to eq("config/routes.rb:3 - overuse route customizations (customize_count > 3)")
|
79
79
|
end
|
80
80
|
|
81
81
|
it "should not overuse route customizations without customization" do
|
@@ -85,7 +85,7 @@ module RailsBestPractices
|
|
85
85
|
end
|
86
86
|
EOF
|
87
87
|
runner.review('config/routes.rb', content)
|
88
|
-
runner.
|
88
|
+
expect(runner.errors.size).to eq(0)
|
89
89
|
end
|
90
90
|
|
91
91
|
it "should not overuse route customizations when customize route is only one" do
|
@@ -95,7 +95,7 @@ module RailsBestPractices
|
|
95
95
|
end
|
96
96
|
EOF
|
97
97
|
runner.review('config/routes.rb', content)
|
98
|
-
runner.
|
98
|
+
expect(runner.errors.size).to eq(0)
|
99
99
|
end
|
100
100
|
|
101
101
|
it "should not raise error for constants in routes" do
|
@@ -108,7 +108,7 @@ module RailsBestPractices
|
|
108
108
|
end
|
109
109
|
EOF
|
110
110
|
runner.review('config/routes.rb', content)
|
111
|
-
runner.
|
111
|
+
expect(runner.errors.size).to eq(0)
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
@@ -130,8 +130,8 @@ module RailsBestPractices
|
|
130
130
|
end
|
131
131
|
EOF
|
132
132
|
runner.review('config/routes.rb', content)
|
133
|
-
runner.
|
134
|
-
runner.errors[0].to_s.
|
133
|
+
expect(runner.errors.size).to eq(1)
|
134
|
+
expect(runner.errors[0].to_s).to eq("config/routes.rb:2 - overuse route customizations (customize_count > 3)")
|
135
135
|
end
|
136
136
|
|
137
137
|
it "should overuse route customizations another way" do
|
@@ -146,8 +146,8 @@ module RailsBestPractices
|
|
146
146
|
end
|
147
147
|
EOF
|
148
148
|
runner.review('config/routes.rb', content)
|
149
|
-
runner.
|
150
|
-
runner.errors[0].to_s.
|
149
|
+
expect(runner.errors.size).to eq(1)
|
150
|
+
expect(runner.errors[0].to_s).to eq("config/routes.rb:2 - overuse route customizations (customize_count > 3)")
|
151
151
|
end
|
152
152
|
|
153
153
|
it "should not overuse route customizations without customization" do
|
@@ -157,7 +157,7 @@ module RailsBestPractices
|
|
157
157
|
end
|
158
158
|
EOF
|
159
159
|
runner.review('config/routes.rb', content)
|
160
|
-
runner.
|
160
|
+
expect(runner.errors.size).to eq(0)
|
161
161
|
end
|
162
162
|
|
163
163
|
it "should not overuse route customizations when customize route is only one" do
|
@@ -171,9 +171,23 @@ module RailsBestPractices
|
|
171
171
|
end
|
172
172
|
EOF
|
173
173
|
runner.review('config/routes.rb', content)
|
174
|
-
runner.
|
174
|
+
expect(runner.errors.size).to eq(0)
|
175
175
|
end
|
176
176
|
end
|
177
|
+
|
178
|
+
it "should not check ignored files" do
|
179
|
+
runner = Core::Runner.new(reviews: OveruseRouteCustomizationsReview.new(ignored_files: /config\/routes\.rb/))
|
180
|
+
content = <<-EOF
|
181
|
+
ActionController::Routing::Routes.draw do |map|
|
182
|
+
map.resources :posts, member: { comments: :get,
|
183
|
+
create_comment: :post,
|
184
|
+
update_comment: :update,
|
185
|
+
delete_comment: :delete }
|
186
|
+
end
|
187
|
+
EOF
|
188
|
+
runner.review('config/routes.rb', content)
|
189
|
+
expect(runner.errors.size).to eq(0)
|
190
|
+
end
|
177
191
|
end
|
178
192
|
end
|
179
193
|
end
|
@@ -12,8 +12,8 @@ module RailsBestPractices
|
|
12
12
|
end
|
13
13
|
EOF
|
14
14
|
runner.review('app/models/user.rb', content)
|
15
|
-
runner.
|
16
|
-
runner.errors[0].to_s.
|
15
|
+
expect(runner.errors.size).to eq(1)
|
16
|
+
expect(runner.errors[0].to_s).to eq("app/models/user.rb:1 - protect mass assignment")
|
17
17
|
end
|
18
18
|
|
19
19
|
it "should not protect mass assignment if attr_accessible is used with arguments and user set config.active_record.whitelist_attributes" do
|
@@ -31,7 +31,7 @@ module RailsBestPractices
|
|
31
31
|
end
|
32
32
|
EOF
|
33
33
|
runner.review('app/models/user.rb', content)
|
34
|
-
runner.
|
34
|
+
expect(runner.errors.size).to eq(0)
|
35
35
|
end
|
36
36
|
|
37
37
|
it "should not protect mass assignment if attr_accessible is used without arguments and user set config.active_record.whitelist_attributes" do
|
@@ -49,7 +49,7 @@ module RailsBestPractices
|
|
49
49
|
end
|
50
50
|
EOF
|
51
51
|
runner.review('app/models/user.rb', content)
|
52
|
-
runner.
|
52
|
+
expect(runner.errors.size).to eq(0)
|
53
53
|
end
|
54
54
|
|
55
55
|
it "should not protect mass assignment with attr_protected if user set config.active_record.whitelist_attributes" do
|
@@ -67,7 +67,7 @@ module RailsBestPractices
|
|
67
67
|
end
|
68
68
|
EOF
|
69
69
|
runner.review('app/models/user.rb', content)
|
70
|
-
runner.
|
70
|
+
expect(runner.errors.size).to eq(0)
|
71
71
|
end
|
72
72
|
|
73
73
|
it "should not protect mass assignment if using devise" do
|
@@ -77,7 +77,7 @@ module RailsBestPractices
|
|
77
77
|
end
|
78
78
|
EOF
|
79
79
|
runner.review('app/models/user.rb', content)
|
80
|
-
runner.
|
80
|
+
expect(runner.errors.size).to eq(0)
|
81
81
|
end
|
82
82
|
|
83
83
|
it "should not protect mass assignment if using authlogic with configuration" do
|
@@ -89,7 +89,7 @@ module RailsBestPractices
|
|
89
89
|
end
|
90
90
|
EOF
|
91
91
|
runner.review('app/models/user.rb', content)
|
92
|
-
runner.
|
92
|
+
expect(runner.errors.size).to eq(0)
|
93
93
|
end
|
94
94
|
|
95
95
|
it "should not protect mass assignment if using authlogic without configuration" do
|
@@ -99,7 +99,7 @@ module RailsBestPractices
|
|
99
99
|
end
|
100
100
|
EOF
|
101
101
|
runner.review('app/models/user.rb', content)
|
102
|
-
runner.
|
102
|
+
expect(runner.errors.size).to eq(0)
|
103
103
|
end
|
104
104
|
|
105
105
|
it "should not protect mass assignment if checking non ActiveRecord::Base inherited model" do
|
@@ -108,7 +108,7 @@ module RailsBestPractices
|
|
108
108
|
end
|
109
109
|
EOF
|
110
110
|
runner.review('app/models/user.rb', content)
|
111
|
-
runner.
|
111
|
+
expect(runner.errors.size).to eq(0)
|
112
112
|
end
|
113
113
|
|
114
114
|
context "strong_parameters" do
|
@@ -119,7 +119,7 @@ module RailsBestPractices
|
|
119
119
|
end
|
120
120
|
EOF
|
121
121
|
runner.review('app/models/user.rb', content)
|
122
|
-
runner.
|
122
|
+
expect(runner.errors.size).to eq(0)
|
123
123
|
end
|
124
124
|
|
125
125
|
it "should not protect mass assignment for strong_parameters" do
|
@@ -134,7 +134,7 @@ module RailsBestPractices
|
|
134
134
|
end
|
135
135
|
EOF
|
136
136
|
runner.review('app/models/user.rb', content)
|
137
|
-
runner.
|
137
|
+
expect(runner.errors.size).to eq(0)
|
138
138
|
end
|
139
139
|
end
|
140
140
|
|
@@ -152,7 +152,7 @@ module RailsBestPractices
|
|
152
152
|
end
|
153
153
|
EOF
|
154
154
|
runner.review('app/models/user.rb', content)
|
155
|
-
runner.
|
155
|
+
expect(runner.errors.size).to eq(0)
|
156
156
|
end
|
157
157
|
|
158
158
|
it "should protect mass assignment for rails 3" do
|
@@ -168,9 +168,21 @@ module RailsBestPractices
|
|
168
168
|
end
|
169
169
|
EOF
|
170
170
|
runner.review('app/models/user.rb', content)
|
171
|
-
runner.
|
171
|
+
expect(runner.errors.size).to eq(1)
|
172
172
|
end
|
173
173
|
end
|
174
|
+
|
175
|
+
|
176
|
+
it "should not check ignored files" do
|
177
|
+
runner = Core::Runner.new(prepares: [Prepares::GemfilePrepare.new, Prepares::ConfigPrepare.new, Prepares::InitializerPrepare.new],
|
178
|
+
reviews: ProtectMassAssignmentReview.new(ignored_files: /app\/models\/user\.rb/))
|
179
|
+
content =<<-EOF
|
180
|
+
class User < ActiveRecord::Base
|
181
|
+
end
|
182
|
+
EOF
|
183
|
+
runner.review('app/models/user.rb', content)
|
184
|
+
expect(runner.errors.size).to eq(0)
|
185
|
+
end
|
174
186
|
end
|
175
187
|
end
|
176
188
|
end
|
@@ -11,8 +11,8 @@ module RailsBestPractices
|
|
11
11
|
end
|
12
12
|
EOF
|
13
13
|
runner.review('app/helpers/posts_helper.rb', content)
|
14
|
-
runner.
|
15
|
-
runner.errors[0].to_s.
|
14
|
+
expect(runner.errors.size).to eq(1)
|
15
|
+
expect(runner.errors[0].to_s).to eq("app/helpers/posts_helper.rb:1 - remove empty helpers")
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should not remove empty helpers" do
|
@@ -24,7 +24,7 @@ module RailsBestPractices
|
|
24
24
|
end
|
25
25
|
EOF
|
26
26
|
runner.review('app/helpers/posts_helper.rb', content)
|
27
|
-
runner.
|
27
|
+
expect(runner.errors.size).to eq(0)
|
28
28
|
end
|
29
29
|
|
30
30
|
it "should not remove empty application_helper" do
|
@@ -33,7 +33,17 @@ module RailsBestPractices
|
|
33
33
|
end
|
34
34
|
EOF
|
35
35
|
runner.review('app/helpers/application_helper.rb', content)
|
36
|
-
runner.
|
36
|
+
expect(runner.errors.size).to eq(0)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should not check ignored files" do
|
40
|
+
runner = Core::Runner.new(reviews: RemoveEmptyHelpersReview.new(ignored_files: /posts_helper/))
|
41
|
+
content =<<-EOF
|
42
|
+
module PostsHelper
|
43
|
+
end
|
44
|
+
EOF
|
45
|
+
runner.review('app/helpers/posts_helper.rb', content)
|
46
|
+
expect(runner.errors.size).to eq(0)
|
37
47
|
end
|
38
48
|
end
|
39
49
|
end
|
@@ -35,9 +35,9 @@ module RailsBestPractices
|
|
35
35
|
runner.prepare('app/controllers/posts_controller.rb', content)
|
36
36
|
runner.review('app/controllers/posts_controller.rb', content)
|
37
37
|
runner.after_review
|
38
|
-
runner.
|
39
|
-
runner.errors[0].to_s.
|
40
|
-
runner.errors[1].to_s.
|
38
|
+
expect(runner.errors.size).to eq(2)
|
39
|
+
expect(runner.errors[0].to_s).to eq("app/controllers/posts_controller.rb:6 - remove unused methods (PostsController#load_post)")
|
40
|
+
expect(runner.errors[1].to_s).to eq("app/controllers/posts_controller.rb:8 - remove unused methods (PostsController#load_user)")
|
41
41
|
end
|
42
42
|
|
43
43
|
it "should not remove unused methods for before_filter" do
|
@@ -59,7 +59,7 @@ module RailsBestPractices
|
|
59
59
|
runner.prepare('app/controllers/posts_controller.rb', content)
|
60
60
|
runner.review('app/controllers/posts_controller.rb', content)
|
61
61
|
runner.after_review
|
62
|
-
runner.
|
62
|
+
expect(runner.errors.size).to eq(0)
|
63
63
|
end
|
64
64
|
|
65
65
|
it "should not remove unused methods for around_filter" do
|
@@ -84,7 +84,7 @@ module RailsBestPractices
|
|
84
84
|
runner.prepare('app/controllers/posts_controller.rb', content)
|
85
85
|
runner.review('app/controllers/posts_controller.rb', content)
|
86
86
|
runner.after_review
|
87
|
-
runner.
|
87
|
+
expect(runner.errors.size).to eq(0)
|
88
88
|
end
|
89
89
|
|
90
90
|
it "should not remove unused methods for layout" do
|
@@ -106,7 +106,7 @@ module RailsBestPractices
|
|
106
106
|
runner.prepare('app/controllers/posts_controller.rb', content)
|
107
107
|
runner.review('app/controllers/posts_controller.rb', content)
|
108
108
|
runner.after_review
|
109
|
-
runner.
|
109
|
+
expect(runner.errors.size).to eq(0)
|
110
110
|
end
|
111
111
|
|
112
112
|
it "should not remove inherited_resources methods" do
|
@@ -128,7 +128,7 @@ module RailsBestPractices
|
|
128
128
|
runner.prepare('app/controllers/posts_controller.rb', content)
|
129
129
|
runner.review('app/controllers/posts_controller.rb', content)
|
130
130
|
runner.after_review
|
131
|
-
runner.
|
131
|
+
expect(runner.errors.size).to eq(0)
|
132
132
|
end
|
133
133
|
end
|
134
134
|
|
@@ -149,8 +149,8 @@ module RailsBestPractices
|
|
149
149
|
runner.prepare('app/controllers/posts_controller.rb', content)
|
150
150
|
runner.review('app/controllers/posts_controller.rb', content)
|
151
151
|
runner.after_review
|
152
|
-
runner.
|
153
|
-
runner.errors[0].to_s.
|
152
|
+
expect(runner.errors.size).to eq(1)
|
153
|
+
expect(runner.errors[0].to_s).to eq("app/controllers/posts_controller.rb:3 - remove unused methods (PostsController#list)")
|
154
154
|
end
|
155
155
|
|
156
156
|
it "should not remove unused methods if all actions are used in route" do
|
@@ -170,7 +170,7 @@ module RailsBestPractices
|
|
170
170
|
runner.prepare('app/controllers/internal_controller.rb', content)
|
171
171
|
runner.review('app/controllers/internal_controller.rb', content)
|
172
172
|
runner.after_review
|
173
|
-
runner.
|
173
|
+
expect(runner.errors.size).to eq(0)
|
174
174
|
end
|
175
175
|
|
176
176
|
it "should not remove unused methods if they are except_methods" do
|
@@ -182,7 +182,7 @@ module RailsBestPractices
|
|
182
182
|
runner.prepare('app/controllers/exceptable_controller.rb', content)
|
183
183
|
runner.review('app/controllers/exceptable_controller.rb', content)
|
184
184
|
runner.after_review
|
185
|
-
runner.
|
185
|
+
expect(runner.errors.size).to eq(0)
|
186
186
|
end
|
187
187
|
end
|
188
188
|
|
@@ -211,7 +211,7 @@ module RailsBestPractices
|
|
211
211
|
runner.review('app/controllers/application_controller.rb', application_content)
|
212
212
|
runner.review('app/controllers/users_controller.rb', users_content)
|
213
213
|
runner.after_review
|
214
|
-
runner.
|
214
|
+
expect(runner.errors.size).to eq(0)
|
215
215
|
end
|
216
216
|
end
|
217
217
|
|
@@ -227,8 +227,8 @@ module RailsBestPractices
|
|
227
227
|
runner.prepare('app/controllers/posts_controller.rb', content)
|
228
228
|
runner.review('app/controllers/posts_controller.rb', content)
|
229
229
|
runner.after_review
|
230
|
-
runner.
|
231
|
-
runner.errors[0].to_s.
|
230
|
+
expect(runner.errors.size).to eq(1)
|
231
|
+
expect(runner.errors[0].to_s).to eq("app/controllers/posts_controller.rb:4 - remove unused methods (PostsController#helper_post)")
|
232
232
|
end
|
233
233
|
|
234
234
|
it "should not remove unused methods if call helper method in views" do
|
@@ -246,7 +246,7 @@ module RailsBestPractices
|
|
246
246
|
EOF
|
247
247
|
runner.review('app/views/posts/show.html.erb', content)
|
248
248
|
runner.after_review
|
249
|
-
runner.
|
249
|
+
expect(runner.errors.size).to eq(0)
|
250
250
|
end
|
251
251
|
|
252
252
|
it "should not remove unused methods if call helper method in helpers" do
|
@@ -268,7 +268,7 @@ module RailsBestPractices
|
|
268
268
|
EOF
|
269
269
|
runner.review('app/helpers/posts_helper.rb', content)
|
270
270
|
runner.after_review
|
271
|
-
runner.
|
271
|
+
expect(runner.errors.size).to eq(0)
|
272
272
|
end
|
273
273
|
end
|
274
274
|
|
@@ -289,8 +289,8 @@ module RailsBestPractices
|
|
289
289
|
EOF
|
290
290
|
runner.review('app/helpers/posts_helper.rb', content)
|
291
291
|
runner.after_review
|
292
|
-
runner.
|
293
|
-
runner.errors[0].to_s.
|
292
|
+
expect(runner.errors.size).to eq(1)
|
293
|
+
expect(runner.errors[0].to_s).to eq("app/controllers/posts_controller.rb:3 - remove unused methods (PostsController#helper_post)")
|
294
294
|
end
|
295
295
|
|
296
296
|
it "should remove unused methods if delegate method is called" do
|
@@ -313,7 +313,7 @@ module RailsBestPractices
|
|
313
313
|
EOF
|
314
314
|
runner.review('app/views/posts/show.html.erb', content)
|
315
315
|
runner.after_review
|
316
|
-
runner.
|
316
|
+
expect(runner.errors.size).to eq(0)
|
317
317
|
end
|
318
318
|
end
|
319
319
|
|
@@ -327,8 +327,8 @@ module RailsBestPractices
|
|
327
327
|
runner.prepare('app/cells/posts_cell.rb', content)
|
328
328
|
runner.review('app/cells/posts_cell.rb', content)
|
329
329
|
runner.after_review
|
330
|
-
runner.
|
331
|
-
runner.errors[0].to_s.
|
330
|
+
expect(runner.errors.size).to eq(1)
|
331
|
+
expect(runner.errors[0].to_s).to eq("app/cells/posts_cell.rb:2 - remove unused methods (PostsCell#list)")
|
332
332
|
end
|
333
333
|
|
334
334
|
it "should not remove unused methods if render_cell" do
|
@@ -346,7 +346,7 @@ module RailsBestPractices
|
|
346
346
|
EOF
|
347
347
|
runner.review('app/views/posts/index.html.erb', content)
|
348
348
|
runner.after_review
|
349
|
-
runner.
|
349
|
+
expect(runner.errors.size).to eq(0)
|
350
350
|
end
|
351
351
|
|
352
352
|
it "should not remove unused methods if render with state" do
|
@@ -368,9 +368,77 @@ module RailsBestPractices
|
|
368
368
|
EOF
|
369
369
|
runner.review('app/views/posts/index.html.erb', content)
|
370
370
|
runner.after_review
|
371
|
-
runner.
|
371
|
+
expect(runner.errors.size).to eq(0)
|
372
372
|
end
|
373
373
|
end
|
374
|
+
|
375
|
+
it "should not remove unused methods" do
|
376
|
+
route_content =<<-EOF
|
377
|
+
RailsBestPracticesCom::Application.routes.draw do
|
378
|
+
namespace :admin do
|
379
|
+
resources :users, only: :index
|
380
|
+
end
|
381
|
+
end
|
382
|
+
EOF
|
383
|
+
app_controller_content =<<-EOF
|
384
|
+
module Admin
|
385
|
+
class AppController < ApplicationController
|
386
|
+
def index
|
387
|
+
@collection = model.all
|
388
|
+
end
|
389
|
+
end
|
390
|
+
end
|
391
|
+
EOF
|
392
|
+
users_controller_content =<<-EOF
|
393
|
+
module Admin
|
394
|
+
class UsersController < AppController
|
395
|
+
private
|
396
|
+
|
397
|
+
def model
|
398
|
+
User
|
399
|
+
end
|
400
|
+
end
|
401
|
+
end
|
402
|
+
EOF
|
403
|
+
runner.prepare('config/routes.rb', route_content)
|
404
|
+
runner.prepare('app/controllers/admin/app_controller.rb', app_controller_content)
|
405
|
+
runner.prepare('app/controllers/admin/users_controller.rb', users_controller_content)
|
406
|
+
runner.review('app/controllers/admin/app_controller.rb', app_controller_content)
|
407
|
+
runner.review('app/controllers/admin/users_controller.rb', users_controller_content)
|
408
|
+
runner.after_review
|
409
|
+
expect(runner.errors.size).to eq(0)
|
410
|
+
end
|
411
|
+
|
412
|
+
it "should not check ignored files" do
|
413
|
+
runner = Core::Runner.new(prepares: [Prepares::ControllerPrepare.new, Prepares::RoutePrepare.new],
|
414
|
+
reviews: RemoveUnusedMethodsInControllersReview.new(ignored_files: /posts_controller/, except_methods: []))
|
415
|
+
content =<<-EOF
|
416
|
+
RailsBestPracticesCom::Application.routes.draw do
|
417
|
+
resources :posts do
|
418
|
+
member do
|
419
|
+
post 'link_to/:other_id' => 'posts#link_to_post'
|
420
|
+
post 'extra_update' => 'posts#extra_update'
|
421
|
+
end
|
422
|
+
end
|
423
|
+
end
|
424
|
+
EOF
|
425
|
+
runner.prepare('config/routes.rb', content)
|
426
|
+
content =<<-EOF
|
427
|
+
class PostsController < ActiveRecord::Base
|
428
|
+
def show; end
|
429
|
+
def extra_update; end
|
430
|
+
def link_to_post; end
|
431
|
+
protected
|
432
|
+
def load_post; end
|
433
|
+
private
|
434
|
+
def load_user; end
|
435
|
+
end
|
436
|
+
EOF
|
437
|
+
runner.prepare('app/controllers/posts_controller.rb', content)
|
438
|
+
runner.review('app/controllers/posts_controller.rb', content)
|
439
|
+
runner.after_review
|
440
|
+
expect(runner.errors.size).to eq(0)
|
441
|
+
end
|
374
442
|
end
|
375
443
|
end
|
376
444
|
end
|
@@ -17,8 +17,8 @@ module RailsBestPractices
|
|
17
17
|
runner.prepare('app/helpers/posts_helper.rb', content)
|
18
18
|
runner.review('app/helpers/posts_helper.rb', content)
|
19
19
|
runner.after_review
|
20
|
-
runner.
|
21
|
-
runner.errors[0].to_s.
|
20
|
+
expect(runner.errors.size).to eq(1)
|
21
|
+
expect(runner.errors[0].to_s).to eq("app/helpers/posts_helper.rb:2 - remove unused methods (PostsHelper#unused)")
|
22
22
|
end
|
23
23
|
|
24
24
|
it "should not remove unused methods if called on views" do
|
@@ -35,7 +35,7 @@ module RailsBestPractices
|
|
35
35
|
EOF
|
36
36
|
runner.review('app/views/posts/show.html.erb', content)
|
37
37
|
runner.after_review
|
38
|
-
runner.
|
38
|
+
expect(runner.errors.size).to eq(0)
|
39
39
|
end
|
40
40
|
|
41
41
|
it "should not remove unused methods if called on helpers" do
|
@@ -56,7 +56,7 @@ module RailsBestPractices
|
|
56
56
|
EOF
|
57
57
|
runner.review('app/views/posts/show.html.erb', content)
|
58
58
|
runner.after_review
|
59
|
-
runner.
|
59
|
+
expect(runner.errors.size).to eq(0)
|
60
60
|
end
|
61
61
|
|
62
62
|
it "should not remove unused methods if called on controllers" do
|
@@ -81,8 +81,23 @@ module RailsBestPractices
|
|
81
81
|
runner.review('app/helpers/posts_helper.rb', helper_content)
|
82
82
|
runner.review('app/controllers/posts_controller.rb', controller_content)
|
83
83
|
runner.after_review
|
84
|
-
runner.
|
84
|
+
expect(runner.errors.size).to eq(0)
|
85
85
|
end
|
86
|
+
|
87
|
+
it "should not check ignored files" do
|
88
|
+
runner = Core::Runner.new(prepares: [Prepares::ControllerPrepare.new, Prepares::HelperPrepare.new],
|
89
|
+
reviews: RemoveUnusedMethodsInHelpersReview.new(ignored_files: /posts_helper/, except_methods: []))
|
90
|
+
|
91
|
+
content =<<-EOF
|
92
|
+
module PostsHelper
|
93
|
+
def unused; end
|
94
|
+
end
|
95
|
+
EOF
|
96
|
+
runner.prepare('app/helpers/posts_helper.rb', content)
|
97
|
+
runner.review('app/helpers/posts_helper.rb', content)
|
98
|
+
runner.after_review
|
99
|
+
expect(runner.errors.size).to eq(0)
|
100
|
+
end
|
86
101
|
end
|
87
102
|
end
|
88
103
|
end
|