rails_best_practices 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. data/{LICENSE → MIT_LICENSE} +1 -1
  2. data/README.textile +27 -18
  3. data/lib/rails_best_practices/command.rb +8 -5
  4. data/lib/rails_best_practices/version.rb +4 -0
  5. metadata +115 -82
  6. data/.gitignore +0 -2
  7. data/Rakefile +0 -33
  8. data/VERSION +0 -1
  9. data/identifier +0 -0
  10. data/rails_best_practices.gemspec +0 -126
  11. data/spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb +0 -113
  12. data/spec/rails_best_practices/checks/always_add_db_index_check_spec.rb +0 -73
  13. data/spec/rails_best_practices/checks/isolate_seed_data_check_spec.rb +0 -76
  14. data/spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb +0 -103
  15. data/spec/rails_best_practices/checks/law_of_demeter_check_spec.rb +0 -76
  16. data/spec/rails_best_practices/checks/move_code_into_controller_check_spec.rb +0 -33
  17. data/spec/rails_best_practices/checks/move_code_into_helper_check_spec.rb +0 -29
  18. data/spec/rails_best_practices/checks/move_code_into_model_check_spec.rb +0 -55
  19. data/spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb +0 -82
  20. data/spec/rails_best_practices/checks/move_model_logic_into_model_check_spec.rb +0 -49
  21. data/spec/rails_best_practices/checks/needless_deep_nesting_check_spec.rb +0 -70
  22. data/spec/rails_best_practices/checks/not_use_default_route_check_spec.rb +0 -63
  23. data/spec/rails_best_practices/checks/overuse_route_customizations_check_spec.rb +0 -145
  24. data/spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb +0 -76
  25. data/spec/rails_best_practices/checks/replace_instance_variable_with_local_variable_check_spec.rb +0 -36
  26. data/spec/rails_best_practices/checks/use_before_filter_check_spec.rb +0 -85
  27. data/spec/rails_best_practices/checks/use_model_association_check_spec.rb +0 -71
  28. data/spec/rails_best_practices/checks/use_observer_check_spec.rb +0 -67
  29. data/spec/rails_best_practices/checks/use_scope_access_check_spec.rb +0 -193
  30. data/spec/spec.opts +0 -8
  31. data/spec/spec_helper.rb +0 -5
