rails_best_practices 0.6.6 → 0.6.7
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +0 -1
- data/README.md +28 -24
- data/Rakefile +0 -8
- data/install_supported_rubies.sh +2 -3
- data/lib/rails_best_practices.rb +8 -7
- data/lib/rails_best_practices/core.rb +1 -0
- data/lib/rails_best_practices/core/check.rb +68 -0
- data/lib/rails_best_practices/core/checking_visitor.rb +18 -15
- data/lib/rails_best_practices/core/runner.rb +22 -12
- data/lib/rails_best_practices/core/visitable_sexp.rb +6 -2
- data/lib/rails_best_practices/prepares.rb +11 -0
- data/lib/rails_best_practices/prepares/mailer_prepare.rb +33 -0
- data/lib/rails_best_practices/prepares/model_prepare.rb +60 -0
- data/lib/rails_best_practices/reviews.rb +23 -0
- data/lib/rails_best_practices/{checks/add_model_virtual_attribute_check.rb → reviews/add_model_virtual_attribute_review.rb} +6 -9
- data/lib/rails_best_practices/{checks/always_add_db_index_check.rb → reviews/always_add_db_index_review.rb} +9 -12
- data/lib/rails_best_practices/{checks/dry_bundler_in_capistrano_check.rb → reviews/dry_bundler_in_capistrano_review.rb} +8 -11
- data/lib/rails_best_practices/{checks/isolate_seed_data_check.rb → reviews/isolate_seed_data_review.rb} +11 -14
- data/lib/rails_best_practices/{checks/keep_finders_on_their_own_model_check.rb → reviews/keep_finders_on_their_own_model_review.rb} +7 -10
- data/lib/rails_best_practices/{checks/law_of_demeter_check.rb → reviews/law_of_demeter_review.rb} +10 -14
- data/lib/rails_best_practices/{checks/move_code_into_controller_check.rb → reviews/move_code_into_controller_review.rb} +8 -11
- data/lib/rails_best_practices/{checks/move_code_into_helper_check.rb → reviews/move_code_into_helper_review.rb} +7 -10
- data/lib/rails_best_practices/{checks/move_code_into_model_check.rb → reviews/move_code_into_model_review.rb} +7 -10
- data/lib/rails_best_practices/{checks/move_finder_to_named_scope_check.rb → reviews/move_finder_to_named_scope_review.rb} +7 -10
- data/lib/rails_best_practices/{checks/move_model_logic_into_model_check.rb → reviews/move_model_logic_into_model_review.rb} +8 -11
- data/lib/rails_best_practices/{checks/needless_deep_nesting_check.rb → reviews/needless_deep_nesting_review.rb} +8 -11
- data/lib/rails_best_practices/{checks/not_use_default_route_check.rb → reviews/not_use_default_route_review.rb} +7 -10
- data/lib/rails_best_practices/{checks/overuse_route_customizations_check.rb → reviews/overuse_route_customizations_review.rb} +10 -13
- data/lib/rails_best_practices/{checks/replace_complex_creation_with_factory_method_check.rb → reviews/replace_complex_creation_with_factory_method_review.rb} +8 -11
- data/lib/rails_best_practices/{checks/replace_instance_variable_with_local_variable_check.rb → reviews/replace_instance_variable_with_local_variable_review.rb} +7 -10
- data/lib/rails_best_practices/reviews/review.rb +92 -0
- data/lib/rails_best_practices/{checks/use_before_filter_check.rb → reviews/use_before_filter_review.rb} +8 -11
- data/lib/rails_best_practices/{checks/use_model_association_check.rb → reviews/use_model_association_review.rb} +8 -11
- data/lib/rails_best_practices/{checks/use_observer_check.rb → reviews/use_observer_review.rb} +14 -41
- data/lib/rails_best_practices/{checks/use_query_attribute_check.rb → reviews/use_query_attribute_review.rb} +20 -16
- data/lib/rails_best_practices/{checks/use_say_with_time_in_migrations_check.rb → reviews/use_say_with_time_in_migrations_review.rb} +8 -11
- data/lib/rails_best_practices/{checks/use_scope_access_check.rb → reviews/use_scope_access_review.rb} +8 -11
- data/lib/rails_best_practices/version.rb +1 -1
- data/rails_best_practices.gemspec +3 -2
- data/rails_best_practices.yml +22 -22
- data/rake_rubies.sh +3 -2
- data/spec/rails_best_practices/core/check_spec.rb +41 -0
- data/spec/rails_best_practices/core/checking_visitor_spec.rb +78 -0
- data/spec/rails_best_practices/core/visitable_sexp_spec.rb +39 -36
- data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +14 -0
- data/spec/rails_best_practices/prepares/model_prepare_spec.rb +22 -0
- data/spec/rails_best_practices/{checks/add_model_virtual_attribute_check_spec.rb → reviews/add_model_virtual_attribute_review_spec.rb} +17 -25
- data/spec/rails_best_practices/{checks/always_add_db_index_check_spec.rb → reviews/always_add_db_index_review_spec.rb} +28 -41
- data/spec/rails_best_practices/{checks/dry_bundler_in_capistrano_check_spec.rb → reviews/dry_bundler_in_capistrano_review_spec.rb} +7 -11
- data/spec/rails_best_practices/{checks/isolate_seed_data_check_spec.rb → reviews/isolate_seed_data_review_spec.rb} +13 -20
- data/spec/rails_best_practices/{checks/keep_finders_on_their_own_model_check_spec.rb → reviews/keep_finders_on_their_own_model_review_spec.rb} +16 -24
- data/spec/rails_best_practices/{checks/law_of_demeter_check_spec.rb → reviews/law_of_demeter_review_spec.rb} +24 -26
- data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +29 -0
- data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +24 -0
- data/spec/rails_best_practices/{checks/move_code_into_model_check_spec.rb → reviews/move_code_into_model_review_spec.rb} +12 -18
- data/spec/rails_best_practices/{checks/move_finder_to_named_scope_check_spec.rb → reviews/move_finder_to_named_scope_review_spec.rb} +12 -16
- data/spec/rails_best_practices/{checks/move_model_logic_into_model_check_spec.rb → reviews/move_model_logic_into_model_review_spec.rb} +7 -11
- data/spec/rails_best_practices/{checks/needless_deep_nesting_check_spec.rb → reviews/needless_deep_nesting_review_spec.rb} +26 -37
- data/spec/rails_best_practices/{checks/not_use_default_route_check_spec.rb → reviews/not_use_default_route_review_spec.rb} +13 -19
- data/spec/rails_best_practices/{checks/overuse_route_customizations_check_spec.rb → reviews/overuse_route_customizations_review_spec.rb} +27 -39
- data/spec/rails_best_practices/{checks/replace_complex_creation_with_factory_method_check_spec.rb → reviews/replace_complex_creation_with_factory_method_review_spec.rb} +10 -15
- data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +31 -0
- data/spec/rails_best_practices/reviews/review_spec.rb +11 -0
- data/spec/rails_best_practices/{checks/use_before_filter_check_spec.rb → reviews/use_before_filter_review_spec.rb} +11 -17
- data/spec/rails_best_practices/{checks/use_model_association_check_spec.rb → reviews/use_model_association_review_spec.rb} +12 -18
- data/spec/rails_best_practices/{checks/use_observer_check_spec.rb → reviews/use_observer_review_spec.rb} +28 -29
- data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +190 -0
- data/spec/rails_best_practices/{checks/use_say_with_time_in_migrations_check_spec.rb → reviews/use_say_with_time_in_migrations_review_spec.rb} +14 -23
- data/spec/rails_best_practices/{checks/use_scope_access_check_spec.rb → reviews/use_scope_access_review_spec.rb} +28 -37
- data/spec/rails_best_practices_spec.rb +4 -4
- data/spec/spec_helper.rb +1 -0
- metadata +128 -102
- data/lib/rails_best_practices/checks.rb +0 -23
- data/lib/rails_best_practices/checks/check.rb +0 -203
- data/spec/rails_best_practices/checks/check_spec.rb +0 -57
- data/spec/rails_best_practices/checks/move_code_into_controller_check_spec.rb +0 -33
- data/spec/rails_best_practices/checks/move_code_into_helper_check_spec.rb +0 -28
- data/spec/rails_best_practices/checks/replace_instance_variable_with_local_variable_check_spec.rb +0 -36
- data/spec/rails_best_practices/checks/use_query_attribute_check_spec.rb +0 -192
@@ -1,9 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe RailsBestPractices::
|
4
|
-
|
5
|
-
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::AlwaysAddDbIndexCheck.new)
|
6
|
-
end
|
3
|
+
describe RailsBestPractices::Reviews::AlwaysAddDbIndexReview do
|
4
|
+
let(:runner) { RailsBestPractices::Core::Runner.new(:reviews => RailsBestPractices::Reviews::AlwaysAddDbIndexReview.new) }
|
7
5
|
|
8
6
|
it "should always add db index" do
|
9
7
|
content = <<-EOF
|
@@ -15,11 +13,10 @@ describe RailsBestPractices::Checks::AlwaysAddDbIndexCheck do
|
|
15
13
|
end
|
16
14
|
end
|
17
15
|
EOF
|
18
|
-
|
19
|
-
|
20
|
-
errors.
|
21
|
-
errors[
|
22
|
-
errors[1].to_s.should == "db/schema.rb:2 - always add db index (comments => [user_id])"
|
16
|
+
runner.review('db/schema.rb', content)
|
17
|
+
runner.should have(2).errors
|
18
|
+
runner.errors[0].to_s.should == "db/schema.rb:2 - always add db index (comments => [post_id])"
|
19
|
+
runner.errors[1].to_s.should == "db/schema.rb:2 - always add db index (comments => [user_id])"
|
23
20
|
end
|
24
21
|
|
25
22
|
it "should always add db index with polymorphic foreign key" do
|
@@ -32,10 +29,9 @@ describe RailsBestPractices::Checks::AlwaysAddDbIndexCheck do
|
|
32
29
|
end
|
33
30
|
end
|
34
31
|
EOF
|
35
|
-
|
36
|
-
|
37
|
-
errors.
|
38
|
-
errors[0].to_s.should == "db/schema.rb:2 - always add db index (versions => [versioned_id, versioned_type])"
|
32
|
+
runner.review('db/schema.rb', content)
|
33
|
+
runner.should have(1).errors
|
34
|
+
runner.errors[0].to_s.should == "db/schema.rb:2 - always add db index (versions => [versioned_id, versioned_type])"
|
39
35
|
end
|
40
36
|
|
41
37
|
it "should always add db index with polymorphic foreign key and _type is defined before _id" do
|
@@ -48,11 +44,9 @@ describe RailsBestPractices::Checks::AlwaysAddDbIndexCheck do
|
|
48
44
|
end
|
49
45
|
end
|
50
46
|
EOF
|
51
|
-
|
52
|
-
|
53
|
-
errors.
|
54
|
-
errors.size.should == 1
|
55
|
-
errors[0].to_s.should == "db/schema.rb:2 - always add db index (versions => [versioned_id, versioned_type])"
|
47
|
+
runner.review('db/schema.rb', content)
|
48
|
+
runner.should have(1).errors
|
49
|
+
runner.errors[0].to_s.should == "db/schema.rb:2 - always add db index (versions => [versioned_id, versioned_type])"
|
56
50
|
end
|
57
51
|
|
58
52
|
it "should always add db index with single index, but without polymorphic foreign key" do
|
@@ -67,10 +61,9 @@ describe RailsBestPractices::Checks::AlwaysAddDbIndexCheck do
|
|
67
61
|
add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
|
68
62
|
end
|
69
63
|
EOF
|
70
|
-
|
71
|
-
|
72
|
-
errors.
|
73
|
-
errors[0].to_s.should == "db/schema.rb:2 - always add db index (taggings => [taggable_id, taggable_type])"
|
64
|
+
runner.review('db/schema.rb', content)
|
65
|
+
runner.should have(1).errors
|
66
|
+
runner.errors[0].to_s.should == "db/schema.rb:2 - always add db index (taggings => [taggable_id, taggable_type])"
|
74
67
|
end
|
75
68
|
|
76
69
|
it "should always add db index with polymorphic foreign key, but without single index" do
|
@@ -85,10 +78,9 @@ describe RailsBestPractices::Checks::AlwaysAddDbIndexCheck do
|
|
85
78
|
add_index "taggings", ["taggable_id", "taggable_type"], :name => "index_taggings_on_taggable_id_and_taggable_type"
|
86
79
|
end
|
87
80
|
EOF
|
88
|
-
|
89
|
-
|
90
|
-
errors.
|
91
|
-
errors[0].to_s.should == "db/schema.rb:2 - always add db index (taggings => [tag_id])"
|
81
|
+
runner.review('db/schema.rb', content)
|
82
|
+
runner.should have(1).errors
|
83
|
+
runner.errors[0].to_s.should == "db/schema.rb:2 - always add db index (taggings => [tag_id])"
|
92
84
|
end
|
93
85
|
|
94
86
|
it "should not always add db index with column has no id" do
|
@@ -100,9 +92,8 @@ describe RailsBestPractices::Checks::AlwaysAddDbIndexCheck do
|
|
100
92
|
end
|
101
93
|
end
|
102
94
|
EOF
|
103
|
-
|
104
|
-
|
105
|
-
errors.should be_empty
|
95
|
+
runner.review('db/schema.rb', content)
|
96
|
+
runner.should have(0).errors
|
106
97
|
end
|
107
98
|
|
108
99
|
it "should not always add db index with add_index" do
|
@@ -118,9 +109,8 @@ describe RailsBestPractices::Checks::AlwaysAddDbIndexCheck do
|
|
118
109
|
add_index "comments", ["user_id"], :name => "index_comments_on_user_id"
|
119
110
|
end
|
120
111
|
EOF
|
121
|
-
|
122
|
-
|
123
|
-
errors.should be_empty
|
112
|
+
runner.review('db/schema.rb', content)
|
113
|
+
runner.should have(0).errors
|
124
114
|
end
|
125
115
|
|
126
116
|
it "should not always add db index with only _type column" do
|
@@ -131,9 +121,8 @@ describe RailsBestPractices::Checks::AlwaysAddDbIndexCheck do
|
|
131
121
|
end
|
132
122
|
end
|
133
123
|
EOF
|
134
|
-
|
135
|
-
|
136
|
-
errors.should be_empty
|
124
|
+
runner.review('db/schema.rb', content)
|
125
|
+
runner.should have(0).errors
|
137
126
|
end
|
138
127
|
|
139
128
|
it "should not always add db index with multi-column index" do
|
@@ -148,9 +137,8 @@ describe RailsBestPractices::Checks::AlwaysAddDbIndexCheck do
|
|
148
137
|
add_index "versions", ["versioned_id", "versioned_type"], :name => "index_versions_on_versioned_id_and_versioned_type"
|
149
138
|
end
|
150
139
|
EOF
|
151
|
-
|
152
|
-
|
153
|
-
errors.should be_empty
|
140
|
+
runner.review('db/schema.rb', content)
|
141
|
+
runner.should have(0).errors
|
154
142
|
end
|
155
143
|
|
156
144
|
it "should not always add db index if there is an index contains more columns" do
|
@@ -165,8 +153,7 @@ describe RailsBestPractices::Checks::AlwaysAddDbIndexCheck do
|
|
165
153
|
add_index "taggings", ["taggable_id", "taggable_type", "context"], :name => "index_taggings_on_taggable_id_and_taggable_type_and_context"
|
166
154
|
end
|
167
155
|
EOF
|
168
|
-
|
169
|
-
|
170
|
-
errors.should be_empty
|
156
|
+
runner.review('db/schema.rb', content)
|
157
|
+
runner.should have(0).errors
|
171
158
|
end
|
172
159
|
end
|
@@ -1,9 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe RailsBestPractices::
|
4
|
-
|
5
|
-
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::DryBundlerInCapistranoCheck.new)
|
6
|
-
end
|
3
|
+
describe RailsBestPractices::Reviews::DryBundlerInCapistranoReview do
|
4
|
+
let(:runner) { runner = RailsBestPractices::Core::Runner.new(:reviews => RailsBestPractices::Reviews::DryBundlerInCapistranoReview.new) }
|
7
5
|
|
8
6
|
it "should dry bundler in capistrno" do
|
9
7
|
content = <<-EOF
|
@@ -22,18 +20,16 @@ describe RailsBestPractices::Checks::DryBundlerInCapistranoCheck do
|
|
22
20
|
|
23
21
|
after 'deploy:update_code', 'bundler:bundle_new_release'
|
24
22
|
EOF
|
25
|
-
|
26
|
-
|
27
|
-
errors.
|
28
|
-
errors[0].to_s.should == "config/deploy.rb:1 - dry bundler in capistrano"
|
23
|
+
runner.review('config/deploy.rb', content)
|
24
|
+
runner.should have(1).errors
|
25
|
+
runner.errors[0].to_s.should == "config/deploy.rb:1 - dry bundler in capistrano"
|
29
26
|
end
|
30
27
|
|
31
28
|
it "should not dry bundler in capistrano" do
|
32
29
|
content = <<-EOF
|
33
30
|
require 'bundler/capistrano'
|
34
31
|
EOF
|
35
|
-
|
36
|
-
|
37
|
-
errors.should be_empty
|
32
|
+
runner.review('config/deploy.rb', content)
|
33
|
+
runner.should have(0).errors
|
38
34
|
end
|
39
35
|
end
|
@@ -1,9 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe RailsBestPractices::
|
4
|
-
|
5
|
-
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::IsolateSeedDataCheck.new)
|
6
|
-
end
|
3
|
+
describe RailsBestPractices::Reviews::IsolateSeedDataReview do
|
4
|
+
let(:runner) { RailsBestPractices::Core::Runner.new(:reviews => RailsBestPractices::Reviews::IsolateSeedDataReview.new) }
|
7
5
|
|
8
6
|
context "create" do
|
9
7
|
it "should isolate seed data" do
|
@@ -24,11 +22,9 @@ describe RailsBestPractices::Checks::IsolateSeedDataCheck do
|
|
24
22
|
end
|
25
23
|
end
|
26
24
|
EOF
|
27
|
-
|
28
|
-
|
29
|
-
errors.
|
30
|
-
errors[0].to_s.should == "db/migrate/20090818130258_create_roles.rb:8 - isolate seed data"
|
31
|
-
errors.size.should == 1
|
25
|
+
runner.review('db/migrate/20090818130258_create_roles.rb', content)
|
26
|
+
runner.should have(1).errors
|
27
|
+
runner.errors[0].to_s.should == "db/migrate/20090818130258_create_roles.rb:8 - isolate seed data"
|
32
28
|
end
|
33
29
|
end
|
34
30
|
|
@@ -52,10 +48,9 @@ describe RailsBestPractices::Checks::IsolateSeedDataCheck do
|
|
52
48
|
end
|
53
49
|
end
|
54
50
|
EOF
|
55
|
-
|
56
|
-
|
57
|
-
errors.
|
58
|
-
errors[0].to_s.should == "db/migrate/20090818130258_create_roles.rb:9 - isolate seed data"
|
51
|
+
runner.review('db/migrate/20090818130258_create_roles.rb', content)
|
52
|
+
runner.should have(1).errors
|
53
|
+
runner.errors[0].to_s.should == "db/migrate/20090818130258_create_roles.rb:9 - isolate seed data"
|
59
54
|
end
|
60
55
|
|
61
56
|
it "should isolate seed data for instance variable" do
|
@@ -77,10 +72,9 @@ describe RailsBestPractices::Checks::IsolateSeedDataCheck do
|
|
77
72
|
end
|
78
73
|
end
|
79
74
|
EOF
|
80
|
-
|
81
|
-
|
82
|
-
errors.
|
83
|
-
errors[0].to_s.should == "db/migrate/20090818130258_create_roles.rb:9 - isolate seed data"
|
75
|
+
runner.review('db/migrate/20090818130258_create_roles.rb', content)
|
76
|
+
runner.should have(1).errors
|
77
|
+
runner.errors[0].to_s.should == "db/migrate/20090818130258_create_roles.rb:9 - isolate seed data"
|
84
78
|
end
|
85
79
|
end
|
86
80
|
|
@@ -98,8 +92,7 @@ describe RailsBestPractices::Checks::IsolateSeedDataCheck do
|
|
98
92
|
end
|
99
93
|
end
|
100
94
|
EOF
|
101
|
-
|
102
|
-
|
103
|
-
errors.should be_empty
|
95
|
+
runner.review('db/migrate/20090818130258_create_roles.rb', content)
|
96
|
+
runner.should have(0).errors
|
104
97
|
end
|
105
98
|
end
|
@@ -1,9 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe RailsBestPractices::
|
4
|
-
|
5
|
-
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::KeepFindersOnTheirOwnModelCheck.new)
|
6
|
-
end
|
3
|
+
describe RailsBestPractices::Reviews::KeepFindersOnTheirOwnModelReview do
|
4
|
+
let(:runner) { RailsBestPractices::Core::Runner.new(:reviews => RailsBestPractices::Reviews::KeepFindersOnTheirOwnModelReview.new) }
|
7
5
|
|
8
6
|
it "should keep finders on thier own model" do
|
9
7
|
content = <<-EOF
|
@@ -16,10 +14,9 @@ describe RailsBestPractices::Checks::KeepFindersOnTheirOwnModelCheck do
|
|
16
14
|
end
|
17
15
|
end
|
18
16
|
EOF
|
19
|
-
|
20
|
-
|
21
|
-
errors.
|
22
|
-
errors[0].to_s.should == "app/models/post.rb:5 - keep finders on their own model"
|
17
|
+
runner.review('app/models/post.rb', content)
|
18
|
+
runner.should have(1).errors
|
19
|
+
runner.errors[0].to_s.should == "app/models/post.rb:5 - keep finders on their own model"
|
23
20
|
end
|
24
21
|
|
25
22
|
it "should keep finders on thier own model with all method" do
|
@@ -33,10 +30,9 @@ describe RailsBestPractices::Checks::KeepFindersOnTheirOwnModelCheck do
|
|
33
30
|
end
|
34
31
|
end
|
35
32
|
EOF
|
36
|
-
|
37
|
-
|
38
|
-
errors.
|
39
|
-
errors[0].to_s.should == "app/models/post.rb:5 - keep finders on their own model"
|
33
|
+
runner.review('app/models/post.rb', content)
|
34
|
+
runner.should have(1).errors
|
35
|
+
runner.errors[0].to_s.should == "app/models/post.rb:5 - keep finders on their own model"
|
40
36
|
end
|
41
37
|
|
42
38
|
it "should not keep finders on thier own model with self finder" do
|
@@ -50,9 +46,8 @@ describe RailsBestPractices::Checks::KeepFindersOnTheirOwnModelCheck do
|
|
50
46
|
end
|
51
47
|
end
|
52
48
|
EOF
|
53
|
-
|
54
|
-
|
55
|
-
errors.should be_empty
|
49
|
+
runner.review('app/models/post.rb', content)
|
50
|
+
runner.should have(0).errors
|
56
51
|
end
|
57
52
|
|
58
53
|
it "should not keep finders on thier own model with own finder" do
|
@@ -66,9 +61,8 @@ describe RailsBestPractices::Checks::KeepFindersOnTheirOwnModelCheck do
|
|
66
61
|
end
|
67
62
|
end
|
68
63
|
EOF
|
69
|
-
|
70
|
-
|
71
|
-
errors.should be_empty
|
64
|
+
runner.review('app/models/post.rb', content)
|
65
|
+
runner.should have(0).errors
|
72
66
|
end
|
73
67
|
|
74
68
|
it "should not keep finders on their own model without finder" do
|
@@ -81,9 +75,8 @@ describe RailsBestPractices::Checks::KeepFindersOnTheirOwnModelCheck do
|
|
81
75
|
end
|
82
76
|
end
|
83
77
|
EOF
|
84
|
-
|
85
|
-
|
86
|
-
errors.should be_empty
|
78
|
+
runner.review('app/models/post.rb', content)
|
79
|
+
runner.should have(0).errors
|
87
80
|
end
|
88
81
|
|
89
82
|
it "should not keep finders on their own model with ruby Array#find" do
|
@@ -96,8 +89,7 @@ describe RailsBestPractices::Checks::KeepFindersOnTheirOwnModelCheck do
|
|
96
89
|
end
|
97
90
|
end
|
98
91
|
EOF
|
99
|
-
|
100
|
-
|
101
|
-
errors.should be_empty
|
92
|
+
runner.review('app/models/post.rb', content)
|
93
|
+
runner.should have(0).errors
|
102
94
|
end
|
103
95
|
end
|
@@ -1,10 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe RailsBestPractices::
|
3
|
+
describe RailsBestPractices::Reviews::LawOfDemeterReview do
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
let(:runner) {
|
6
|
+
RailsBestPractices::Core::Runner.new(
|
7
|
+
:prepares => RailsBestPractices::Prepares::ModelPrepare.new,
|
8
|
+
:reviews => RailsBestPractices::Reviews::LawOfDemeterReview.new
|
9
|
+
)
|
10
|
+
}
|
8
11
|
|
9
12
|
describe "belongs_to" do
|
10
13
|
before(:each) do
|
@@ -13,7 +16,7 @@ describe RailsBestPractices::Checks::LawOfDemeterCheck do
|
|
13
16
|
belongs_to :user
|
14
17
|
end
|
15
18
|
EOF
|
16
|
-
|
19
|
+
runner.prepare('app/models/invoice.rb', content)
|
17
20
|
end
|
18
21
|
|
19
22
|
it "should law of demeter" do
|
@@ -22,10 +25,9 @@ describe RailsBestPractices::Checks::LawOfDemeterCheck do
|
|
22
25
|
<%= @invoice.user.address %>
|
23
26
|
<%= @invoice.user.cellphone %>
|
24
27
|
EOF
|
25
|
-
|
26
|
-
|
27
|
-
errors.
|
28
|
-
errors[0].to_s.should == "app/views/invoices/show.html.erb:1 - law of demeter"
|
28
|
+
runner.review('app/views/invoices/show.html.erb', content)
|
29
|
+
runner.should have(3).errors
|
30
|
+
runner.errors[0].to_s.should == "app/views/invoices/show.html.erb:1 - law of demeter"
|
29
31
|
end
|
30
32
|
|
31
33
|
it "should law of demeter" do
|
@@ -34,10 +36,9 @@ describe RailsBestPractices::Checks::LawOfDemeterCheck do
|
|
34
36
|
= @invoice.user.address
|
35
37
|
= @invoice.user.cellphone
|
36
38
|
EOF
|
37
|
-
|
38
|
-
|
39
|
-
errors.
|
40
|
-
errors[0].to_s.should == "app/views/invoices/show.html.haml:1 - law of demeter"
|
39
|
+
runner.review('app/views/invoices/show.html.haml', content)
|
40
|
+
runner.should have(3).errors
|
41
|
+
runner.errors[0].to_s.should == "app/views/invoices/show.html.haml:1 - law of demeter"
|
41
42
|
end
|
42
43
|
|
43
44
|
it "should no law of demeter" do
|
@@ -46,9 +47,8 @@ describe RailsBestPractices::Checks::LawOfDemeterCheck do
|
|
46
47
|
<%= @invoice.user_address %>
|
47
48
|
<%= @invoice.user_cellphone %>
|
48
49
|
EOF
|
49
|
-
|
50
|
-
|
51
|
-
errors.should be_empty
|
50
|
+
runner.review('app/views/invoices/show.html.erb', content)
|
51
|
+
runner.should have(0).errors
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
@@ -59,7 +59,7 @@ describe RailsBestPractices::Checks::LawOfDemeterCheck do
|
|
59
59
|
has_one :price
|
60
60
|
end
|
61
61
|
EOF
|
62
|
-
|
62
|
+
runner.prepare('app/models/invoice.rb', content)
|
63
63
|
end
|
64
64
|
|
65
65
|
it "should law of demeter" do
|
@@ -67,10 +67,9 @@ describe RailsBestPractices::Checks::LawOfDemeterCheck do
|
|
67
67
|
<%= @invoice.price.currency %>
|
68
68
|
<%= @invoice.price.number %>
|
69
69
|
EOF
|
70
|
-
|
71
|
-
|
72
|
-
errors.
|
73
|
-
errors[0].to_s.should == "app/views/invoices/show.html.erb:1 - law of demeter"
|
70
|
+
runner.review('app/views/invoices/show.html.erb', content)
|
71
|
+
runner.should have(2).errors
|
72
|
+
runner.errors[0].to_s.should == "app/views/invoices/show.html.erb:1 - law of demeter"
|
74
73
|
end
|
75
74
|
end
|
76
75
|
|
@@ -80,13 +79,13 @@ describe RailsBestPractices::Checks::LawOfDemeterCheck do
|
|
80
79
|
has_many :answers, :dependent => :destroy
|
81
80
|
end
|
82
81
|
EOF
|
83
|
-
|
82
|
+
runner.prepare('app/models/question.rb', content)
|
84
83
|
content = <<-EOF
|
85
84
|
class Answer < ActiveRecord::Base
|
86
85
|
belongs_to :question, :counter_cache => true, :touch => true
|
87
86
|
end
|
88
87
|
EOF
|
89
|
-
|
88
|
+
runner.prepare('app/models/answer.rb', content)
|
90
89
|
content = <<-EOF
|
91
90
|
class CommentsController < ApplicationController
|
92
91
|
def comment_url
|
@@ -94,8 +93,7 @@ describe RailsBestPractices::Checks::LawOfDemeterCheck do
|
|
94
93
|
end
|
95
94
|
end
|
96
95
|
EOF
|
97
|
-
|
98
|
-
|
99
|
-
errors.should be_empty
|
96
|
+
runner.review('app/controllers/comments_controller.rb', content)
|
97
|
+
runner.should have(0).errors
|
100
98
|
end
|
101
99
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RailsBestPractices::Reviews::MoveCodeIntoControllerReview do
|
4
|
+
let(:runner) { RailsBestPractices::Core::Runner.new(:reviews => RailsBestPractices::Reviews::MoveCodeIntoControllerReview.new) }
|
5
|
+
|
6
|
+
it "should move code into controller" do
|
7
|
+
content = <<-EOF
|
8
|
+
<% @posts = Post.find(:all) %>
|
9
|
+
<% @posts.each do |post| %>
|
10
|
+
<%=h post.title %>
|
11
|
+
<%=h post.content %>
|
12
|
+
<% end %>
|
13
|
+
EOF
|
14
|
+
runner.review('app/views/posts/index.html.erb', content)
|
15
|
+
runner.should have(1).errors
|
16
|
+
runner.errors[0].to_s.should == "app/views/posts/index.html.erb:1 - move code into controller"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should not move code into controller" do
|
20
|
+
content = <<-EOF
|
21
|
+
<% @posts.each do |post| %>
|
22
|
+
<%=h post.title %>
|
23
|
+
<%=h post.content %>
|
24
|
+
<% end %>
|
25
|
+
EOF
|
26
|
+
runner.review('app/views/posts/index.html.erb', content)
|
27
|
+
runner.should have(0).errors
|
28
|
+
end
|
29
|
+
end
|