rails_best_practices 1.15.7 → 1.16.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,7 @@
1
1
  AddModelVirtualAttributeCheck: { }
2
2
  AlwaysAddDbIndexCheck: { }
3
3
  #CheckSaveReturnValueCheck: { }
4
+ #CheckDestroyReturnValueCheck: { }
4
5
  DefaultScopeIsEvilCheck: { }
5
6
  DryBundlerInCapistranoCheck: { }
6
7
  #HashSyntaxCheck: { }
@@ -101,7 +101,11 @@ module RailsBestPractices
101
101
  subject.output_terminal_errors
102
102
  result = $stdout.string
103
103
  $stdout = $origin_stdout
104
- expect(result).to eq(["app/models/user.rb:10 - law of demeter".red, "app/models/post.rb:100 - use query attribute".red, "\nPlease go to http://rails-bestpractices.com to see more useful Rails Best Practices.".green, "\nFound 2 warnings.".red].join("\n") + "\n")
104
+ expect(result).to eq([
105
+ "\e[31mapp/models/user.rb:10 - law of demeter\e[0m",
106
+ "\e[31mapp/models/post.rb:100 - use query attribute\e[0m",
107
+ "\e[32m\nPlease go to http://rails-bestpractices.com to see more useful Rails Best Practices.\e[0m",
108
+ "\e[31m\nFound 2 warnings.\e[0m"].join("\n") + "\n")
105
109
  end
106
110
  end
107
111
 
@@ -9,20 +9,20 @@ module RailsBestPractices::Core
9
9
  @mod = Mod.new("PostsHelper", [])
10
10
  end
11
11
  subject { Modules.new.tap { |modules| modules << @mod } }
12
- it "should add decendant to the corresponding module" do
13
- expect(@mod).to receive(:add_decendant).with("PostsController")
14
- subject.add_module_decendant("PostsHelper", "PostsController")
12
+ it "should add descendant to the corresponding module" do
13
+ expect(@mod).to receive(:add_descendant).with("PostsController")
14
+ subject.add_module_descendant("PostsHelper", "PostsController")
15
15
  end
16
16
  end
17
17
 
18
18
  context "Mod" do
19
19
  subject {
20
20
  Mod.new("UsersHelper", ["Admin"]).tap do |mod|
21
- mod.add_decendant("Admin::UsersController")
21
+ mod.add_descendant("Admin::UsersController")
22
22
  end
23
23
  }
24
24
  it { expect(subject.to_s).to eq("Admin::UsersHelper") }
25
- it { expect(subject.decendants).to eq(["Admin::UsersController"]) }
25
+ it { expect(subject.descendants).to eq(["Admin::UsersController"]) }
26
26
  end
27
27
  end
28
28
  end
@@ -123,7 +123,7 @@ module RailsBestPractices
123
123
  end
124
124
 
125
125
  context "helpers" do
126
- it "should add helper decendant" do
126
+ it "should add helper descendant" do
127
127
  content =<<-EOF
128
128
  module PostsHelper
129
129
  end
@@ -136,7 +136,7 @@ module RailsBestPractices
136
136
  EOF
137
137
  runner.prepare('app/controllers/posts_controller.rb', content)
138
138
  helpers = Prepares.helpers
139
- expect(helpers.first.decendants).to eq(["PostsController"])
139
+ expect(helpers.first.descendants).to eq(["PostsController"])
140
140
  end
141
141
  end
142
142
  end
