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.
- 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,177 +1,179 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
module RailsBestPractices
|
4
|
+
module Reviews
|
5
|
+
describe OveruseRouteCustomizationsReview do
|
6
|
+
let(:runner) { Core::Runner.new(:reviews => OveruseRouteCustomizationsReview.new) }
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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 => :update,
|
15
|
+
:delete_comment => :delete }
|
16
|
+
end
|
17
|
+
EOF
|
18
|
+
runner.review('config/routes.rb', content)
|
19
|
+
runner.should have(1).errors
|
20
|
+
runner.errors[0].to_s.should == "config/routes.rb:2 - overuse route customizations (customize_count > 3)"
|
21
|
+
end
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
23
|
+
it "should overuse route customizations with member" do
|
24
|
+
content = <<-EOF
|
25
|
+
ActionController::Routing::Routes.draw do |map|
|
26
|
+
map.resources :posts, :member => { :create_comment => :post,
|
27
|
+
:update_comment => :update,
|
28
|
+
:delete_comment => :delete,
|
29
|
+
:disable_comment => :post }
|
30
|
+
end
|
31
|
+
EOF
|
32
|
+
runner.review('config/routes.rb', content)
|
33
|
+
runner.should have(1).errors
|
34
|
+
runner.errors[0].to_s.should == "config/routes.rb:2 - overuse route customizations (customize_count > 3)"
|
35
|
+
end
|
36
36
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
37
|
+
it "should overuse route customizations with collection" do
|
38
|
+
content = <<-EOF
|
39
|
+
ActionController::Routing::Routes.draw do |map|
|
40
|
+
map.resources :posts, :collection => { :list_comments => :get,
|
41
|
+
:update_comments => :get,
|
42
|
+
:delete_comments => :get,
|
43
|
+
:disable_comments => :get }
|
44
|
+
end
|
45
|
+
EOF
|
46
|
+
runner.review('config/routes.rb', content)
|
47
|
+
runner.should have(1).errors
|
48
|
+
runner.errors[0].to_s.should == "config/routes.rb:2 - overuse route customizations (customize_count > 3)"
|
49
|
+
end
|
50
50
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
51
|
+
it "should overuse route customizations with hash member and collection" do
|
52
|
+
content = <<-EOF
|
53
|
+
ActionController::Routing::Routes.draw do |map|
|
54
|
+
map.resources :categories do |category|
|
55
|
+
category.resources :posts, :member => { :create_comment => :post,
|
56
|
+
:update_comment => :update,
|
57
|
+
:delete_comment => :delete },
|
58
|
+
:collection => { :comments => :get }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
EOF
|
62
|
+
runner.review('config/routes.rb', content)
|
63
|
+
runner.should have(1).errors
|
64
|
+
runner.errors[0].to_s.should == "config/routes.rb:3 - overuse route customizations (customize_count > 3)"
|
59
65
|
end
|
60
|
-
end
|
61
|
-
EOF
|
62
|
-
runner.review('config/routes.rb', content)
|
63
|
-
runner.should have(1).errors
|
64
|
-
runner.errors[0].to_s.should == "config/routes.rb:3 - overuse route customizations (customize_count > 3)"
|
65
|
-
end
|
66
66
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
67
|
+
it "should overuse route customizations with array member and collection" do
|
68
|
+
content = <<-EOF
|
69
|
+
ActionController::Routing::Routes.draw do |map|
|
70
|
+
map.resources :categories do |category|
|
71
|
+
category.resources :posts, :member => [:create_comment, :update_comment, :delete_comment],
|
72
|
+
:collection => [:comments]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
EOF
|
76
|
+
runner.review('config/routes.rb', content)
|
77
|
+
runner.should have(1).errors
|
78
|
+
runner.errors[0].to_s.should == "config/routes.rb:3 - overuse route customizations (customize_count > 3)"
|
73
79
|
end
|
74
|
-
end
|
75
|
-
EOF
|
76
|
-
runner.review('config/routes.rb', content)
|
77
|
-
runner.should have(1).errors
|
78
|
-
runner.errors[0].to_s.should == "config/routes.rb:3 - overuse route customizations (customize_count > 3)"
|
79
|
-
end
|
80
80
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
81
|
+
it "should not overuse route customizations without customization" do
|
82
|
+
content = <<-EOF
|
83
|
+
ActionController::Routing::Routes.draw do |map|
|
84
|
+
map.resources :posts
|
85
|
+
end
|
86
|
+
EOF
|
87
|
+
runner.review('config/routes.rb', content)
|
88
|
+
runner.should have(0).errors
|
89
|
+
end
|
90
90
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
91
|
+
it "should not overuse route customizations when customize route is only one" do
|
92
|
+
content = <<-EOF
|
93
|
+
ActionController::Routing::Routes.draw do |map|
|
94
|
+
map.resources :posts, :member => { :vote => :post }
|
95
|
+
end
|
96
|
+
EOF
|
97
|
+
runner.review('config/routes.rb', content)
|
98
|
+
runner.should have(0).errors
|
99
|
+
end
|
100
100
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
101
|
+
it "should not raise error for constants in routes" do
|
102
|
+
content =<<-EOF
|
103
|
+
IP_PATTERN = /(((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2}))|[\d]+/.freeze
|
104
|
+
map.resources :vlans do |vlan|
|
105
|
+
vlan.resources :ip_ranges, :member => {:move => [:get, :post]} do |range|
|
106
|
+
range.resources :ips, :requirements => { :id => IP_PATTERN }
|
107
|
+
end
|
108
|
+
end
|
109
|
+
EOF
|
110
|
+
runner.review('config/routes.rb', content)
|
111
|
+
runner.should have(0).errors
|
107
112
|
end
|
108
113
|
end
|
109
|
-
EOF
|
110
|
-
runner.review('config/routes.rb', content)
|
111
|
-
runner.should have(0).errors
|
112
|
-
end
|
113
|
-
end
|
114
114
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
115
|
+
describe "rails3" do
|
116
|
+
it "should overuse route customizations" do
|
117
|
+
content = <<-EOF
|
118
|
+
RailsBestpracticesCom::Application.routes.draw do
|
119
|
+
resources :posts do
|
120
|
+
member do
|
121
|
+
post :create_comment
|
122
|
+
update :update_comment
|
123
|
+
delete :delete_comment
|
124
|
+
end
|
125
125
|
|
126
|
-
|
127
|
-
|
126
|
+
collection do
|
127
|
+
get :comments
|
128
|
+
end
|
129
|
+
end
|
128
130
|
end
|
131
|
+
EOF
|
132
|
+
runner.review('config/routes.rb', content)
|
133
|
+
runner.should have(1).errors
|
134
|
+
runner.errors[0].to_s.should == "config/routes.rb:2 - overuse route customizations (customize_count > 3)"
|
129
135
|
end
|
130
|
-
end
|
131
|
-
EOF
|
132
|
-
runner.review('config/routes.rb', content)
|
133
|
-
runner.should have(1).errors
|
134
|
-
runner.errors[0].to_s.should == "config/routes.rb:2 - overuse route customizations (customize_count > 3)"
|
135
|
-
end
|
136
136
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
137
|
+
it "should overuse route customizations another way" do
|
138
|
+
content = <<-EOF
|
139
|
+
RailsBestpracticesCom::Application.routes.draw do
|
140
|
+
resources :posts do
|
141
|
+
post :create_comment, :on => :member
|
142
|
+
update :update_comment, :on => :member
|
143
|
+
delete :delete_comment, :on => :member
|
144
|
+
get :comments, :on => :collection
|
145
|
+
end
|
146
|
+
end
|
147
|
+
EOF
|
148
|
+
runner.review('config/routes.rb', content)
|
149
|
+
runner.should have(1).errors
|
150
|
+
runner.errors[0].to_s.should == "config/routes.rb:2 - overuse route customizations (customize_count > 3)"
|
145
151
|
end
|
146
|
-
end
|
147
|
-
EOF
|
148
|
-
runner.review('config/routes.rb', content)
|
149
|
-
runner.should have(1).errors
|
150
|
-
runner.errors[0].to_s.should == "config/routes.rb:2 - overuse route customizations (customize_count > 3)"
|
151
|
-
end
|
152
152
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
153
|
+
it "should not overuse route customizations without customization" do
|
154
|
+
content = <<-EOF
|
155
|
+
RailsBestpracticesCom::Application.routes.draw do
|
156
|
+
resources :posts
|
157
|
+
end
|
158
|
+
EOF
|
159
|
+
runner.review('config/routes.rb', content)
|
160
|
+
runner.should have(0).errors
|
161
|
+
end
|
162
162
|
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
163
|
+
it "should not overuse route customizations when customize route is only one" do
|
164
|
+
content = <<-EOF
|
165
|
+
RailsBestpracticesCom::Application.routes.draw do
|
166
|
+
resources :posts do
|
167
|
+
member do
|
168
|
+
post :vote
|
169
|
+
end
|
170
|
+
end
|
169
171
|
end
|
172
|
+
EOF
|
173
|
+
runner.review('config/routes.rb', content)
|
174
|
+
runner.should have(0).errors
|
170
175
|
end
|
171
176
|
end
|
172
|
-
EOF
|
173
|
-
runner.review('config/routes.rb', content)
|
174
|
-
runner.should have(0).errors
|
175
177
|
end
|
176
178
|
end
|
177
179
|
end
|
@@ -1,67 +1,98 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module RailsBestPractices
|
4
|
+
module Reviews
|
5
|
+
describe ProtectMassAssignmentReview do
|
6
|
+
let(:runner) { Core::Runner.new(:reviews => ProtectMassAssignmentReview.new) }
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
8
|
+
it "should protect mass assignment" do
|
9
|
+
content =<<-EOF
|
10
|
+
class User < ActiveRecord::Base
|
11
|
+
end
|
12
|
+
EOF
|
13
|
+
runner.review('app/models/user.rb', content)
|
14
|
+
runner.should have(1).errors
|
15
|
+
runner.errors[0].to_s.should == "app/models/user.rb:1 - protect mass assignment"
|
16
|
+
end
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
18
|
+
it "should not protect mass assignment if attr_accessible is used with arguments" do
|
19
|
+
content =<<-EOF
|
20
|
+
class User < ActiveRecord::Base
|
21
|
+
attr_accessible :email, :password, :password_confirmation
|
22
|
+
end
|
23
|
+
EOF
|
24
|
+
runner.review('app/models/user.rb', content)
|
25
|
+
runner.should have(0).errors
|
26
|
+
end
|
25
27
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
28
|
+
it "should not protect mass assignment if attr_accessible is used without arguments" do
|
29
|
+
content =<<-EOF
|
30
|
+
class User < ActiveRecord::Base
|
31
|
+
attr_accessible
|
32
|
+
end
|
33
|
+
EOF
|
34
|
+
runner.review('app/models/user.rb', content)
|
35
|
+
runner.should have(0).errors
|
36
|
+
end
|
35
37
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
38
|
+
it "should not protect mass assignment with attr_protected" do
|
39
|
+
content =<<-EOF
|
40
|
+
class User < ActiveRecord::Base
|
41
|
+
attr_protected :role
|
42
|
+
end
|
43
|
+
EOF
|
44
|
+
runner.review('app/models/user.rb', content)
|
45
|
+
runner.should have(0).errors
|
46
|
+
end
|
45
47
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
48
|
+
it "should not protect mass assignment if using devise" do
|
49
|
+
content =<<-EOF
|
50
|
+
class User < ActiveRecord::Base
|
51
|
+
devise :database_authenticatable, :registerable, :confirmable, :recoverable, :stretches => 20
|
52
|
+
end
|
53
|
+
EOF
|
54
|
+
runner.review('app/models/user.rb', content)
|
55
|
+
runner.should have(0).errors
|
51
56
|
end
|
52
|
-
end
|
53
|
-
EOF
|
54
|
-
runner.review('app/models/user.rb', content)
|
55
|
-
runner.should have(0).errors
|
56
|
-
end
|
57
57
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
58
|
+
it "should not protect mass assignment if using authlogic with configuration" do
|
59
|
+
content =<<-EOF
|
60
|
+
class User < ActiveRecord::Base
|
61
|
+
acts_as_authentic do |c|
|
62
|
+
c.my_config_option = my_value
|
63
|
+
end
|
64
|
+
end
|
65
|
+
EOF
|
66
|
+
runner.review('app/models/user.rb', content)
|
67
|
+
runner.should have(0).errors
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should not protect mass assignment if using authlogic without configuration" do
|
71
|
+
content =<<-EOF
|
72
|
+
class User < ActiveRecord::Base
|
73
|
+
acts_as_authentic
|
74
|
+
end
|
75
|
+
EOF
|
76
|
+
runner.review('app/models/user.rb', content)
|
77
|
+
runner.should have(0).errors
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should not protect mass assignment if user set config.active_record.whitelist_attributes" do
|
81
|
+
content =<<-EOF
|
82
|
+
module RailsBestPracticesCom
|
83
|
+
class Application < Rails::Application
|
84
|
+
config.active_record.whitelist_attributes = true
|
85
|
+
end
|
86
|
+
end
|
87
|
+
EOF
|
88
|
+
runner.prepare('config/application.rb', content)
|
89
|
+
content =<<-EOF
|
90
|
+
class User < ActiveRecord::Base
|
91
|
+
end
|
92
|
+
EOF
|
93
|
+
runner.review('app/models/user.rb', content)
|
94
|
+
runner.should have(0).errors
|
95
|
+
end
|
62
96
|
end
|
63
|
-
EOF
|
64
|
-
runner.review('app/models/user.rb', content)
|
65
|
-
runner.should have(0).errors
|
66
97
|
end
|
67
98
|
end
|