@@ -1,76 +0,0 @@
1
- require File.join(File.dirname(__FILE__) + '/../../spec_helper')
2
-
3
- describe RailsBestPractices::Checks::ReplaceComplexCreationWithFactoryMethodCheck do
4
- before(:each) do
5
- @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::ReplaceComplexCreationWithFactoryMethodCheck.new)
6
- end
7
-
8
- it "should replace complex creation with factory method" do
9
- content = <<-EOF
10
- class InvoiceController < ApplicationController
11
-
12
- def create
13
- @invoice = Invoice.new(params[:invoice])
14
- @invoice.address = current_user.address
15
- @invoice.phone = current_user.phone
16
- @invoice.vip = (@invoice.amount > 1000)
17
-
18
- if Time.now.day > 15
19
- @invoice.deliver_time = Time.now + 2.month
20
- else
21
- @invoice.deliver_time = Time.now + 1.month
22
- end
23
-
24
- @invoice.save
25
- end
26
- end
27
- EOF
28
- @runner.check('app/controllers/invoices_controller.rb', content)
29
- errors = @runner.errors
30
- errors.should_not be_empty
31
- errors[0].to_s.should == "app/controllers/invoices_controller.rb:3 - replace complex creation with factory method (@invoice attribute_assignment_count > 2)"
32
- end
33
-
34
- it "should not replace complex creation with factory method with simple creation" do
35
- content = <<-EOF
36
- class InvoiceController < ApplicationController
37
-
38
- def create
39
- @invoice = Invoice.new(params[:invoice])
40
- @invoice.address = current_user.address
41
- @invoice.phone = current_user.phone
42
- @invoice.save
43
- end
44
- end
45
- EOF
46
- @runner.check('app/controllers/invoices_controller.rb', content)
47
- errors = @runner.errors
48
- errors.should be_empty
49
- end
50
-
51
- it "should not replace complex creation with factory method when attrasgn_count is 5" do
52
- content = <<-EOF
53
- class InvoiceController < ApplicationController
54
-
55
- def create
56
- @invoice = Invoice.new(params[:invoice])
57
- @invoice.address = current_user.address
58
- @invoice.phone = current_user.phone
59
- @invoice.vip = (@invoice.amount > 1000)
60
-
61
- if Time.now.day > 15
62
- @invoice.deliver_time = Time.now + 2.month
63
- else
64
- @invoice.deliver_time = Time.now + 1.month
65
- end
66
-
67
- @invoice.save
68
- end
69
- end
70
- EOF
71
- @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::ReplaceComplexCreationWithFactoryMethodCheck.new('attribute_assignment_count' => 5))
72
- @runner.check('app/controllers/invoices_controller.rb', content)
73
- errors = @runner.errors
74
- errors.should be_empty
75
- end
76
- end
@@ -1,36 +0,0 @@
1
- require File.join(File.dirname(__FILE__) + '/../../spec_helper')
2
-
3
- describe RailsBestPractices::Checks::ReplaceInstanceVariableWithLocalVariableCheck do
4
- before(:each) do
5
- @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::ReplaceInstanceVariableWithLocalVariableCheck.new)
6
- end
7
-
8
- it "should replace instance variable with local varialbe" do
9
- content = <<-EOF
10
- <%= @post.title %>
11
- EOF
12
- @runner.check('app/views/posts/_post.html.erb', content)
13
- errors = @runner.errors
14
- errors.should_not be_empty
15
- errors[0].to_s.should == "app/views/posts/_post.html.erb:1 - replace instance variable with local variable"
16
- end
17
-
18
- it "should replace instance variable with local varialbe in haml file" do
19
- content = <<-EOF
20
- = @post.title
21
- EOF
22
- @runner.check('app/views/posts/_post.html.haml', content)
23
- errors = @runner.errors
24
- errors.should_not be_empty
25
- errors[0].to_s.should == "app/views/posts/_post.html.haml:1 - replace instance variable with local variable"
26
- end
27
-
28
- it "should not replace instance variable with local varialbe" do
29
- content = <<-EOF
30
- <%= post.title %>
31
- EOF
32
- @runner.check('app/views/posts/_post.html.erb', content)
33
- errors = @runner.errors
34
- errors.should be_empty
35
- end
36
- end
@@ -1,85 +0,0 @@
1
- require File.join(File.dirname(__FILE__) + '/../../spec_helper')
2
-
3
- describe RailsBestPractices::Checks::UseBeforeFilterCheck do
4
- before(:each) do
5
- @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::UseBeforeFilterCheck.new)
6
- end
7
-
8
- it "should use before_filter" do
9
- content = <<-EOF
10
- class PostsController < ApplicationController
11
-
12
- def show
13
- @post = current_user.posts.find(params[:id])
14
- end
15
-
16
- def edit
17
- @post = current_user.posts.find(params[:id])
18
- end
19
-
20
- def update
21
- @post = current_user.posts.find(params[:id])
22
- @post.update_attributes(params[:post])
23
- end
24
-
25
- def destroy
26
- @post = current_user.posts.find(params[:id])
27
- @post.destroy
28
- end
29
-
30
- end
31
- EOF
32
- @runner.check('app/controllers/posts_controller.rb', content)
33
- errors = @runner.errors
34
- errors.should_not be_empty
35
- errors[0].to_s.should == "app/controllers/posts_controller.rb:3,7,11,16 - use before_filter for show,edit,update,destroy"
36
- end
37
-
38
- it "should not use before_filter" do
39
- content = <<-EOF
40
- class PostsController < ApplicationController
41
- before_filter :find_post, :only => [:show, :edit, :update, :destroy]
42
-
43
- def update
44
- @post.update_attributes(params[:post])
45
- end
46
-
47
- def destroy
48
- @post.destroy
49
- end
50
-
51
- protected
52
-
53
- def find_post
54
- @post = current_user.posts.find(params[:id])
55
- end
56
- end
57
- EOF
58
- @runner.check('app/controllers/posts_controller.rb', content)
59
- errors = @runner.errors
60
- errors.should be_empty
61
- end
62
-
63
- it "should not use before_filter by nil" do
64
- content = <<-EOF
65
- class PostsController < ApplicationController
66
-
67
- def show
68
- end
69
-
70
- def edit
71
- end
72
-
73
- def update
74
- end
75
-
76
- def destroy
77
- end
78
-
79
- end
80
- EOF
81
- @runner.check('app/controllers/posts_controller.rb', content)
82
- errors = @runner.errors
83
- errors.should be_empty
84
- end
85
- end
@@ -1,71 +0,0 @@
1
- require File.join(File.dirname(__FILE__) + '/../../spec_helper')
2
-
3
- describe RailsBestPractices::Checks::UseModelAssociationCheck do
4
- before(:each) do
5
- @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::UseModelAssociationCheck.new)
6
- end
7
-
8
- it "should use model association for instance variable" do
9
- content = <<-EOF
10
- class PostsController < ApplicationController
11
-
12
- def create
13
- @post = Post.new(params[:post])
14
- @post.user_id = current_user.id
15
- @post.save
16
- end
17
- end
18
- EOF
19
- @runner.check('app/controllers/posts_controller.rb', content)
20
- errors = @runner.errors
21
- errors.should_not be_empty
22
- errors[0].to_s.should == "app/controllers/posts_controller.rb:3 - use model association (for @post)"
23
- end
24
-
25
- it "should not use model association without association assign" do
26
- content = <<-EOF
27
- class PostsController < ApplicationController
28
-
29
- def create
30
- @post = Post.new(params[:post])
31
- @post.save
32
- end
33
- end
34
- EOF
35
- @runner.check('app/controllers/posts_controller.rb', content)
36
- errors = @runner.errors
37
- errors.should be_empty
38
- end
39
-
40
- it "should use model association for local variable" do
41
- content = <<-EOF
42
- class PostsController < ApplicationController
43
-
44
- def create
45
- post = Post.new(params[:post])
46
- post.user_id = current_user.id
47
- post.save
48
- end
49
- end
50
- EOF
51
- @runner.check('app/controllers/posts_controller.rb', content)
52
- errors = @runner.errors
53
- errors.should_not be_empty
54
- errors[0].to_s.should == "app/controllers/posts_controller.rb:3 - use model association (for post)"
55
- end
56
-
57
- it "should not use model association" do
58
- content = <<-EOF
59
- class PostsController < ApplicationController
60
-
61
- def create
62
- post = current_user.posts.buid(params[:post])
63
- post.save
64
- end
65
- end
66
- EOF
67
- @runner.check('app/controllers/posts_controller.rb', content)
68
- errors = @runner.errors
69
- errors.should be_empty
70
- end
71
- end
@@ -1,67 +0,0 @@
1
- require File.join(File.dirname(__FILE__) + '/../../spec_helper')
2
-
3
- describe RailsBestPractices::Checks::UseObserverCheck do
4
- before(:each) do
5
- @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::UseObserverCheck.new)
6
- end
7
-
8
- it "should use observer" do
9
- content =<<-EOF
10
- class Project < ActiveRecord::Base
11
- after_create :send_create_notification
12
-
13
- private
14
-
15
- def send_create_notification
16
- self.members.each do |member|
17
- ProjectMailer.deliver_notification(self, member)
18
- end
19
- end
20
- end
21
- EOF
22
- @runner.check('app/models/project.rb', content)
23
- errors = @runner.errors
24
- errors.should_not be_empty
25
- errors[0].to_s.should == "app/models/project.rb:6 - use observer"
26
- end
27
-
28
- it "should not use observer without callback" do
29
- content =<<-EOF
30
- class Project < ActiveRecord::Base
31
- private
32
-
33
- def send_create_notification
34
- self.members.each do |member|
35
- ProjectMailer.deliver_notification(self, member)
36
- end
37
- end
38
- end
39
- EOF
40
- @runner.check('app/models/project.rb', content)
41
- errors = @runner.errors
42
- errors.should be_empty
43
- end
44
-
45
- it "should use observer with two after_create" do
46
- content =<<-EOF
47
- class Project < ActiveRecord::Base
48
- after_create :send_create_notification, :update_author
49
-
50
- private
51
-
52
- def send_create_notification
53
- self.members.each do |member|
54
- ProjectMailer.deliver_notification(self, member)
55
- end
56
- end
57
-
58
- def update_author
59
- end
60
- end
61
- EOF
62
- @runner.check('app/models/project.rb', content)
63
- errors = @runner.errors
64
- errors.should_not be_empty
65
- errors[0].to_s.should == "app/models/project.rb:6 - use observer"
66
- end
67
- end
@@ -1,193 +0,0 @@
1
- require File.join(File.dirname(__FILE__) + '/../../spec_helper')
2
-
3
- describe RailsBestPractices::Checks::UseScopeAccessCheck do
4
- before(:each) do
5
- @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::UseScopeAccessCheck.new)
6
- end
7
-
8
- context "if" do
9
- it "shoud use scope access" do
10
- content = <<-EOF
11
- class PostsController < ApplicationController
12
-
13
- def edit
14
- @post = Post.find(params[:id])
15
-
16
- if @post.user != current_user
17
- flash[:warning] = 'Access Denied'
18
- redirect_to posts_url
19
- end
20
- end
21
- end
22
- EOF
23
- @runner.check('app/controllers/posts_controller.rb', content)
24
- errors = @runner.errors
25
- errors.should_not be_empty
26
- errors[0].to_s.should == "app/controllers/posts_controller.rb:7 - use scope access"
27
- end
28
-
29
- it "shoud use scope access by comparing with id" do
30
- content = <<-EOF
31
- class PostsController < ApplicationController
32
-
33
- def edit
34
- @post = Post.find(params[:id])
35
-
36
- if @post.user_id != current_user.id
37
- flash[:warning] = 'Access Denied'
38
- redirect_to posts_url
39
- end
40
- end
41
- end
42
- EOF
43
- @runner.check('app/controllers/posts_controller.rb', content)
44
- errors = @runner.errors
45
- errors.should_not be_empty
46
- errors[0].to_s.should == "app/controllers/posts_controller.rb:7 - use scope access"
47
- end
48
-
49
- it "shoud use scope access with current_user ==" do
50
- content = <<-EOF
51
- class PostsController < ApplicationController
52
-
53
- def edit
54
- @post = Post.find(params[:id])
55
-
56
- if current_user != @post.user
57
- flash[:warning] = 'Access Denied'
58
- redirect_to posts_url
59
- end
60
- end
61
- end
62
- EOF
63
- @runner.check('app/controllers/posts_controller.rb', content)
64
- errors = @runner.errors
65
- errors.should_not be_empty
66
- errors[0].to_s.should == "app/controllers/posts_controller.rb:7 - use scope access"
67
- end
68
-
69
- it "shoud use scope access by current_user.id ==" do
70
- content = <<-EOF
71
- class PostsController < ApplicationController
72
-
73
- def edit
74
- @post = Post.find(params[:id])
75
-
76
- if current_user.id != @post.user_id
77
- flash[:warning] = 'Access Denied'
78
- redirect_to posts_url
79
- end
80
- end
81
- end
82
- EOF
83
- @runner.check('app/controllers/posts_controller.rb', content)
84
- errors = @runner.errors
85
- errors.should_not be_empty
86
- errors[0].to_s.should == "app/controllers/posts_controller.rb:7 - use scope access"
87
- end
88
- end
89
-
90
- context "unless" do
91
- it "shoud use scope access" do
92
- content = <<-EOF
93
- class PostsController < ApplicationController
94
-
95
- def edit
96
- @post = Post.find(params[:id])
97
-
98
- unless @post.user == current_user
99
- flash[:warning] = 'Access Denied'
100
- redirect_to posts_url
101
- end
102
- end
103
- end
104
- EOF
105
- @runner.check('app/controllers/posts_controller.rb', content)
106
- errors = @runner.errors
107
- errors.should_not be_empty
108
- errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
109
- end
110
-
111
- it "shoud use scope access by comparing with id" do
112
- content = <<-EOF
113
- class PostsController < ApplicationController
114
-
115
- def edit
116
- @post = Post.find(params[:id])
117
-
118
- unless @post.user_id == current_user.id
119
- flash[:warning] = 'Access Denied'
120
- redirect_to posts_url
121
- end
122
- end
123
- end
124
- EOF
125
- @runner.check('app/controllers/posts_controller.rb', content)
126
- errors = @runner.errors
127
- errors.should_not be_empty
128
- errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
129
- end
130
-
131
- it "shoud use scope access with current_user ==" do
132
- content = <<-EOF
133
- class PostsController < ApplicationController
134
-
135
- def edit
136
- @post = Post.find(params[:id])
137
-
138
- unless current_user == @post.user
139
- flash[:warning] = 'Access Denied'
140
- redirect_to posts_url
141
- end
142
- end
143
- end
144
- EOF
145
- @runner.check('app/controllers/posts_controller.rb', content)
146
- errors = @runner.errors
147
- errors.should_not be_empty
148
- errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
149
- end
150
-
151
- it "shoud use scope access by current_user.id ==" do
152
- content = <<-EOF
153
- class PostsController < ApplicationController
154
-
155
- def edit
156
- @post = Post.find(params[:id])
157
-
158
- unless current_user.id == @post.user_id
159
- flash[:warning] = 'Access Denied'
160
- redirect_to posts_url
161
- end
162
- end
163
- end
164
- EOF
165
- @runner.check('app/controllers/posts_controller.rb', content)
166
- errors = @runner.errors
167
- errors.should_not be_empty
168
- errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
169
- end
170
-
171
- it "should no error in use_scope_access_check" do
172
- content = <<-EOF
173
- class CommentsController < ApplicationController
174
-
175
- def add_comment
176
- @current_user = User.find_by_id(session[:user_id])
177
- @id = params[:post_id]
178
- @error = ""
179
- if (@text = params[:text]) == ""
180
- @error = "Please enter a comment!"
181
- else
182
- @comment = Comment.create_object(@text, @id, @current_user.id)
183
- end
184
- unless @comment
185
- @error = "Comment could not be saved."
186
- end
187
- end
188
- end
189
- EOF
190
- @runner.check('app/controllers/comments_controller.rb', content)
191
- end
192
- end
193
- end