@@ -0,0 +1,151 @@
1
+ require 'spec_helper'
2
+
3
+ module RailsBestPractices
4
+ module Reviews
5
+ describe CheckDestroyReturnValueReview do
6
+ let(:runner) { Core::Runner.new(reviews: CheckDestroyReturnValueReview.new) }
7
+
8
+ describe "check_destroy_return_value" do
9
+ it "should warn you if you fail to check the destroy return value" do
10
+ content =<<-EOF
11
+ def my_method
12
+ post = Posts.create do |p|
13
+ p.title = "foo"
14
+ end
15
+ post.destroy
16
+ end
17
+ EOF
18
+ runner.review('app/helpers/posts_helper.rb', content)
19
+ expect(runner.errors.size).to eq(1)
20
+ expect(runner.errors[0].to_s).to eq("app/helpers/posts_helper.rb:5 - check 'destroy' return value or use 'destroy!'")
21
+ end
22
+
23
+ it "should allow destroy return value if assigned to a var" do
24
+ content =<<-EOF
25
+ def my_method
26
+ post = Posts.create do |p|
27
+ p.title = "foo"
28
+ end
29
+ check_this_later = post.destroy
30
+ end
31
+ EOF
32
+ runner.review('app/helpers/posts_helper.rb', content)
33
+ expect(runner.errors.size).to eq(0)
34
+ end
35
+
36
+ it "should allow destroy return value used in if" do
37
+ content =<<-EOF
38
+ def my_method
39
+ post = Posts.create do |p|
40
+ p.title = "foo"
41
+ end
42
+ if post.destroy
43
+ "OK"
44
+ else
45
+ raise "could not delete"
46
+ end
47
+ end
48
+ EOF
49
+ runner.review('app/helpers/posts_helper.rb', content)
50
+ expect(runner.errors.size).to eq(0)
51
+ end
52
+
53
+ it "should allow destroy return value used in elsif" do
54
+ content =<<-EOF
55
+ def my_method
56
+ post = Posts.create do |p|
57
+ p.title = "foo"
58
+ end
59
+ if current_user
60
+ "YES"
61
+ elsif post.destroy
62
+ "OK"
63
+ else
64
+ raise "could not delete"
65
+ end
66
+ end
67
+ EOF
68
+ runner.review('app/helpers/posts_helper.rb', content)
69
+ expect(runner.errors.size).to eq(0)
70
+ end
71
+
72
+ it "should allow destroy return value used in unless" do
73
+ content =<<-EOF
74
+ def my_method
75
+ unless @post.destroy
76
+ raise "could not destroy"
77
+ end
78
+ end
79
+ EOF
80
+ runner.review('app/helpers/posts_helper.rb', content)
81
+ expect(runner.errors.size).to eq(0)
82
+ end
83
+
84
+ it "should allow destroy return value used in if_mod" do
85
+ content =<<-EOF
86
+ def my_method
87
+ post = Posts.create do |p|
88
+ p.title = "foo"
89
+ end
90
+ "OK" if post.destroy
91
+ end
92
+ EOF
93
+ runner.review('app/helpers/posts_helper.rb', content)
94
+ expect(runner.errors.size).to eq(0)
95
+ end
96
+
97
+ it "should allow destroy return value used in unless_mod" do
98
+ content =<<-EOF
99
+ def my_method
100
+ post = Posts.create do |p|
101
+ p.title = "foo"
102
+ end
103
+ "NO" unless post.destroy
104
+ end
105
+ EOF
106
+ runner.review('app/helpers/posts_helper.rb', content)
107
+ expect(runner.errors.size).to eq(0)
108
+ end
109
+
110
+ it "should allow destroy return value used in unless with &&" do
111
+ content =<<-EOF
112
+ def my_method
113
+ unless some_method(1) && other_method(2) && @post.destroy
114
+ raise "could not destroy"
115
+ end
116
+ end
117
+ EOF
118
+ runner.review('app/helpers/posts_helper.rb', content)
119
+ expect(runner.errors.size).to eq(0)
120
+ end
121
+
122
+ it "should allow destroy!" do
123
+ content =<<-EOF
124
+ def my_method
125
+ post = Posts.create do |p|
126
+ p.title = "foo"
127
+ end
128
+ post.destroy!
129
+ end
130
+ EOF
131
+ runner.review('app/helpers/posts_helper.rb', content)
132
+ expect(runner.errors.size).to eq(0)
133
+ end
134
+ end
135
+
136
+ it "should not check ignored files" do
137
+ runner = Core::Runner.new(reviews: CheckDestroyReturnValueReview.new(ignored_files: /helpers/))
138
+ content =<<-EOF
139
+ def my_method
140
+ post = Posts.create do |p|
141
+ p.title = "foo"
142
+ end
143
+ post.destroy
144
+ end
145
+ EOF
146
+ runner.review('app/helpers/posts_helper.rb', content)
147
+ expect(runner.errors.size).to eq(0)
148
+ end
149
+ end
150
+ end
151
+ end
@@ -84,6 +84,36 @@ module RailsBestPractices
84
84
  expect(runner.errors.size).to eq(0)
85
85
  end
86
86
 
87
+ it "should not remove unused methods if called in descendant controllers" do
88
+ application_helper_content =<<-EOF
89
+ module ApplicationHelper
90
+ def admin?; end
91
+ end
92
+ EOF
93
+ application_controller_content =<<-EOF
94
+ class ApplicationController
95
+ include ApplicationHelper
96
+ end
97
+ EOF
98
+ controller_content =<<-EOF
99
+ class PostsController < ApplicationController
100
+
101
+ def show
102
+ head(:forbidden) unless admin?
103
+ end
104
+ end
105
+ EOF
106
+ runner.prepare('app/helpers/application_helper.rb', application_helper_content)
107
+ runner.prepare('app/controllers/application_controller.rb', application_controller_content)
108
+ runner.prepare('app/controllers/posts_controller.rb', controller_content)
109
+ runner.after_prepare
110
+ runner.review('app/helpers/application_helper.rb', application_helper_content)
111
+ runner.review('app/controllers/application_controller.rb', application_controller_content)
112
+ runner.review('app/controllers/posts_controller.rb', controller_content)
113
+ runner.after_review
114
+ expect(runner.errors.size).to eq(0)
115
+ end
116
+
87
117
  it "should not check ignored files" do
