rails_best_practices 1.13.1 → 1.13.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,11 +15,11 @@ module RailsBestPractices
15
15
  # Check all "save" calls to check the return value is used by a node we have visited.
16
16
  class CheckSaveReturnValueReview < Review
17
17
  include Classable
18
- interesting_nodes :call, :command_call, :method_add_arg, :if, :ifop, :elsif, :unless, :assign, :binary
18
+ interesting_nodes :call, :command_call, :method_add_arg, :if, :ifop, :elsif, :unless, :if_mod, :unless_mod, :assign, :binary
19
19
  interesting_files ALL_FILES
20
20
  url "http://rails-bestpractices.com/posts/703-check-the-return-value-of-save-otherwise-use-save"
21
21
 
22
- add_callback :start_if, :start_ifop, :start_elsif, :start_unless do |node|
22
+ add_callback :start_if, :start_ifop, :start_elsif, :start_unless, :start_if_mod, :start_unless_mod do |node|
23
23
  @used_return_value_of = node.conditional_statement.all_conditions
24
24
  end
25
25
 
@@ -74,5 +74,3 @@ module RailsBestPractices
74
74
  end
75
75
  end
76
76
  end
77
-
78
-
@@ -13,7 +13,7 @@ module RailsBestPractices
13
13
  # check if, unless, elsif there are multiple method calls or attribute assignments apply to one receiver,
14
14
  # and the receiver is a variable, then they should be moved into model.
15
15
  class MoveCodeIntoModelReview < Review
16
- interesting_nodes :if, :unless, :elsif
16
+ interesting_nodes :if, :unless, :elsif, :ifop, :if_mod, :unless_mod
17
17
  interesting_files VIEW_FILES
18
18
  url "http://rails-bestpractices.com/posts/25-move-code-into-model"
19
19
 
@@ -28,7 +28,7 @@ module RailsBestPractices
28
28
  #
29
29
  # if there are multiple call and assignment nodes who have the same receiver,
30
30
  # and the receiver is a variable, then the conditional statement nodes should be moved into model.
31
- add_callback :start_if, :start_unless, :start_elsif do |node|
31
+ add_callback :start_if, :start_unless, :start_elsif, :start_ifop, :start_if_mod, :start_unless_mod do |node|
32
32
  node.conditional_statement.grep_nodes(sexp_type: :call) { |child_node| remember_variable_use_count(child_node) }
33
33
 
34
34
  variable_use_count.each do |variable_node, count|
@@ -16,7 +16,7 @@ module RailsBestPractices
16
16
  # and their messages of second call are one of nil?, blank?, present?, or they are == ""
17
17
  # then you can use query attribute instead.
18
18
  class UseQueryAttributeReview < Review
19
- interesting_nodes :if, :unless, :elsif
19
+ interesting_nodes :if, :unless, :elsif, :ifop, :if_mod, :unless_mod
20
20
  interesting_files ALL_FILES
21
21
  url "http://rails-bestpractices.com/posts/56-use-query-attribute"
22
22
 
@@ -34,7 +34,7 @@ module RailsBestPractices
34
34
  # the message is == and the argument is ""
35
35
  #
36
36
  # then the call node can use query attribute instead.
37
- add_callback :start_if, :start_unless, :start_elsif do |node|
37
+ add_callback :start_if, :start_unless, :start_elsif, :start_ifop, :start_if_mod, :start_unless_mod do |node|
38
38
  all_conditions = if node.conditional_statement == node.conditional_statement.all_conditions
39
39
  [node.conditional_statement]
40
40
  else
@@ -16,7 +16,7 @@ module RailsBestPractices
16
16
  # and there is redirect_to method call in if block body,
17
17
  # then it should be replaced by using scope access.
18
18
  class UseScopeAccessReview < Review
19
- interesting_nodes :if, :unless, :elsif
19
+ interesting_nodes :if, :unless, :elsif, :ifop, :if_mod, :unless_mod
20
20
  interesting_files CONTROLLER_FILES
21
21
  url "http://rails-bestpractices.com/posts/3-use-scope-access"
22
22
 
@@ -25,7 +25,7 @@ module RailsBestPractices
25
25
  # if it is a method call compared with current_user or current_user.id,
26
26
  # and there is a redirect_to method call in the block body,
27
27
  # then it should be replaced by using scope access.
28
- add_callback :start_if, :start_unless, :start_elsif do |node|
28
+ add_callback :start_if, :start_unless, :start_elsif, :start_ifop, :start_if_mod, :start_unless_mod do |node|
29
29
  add_error "use scope access" if current_user_redirect?(node)
30
30
  end
31
31
 
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module RailsBestPractices
3
- VERSION = "1.13.1"
3
+ VERSION = "1.13.2"
4
4
  end
@@ -81,6 +81,32 @@ module RailsBestPractices
81
81
  runner.should have(0).errors
82
82
  end
83
83
 
