rails_best_practices 0.4.0 → 0.4.1

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.
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,29 +0,0 @@
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('array_count' => 2))
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 (array_count >= 2)"
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
@@ -1,55 +0,0 @@
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
@@ -1,82 +0,0 @@
1
- require File.join(File.dirname(__FILE__) + '/../../spec_helper')
2
-
3
- describe RailsBestPractices::Checks::MoveFinderToNamedScopeCheck do
4
- before(:each) do
5
- @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::MoveFinderToNamedScopeCheck.new)
6
- end
7
-
8
- it "should move finder to named_scope" do
9
- content = <<-EOF
10
- class PostsController < ActionController::Base
11
-
12
- def index
13
- @public_posts = Post.find(:all, :conditions => { :state => 'public' },
14
- :limit => 10,
15
- :order => 'created_at desc')
16
-
17
- @draft_posts = Post.find(:all, :conditions => { :state => 'draft' },
18
- :limit => 10,
19
- :order => 'created_at desc')
20
- end
21
- end
22
- EOF
23
- @runner.check('app/controllers/posts_controller.rb', content)
24
- errors = @runner.errors
25
- errors.size.should == 2
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
- end
29
-
30
- it "should not move simple finder" do
31
- content = <<-EOF
32
- class PostsController < ActionController::Base
33
-
34
- def index
35
- @all_posts = Post.find(:all)
36
- @another_all_posts = Post.all
37
- @first_post = Post.find(:first)
38
- @another_first_post = Post.first
39
- @last_post = Post.find(:last)
40
- @another_last_post = Post.last
41
- end
42
- end
43
- EOF
44
- @runner.check('app/controllers/posts_controller.rb', content)
45
- @runner.errors.should be_empty
46
- end
47
-
48
- it "should not move namd_scope" do
49
- content = <<-EOF
50
- class PostsController < ActionController::Base
51
-
52
- def index
53
- @public_posts = Post.published
54
- @draft_posts = Post.draft
55
- end
56
- end
57
- EOF
58
- @runner.check('app/controllers/posts_controller.rb', content)
59
- @runner.errors.should be_empty
60
- end
61
-
62
- it "should not check model file" do
63
- content = <<-EOF
64
- class Post < ActiveRecord::Base
65
-
66
- def published
67
- Post.find(:all, :conditions => { :state => 'public' },
68
- :limit => 10, :order => 'created_at desc')
69
- end
70
-
71
- def published
72
- Post.find(:all, :conditions => { :state => 'draft' },
73
- :limit => 10, :order => 'created_at desc')
74
- end
75
-
76
- end
77
- EOF
78
- @runner.check('app/model/post.rb', content)
79
- @runner.errors.should be_empty
80
-
81
- end
82
- end
@@ -1,49 +0,0 @@
1
- require File.join(File.dirname(__FILE__) + '/../../spec_helper')
2
-
3
- describe RailsBestPractices::Checks::MoveModelLogicIntoModelCheck do
4
- before(:each) do
5
- @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::MoveModelLogicIntoModelCheck.new)
6
- end
7
-
8
- it "should move model logic into model" do
9
- content = <<-EOF
10
- class PostsController < ApplicationController
11
-
12
- def publish
13
- @post = Post.find(params[:id])
14
- @post.update_attributes(:is_published, true)
15
- @post.approved_by = current_user
16
- if @post.created_at > Time.now - 7.days
17
- @post.popular = 100
18
- else
19
- @post.popular = 0
20
- end
21
- end
22
-
23
- redirect_to post_url(@post)
24
- end
25
- EOF
26
- @runner.check('app/controllers/posts_controller.rb', content)
27
- errors = @runner.errors
28
- errors.should_not be_empty
29
- errors[0].to_s.should == "app/controllers/posts_controller.rb:3 - move model logic into model (@post called_count > 4)"
30
- end
31
-
32
- it "should not move model logic into model with simple model calling" do
33
- content = <<-EOF
34
- class PostsController < ApplicationController
35
-
36
- def publish
37
- @post = Post.find(params[:id])
38
- @post.update_attributes(:is_published, true)
39
- @post.approved_by = current_user
40
- end
41
-
42
- redirect_to post_url(@post)
43
- end
44
- EOF
45
- @runner.check('app/controllers/posts_controller.rb', content)
46
- errors = @runner.errors
47
- errors.should be_empty
48
- end
49
- end
@@ -1,70 +0,0 @@
1
- require File.join(File.dirname(__FILE__) + '/../../spec_helper')
2
-
3
- describe RailsBestPractices::Checks::NeedlessDeepNestingCheck do
4
- before(:each) do
5
- @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::NeedlessDeepNestingCheck.new)
6
- end
7
-
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|
30
- comment.resources :favorites
31
- end
32
- EOF
33
- @runner.check('config/routes.rb', content)
34
- errors = @runner.errors
35
- errors.should be_empty
36
- end
37
- end
38
-
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)"
52
- end
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
68
- end
69
- end
70
- end
@@ -1,63 +0,0 @@
1
- require File.join(File.dirname(__FILE__) + '/../../spec_helper')
2
-
3
- describe RailsBestPractices::Checks::NotUseDefaultRouteCheck do
4
- before(:each) do
5
- @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::NotUseDefaultRouteCheck.new)
6
- end
7
-
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
34
- end
35
- end
36
-
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
61
- end
62
- end
63
- end
@@ -1,145 +0,0 @@
1
- require File.join(File.dirname(__FILE__) + '/../../spec_helper')
2
-
3
- describe RailsBestPractices::Checks::OveruseRouteCustomizationsCheck do
4
- before(:each) do
5
- @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::OveruseRouteCustomizationsCheck.new)
6
- end
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
77
- end
78
-
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
143
- end
144
- end
145
- end