88
118
  runner = Core::Runner.new(prepares: [Prepares::ControllerPrepare.new, Prepares::HelperPrepare.new],
89
119
  reviews: RemoveUnusedMethodsInHelpersReview.new(ignored_files: /posts_helper/, except_methods: []))
@@ -52,11 +52,10 @@ GEM
52
52
  expect(runner.errors[0].to_s).to eq("app/mailers/project_mailer.rb:2 - use multipart/alternative as content_type of email")
53
53
  end
54
54
 
55
- it "should use multiple/alternative as content_type of email when only plain text" do
55
+ it "should not use multiple/alternative as content_type of email when only plain text" do
56
56
  mock_email_files(["send_email.text.plain.erb"])
57
57
  runner.review('app/mailers/project_mailer.rb', content)
58
- expect(runner.errors.size).to eq(1)
59
- expect(runner.errors[0].to_s).to eq("app/mailers/project_mailer.rb:2 - use multipart/alternative as content_type of email")
58
+ expect(runner.errors.size).to eq(0)
60
59
  end
61
60
 
62
61
  it "should not use multipart/alternative as content_type of email" do
@@ -74,11 +73,10 @@ GEM
74
73
  expect(runner.errors[0].to_s).to eq("app/mailers/project_mailer.rb:2 - use multipart/alternative as content_type of email")
75
74
  end
76
75
 
77
- it "should use multiple/alternative as content_type of email when only plain text" do
76
+ it "should not use multiple/alternative as content_type of email when only plain text" do
78
77
  mock_email_files(["send_email.text.plain.haml"])
79
78
  runner.review('app/mailers/project_mailer.rb', content)
80
- expect(runner.errors.size).to eq(1)
81
- expect(runner.errors[0].to_s).to eq("app/mailers/project_mailer.rb:2 - use multipart/alternative as content_type of email")
79
+ expect(runner.errors.size).to eq(0)
82
80
  end
83
81
 
84
82
  it "should not use multipart/alternative as content_type of email" do
@@ -96,11 +94,10 @@ GEM
96
94
  expect(runner.errors[0].to_s).to eq("app/mailers/project_mailer.rb:2 - use multipart/alternative as content_type of email")
97
95
  end
98
96
 
99
- it "should use multiple/alternative as content_type of email when only plain text" do
97
+ it "should not use multiple/alternative as content_type of email when only plain text" do
100
98
  mock_email_files(["send_email.text.plain.slim"])
101
99
  runner.review('app/mailers/project_mailer.rb', content)
102
- expect(runner.errors.size).to eq(1)
103
- expect(runner.errors[0].to_s).to eq("app/mailers/project_mailer.rb:2 - use multipart/alternative as content_type of email")
100
+ expect(runner.errors.size).to eq(0)
104
101
  end
105
102
 
106
103
  it "should not use multipart/alternative as content_type of email" do
@@ -118,11 +115,10 @@ GEM
118
115
  expect(runner.errors[0].to_s).to eq("app/mailers/project_mailer.rb:2 - use multipart/alternative as content_type of email")
119
116
  end
120
117
 
121
- it "should use multiple/alternative as content_type of email when only plain text" do
118
+ it "should not use multiple/alternative as content_type of email when only plain text" do
122
119
  mock_email_files(["send_email.text.plain.rhtml"])
123
120
  runner.review('app/mailers/project_mailer.rb', content)
124
- expect(runner.errors.size).to eq(1)
125
- expect(runner.errors[0].to_s).to eq("app/mailers/project_mailer.rb:2 - use multipart/alternative as content_type of email")
121
+ expect(runner.errors.size).to eq(0)
126
122
  end
127
123
 
128
124
  it "should not use multipart/alternative as content_type of email" do
@@ -187,11 +183,10 @@ GEM
187
183
  expect(runner.errors[0].to_s).to eq("app/mailers/project_mailer.rb:2 - use multipart/alternative as content_type of email")
188
184
  end
189
185
 
190
- it "should use multiple/alternative as content_type of email when only plain text" do
186
+ it "should not use multiple/alternative as content_type of email when only plain text" do
191
187
  mock_email_files(["send_email.text.erb"])
