rails_best_practices 0.2.0 → 0.2.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.
data/README.textile CHANGED
@@ -41,6 +41,7 @@ MoveModelLogicIntoModelCheck: { called_count: 4 }
41
41
  OveruseRouteCustomizationsCheck: { customize_count: 3 }
42
42
  NeedlessDeepNestingCheck: { nested_count: 2 }
43
43
  NotUseDefaultRouteCheck: { }
44
+ KeepFindersOnTheirOwnModelCheck: { }
44
45
  </code></pre>
45
46
 
46
47
  *************************************************
@@ -65,7 +66,7 @@ h2. Progress
65
66
  ## [-Not use default route-]
66
67
 
67
68
  * Lesson 3. Model
68
- ## Keep Finders on Their Own Model
69
+ ## [-Keep Finders on Their Own Model-]
69
70
  ## Love named_scope
70
71
  ## the Law of Demeter
71
72
  ## DRY: metaprogramming
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -9,4 +9,5 @@ require 'rails_best_practices/checks/many_to_many_collection_check'
9
9
  require 'rails_best_practices/checks/nested_model_forms_check'
10
10
  require 'rails_best_practices/checks/overuse_route_customizations_check'
11
11
  require 'rails_best_practices/checks/needless_deep_nesting_check'
12
- require 'rails_best_practices/checks/not_use_default_route_check'
12
+ require 'rails_best_practices/checks/not_use_default_route_check'
13
+ require 'rails_best_practices/checks/keep_finders_on_their_own_model_check'
@@ -0,0 +1,26 @@
1
+ require 'rails_best_practices/checks/check'
2
+
3
+ module RailsBestPractices
4
+ module Checks
5
+ class KeepFindersOnTheirOwnModelCheck < Check
6
+
7
+ def interesting_nodes
8
+ [:call]
9
+ end
10
+
11
+ def interesting_files
12
+ /models\/.*rb/
13
+ end
14
+
15
+ def evaluate_start(node)
16
+ add_error "keep finders on their own model" if others_finder?(node)
17
+ end
18
+
19
+ private
20
+
21
+ def others_finder?(node)
22
+ node.message == :find and node.subject.node_type == :call
23
+ end
24
+ end
25
+ end
26
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rails_best_practices}
8
- s.version = "0.2.0"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Richard Huang"]
12
- s.date = %q{2009-11-06}
12
+ s.date = %q{2009-11-07}
13
13
  s.default_executable = %q{rails_best_practices}