84
+ it "should allow save return value used in if_mod" do
85
+ content =<<-EOF
86
+ def my_method
87
+ post = Posts.new do |p|
88
+ p.title = "foo"
89
+ end
90
+ "OK" if post.save
91
+ end
92
+ EOF
93
+ runner.review('app/helpers/posts_helper.rb', content)
94
+ expect(runner).to have(0).errors
95
+ end
96
+
97
+ it "should allow save return value used in unless_mod" do
98
+ content =<<-EOF
99
+ def my_method
100
+ post = Posts.new do |p|
101
+ p.title = "foo"
102
+ end
103
+ "NO" unless post.save
104
+ end
105
+ EOF
106
+ runner.review('app/helpers/posts_helper.rb', content)
107
+ expect(runner).to have(0).errors
108
+ end
109
+
84
110
  it "should allow save return value used in unless with &&" do
85
111
  content =<<-EOF
86
112
  def my_method
@@ -36,6 +36,24 @@ module RailsBestPractices
36
36
  runner.errors[0].to_s.should == "app/views/posts/show.html.slim:1 - move code into model (@post use_count > 2)"
37
37
  end
38
38
 
39
+ it "should move code into model with if in one line" do
40
+ content =<<-EOF
41
+ <%= link_to 'Edit this post', edit_post_url(@post) if current_user && @post.user && (current_user == @post.user || @post.editors.include?(current_user)) %>
42
+ EOF
43
+ runner.review('app/views/posts/show.html.erb', content)
44
+ runner.should have(1).errors
45
+ runner.errors[0].to_s.should == "app/views/posts/show.html.erb:1 - move code into model (@post use_count > 2)"
46
+ end
47
+
48
+ it "should move code into model with '? :'" do
49
+ content =<<-EOF
50
+ <%= current_user && @post.user && (current_user == @post.user || @post.editors.include?(current_user)) ? link_to('Edit this post', edit_post_url(@post)) : '' %>
51
+ EOF
52
+ runner.review('app/views/posts/show.html.erb', content)
53
+ runner.should have(1).errors
54
+ runner.errors[0].to_s.should == "app/views/posts/show.html.erb:1 - move code into model (@post use_count > 2)"
55
+ end
56
+
39
57
  it "should move code into model only review for current if conditional statement" do
40
58
  content =<<-EOF
41
59
  <% if @post.title %>
@@ -39,6 +39,24 @@ module RailsBestPractices
39
39
  runner.errors[0].to_s.should == "app/views/users/show.html.erb:1 - use query attribute (@user.login?)"
40
40
  end
41
41
 
42
+ it "should use query attribute by blank call with if in one line" do
43
+ content = <<-EOF
44
+ <%= link_to 'login', new_session_path if @user.login.blank? %>
45
+ EOF
46
+ runner.review('app/views/users/show.html.erb', content)
47
+ runner.should have(1).errors
48
+ runner.errors[0].to_s.should == "app/views/users/show.html.erb:1 - use query attribute (@user.login?)"
49
+ end
50
+
51
+ it "should use query attribute by blank call with '? :'" do
52
+ content = <<-EOF
53
+ <%= @user.login.blank? ? link_to('login', new_session_path) : '' %>
54
+ EOF
55
+ runner.review('app/views/users/show.html.erb', content)
56
+ runner.should have(1).errors
57
+ runner.errors[0].to_s.should == "app/views/users/show.html.erb:1 - use query attribute (@user.login?)"
58
+ end
59
+
42
60
  it "should use query attribute by comparing empty string" do
43
61
  content = <<-EOF
44
62
  <% if @user.login == "" %>
@@ -24,6 +24,36 @@ module RailsBestPractices
24
24
  runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:5 - use scope access"
25
25
  end
26
26
 
27
+ it "shoud use scope access with if in one line" do
28
+ content = <<-EOF
29
+ class PostsController < ApplicationController
30
+ def edit
31
+ @post = Post.find(params[:id])
32
+
33
+ redirect_to posts_url if @post.user != current_user
34
+ end
35
+ end
36
+ EOF
37
+ runner.review('app/controllers/posts_controller.rb', content)
38
+ runner.should have(1).errors
39
+ runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:5 - use scope access"
40
+ end
41
+
42
+ it "shoud use scope access with '? :'" do
43
+ content = <<-EOF
44
+ class PostsController < ApplicationController
45
+ def edit
46
+ @post = Post.find(params[:id])
47
+
48
+ @post.user != current_user ? redirect_to(posts_url) : render
49
+ end
50
+ end
51
+ EOF
52
+ runner.review('app/controllers/posts_controller.rb', content)
53
+ runner.should have(1).errors
54
+ runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:5 - use scope access"
55
+ end
56
+
27
57
  it "shoud use scope access by comparing with id" do
28
58
  content = <<-EOF
29
59
  class PostsController < ApplicationController
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_best_practices
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.13.1
4
+ version: 1.13.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-03 00:00:00.000000000 Z
12
+ date: 2012-12-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: code_analyzer
@@ -386,7 +386,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
386
386
  version: '0'
387
387
  segments:
388
388
  - 0
389
- hash: 4362184298328338159
389
+ hash: -3343139253340945050
390
390
  required_rubygems_version: !ruby/object:Gem::Requirement
391
391
  none: false
392
392
  requirements: