rails_best_practices 0.2.0 → 0.3.0
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.
- data/README.textile +32 -14
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb +12 -7
- data/lib/rails_best_practices/checks/always_add_db_index_check.rb +90 -0
- data/lib/rails_best_practices/checks/check.rb +9 -3
- data/lib/rails_best_practices/checks/isolate_seed_data_check.rb +59 -0
- data/lib/rails_best_practices/checks/keep_finders_on_their_own_model_check.rb +29 -0
- data/lib/rails_best_practices/checks/law_of_demeter_check.rb +47 -0
- data/lib/rails_best_practices/checks/move_code_into_controller_check.rb +31 -0
- data/lib/rails_best_practices/checks/move_code_into_helper_check.rb +29 -0
- data/lib/rails_best_practices/checks/move_finder_to_named_scope_check.rb +1 -1
- data/lib/rails_best_practices/checks/move_model_logic_into_model_check.rb +1 -1
- data/lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb +1 -1
- data/lib/rails_best_practices/checks/replace_instance_variable_with_local_variable_check.rb +23 -0
- data/lib/rails_best_practices/checks/use_filter_check.rb +38 -0
- data/lib/rails_best_practices/checks/use_observer_check.rb +49 -0
- data/lib/rails_best_practices/checks/use_scope_access_check.rb +3 -3
- data/lib/rails_best_practices/checks.rb +10 -1
- data/lib/rails_best_practices/command.rb +50 -5
- data/lib/rails_best_practices/core/core_ext.rb +12 -1
- data/lib/rails_best_practices/core/runner.rb +12 -3
- data/lib/rails_best_practices/core/visitable_sexp.rb +21 -5
- data/rails_best_practices.gemspec +43 -16
- data/rails_best_practices.yml +9 -0
- data/spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb +57 -4
- data/spec/rails_best_practices/checks/always_add_db_index_check_spec.rb +153 -0
- data/spec/rails_best_practices/checks/isolate_seed_data_check_spec.rb +79 -0
- data/spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb +71 -0
- data/spec/rails_best_practices/checks/law_of_demeter_check_spec.rb +37 -0
- data/spec/rails_best_practices/checks/move_code_into_controller_check_spec.rb +33 -0
- data/spec/rails_best_practices/checks/move_code_into_helper_check_spec.rb +29 -0
- data/spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb +6 -6
- data/spec/rails_best_practices/checks/move_model_logic_into_model_check_spec.rb +4 -4
- data/spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb +5 -5
- data/spec/rails_best_practices/checks/replace_instance_variable_with_local_variable_check_spec.rb +26 -0
- data/spec/rails_best_practices/checks/use_filter_check_spec.rb +85 -0
- data/spec/rails_best_practices/checks/use_model_association_check_spec.rb +6 -6
- data/spec/rails_best_practices/checks/use_observer_check_spec.rb +67 -0
- data/spec/rails_best_practices/checks/use_scope_access_check_spec.rb +17 -17
- metadata +38 -11
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__) + '/../../spec_helper')
|
|
2
|
+
|
|
3
|
+
describe RailsBestPractices::Checks::KeepFindersOnTheirOwnModelCheck do
|
|
4
|
+
before(:each) do
|
|
5
|
+
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::KeepFindersOnTheirOwnModelCheck.new)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "should keep finders on thier own model" do
|
|
9
|
+
content = <<-EOF
|
|
10
|
+
class Post < ActiveRecord::Base
|
|
11
|
+
has_many :comments
|
|
12
|
+
|
|
13
|
+
def find_valid_comments
|
|
14
|
+
self.comment.find(:all, :conditions => { :is_spam => false },
|
|
15
|
+
:limit => 10)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
EOF
|
|
19
|
+
@runner.check('app/models/post.rb', content)
|
|
20
|
+
errors = @runner.errors
|
|
21
|
+
errors.should_not be_empty
|
|
22
|
+
errors[0].to_s.should == "app/models/post.rb:5 - keep finders on their own model"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "should not keep finders on thier own model with self finder" do
|
|
26
|
+
content = <<-EOF
|
|
27
|
+
class Post < ActiveRecord::Base
|
|
28
|
+
has_many :comments
|
|
29
|
+
|
|
30
|
+
def find_valid_comments
|
|
31
|
+
self.find(:all, :conditions => { :is_spam => false },
|
|
32
|
+
:limit => 10)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
EOF
|
|
36
|
+
@runner.check('app/models/post.rb', content)
|
|
37
|
+
errors = @runner.errors
|
|
38
|
+
errors.should be_empty
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "should not keep finders on thier own model with own finder" do
|
|
42
|
+
content = <<-EOF
|
|
43
|
+
class Post < ActiveRecord::Base
|
|
44
|
+
has_many :comments
|
|
45
|
+
|
|
46
|
+
def find_valid_comments
|
|
47
|
+
Post.find(:all, :conditions => { :is_spam => false },
|
|
48
|
+
:limit => 10)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
EOF
|
|
52
|
+
@runner.check('app/models/post.rb', content)
|
|
53
|
+
errors = @runner.errors
|
|
54
|
+
errors.should be_empty
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it "should not keep finders on thier own model without finder" do
|
|
58
|
+
content = <<-EOF
|
|
59
|
+
class Post < ActiveRecord::Base
|
|
60
|
+
has_many :comments
|
|
61
|
+
|
|
62
|
+
def find_valid_comments
|
|
63
|
+
self.comments.destroy_all
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
EOF
|
|
67
|
+
@runner.check('app/models/post.rb', content)
|
|
68
|
+
errors = @runner.errors
|
|
69
|
+
errors.should be_empty
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__) + '/../../spec_helper')
|
|
2
|
+
|
|
3
|
+
describe RailsBestPractices::Checks::LawOfDemeterCheck do
|
|
4
|
+
before(:each) do
|
|
5
|
+
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::LawOfDemeterCheck.new)
|
|
6
|
+
|
|
7
|
+
content = <<-EOF
|
|
8
|
+
class Invoice < ActiveRecord::Base
|
|
9
|
+
belongs_to :user
|
|
10
|
+
end
|
|
11
|
+
EOF
|
|
12
|
+
@runner.check('app/models/invoice.rb', content)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "should law of demeter" do
|
|
16
|
+
content = <<-EOF
|
|
17
|
+
<%= @invoice.user.name %>
|
|
18
|
+
<%= @invoice.user.address %>
|
|
19
|
+
<%= @invoice.user.cellphone %>
|
|
20
|
+
EOF
|
|
21
|
+
@runner.check('app/views/invoices/show.html.erb', content)
|
|
22
|
+
errors = @runner.errors
|
|
23
|
+
errors.should_not be_empty
|
|
24
|
+
errors[0].to_s.should == "app/views/invoices/show.html.erb:1 - law of demeter"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "should no law of demeter" do
|
|
28
|
+
content = <<-EOF
|
|
29
|
+
<%= @invoice.user_name %>
|
|
30
|
+
<%= @invoice.user_address %>
|
|
31
|
+
<%= @invoice.user_cellphone %>
|
|
32
|
+
EOF
|
|
33
|
+
@runner.check('app/views/invoices/show.html.erb', content)
|
|
34
|
+
errors = @runner.errors
|
|
35
|
+
errors.should be_empty
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__) + '/../../spec_helper')
|
|
2
|
+
|
|
3
|
+
describe RailsBestPractices::Checks::MoveCodeIntoControllerCheck do
|
|
4
|
+
before(:each) do
|
|
5
|
+
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::MoveCodeIntoControllerCheck.new)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "should move code into controller" do
|
|
9
|
+
content = <<-EOF
|
|
10
|
+
<% @posts = Post.find(:all) %>
|
|
11
|
+
<% @posts.each do |post| %>
|
|
12
|
+
<%=h post.title %>
|
|
13
|
+
<%=h post.content %>
|
|
14
|
+
<% end %>
|
|
15
|
+
EOF
|
|
16
|
+
@runner.check('app/views/posts/index.html.erb', content)
|
|
17
|
+
errors = @runner.errors
|
|
18
|
+
errors.should_not be_empty
|
|
19
|
+
errors[0].to_s.should == "app/views/posts/index.html.erb:1 - move code into controller"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it "should not move code into controller" do
|
|
23
|
+
content = <<-EOF
|
|
24
|
+
<% @posts.each do |post| %>
|
|
25
|
+
<%=h post.title %>
|
|
26
|
+
<%=h post.content %>
|
|
27
|
+
<% end %>
|
|
28
|
+
EOF
|
|
29
|
+
@runner.check('app/views/posts/index.html.erb', content)
|
|
30
|
+
errors = @runner.errors
|
|
31
|
+
errors.should be_empty
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__) + '/../../spec_helper')
|
|
2
|
+
|
|
3
|
+
describe RailsBestPractices::Checks::MoveCodeIntoHelperCheck do
|
|
4
|
+
before(:each) do
|
|
5
|
+
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::MoveCodeIntoHelperCheck.new)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "should move code into helper" do
|
|
9
|
+
content = <<-EOF
|
|
10
|
+
<%= select_tag :state, options_for_select( [[t(:draft), "draft"],
|
|
11
|
+
[t(:published), "published"]],
|
|
12
|
+
params[:default_state] ) %>
|
|
13
|
+
|
|
14
|
+
EOF
|
|
15
|
+
@runner.check('app/views/posts/show.html.erb', content)
|
|
16
|
+
errors = @runner.errors
|
|
17
|
+
errors.should_not be_empty
|
|
18
|
+
errors[0].to_s.should == "app/views/posts/show.html.erb:3 - move code into helper"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it "should not move code into helper with simple arguments" do
|
|
22
|
+
content = <<-EOF
|
|
23
|
+
<%= select_tag :state, options_for_select( Post.STATES ) %>
|
|
24
|
+
EOF
|
|
25
|
+
@runner.check('app/views/posts/show.html.erb', content)
|
|
26
|
+
errors = @runner.errors
|
|
27
|
+
errors.should be_empty
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -20,11 +20,11 @@ describe RailsBestPractices::Checks::MoveFinderToNamedScopeCheck do
|
|
|
20
20
|
end
|
|
21
21
|
end
|
|
22
22
|
EOF
|
|
23
|
-
@runner.check('app/
|
|
23
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
24
24
|
errors = @runner.errors
|
|
25
25
|
errors.size.should == 2
|
|
26
|
-
errors[0].to_s.should == "app/
|
|
27
|
-
errors[1].to_s.should == "app/
|
|
26
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:4 - move finder to named_scope"
|
|
27
|
+
errors[1].to_s.should == "app/controllers/posts_controller.rb:8 - move finder to named_scope"
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
it "should not move simple finder" do
|
|
@@ -41,7 +41,7 @@ describe RailsBestPractices::Checks::MoveFinderToNamedScopeCheck do
|
|
|
41
41
|
end
|
|
42
42
|
end
|
|
43
43
|
EOF
|
|
44
|
-
@runner.check('app/
|
|
44
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
45
45
|
@runner.errors.should be_empty
|
|
46
46
|
end
|
|
47
47
|
|
|
@@ -55,7 +55,7 @@ describe RailsBestPractices::Checks::MoveFinderToNamedScopeCheck do
|
|
|
55
55
|
end
|
|
56
56
|
end
|
|
57
57
|
EOF
|
|
58
|
-
@runner.check('app/
|
|
58
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
59
59
|
@runner.errors.should be_empty
|
|
60
60
|
end
|
|
61
61
|
|
|
@@ -79,4 +79,4 @@ describe RailsBestPractices::Checks::MoveFinderToNamedScopeCheck do
|
|
|
79
79
|
@runner.errors.should be_empty
|
|
80
80
|
|
|
81
81
|
end
|
|
82
|
-
end
|
|
82
|
+
end
|
|
@@ -23,10 +23,10 @@ describe RailsBestPractices::Checks::MoveModelLogicIntoModelCheck do
|
|
|
23
23
|
redirect_to post_url(@post)
|
|
24
24
|
end
|
|
25
25
|
EOF
|
|
26
|
-
@runner.check('app/
|
|
26
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
27
27
|
errors = @runner.errors
|
|
28
28
|
errors.should_not be_empty
|
|
29
|
-
errors[0].to_s.should == "app/
|
|
29
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:3 - move model logic into model (@post called_count > 4)"
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
it "should not move model logic into model with simple model calling" do
|
|
@@ -42,8 +42,8 @@ describe RailsBestPractices::Checks::MoveModelLogicIntoModelCheck do
|
|
|
42
42
|
redirect_to post_url(@post)
|
|
43
43
|
end
|
|
44
44
|
EOF
|
|
45
|
-
@runner.check('app/
|
|
45
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
46
46
|
errors = @runner.errors
|
|
47
47
|
errors.should be_empty
|
|
48
48
|
end
|
|
49
|
-
end
|
|
49
|
+
end
|
data/spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb
CHANGED
|
@@ -25,10 +25,10 @@ describe RailsBestPractices::Checks::ReplaceComplexCreationWithFactoryMethodChec
|
|
|
25
25
|
end
|
|
26
26
|
end
|
|
27
27
|
EOF
|
|
28
|
-
@runner.check('app/
|
|
28
|
+
@runner.check('app/controllers/invoices_controller.rb', content)
|
|
29
29
|
errors = @runner.errors
|
|
30
30
|
errors.should_not be_empty
|
|
31
|
-
errors[0].to_s.should == "app/
|
|
31
|
+
errors[0].to_s.should == "app/controllers/invoices_controller.rb:3 - replace complex creation with factory method (@invoice attribute_assignment_count > 2)"
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
it "should not replace complex creation with factory method with simple creation" do
|
|
@@ -43,7 +43,7 @@ describe RailsBestPractices::Checks::ReplaceComplexCreationWithFactoryMethodChec
|
|
|
43
43
|
end
|
|
44
44
|
end
|
|
45
45
|
EOF
|
|
46
|
-
@runner.check('app/
|
|
46
|
+
@runner.check('app/controllers/invoices_controller.rb', content)
|
|
47
47
|
errors = @runner.errors
|
|
48
48
|
errors.should be_empty
|
|
49
49
|
end
|
|
@@ -69,8 +69,8 @@ describe RailsBestPractices::Checks::ReplaceComplexCreationWithFactoryMethodChec
|
|
|
69
69
|
end
|
|
70
70
|
EOF
|
|
71
71
|
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::ReplaceComplexCreationWithFactoryMethodCheck.new('attribute_assignment_count' => 5))
|
|
72
|
-
@runner.check('app/
|
|
72
|
+
@runner.check('app/controllers/invoices_controller.rb', content)
|
|
73
73
|
errors = @runner.errors
|
|
74
74
|
errors.should be_empty
|
|
75
75
|
end
|
|
76
|
-
end
|
|
76
|
+
end
|
data/spec/rails_best_practices/checks/replace_instance_variable_with_local_variable_check_spec.rb
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
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 not replace instance variable with local varialbe" do
|
|
19
|
+
content = <<-EOF
|
|
20
|
+
<%= post.title %>
|
|
21
|
+
EOF
|
|
22
|
+
@runner.check('app/views/posts/_post.html.erb', content)
|
|
23
|
+
errors = @runner.errors
|
|
24
|
+
errors.should be_empty
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__) + '/../../spec_helper')
|
|
2
|
+
|
|
3
|
+
describe RailsBestPractices::Checks::UseFilterCheck do
|
|
4
|
+
before(:each) do
|
|
5
|
+
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::UseFilterCheck.new)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "should use 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:1 - use filter for @post = current_user.posts.find(params[:id]) in show,edit,update,destroy"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it "should not use 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 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
|
|
@@ -16,10 +16,10 @@ describe RailsBestPractices::Checks::UseModelAssociationCheck do
|
|
|
16
16
|
end
|
|
17
17
|
end
|
|
18
18
|
EOF
|
|
19
|
-
@runner.check('app/
|
|
19
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
20
20
|
errors = @runner.errors
|
|
21
21
|
errors.should_not be_empty
|
|
22
|
-
errors[0].to_s.should == "app/
|
|
22
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:3 - use model association (for @post)"
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
it "should not use model association without association assign" do
|
|
@@ -32,7 +32,7 @@ describe RailsBestPractices::Checks::UseModelAssociationCheck do
|
|
|
32
32
|
end
|
|
33
33
|
end
|
|
34
34
|
EOF
|
|
35
|
-
@runner.check('app/
|
|
35
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
36
36
|
errors = @runner.errors
|
|
37
37
|
errors.should be_empty
|
|
38
38
|
end
|
|
@@ -48,10 +48,10 @@ describe RailsBestPractices::Checks::UseModelAssociationCheck do
|
|
|
48
48
|
end
|
|
49
49
|
end
|
|
50
50
|
EOF
|
|
51
|
-
@runner.check('app/
|
|
51
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
52
52
|
errors = @runner.errors
|
|
53
53
|
errors.should_not be_empty
|
|
54
|
-
errors[0].to_s.should == "app/
|
|
54
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:3 - use model association (for post)"
|
|
55
55
|
end
|
|
56
56
|
|
|
57
57
|
it "should not use model association" do
|
|
@@ -64,7 +64,7 @@ describe RailsBestPractices::Checks::UseModelAssociationCheck do
|
|
|
64
64
|
end
|
|
65
65
|
end
|
|
66
66
|
EOF
|
|
67
|
-
@runner.check('app/
|
|
67
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
68
68
|
errors = @runner.errors
|
|
69
69
|
errors.should be_empty
|
|
70
70
|
end
|
|
@@ -0,0 +1,67 @@
|
|
|
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
|
|
@@ -20,10 +20,10 @@ describe RailsBestPractices::Checks::UseScopeAccessCheck do
|
|
|
20
20
|
end
|
|
21
21
|
end
|
|
22
22
|
EOF
|
|
23
|
-
@runner.check('app/
|
|
23
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
24
24
|
errors = @runner.errors
|
|
25
25
|
errors.should_not be_empty
|
|
26
|
-
errors[0].to_s.should == "app/
|
|
26
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:7 - use scope access"
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
it "shoud use scope access by comparing with id" do
|
|
@@ -40,10 +40,10 @@ describe RailsBestPractices::Checks::UseScopeAccessCheck do
|
|
|
40
40
|
end
|
|
41
41
|
end
|
|
42
42
|
EOF
|
|
43
|
-
@runner.check('app/
|
|
43
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
44
44
|
errors = @runner.errors
|
|
45
45
|
errors.should_not be_empty
|
|
46
|
-
errors[0].to_s.should == "app/
|
|
46
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:7 - use scope access"
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
it "shoud use scope access with current_user ==" do
|
|
@@ -60,10 +60,10 @@ describe RailsBestPractices::Checks::UseScopeAccessCheck do
|
|
|
60
60
|
end
|
|
61
61
|
end
|
|
62
62
|
EOF
|
|
63
|
-
@runner.check('app/
|
|
63
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
64
64
|
errors = @runner.errors
|
|
65
65
|
errors.should_not be_empty
|
|
66
|
-
errors[0].to_s.should == "app/
|
|
66
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:7 - use scope access"
|
|
67
67
|
end
|
|
68
68
|
|
|
69
69
|
it "shoud use scope access by current_user.id ==" do
|
|
@@ -80,10 +80,10 @@ describe RailsBestPractices::Checks::UseScopeAccessCheck do
|
|
|
80
80
|
end
|
|
81
81
|
end
|
|
82
82
|
EOF
|
|
83
|
-
@runner.check('app/
|
|
83
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
84
84
|
errors = @runner.errors
|
|
85
85
|
errors.should_not be_empty
|
|
86
|
-
errors[0].to_s.should == "app/
|
|
86
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:7 - use scope access"
|
|
87
87
|
end
|
|
88
88
|
end
|
|
89
89
|
|
|
@@ -102,10 +102,10 @@ describe RailsBestPractices::Checks::UseScopeAccessCheck do
|
|
|
102
102
|
end
|
|
103
103
|
end
|
|
104
104
|
EOF
|
|
105
|
-
@runner.check('app/
|
|
105
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
106
106
|
errors = @runner.errors
|
|
107
107
|
errors.should_not be_empty
|
|
108
|
-
errors[0].to_s.should == "app/
|
|
108
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
|
109
109
|
end
|
|
110
110
|
|
|
111
111
|
it "shoud use scope access by comparing with id" do
|
|
@@ -122,10 +122,10 @@ describe RailsBestPractices::Checks::UseScopeAccessCheck do
|
|
|
122
122
|
end
|
|
123
123
|
end
|
|
124
124
|
EOF
|
|
125
|
-
@runner.check('app/
|
|
125
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
126
126
|
errors = @runner.errors
|
|
127
127
|
errors.should_not be_empty
|
|
128
|
-
errors[0].to_s.should == "app/
|
|
128
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
|
129
129
|
end
|
|
130
130
|
|
|
131
131
|
it "shoud use scope access with current_user ==" do
|
|
@@ -142,10 +142,10 @@ describe RailsBestPractices::Checks::UseScopeAccessCheck do
|
|
|
142
142
|
end
|
|
143
143
|
end
|
|
144
144
|
EOF
|
|
145
|
-
@runner.check('app/
|
|
145
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
146
146
|
errors = @runner.errors
|
|
147
147
|
errors.should_not be_empty
|
|
148
|
-
errors[0].to_s.should == "app/
|
|
148
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
|
149
149
|
end
|
|
150
150
|
|
|
151
151
|
it "shoud use scope access by current_user.id ==" do
|
|
@@ -162,10 +162,10 @@ describe RailsBestPractices::Checks::UseScopeAccessCheck do
|
|
|
162
162
|
end
|
|
163
163
|
end
|
|
164
164
|
EOF
|
|
165
|
-
@runner.check('app/
|
|
165
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
166
166
|
errors = @runner.errors
|
|
167
167
|
errors.should_not be_empty
|
|
168
|
-
errors[0].to_s.should == "app/
|
|
168
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
|
169
169
|
end
|
|
170
170
|
end
|
|
171
|
-
end
|
|
171
|
+
end
|