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