14
14
  s.description = %q{check rails files according to ihower's presentation 'rails best practices'}
15
15
  s.email = %q{flyerhzm@gmail.com}
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  "lib/rails_best_practices/checks.rb",
30
30
  "lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb",
31
31
  "lib/rails_best_practices/checks/check.rb",
32
+ "lib/rails_best_practices/checks/keep_finders_on_their_own_model_check.rb",
32
33
  "lib/rails_best_practices/checks/many_to_many_collection_check.rb",
33
34
  "lib/rails_best_practices/checks/move_finder_to_named_scope_check.rb",
34
35
  "lib/rails_best_practices/checks/move_model_logic_into_model_check.rb",
@@ -50,6 +51,7 @@ Gem::Specification.new do |s|
50
51
  "rails_best_practices.gemspec",
51
52
  "rails_best_practices.yml",
52
53
  "spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb",
54
+ "spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb",
53
55
  "spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb",
54
56
  "spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb",
55
57
  "spec/rails_best_practices/checks/move_model_logic_into_model_check_spec.rb",
@@ -73,6 +75,7 @@ Gem::Specification.new do |s|
73
75
  "spec/rails_best_practices/checks/use_scope_access_check_spec.rb",
74
76
  "spec/rails_best_practices/checks/not_use_default_route_check_spec.rb",
75
77
  "spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb",
78
+ "spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb",
76
79
  "spec/rails_best_practices/checks/use_model_callback_check_spec.rb",
77
80
  "spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb",
78
81
  "spec/rails_best_practices/checks/use_model_association_check_spec.rb",
@@ -10,3 +10,4 @@ MoveModelLogicIntoModelCheck: { called_count: 4 }
10
10
  OveruseRouteCustomizationsCheck: { customize_count: 3 }
11
11
  NeedlessDeepNestingCheck: { nested_count: 2 }
12
12
  NotUseDefaultRouteCheck: { }
13
+ KeepFindersOnTheirOwnModelCheck: { }
@@ -17,10 +17,10 @@ describe RailsBestPractices::Checks::AddModelVirtualAttributeCheck do
17
17
  end
18
18
  end
19
19
  EOF
20
- @runner.check('app/controller/users_controller.rb', content)
20
+ @runner.check('app/controllers/users_controller.rb', content)
21
21
  errors = @runner.errors
22
22
  errors.should_not be_empty
23
- errors[0].to_s.should == "app/controller/users_controller.rb:3 - add model virtual attribute (for @user)"
23
+ errors[0].to_s.should == "app/controllers/users_controller.rb:3 - add model virtual attribute (for @user)"
24
24
  end
25
25
 
26
26
  it "should not add model virtual attribute with differen param" do
@@ -35,7 +35,7 @@ describe RailsBestPractices::Checks::AddModelVirtualAttributeCheck do
35
35
  end
36
36
  end
37
37
  EOF
38
- @runner.check('app/controller/users_controller.rb', content)
38
+ @runner.check('app/controllers/users_controller.rb', content)
39
39
  errors = @runner.errors
40
40
  errors.should be_empty
41
41
  end
@@ -53,7 +53,7 @@ describe RailsBestPractices::Checks::AddModelVirtualAttributeCheck do
53
53
  end
54
54
  end
55
55
  EOF
56
- @runner.check('app/controller/users_controller.rb', content)
56
+ @runner.check('app/controllers/users_controller.rb', content)
57
57
  errors = @runner.errors
58
58
  errors.should be_empty
59
59
  end
@@ -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
@@ -20,11 +20,11 @@ describe RailsBestPractices::Checks::MoveFinderToNamedScopeCheck do
20
20
  end
21
21
  end
22
22
  EOF
23
- @runner.check('app/controller/posts_controller.rb', content)
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/controller/posts_controller.rb:4 - move finder to named_scope"
27
- errors[1].to_s.should == "app/controller/posts_controller.rb:8 - move finder to named_scope"
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/controller/posts_controller.rb', content)
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/controller/posts_controller.rb', content)
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/controller/posts_controller.rb', content)
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/controller/posts_controller.rb:3 - move model logic into model (@post called_count > 4)"
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/controller/posts_controller.rb', content)
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
@@ -25,10 +25,10 @@ describe RailsBestPractices::Checks::ReplaceComplexCreationWithFactoryMethodChec
25
25
  end
26
26
  end
27
27
  EOF
28
- @runner.check('app/controller/invoices_controller.rb', content)
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/controller/invoices_controller.rb:3 - replace complex creation with factory method (@invoice attribute_assignment_count > 2)"
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/controller/invoices_controller.rb', content)
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/controller/invoices_controller.rb', content)
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
@@ -16,10 +16,10 @@ describe RailsBestPractices::Checks::UseModelAssociationCheck do
16
16
  end
17
17
  end
18
18
  EOF
19
- @runner.check('app/controller/posts_controller.rb', content)
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/controller/posts_controller.rb:3 - use model association (for @post)"
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/controller/posts_controller.rb', content)
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/controller/posts_controller.rb', content)
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/controller/posts_controller.rb:3 - use model association (for post)"
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/controller/posts_controller.rb', content)
67
+ @runner.check('app/controllers/posts_controller.rb', content)
68
68
  errors = @runner.errors
69
69
  errors.should be_empty
70
70
  end
@@ -20,10 +20,10 @@ describe RailsBestPractices::Checks::UseScopeAccessCheck do
20
20
  end
21
21
  end
22
22
  EOF
23
- @runner.check('app/controller/posts_controller.rb', content)
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/controller/posts_controller.rb:7 - use scope access"
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/controller/posts_controller.rb', content)
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/controller/posts_controller.rb:7 - use scope access"
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/controller/posts_controller.rb', content)
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/controller/posts_controller.rb:7 - use scope access"
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/controller/posts_controller.rb', content)
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/controller/posts_controller.rb:7 - use scope access"
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/controller/posts_controller.rb', content)
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/controller/posts_controller.rb:6 - use scope access"
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/controller/posts_controller.rb', content)
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/controller/posts_controller.rb:6 - use scope access"
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/controller/posts_controller.rb', content)
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/controller/posts_controller.rb:6 - use scope access"
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/controller/posts_controller.rb', content)
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/controller/posts_controller.rb:6 - use scope access"
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
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: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-06 00:00:00 +08:00
12
+ date: 2009-11-07 00:00:00 +08:00
13
13
  default_executable: rails_best_practices
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -52,6 +52,7 @@ files:
52
52
  - lib/rails_best_practices/checks.rb
53
53
  - lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb
54
54
  - lib/rails_best_practices/checks/check.rb
55
+ - lib/rails_best_practices/checks/keep_finders_on_their_own_model_check.rb
55
56
  - lib/rails_best_practices/checks/many_to_many_collection_check.rb
56
57
  - lib/rails_best_practices/checks/move_finder_to_named_scope_check.rb
57
58
  - lib/rails_best_practices/checks/move_model_logic_into_model_check.rb
@@ -73,6 +74,7 @@ files:
73
74
  - rails_best_practices.gemspec
74
75
  - rails_best_practices.yml
75
76
  - spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb
77
+ - spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb
76
78
  - spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb
77
79
  - spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb
78
80
  - spec/rails_best_practices/checks/move_model_logic_into_model_check_spec.rb
@@ -118,6 +120,7 @@ test_files:
118
120
  - spec/rails_best_practices/checks/use_scope_access_check_spec.rb
119
121
  - spec/rails_best_practices/checks/not_use_default_route_check_spec.rb
120
122
  - spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb
123
+ - spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb
121
124
  - spec/rails_best_practices/checks/use_model_callback_check_spec.rb
122
125
  - spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb
123
126
  - spec/rails_best_practices/checks/use_model_association_check_spec.rb