rails_best_practices 1.9.0 → 1.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +4 -1
- data/Gemfile +0 -3
- data/Gemfile.lock +10 -9
- data/README.md +2 -0
- data/lib/rails_best_practices/analyzer.rb +40 -21
- data/lib/rails_best_practices/core.rb +1 -0
- data/lib/rails_best_practices/core/check.rb +12 -0
- data/lib/rails_best_practices/core/configs.rb +7 -0
- data/lib/rails_best_practices/core/error.rb +2 -1
- data/lib/rails_best_practices/core/routes.rb +11 -5
- data/lib/rails_best_practices/core/runner.rb +6 -2
- data/lib/rails_best_practices/core_ext/sexp.rb +3 -1
- data/lib/rails_best_practices/prepares.rb +5 -0
- data/lib/rails_best_practices/prepares/config_prepare.rb +21 -0
- data/lib/rails_best_practices/prepares/route_prepare.rb +24 -7
- data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +11 -3
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +7 -3
- data/lib/rails_best_practices/version.rb +1 -1
- data/rails_best_practices.gemspec +1 -0
- data/spec/rails_best_practices/analyzer_spec.rb +52 -32
- data/spec/rails_best_practices/core/check_spec.rb +64 -45
- data/spec/rails_best_practices/core/checking_visitor_spec.rb +65 -64
- data/spec/rails_best_practices/core/configs_spec.rb +7 -0
- data/spec/rails_best_practices/core/controllers_spec.rb +4 -2
- data/spec/rails_best_practices/core/error_spec.rb +28 -22
- data/spec/rails_best_practices/core/helpers_spec.rb +4 -2
- data/spec/rails_best_practices/core/klasses_spec.rb +8 -6
- data/spec/rails_best_practices/core/mailers_spec.rb +4 -2
- data/spec/rails_best_practices/core/methods_spec.rb +37 -35
- data/spec/rails_best_practices/core/model_associations_spec.rb +17 -15
- data/spec/rails_best_practices/core/model_attributes_spec.rb +17 -15
- data/spec/rails_best_practices/core/models_spec.rb +4 -2
- data/spec/rails_best_practices/core/modules_spec.rb +21 -19
- data/spec/rails_best_practices/core/nil_spec.rb +23 -21
- data/spec/rails_best_practices/core/routes_spec.rb +21 -14
- data/spec/rails_best_practices/core/runner_spec.rb +17 -15
- data/spec/rails_best_practices/core_ext/sexp_spec.rb +32 -7
- data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +35 -31
- data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +35 -31
- data/spec/rails_best_practices/prepares/config_prepare_spec.rb +24 -0
- data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +122 -123
- data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +36 -32
- data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +12 -8
- data/spec/rails_best_practices/prepares/model_prepare_spec.rb +326 -322
- data/spec/rails_best_practices/prepares/route_prepare_spec.rb +619 -573
- data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +25 -21
- data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +85 -87
- data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +224 -222
- data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +30 -26
- data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +79 -75
- data/spec/rails_best_practices/reviews/keep_finders_on_their_own_model_review_spec.rb +76 -72
- data/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb +138 -140
- data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +38 -34
- data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +22 -18
- data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +57 -53
- data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +62 -63
- data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +51 -50
- data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +140 -136
- data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +48 -44
- data/spec/rails_best_practices/reviews/not_use_times_ago_in_words_review_spec.rb +42 -38
- data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +151 -149
- data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +86 -55
- data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +23 -19
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +290 -286
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +76 -72
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +601 -597
- data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +55 -54
- data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +34 -30
- data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +288 -289
- data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +59 -55
- data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +73 -69
- data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +89 -98
- data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +52 -52
- data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +192 -190
- data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +114 -121
- data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +189 -190
- data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +97 -93
- data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +146 -151
- data/spec/spec_helper.rb +11 -20
- metadata +42 -26
- data/.watchr.example +0 -65
@@ -1,66 +1,70 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module RailsBestPractices
|
4
|
+
module Reviews
|
5
|
+
describe SimplifyRenderInControllersReview do
|
6
|
+
let(:runner) { Core::Runner.new(:reviews => SimplifyRenderInControllersReview.new) }
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
8
|
+
it "should simplify render action view" do
|
9
|
+
content =<<-EOF
|
10
|
+
def edit
|
11
|
+
render :action => :edit
|
12
|
+
end
|
13
|
+
EOF
|
14
|
+
runner.review("app/controllers/posts_controller.rb", content)
|
15
|
+
runner.should have(1).errors
|
16
|
+
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:2 - simplify render in controllers"
|
17
|
+
end
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
19
|
+
it "should simplify render actions's template" do
|
20
|
+
content =<<-EOF
|
21
|
+
def edit
|
22
|
+
render :template => 'books/edit'
|
23
|
+
end
|
24
|
+
EOF
|
25
|
+
runner.review("app/controllers/posts_controller.rb", content)
|
26
|
+
runner.should have(1).errors
|
27
|
+
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:2 - simplify render in controllers"
|
28
|
+
end
|
27
29
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
30
|
+
it "should simplify render an arbitrary file" do
|
31
|
+
content =<<-EOF
|
32
|
+
def edit
|
33
|
+
render :file => '/path/to/rails/app/views/books/edit'
|
34
|
+
end
|
35
|
+
EOF
|
36
|
+
runner.review("app/controllers/posts_controller.rb", content)
|
37
|
+
runner.should have(1).errors
|
38
|
+
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:2 - simplify render in controllers"
|
39
|
+
end
|
38
40
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
41
|
+
it "should not simplify render action view" do
|
42
|
+
content =<<-EOF
|
43
|
+
render :edit
|
44
|
+
EOF
|
45
|
+
runner.review("app/controllers/posts_controller", content)
|
46
|
+
runner.should have(0).errors
|
47
|
+
end
|
46
48
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
49
|
+
it "should not simplify render actions's template" do
|
50
|
+
content =<<-EOF
|
51
|
+
def edit
|
52
|
+
render 'books/edit'
|
53
|
+
end
|
54
|
+
EOF
|
55
|
+
runner.review("app/controllers/posts_controller.rb", content)
|
56
|
+
runner.should have(0).errors
|
57
|
+
end
|
56
58
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
59
|
+
it "should not simplify render an arbitrary file" do
|
60
|
+
content =<<-EOF
|
61
|
+
def edit
|
62
|
+
render '/path/to/rails/app/views/books/edit'
|
63
|
+
end
|
64
|
+
EOF
|
65
|
+
runner.review("app/controllers/posts_controller.rb", content)
|
66
|
+
runner.should have(0).errors
|
67
|
+
end
|
61
68
|
end
|
62
|
-
EOF
|
63
|
-
runner.review("app/controllers/posts_controller.rb", content)
|
64
|
-
runner.should have(0).errors
|
65
69
|
end
|
66
70
|
end
|
@@ -1,82 +1,86 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module RailsBestPractices
|
4
|
+
module Reviews
|
5
|
+
describe SimplifyRenderInViewsReview do
|
6
|
+
let(:runner) { Core::Runner.new(:reviews => SimplifyRenderInViewsReview.new) }
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
it "should simplify render simple partial" do
|
9
|
+
content =<<-EOF
|
10
|
+
<%= render :partial => 'sidebar' %>
|
11
|
+
EOF
|
12
|
+
runner.review('app/views/posts/index.html.erb', content)
|
13
|
+
runner.should have(1).errors
|
14
|
+
runner.errors[0].to_s.should == "app/views/posts/index.html.erb:1 - simplify render in views"
|
15
|
+
end
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
17
|
+
it "should simplify render partial with object" do
|
18
|
+
content =<<-EOF
|
19
|
+
<%= render :partial => 'post', :object => @post %>
|
20
|
+
EOF
|
21
|
+
runner.review('app/views/posts/index.html.erb', content)
|
22
|
+
runner.should have(1).errors
|
23
|
+
runner.errors[0].to_s.should == "app/views/posts/index.html.erb:1 - simplify render in views"
|
24
|
+
end
|
23
25
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
26
|
+
it "should simplify render partial with collection" do
|
27
|
+
content =<<-EOF
|
28
|
+
<%= render :partial => 'posts', :collection => @posts %>
|
29
|
+
EOF
|
30
|
+
runner.review('app/views/posts/index.html.erb', content)
|
31
|
+
runner.should have(1).errors
|
32
|
+
runner.errors[0].to_s.should == "app/views/posts/index.html.erb:1 - simplify render in views"
|
33
|
+
end
|
32
34
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
35
|
+
it "should simplify render partial with local variables" do
|
36
|
+
content =<<-EOF
|
37
|
+
<%= render :partial => 'comment', :locals => { :parent => post } %>
|
38
|
+
EOF
|
39
|
+
runner.review('app/views/posts/index.html.erb', content)
|
40
|
+
runner.should have(1).errors
|
41
|
+
runner.errors[0].to_s.should == "app/views/posts/index.html.erb:1 - simplify render in views"
|
42
|
+
end
|
41
43
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
44
|
+
it "should not simplify render simple partial" do
|
45
|
+
content =<<-EOF
|
46
|
+
<%= render 'sidebar' %>
|
47
|
+
<%= render 'shared/sidebar' %>
|
48
|
+
EOF
|
49
|
+
runner.review('app/views/posts/index.html.erb', content)
|
50
|
+
runner.should have(0).errors
|
51
|
+
end
|
50
52
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
53
|
+
it "should not simplify render partial with object" do
|
54
|
+
content =<<-EOF
|
55
|
+
<%= render @post %>
|
56
|
+
EOF
|
57
|
+
runner.review('app/views/posts/index.html.erb', content)
|
58
|
+
runner.should have(0).errors
|
59
|
+
end
|
58
60
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
61
|
+
it "should not simplify render partial with collection" do
|
62
|
+
content =<<-EOF
|
63
|
+
<%= render @posts %>
|
64
|
+
EOF
|
65
|
+
runner.review('app/views/posts/index.html.erb', content)
|
66
|
+
runner.should have(0).errors
|
67
|
+
end
|
66
68
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
69
|
+
it "should not simplify render partial with local variables" do
|
70
|
+
content =<<-EOF
|
71
|
+
<%= render 'comment', :parent => post %>
|
72
|
+
EOF
|
73
|
+
runner.review('app/views/posts/index.html.erb', content)
|
74
|
+
runner.should have(0).errors
|
75
|
+
end
|
74
76
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
77
|
+
it "should not simplify render partial with complex partial" do
|
78
|
+
content =<<-EOF
|
79
|
+
<%= render :partial => 'shared/post', :object => @post %>
|
80
|
+
EOF
|
81
|
+
runner.review('app/views/posts/index.html.erb', content)
|
82
|
+
runner.should have(0).errors
|
83
|
+
end
|
84
|
+
end
|
81
85
|
end
|
82
86
|
end
|
@@ -1,116 +1,107 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
end
|
34
|
-
|
35
|
-
it "should not use before_filter when equal to customize count" do
|
36
|
-
content = <<-EOF
|
37
|
-
class PostsController < ApplicationController
|
38
|
-
|
39
|
-
def show
|
40
|
-
@post = Post.find(params[:id])
|
41
|
-
end
|
42
|
-
|
43
|
-
def edit
|
44
|
-
@post = Post.find(params[:id])
|
45
|
-
end
|
46
|
-
end
|
47
|
-
EOF
|
48
|
-
runner.review('app/controllers/posts_controller.rb', content)
|
49
|
-
runner.should have(0).errors
|
50
|
-
end
|
51
|
-
|
52
|
-
it "should not use before_filter" do
|
53
|
-
content = <<-EOF
|
54
|
-
class PostsController < ApplicationController
|
55
|
-
before_filter :find_post, :only => [:show, :edit, :update, :destroy]
|
56
|
-
|
57
|
-
def update
|
58
|
-
@post.update_attributes(params[:post])
|
59
|
-
end
|
60
|
-
|
61
|
-
def destroy
|
62
|
-
@post.destroy
|
3
|
+
module RailsBestPractices
|
4
|
+
module Reviews
|
5
|
+
describe UseBeforeFilterReview do
|
6
|
+
let(:runner) { Core::Runner.new(:reviews => UseBeforeFilterReview.new('customize_count' => 2)) }
|
7
|
+
|
8
|
+
it "should use before_filter" do
|
9
|
+
content = <<-EOF
|
10
|
+
class PostsController < ApplicationController
|
11
|
+
def show
|
12
|
+
@post = current_user.posts.find(params[:id])
|
13
|
+
end
|
14
|
+
|
15
|
+
def edit
|
16
|
+
@post = current_user.posts.find(params[:id])
|
17
|
+
end
|
18
|
+
|
19
|
+
def update
|
20
|
+
@post = current_user.posts.find(params[:id])
|
21
|
+
@post.update_attributes(params[:post])
|
22
|
+
end
|
23
|
+
|
24
|
+
def destroy
|
25
|
+
@post = current_user.posts.find(params[:id])
|
26
|
+
@post.destroy
|
27
|
+
end
|
28
|
+
end
|
29
|
+
EOF
|
30
|
+
runner.review('app/controllers/posts_controller.rb', content)
|
31
|
+
runner.should have(1).errors
|
32
|
+
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:3,7,11,16 - use before_filter for show,edit,update,destroy"
|
63
33
|
end
|
64
34
|
|
65
|
-
|
35
|
+
it "should not use before_filter when equal to customize count" do
|
36
|
+
content = <<-EOF
|
37
|
+
class PostsController < ApplicationController
|
38
|
+
def show
|
39
|
+
@post = Post.find(params[:id])
|
40
|
+
end
|
66
41
|
|
67
|
-
|
68
|
-
|
42
|
+
def edit
|
43
|
+
@post = Post.find(params[:id])
|
44
|
+
end
|
45
|
+
end
|
46
|
+
EOF
|
47
|
+
runner.review('app/controllers/posts_controller.rb', content)
|
48
|
+
runner.should have(0).errors
|
69
49
|
end
|
70
|
-
end
|
71
|
-
EOF
|
72
|
-
runner.review('app/controllers/posts_controller.rb', content)
|
73
|
-
runner.should have(0).errors
|
74
|
-
end
|
75
|
-
|
76
|
-
it "should not use before_filter by nil" do
|
77
|
-
content = <<-EOF
|
78
|
-
class PostsController < ApplicationController
|
79
50
|
|
80
|
-
|
81
|
-
|
51
|
+
it "should not use before_filter" do
|
52
|
+
content = <<-EOF
|
53
|
+
class PostsController < ApplicationController
|
54
|
+
before_filter :find_post, :only => [:show, :edit, :update, :destroy]
|
82
55
|
|
83
|
-
|
84
|
-
|
56
|
+
def update
|
57
|
+
@post.update_attributes(params[:post])
|
58
|
+
end
|
85
59
|
|
86
|
-
|
87
|
-
|
60
|
+
def destroy
|
61
|
+
@post.destroy
|
62
|
+
end
|
88
63
|
|
89
|
-
|
64
|
+
protected
|
65
|
+
def find_post
|
66
|
+
@post = current_user.posts.find(params[:id])
|
67
|
+
end
|
68
|
+
end
|
69
|
+
EOF
|
70
|
+
runner.review('app/controllers/posts_controller.rb', content)
|
71
|
+
runner.should have(0).errors
|
90
72
|
end
|
91
73
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
content =<<-EOF
|
100
|
-
class PostsController < ApplicationController
|
101
|
-
protected
|
102
|
-
def load_comments
|
103
|
-
load_post
|
104
|
-
@comments = @post.comments
|
74
|
+
it "should not use before_filter by nil" do
|
75
|
+
content = <<-EOF
|
76
|
+
class PostsController < ApplicationController
|
77
|
+
def show; end
|
78
|
+
def edit; end
|
79
|
+
def update; end
|
80
|
+
def destroy; end
|
105
81
|
end
|
82
|
+
EOF
|
83
|
+
runner.review('app/controllers/posts_controller.rb', content)
|
84
|
+
runner.should have(0).errors
|
85
|
+
end
|
106
86
|
|
107
|
-
|
108
|
-
|
109
|
-
|
87
|
+
it "should not use before_filter for protected/private methods" do
|
88
|
+
content =<<-EOF
|
89
|
+
class PostsController < ApplicationController
|
90
|
+
protected
|
91
|
+
def load_comments
|
92
|
+
load_post
|
93
|
+
@comments = @post.comments
|
94
|
+
end
|
95
|
+
|
96
|
+
def load_user
|
97
|
+
load_post
|
98
|
+
@user = @post.user
|
99
|
+
end
|
110
100
|
end
|
101
|
+
EOF
|
102
|
+
runner.review('app/controllers/posts_controller.rb', content)
|
103
|
+
runner.should have(0).errors
|
104
|
+
end
|
111
105
|
end
|
112
|
-
EOF
|
113
|
-
runner.review('app/controllers/posts_controller.rb', content)
|
114
|
-
runner.should have(0).errors
|
115
106
|
end
|
116
107
|
end
|