192
188
  runner.review('app/mailers/project_mailer.rb', content)
193
- expect(runner.errors.size).to eq(1)
194
- expect(runner.errors[0].to_s).to eq("app/mailers/project_mailer.rb:2 - use multipart/alternative as content_type of email")
189
+ expect(runner.errors.size).to eq(0)
195
190
  end
196
191
 
197
192
  it "should not use multipart/alternative as content_type of email" do
@@ -209,11 +204,10 @@ GEM
209
204
  expect(runner.errors[0].to_s).to eq("app/mailers/project_mailer.rb:2 - use multipart/alternative as content_type of email")
210
205
  end
211
206
 
212
- it "should use multiple/alternative as content_type of email when only plain text" do
207
+ it "should not use multiple/alternative as content_type of email when only plain text" do
213
208
  mock_email_files(["send_email.text.haml"])
214
209
  runner.review('app/mailers/project_mailer.rb', content)
215
- expect(runner.errors.size).to eq(1)
216
- expect(runner.errors[0].to_s).to eq("app/mailers/project_mailer.rb:2 - use multipart/alternative as content_type of email")
210
+ expect(runner.errors.size).to eq(0)
217
211
  end
218
212
 
219
213
  it "should not use multipart/alternative as content_type of email" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_best_practices
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.15.7
4
+ version: 1.16.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-27 00:00:00.000000000 Z
11
+ date: 2016-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.4.3
41
- - !ruby/object:Gem::Dependency
42
- name: colored
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: erubis
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -206,6 +192,20 @@ dependencies:
206
192
  - - ">="
207
193
  - !ruby/object:Gem::Version
208
194
  version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: rubocop
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - '='
200
+ - !ruby/object:Gem::Version
201
+ version: 0.30.1
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - '='
207
+ - !ruby/object:Gem::Version
208
+ version: 0.30.1
209
209
  description: a code metric tool for rails codes, written in Ruby.
210
210
  email:
211
211
  - flyerhzm@gmail.com
@@ -216,7 +216,10 @@ extra_rdoc_files: []
216
216
  files:
217
217
  - ".gitignore"
218
218
  - ".rspec"
219
+ - ".rubocop.yml"
220
+ - ".rubocop_todo.yml"
219
221
  - ".travis.yml"
222
+ - CHANGELOG.md
220
223
  - Gemfile
221
224
  - Guardfile
222
225
  - MIT_LICENSE
@@ -227,6 +230,7 @@ files:
227
230
  - install_supported_rubies.sh
228
231
  - lib/rails_best_practices.rb
229
232
  - lib/rails_best_practices/analyzer.rb
233
+ - lib/rails_best_practices/colorize.rb
230
234
  - lib/rails_best_practices/command.rb
231
235
  - lib/rails_best_practices/core.rb
232
236
  - lib/rails_best_practices/core/check.rb
@@ -263,6 +267,7 @@ files:
263
267
  - lib/rails_best_practices/reviews.rb
264
268
  - lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb
265
269
  - lib/rails_best_practices/reviews/always_add_db_index_review.rb
270
+ - lib/rails_best_practices/reviews/check_destroy_return_value_review.rb
266
271
  - lib/rails_best_practices/reviews/check_save_return_value_review.rb
267
272
  - lib/rails_best_practices/reviews/default_scope_is_evil_review.rb
268
273
  - lib/rails_best_practices/reviews/dry_bundler_in_capistrano_review.rb
@@ -338,6 +343,7 @@ files:
338
343
  - spec/rails_best_practices/prepares/schema_prepare_spec.rb
339
344
  - spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb
340
345
  - spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb
346
+ - spec/rails_best_practices/reviews/check_destroy_return_value_spec.rb
341
347
  - spec/rails_best_practices/reviews/check_save_return_value_spec.rb
342
348
  - spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb
343
349
  - spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb
@@ -413,7 +419,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
413
419
  version: 1.3.6
414
420
  requirements: []
415
421
  rubyforge_project:
416
- rubygems_version: 2.4.5
422
+ rubygems_version: 2.5.1
417
423
  signing_key:
418
424
  specification_version: 4
419
425
  summary: a code metric tool for rails codes.
@@ -452,6 +458,7 @@ test_files:
452
458
  - spec/rails_best_practices/prepares/schema_prepare_spec.rb
453
459
  - spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb
454
460
  - spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb
461
+ - spec/rails_best_practices/reviews/check_destroy_return_value_spec.rb
455
462
  - spec/rails_best_practices/reviews/check_save_return_value_spec.rb
456
463
  - spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb
457
464
  - spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb