rails_best_practices 0.3.0 → 0.4.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 +72 -54
- data/Rakefile +5 -5
- data/VERSION +1 -1
- data/identifier +0 -0
- data/lib/rails_best_practices/checks/always_add_db_index_check.rb +27 -62
- data/lib/rails_best_practices/checks/check.rb +12 -13
- data/lib/rails_best_practices/checks/isolate_seed_data_check.rb +7 -20
- data/lib/rails_best_practices/checks/keep_finders_on_their_own_model_check.rb +1 -1
- data/lib/rails_best_practices/checks/law_of_demeter_check.rb +6 -5
- data/lib/rails_best_practices/checks/move_code_into_helper_check.rb +8 -3
- data/lib/rails_best_practices/checks/move_code_into_model_check.rb +53 -0
- data/lib/rails_best_practices/checks/needless_deep_nesting_check.rb +36 -4
- data/lib/rails_best_practices/checks/not_use_default_route_check.rb +3 -2
- data/lib/rails_best_practices/checks/overuse_route_customizations_check.rb +23 -10
- data/lib/rails_best_practices/checks/replace_instance_variable_with_local_variable_check.rb +1 -1
- data/lib/rails_best_practices/checks/{use_filter_check.rb → use_before_filter_check.rb} +9 -7
- data/lib/rails_best_practices/checks/use_scope_access_check.rb +1 -1
- data/lib/rails_best_practices/checks.rb +2 -4
- data/lib/rails_best_practices/command.rb +7 -41
- data/lib/rails_best_practices/core/checking_visitor.rb +2 -2
- data/lib/rails_best_practices/core/runner.rb +8 -6
- data/lib/rails_best_practices/core/visitable_sexp.rb +8 -2
- data/lib/rails_best_practices.rb +41 -1
- data/rails_best_practices.gemspec +25 -30
- data/rails_best_practices.yml +3 -5
- data/spec/rails_best_practices/checks/always_add_db_index_check_spec.rb +32 -112
- data/spec/rails_best_practices/checks/isolate_seed_data_check_spec.rb +0 -3
- data/spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb +33 -1
- data/spec/rails_best_practices/checks/law_of_demeter_check_spec.rb +66 -27
- data/spec/rails_best_practices/checks/move_code_into_helper_check_spec.rb +2 -2
- data/spec/rails_best_practices/checks/move_code_into_model_check_spec.rb +55 -0
- data/spec/rails_best_practices/checks/needless_deep_nesting_check_spec.rb +55 -21
- data/spec/rails_best_practices/checks/not_use_default_route_check_spec.rb +51 -22
- data/spec/rails_best_practices/checks/overuse_route_customizations_check_spec.rb +134 -48
- data/spec/rails_best_practices/checks/replace_instance_variable_with_local_variable_check_spec.rb +11 -1
- data/spec/rails_best_practices/checks/{use_filter_check_spec.rb → use_before_filter_check_spec.rb} +6 -6
- data/spec/rails_best_practices/checks/use_scope_access_check_spec.rb +22 -0
- metadata +49 -39
- data/lib/rails_best_practices/checks/many_to_many_collection_check.rb +0 -12
- data/lib/rails_best_practices/checks/nested_model_forms_check.rb +0 -12
- data/lib/rails_best_practices/checks/use_model_callback_check.rb +0 -12
- data/spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb +0 -11
- data/spec/rails_best_practices/checks/nested_model_forms_check_spec.rb +0 -11
- data/spec/rails_best_practices/checks/use_model_callback_check_spec.rb +0 -11
|
@@ -24,7 +24,6 @@ describe RailsBestPractices::Checks::IsolateSeedDataCheck do
|
|
|
24
24
|
end
|
|
25
25
|
EOF
|
|
26
26
|
@runner.check('db/migrate/20090818130258_create_roles.rb', content)
|
|
27
|
-
@runner.check('db/migrate/20090818130258_create_roles.rb', content)
|
|
28
27
|
errors = @runner.errors
|
|
29
28
|
errors.should_not be_empty
|
|
30
29
|
errors[0].to_s.should == "db/migrate/20090818130258_create_roles.rb:8 - isolate seed data"
|
|
@@ -51,7 +50,6 @@ describe RailsBestPractices::Checks::IsolateSeedDataCheck do
|
|
|
51
50
|
end
|
|
52
51
|
EOF
|
|
53
52
|
@runner.check('db/migrate/20090818130258_create_roles.rb', content)
|
|
54
|
-
@runner.check('db/migrate/20090818130258_create_roles.rb', content)
|
|
55
53
|
errors = @runner.errors
|
|
56
54
|
errors.should_not be_empty
|
|
57
55
|
errors[0].to_s.should == "db/migrate/20090818130258_create_roles.rb:9 - isolate seed data"
|
|
@@ -72,7 +70,6 @@ describe RailsBestPractices::Checks::IsolateSeedDataCheck do
|
|
|
72
70
|
end
|
|
73
71
|
EOF
|
|
74
72
|
@runner.check('db/migrate/20090818130258_create_roles.rb', content)
|
|
75
|
-
@runner.check('db/migrate/20090818130258_create_roles.rb', content)
|
|
76
73
|
errors = @runner.errors
|
|
77
74
|
errors.should be_empty
|
|
78
75
|
end
|
|
@@ -22,6 +22,23 @@ describe RailsBestPractices::Checks::KeepFindersOnTheirOwnModelCheck do
|
|
|
22
22
|
errors[0].to_s.should == "app/models/post.rb:5 - keep finders on their own model"
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
+
it "should keep finders on thier own model with all method" do
|
|
26
|
+
content = <<-EOF
|
|
27
|
+
class Post < ActiveRecord::Base
|
|
28
|
+
has_many :comments
|
|
29
|
+
|
|
30
|
+
def find_valid_comments
|
|
31
|
+
self.comment.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_not be_empty
|
|
39
|
+
errors[0].to_s.should == "app/models/post.rb:5 - keep finders on their own model"
|
|
40
|
+
end
|
|
41
|
+
|
|
25
42
|
it "should not keep finders on thier own model with self finder" do
|
|
26
43
|
content = <<-EOF
|
|
27
44
|
class Post < ActiveRecord::Base
|
|
@@ -54,7 +71,7 @@ describe RailsBestPractices::Checks::KeepFindersOnTheirOwnModelCheck do
|
|
|
54
71
|
errors.should be_empty
|
|
55
72
|
end
|
|
56
73
|
|
|
57
|
-
it "should not keep finders on
|
|
74
|
+
it "should not keep finders on their own model without finder" do
|
|
58
75
|
content = <<-EOF
|
|
59
76
|
class Post < ActiveRecord::Base
|
|
60
77
|
has_many :comments
|
|
@@ -68,4 +85,19 @@ describe RailsBestPractices::Checks::KeepFindersOnTheirOwnModelCheck do
|
|
|
68
85
|
errors = @runner.errors
|
|
69
86
|
errors.should be_empty
|
|
70
87
|
end
|
|
88
|
+
|
|
89
|
+
it "should not keep finders on their own model with ruby Array#find" do
|
|
90
|
+
content = <<-EOF
|
|
91
|
+
class Post < ActiveRecord::Base
|
|
92
|
+
has_many :comments
|
|
93
|
+
|
|
94
|
+
def active_comments
|
|
95
|
+
self.comments.find {|comment| comment.status == 'active'}
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
EOF
|
|
99
|
+
@runner.check('app/models/post.rb', content)
|
|
100
|
+
errors = @runner.errors
|
|
101
|
+
errors.should be_empty
|
|
102
|
+
end
|
|
71
103
|
end
|
|
@@ -1,37 +1,76 @@
|
|
|
1
1
|
require File.join(File.dirname(__FILE__) + '/../../spec_helper')
|
|
2
2
|
|
|
3
3
|
describe RailsBestPractices::Checks::LawOfDemeterCheck do
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
|
|
5
|
+
describe "belongs_to" do
|
|
6
|
+
before(:each) do
|
|
7
|
+
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::LawOfDemeterCheck.new)
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
content = <<-EOF
|
|
10
|
+
class Invoice < ActiveRecord::Base
|
|
11
|
+
belongs_to :user
|
|
12
|
+
end
|
|
13
|
+
EOF
|
|
14
|
+
@runner.check('app/models/invoice.rb', content)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "should law of demeter" do
|
|
18
|
+
content = <<-EOF
|
|
19
|
+
<%= @invoice.user.name %>
|
|
20
|
+
<%= @invoice.user.address %>
|
|
21
|
+
<%= @invoice.user.cellphone %>
|
|
22
|
+
EOF
|
|
23
|
+
@runner.check('app/views/invoices/show.html.erb', content)
|
|
24
|
+
errors = @runner.errors
|
|
25
|
+
errors.should_not be_empty
|
|
26
|
+
errors[0].to_s.should == "app/views/invoices/show.html.erb:1 - law of demeter"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "should law of demeter" do
|
|
30
|
+
content = <<-EOF
|
|
31
|
+
= @invoice.user.name
|
|
32
|
+
= @invoice.user.address
|
|
33
|
+
= @invoice.user.cellphone
|
|
34
|
+
EOF
|
|
35
|
+
@runner.check('app/views/invoices/show.html.haml', content)
|
|
36
|
+
errors = @runner.errors
|
|
37
|
+
errors.should_not be_empty
|
|
38
|
+
errors[0].to_s.should == "app/views/invoices/show.html.haml:1 - law of demeter"
|
|
10
39
|
end
|
|
11
|
-
EOF
|
|
12
|
-
@runner.check('app/models/invoice.rb', content)
|
|
13
|
-
end
|
|
14
40
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
41
|
+
it "should no law of demeter" do
|
|
42
|
+
content = <<-EOF
|
|
43
|
+
<%= @invoice.user_name %>
|
|
44
|
+
<%= @invoice.user_address %>
|
|
45
|
+
<%= @invoice.user_cellphone %>
|
|
46
|
+
EOF
|
|
47
|
+
@runner.check('app/views/invoices/show.html.erb', content)
|
|
48
|
+
errors = @runner.errors
|
|
49
|
+
errors.should be_empty
|
|
50
|
+
end
|
|
25
51
|
end
|
|
26
52
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
53
|
+
describe "has_one" do
|
|
54
|
+
before(:each) do
|
|
55
|
+
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::LawOfDemeterCheck.new)
|
|
56
|
+
|
|
57
|
+
content = <<-EOF
|
|
58
|
+
class Invoice < ActiveRecord::Base
|
|
59
|
+
has_one :price
|
|
60
|
+
end
|
|
61
|
+
EOF
|
|
62
|
+
@runner.check('app/models/invoice.rb', content)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it "should law of demeter" do
|
|
66
|
+
content = <<-EOF
|
|
67
|
+
<%= @invoice.price.currency %>
|
|
68
|
+
<%= @invoice.price.number %>
|
|
69
|
+
EOF
|
|
70
|
+
@runner.check('app/views/invoices/show.html.erb', content)
|
|
71
|
+
errors = @runner.errors
|
|
72
|
+
errors.should_not be_empty
|
|
73
|
+
errors[0].to_s.should == "app/views/invoices/show.html.erb:1 - law of demeter"
|
|
74
|
+
end
|
|
36
75
|
end
|
|
37
76
|
end
|
|
@@ -2,7 +2,7 @@ require File.join(File.dirname(__FILE__) + '/../../spec_helper')
|
|
|
2
2
|
|
|
3
3
|
describe RailsBestPractices::Checks::MoveCodeIntoHelperCheck do
|
|
4
4
|
before(:each) do
|
|
5
|
-
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::MoveCodeIntoHelperCheck.new)
|
|
5
|
+
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::MoveCodeIntoHelperCheck.new('array_count' => 2))
|
|
6
6
|
end
|
|
7
7
|
|
|
8
8
|
it "should move code into helper" do
|
|
@@ -15,7 +15,7 @@ describe RailsBestPractices::Checks::MoveCodeIntoHelperCheck do
|
|
|
15
15
|
@runner.check('app/views/posts/show.html.erb', content)
|
|
16
16
|
errors = @runner.errors
|
|
17
17
|
errors.should_not be_empty
|
|
18
|
-
errors[0].to_s.should == "app/views/posts/show.html.erb:3 - move code into helper"
|
|
18
|
+
errors[0].to_s.should == "app/views/posts/show.html.erb:3 - move code into helper (array_count >= 2)"
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
it "should not move code into helper with simple arguments" do
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__) + '/../../spec_helper')
|
|
2
|
+
|
|
3
|
+
describe RailsBestPractices::Checks::MoveCodeIntoModelCheck do
|
|
4
|
+
before(:each) do
|
|
5
|
+
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::MoveCodeIntoModelCheck.new)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "should move code into model" do
|
|
9
|
+
content =<<-EOF
|
|
10
|
+
<% if current_user && (current_user == @post.user || @post.editors.include?(current_user)) %>
|
|
11
|
+
<%= link_to 'Edit this post', edit_post_url(@post) %>
|
|
12
|
+
<% end %>
|
|
13
|
+
EOF
|
|
14
|
+
@runner.check('app/views/posts/show.html.erb', content)
|
|
15
|
+
errors = @runner.errors
|
|
16
|
+
errors.should_not be_empty
|
|
17
|
+
errors[0].to_s.should == "app/views/posts/show.html.erb:1 - move code into model (@post)"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "should move code into model with haml" do
|
|
21
|
+
content =<<-EOF
|
|
22
|
+
- if current_user && (current_user == @post.user || @post.editors.include?(current_user))
|
|
23
|
+
= link_to 'Edit this post', edit_post_url(@post)
|
|
24
|
+
EOF
|
|
25
|
+
@runner.check('app/views/posts/show.html.haml', content)
|
|
26
|
+
errors = @runner.errors
|
|
27
|
+
errors.should_not be_empty
|
|
28
|
+
errors[0].to_s.should == "app/views/posts/show.html.haml:1 - move code into model (@post)"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "should move code into model only check for current if conditional statement" do
|
|
32
|
+
content =<<-EOF
|
|
33
|
+
<% if @post.title %>
|
|
34
|
+
<% if @post.user %>
|
|
35
|
+
<% if @post.description %>
|
|
36
|
+
<% end %>
|
|
37
|
+
<% end %>
|
|
38
|
+
<% end %>
|
|
39
|
+
EOF
|
|
40
|
+
@runner.check('app/views/posts/show.html.erb', content)
|
|
41
|
+
errors = @runner.errors
|
|
42
|
+
errors.should be_empty
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it "should not move code into model" do
|
|
46
|
+
content =<<-EOF
|
|
47
|
+
<% if @post.editable_by?(current_user) %>
|
|
48
|
+
<%= link_to 'Edit this post', edit_post_url(@post) %>
|
|
49
|
+
<% end %>
|
|
50
|
+
EOF
|
|
51
|
+
@runner.check('app/views/posts/show.html.erb', content)
|
|
52
|
+
errors = @runner.errors
|
|
53
|
+
errors.should be_empty
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -5,32 +5,66 @@ describe RailsBestPractices::Checks::NeedlessDeepNestingCheck do
|
|
|
5
5
|
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::NeedlessDeepNestingCheck.new)
|
|
6
6
|
end
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
describe "rails2" do
|
|
9
|
+
it "should needless deep nesting" do
|
|
10
|
+
content = <<-EOF
|
|
11
|
+
map.resources :posts do |post|
|
|
12
|
+
post.resources :comments do |comment|
|
|
13
|
+
comment.resources :favorites
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
EOF
|
|
17
|
+
@runner.check('config/routes.rb', content)
|
|
18
|
+
errors = @runner.errors
|
|
19
|
+
errors.should_not be_empty
|
|
20
|
+
errors[0].to_s.should == "config/routes.rb:3 - needless deep nesting (nested_count > 2)"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "should no needless deep nesting" do
|
|
24
|
+
content = <<-EOF
|
|
25
|
+
map.resources :posts do |post|
|
|
26
|
+
post.resources :comments
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
map.resources :comments do |comment|
|
|
12
30
|
comment.resources :favorites
|
|
13
31
|
end
|
|
32
|
+
EOF
|
|
33
|
+
@runner.check('config/routes.rb', content)
|
|
34
|
+
errors = @runner.errors
|
|
35
|
+
errors.should be_empty
|
|
14
36
|
end
|
|
15
|
-
EOF
|
|
16
|
-
@runner.check('config/routes.rb', content)
|
|
17
|
-
errors = @runner.errors
|
|
18
|
-
errors.should_not be_empty
|
|
19
|
-
errors[0].to_s.should == "config/routes.rb:3 - needless deep nesting (nested_count > 2)"
|
|
20
37
|
end
|
|
21
38
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
39
|
+
describe "rails3" do
|
|
40
|
+
it "should needless deep nesting" do
|
|
41
|
+
content = <<-EOF
|
|
42
|
+
resources :posts do
|
|
43
|
+
resources :comments do
|
|
44
|
+
resources :favorites
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
EOF
|
|
48
|
+
@runner.check('config/routes.rb', content)
|
|
49
|
+
errors = @runner.errors
|
|
50
|
+
errors.should_not be_empty
|
|
51
|
+
errors[0].to_s.should == "config/routes.rb:4 - needless deep nesting (nested_count > 2)"
|
|
26
52
|
end
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
53
|
+
|
|
54
|
+
it "should no needless deep nesting" do
|
|
55
|
+
content = <<-EOF
|
|
56
|
+
resources :posts do
|
|
57
|
+
resources :comments
|
|
58
|
+
resources :votes
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
resources :comments do
|
|
62
|
+
resources :favorites
|
|
63
|
+
end
|
|
64
|
+
EOF
|
|
65
|
+
@runner.check('config/routes.rb', content)
|
|
66
|
+
errors = @runner.errors
|
|
67
|
+
errors.should be_empty
|
|
30
68
|
end
|
|
31
|
-
EOF
|
|
32
|
-
@runner.check('config/routes.rb', content)
|
|
33
|
-
errors = @runner.errors
|
|
34
|
-
errors.should be_empty
|
|
35
69
|
end
|
|
36
|
-
end
|
|
70
|
+
end
|
|
@@ -5,30 +5,59 @@ describe RailsBestPractices::Checks::NotUseDefaultRouteCheck do
|
|
|
5
5
|
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::NotUseDefaultRouteCheck.new)
|
|
6
6
|
end
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
describe "rails2" do
|
|
9
|
+
it "should not use default route" do
|
|
10
|
+
content = <<-EOF
|
|
11
|
+
ActionController::Routing::Routes.draw do |map|
|
|
12
|
+
map.resources :posts, :member => { :push => :post }
|
|
13
|
+
|
|
14
|
+
map.connect ':controller/:action/:id'
|
|
15
|
+
map.connect ':controller/:action/:id.:format'
|
|
16
|
+
end
|
|
17
|
+
EOF
|
|
18
|
+
@runner.check('config/routes.rb', content)
|
|
19
|
+
errors = @runner.errors
|
|
20
|
+
errors.should_not be_empty
|
|
21
|
+
errors[0].to_s.should == "config/routes.rb:4 - not use default route"
|
|
22
|
+
errors[1].to_s.should == "config/routes.rb:5 - not use default route"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "should no not use default route" do
|
|
26
|
+
content = <<-EOF
|
|
27
|
+
ActionController::Routing::Routes.draw do |map|
|
|
28
|
+
map.resources :posts, :member => { :push => :post }
|
|
29
|
+
end
|
|
30
|
+
EOF
|
|
31
|
+
@runner.check('config/routes.rb', content)
|
|
32
|
+
errors = @runner.errors
|
|
33
|
+
errors.should be_empty
|
|
15
34
|
end
|
|
16
|
-
EOF
|
|
17
|
-
@runner.check('config/routes.rb', content)
|
|
18
|
-
errors = @runner.errors
|
|
19
|
-
errors.should_not be_empty
|
|
20
|
-
errors[0].to_s.should == "config/routes.rb:4 - not use default route"
|
|
21
|
-
errors[1].to_s.should == "config/routes.rb:5 - not use default route"
|
|
22
35
|
end
|
|
23
36
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
37
|
+
describe "rails3" do
|
|
38
|
+
it "should not use default route" do
|
|
39
|
+
content = <<-EOF
|
|
40
|
+
RailsBestpracticesCom::Application.routes.draw do |map|
|
|
41
|
+
resources :posts
|
|
42
|
+
|
|
43
|
+
match ':controller(/:action(/:id(.:format)))'
|
|
44
|
+
end
|
|
45
|
+
EOF
|
|
46
|
+
@runner.check('config/routes.rb', content)
|
|
47
|
+
errors = @runner.errors
|
|
48
|
+
errors.should_not be_empty
|
|
49
|
+
errors[0].to_s.should == "config/routes.rb:5 - not use default route"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "should no not use default route" do
|
|
53
|
+
content = <<-EOF
|
|
54
|
+
RailsBestpracticesCom::Application.routes.draw do |map|
|
|
55
|
+
resources :posts
|
|
56
|
+
end
|
|
57
|
+
EOF
|
|
58
|
+
@runner.check('config/routes.rb', content)
|
|
59
|
+
errors = @runner.errors
|
|
60
|
+
errors.should be_empty
|
|
28
61
|
end
|
|
29
|
-
EOF
|
|
30
|
-
@runner.check('config/routes.rb', content)
|
|
31
|
-
errors = @runner.errors
|
|
32
|
-
errors.should be_empty
|
|
33
62
|
end
|
|
34
|
-
end
|
|
63
|
+
end
|
|
@@ -4,56 +4,142 @@ describe RailsBestPractices::Checks::OveruseRouteCustomizationsCheck do
|
|
|
4
4
|
before(:each) do
|
|
5
5
|
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::OveruseRouteCustomizationsCheck.new)
|
|
6
6
|
end
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
7
|
+
|
|
8
|
+
describe "rails2" do
|
|
9
|
+
it "should overuse route customizations" do
|
|
10
|
+
content = <<-EOF
|
|
11
|
+
ActionController::Routing::Routes.draw do |map|
|
|
12
|
+
map.resources :posts, :member => { :comments => :get,
|
|
13
|
+
:create_comment => :post,
|
|
14
|
+
:update_comment => :post,
|
|
15
|
+
:delete_comment => :post }
|
|
16
|
+
end
|
|
17
|
+
EOF
|
|
18
|
+
@runner.check('config/routes.rb', content)
|
|
19
|
+
errors = @runner.errors
|
|
20
|
+
errors.should_not be_empty
|
|
21
|
+
errors[0].to_s.should == "config/routes.rb:2 - overuse route customizations (customize_count > 3)"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it "should overuse route customizations with collection" do
|
|
25
|
+
content = <<-EOF
|
|
26
|
+
ActionController::Routing::Routes.draw do |map|
|
|
27
|
+
map.resources :posts, :member => { :create_comment => :post,
|
|
28
|
+
:update_comment => :post,
|
|
29
|
+
:delete_comment => :post },
|
|
30
|
+
:collection => { :comments => :get }
|
|
31
|
+
end
|
|
32
|
+
EOF
|
|
33
|
+
@runner.check('config/routes.rb', content)
|
|
34
|
+
errors = @runner.errors
|
|
35
|
+
errors.should_not be_empty
|
|
36
|
+
errors[0].to_s.should == "config/routes.rb:2 - overuse route customizations (customize_count > 3)"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it "should overuse route customizations with collection 2" do
|
|
40
|
+
content = <<-EOF
|
|
41
|
+
ActionController::Routing::Routes.draw do |map|
|
|
42
|
+
map.resources :categories do |category|
|
|
43
|
+
category.resources :posts, :member => { :create_comment => :post,
|
|
44
|
+
:update_comment => :post,
|
|
45
|
+
:delete_comment => :post },
|
|
46
|
+
:collection => { :comments => :get }
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
EOF
|
|
50
|
+
@runner.check('config/routes.rb', content)
|
|
51
|
+
errors = @runner.errors
|
|
52
|
+
errors.should_not be_empty
|
|
53
|
+
errors[0].to_s.should == "config/routes.rb:3 - overuse route customizations (customize_count > 3)"
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it "should not overuse route customizations without customization" do
|
|
57
|
+
content = <<-EOF
|
|
58
|
+
ActionController::Routing::Routes.draw do |map|
|
|
59
|
+
map.resources :posts
|
|
60
|
+
end
|
|
61
|
+
EOF
|
|
62
|
+
@runner.check('config/routes.rb', content)
|
|
63
|
+
errors = @runner.errors
|
|
64
|
+
errors.should be_empty
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it "should not overuse route customizations when customize route is only one" do
|
|
68
|
+
content = <<-EOF
|
|
69
|
+
ActionController::Routing::Routes.draw do |map|
|
|
70
|
+
map.resources :posts, :member => { :vote => :post }
|
|
71
|
+
end
|
|
72
|
+
EOF
|
|
73
|
+
@runner.check('config/routes.rb', content)
|
|
74
|
+
errors = @runner.errors
|
|
75
|
+
errors.should be_empty
|
|
76
|
+
end
|
|
47
77
|
end
|
|
48
78
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
79
|
+
describe "rails3" do
|
|
80
|
+
it "should overuse route customizations" do
|
|
81
|
+
content = <<-EOF
|
|
82
|
+
RailsBestpracticesCom::Application.routes.draw do |map|
|
|
83
|
+
resources :posts do
|
|
84
|
+
member do
|
|
85
|
+
post :create_comment
|
|
86
|
+
post :update_comment
|
|
87
|
+
post :delete_comment
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
collection do
|
|
91
|
+
get :comments
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
EOF
|
|
96
|
+
@runner.check('config/routes.rb', content)
|
|
97
|
+
errors = @runner.errors
|
|
98
|
+
errors.should_not be_empty
|
|
99
|
+
errors[0].to_s.should == "config/routes.rb:2 - overuse route customizations (customize_count > 3)"
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
it "should overuse route customizations another way" do
|
|
103
|
+
content = <<-EOF
|
|
104
|
+
RailsBestpracticesCom::Application.routes.draw do |map|
|
|
105
|
+
resources :posts do
|
|
106
|
+
post :create_comment, :on => :member
|
|
107
|
+
post :update_comment, :on => :member
|
|
108
|
+
post :delete_comment, :on => :member
|
|
109
|
+
get :comments, :on => :collection
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
EOF
|
|
113
|
+
@runner.check('config/routes.rb', content)
|
|
114
|
+
errors = @runner.errors
|
|
115
|
+
errors.should_not be_empty
|
|
116
|
+
errors[0].to_s.should == "config/routes.rb:2 - overuse route customizations (customize_count > 3)"
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
it "should not overuse route customizations without customization" do
|
|
120
|
+
content = <<-EOF
|
|
121
|
+
RailsBestpracticesCom::Application.routes.draw do |map|
|
|
122
|
+
resources :posts
|
|
123
|
+
end
|
|
124
|
+
EOF
|
|
125
|
+
@runner.check('config/routes.rb', content)
|
|
126
|
+
errors = @runner.errors
|
|
127
|
+
errors.should be_empty
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
it "should not overuse route customizations when customize route is only one" do
|
|
131
|
+
content = <<-EOF
|
|
132
|
+
RailsBestpracticesCom::Application.routes.draw do |map|
|
|
133
|
+
resources :posts do
|
|
134
|
+
member do
|
|
135
|
+
post :vote
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
EOF
|
|
140
|
+
@runner.check('config/routes.rb', content)
|
|
141
|
+
errors = @runner.errors
|
|
142
|
+
errors.should be_empty
|
|
53
143
|
end
|
|
54
|
-
EOF
|
|
55
|
-
@runner.check('config/routes.rb', content)
|
|
56
|
-
errors = @runner.errors
|
|
57
|
-
errors.should be_empty
|
|
58
144
|
end
|
|
59
145
|
end
|
data/spec/rails_best_practices/checks/replace_instance_variable_with_local_variable_check_spec.rb
CHANGED
|
@@ -15,6 +15,16 @@ describe RailsBestPractices::Checks::ReplaceInstanceVariableWithLocalVariableChe
|
|
|
15
15
|
errors[0].to_s.should == "app/views/posts/_post.html.erb:1 - replace instance variable with local variable"
|
|
16
16
|
end
|
|
17
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
|
+
|
|
18
28
|
it "should not replace instance variable with local varialbe" do
|
|
19
29
|
content = <<-EOF
|
|
20
30
|
<%= post.title %>
|
|
@@ -23,4 +33,4 @@ describe RailsBestPractices::Checks::ReplaceInstanceVariableWithLocalVariableChe
|
|
|
23
33
|
errors = @runner.errors
|
|
24
34
|
errors.should be_empty
|
|
25
35
|
end
|
|
26
|
-
end
|
|
36
|
+
end
|