rails_best_practices 1.9.1 → 1.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +1 -1
- data/Gemfile +1 -1
- data/Gemfile.lock +10 -2
- data/README.md +33 -27
- data/lib/rails_best_practices/analyzer.rb +13 -13
- data/lib/rails_best_practices/core/check.rb +7 -6
- data/lib/rails_best_practices/core/checking_visitor.rb +1 -1
- data/lib/rails_best_practices/core/runner.rb +2 -3
- data/lib/rails_best_practices/core_ext/sexp.rb +18 -17
- data/lib/rails_best_practices/lexicals.rb +1 -0
- data/lib/rails_best_practices/lexicals/long_line_check.rb +31 -0
- data/lib/rails_best_practices/prepares/config_prepare.rb +1 -1
- data/lib/rails_best_practices/reviews.rb +2 -0
- data/lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb +2 -2
- data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +1 -1
- data/lib/rails_best_practices/reviews/hash_syntax_review.rb +63 -0
- data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +1 -1
- data/lib/rails_best_practices/reviews/move_code_into_model_review.rb +1 -1
- data/lib/rails_best_practices/reviews/move_finder_to_named_scope_review.rb +1 -1
- data/lib/rails_best_practices/reviews/move_model_logic_into_model_review.rb +1 -1
- data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +1 -1
- data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +1 -1
- data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +15 -6
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +4 -2
- data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +8 -4
- data/lib/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review.rb +2 -1
- data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +52 -8
- data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +14 -2
- data/lib/rails_best_practices/reviews/use_before_filter_review.rb +8 -3
- data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +2 -1
- data/lib/rails_best_practices/reviews/use_observer_review.rb +1 -1
- data/lib/rails_best_practices/reviews/use_parentheses_in_method_def_review.rb +33 -0
- data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +16 -6
- data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +9 -6
- data/lib/rails_best_practices/reviews/use_scope_access_review.rb +8 -3
- data/lib/rails_best_practices/version.rb +1 -1
- data/rails_best_practices.yml +28 -25
- data/rake_rubies.sh +1 -1
- data/spec/rails_best_practices/analyzer_spec.rb +1 -1
- data/spec/rails_best_practices/core/check_spec.rb +7 -7
- data/spec/rails_best_practices/core/checking_visitor_spec.rb +7 -7
- data/spec/rails_best_practices/core/error_spec.rb +12 -12
- data/spec/rails_best_practices/core_ext/sexp_spec.rb +103 -85
- data/spec/rails_best_practices/lexicals/long_line_check_spec.rb +47 -0
- data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +1 -1
- data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +1 -1
- data/spec/rails_best_practices/prepares/config_prepare_spec.rb +1 -1
- data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +1 -1
- data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +1 -1
- data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +1 -1
- data/spec/rails_best_practices/prepares/model_prepare_spec.rb +4 -4
- data/spec/rails_best_practices/prepares/route_prepare_spec.rb +54 -54
- data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +4 -4
- data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +46 -46
- data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +3 -3
- data/spec/rails_best_practices/reviews/hash_syntax_review_spec.rb +67 -0
- data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +8 -8
- data/spec/rails_best_practices/reviews/keep_finders_on_their_own_model_review_spec.rb +9 -9
- data/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +1 -1
- data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +1 -1
- data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +1 -1
- data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +13 -13
- data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +2 -2
- data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +3 -3
- data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +3 -3
- data/spec/rails_best_practices/reviews/not_use_times_ago_in_words_review_spec.rb +1 -1
- data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +26 -26
- data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +11 -2
- data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +1 -1
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +8 -8
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +2 -2
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +25 -10
- data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +3 -3
- data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +1 -1
- data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +28 -28
- data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +4 -4
- data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +15 -7
- data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +3 -3
- data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +3 -3
- data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +3 -3
- data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +5 -5
- data/spec/rails_best_practices/reviews/use_parenthesis_in_method_def_spec.rb +41 -0
- data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +3 -3
- data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +8 -8
- data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +9 -9
- data/spec/spec_helper.rb +1 -1
- metadata +36 -29
- data/.rspec.example +0 -2
- data/.rvmrc.example +0 -2
@@ -4,8 +4,8 @@ module RailsBestPractices
|
|
4
4
|
module Reviews
|
5
5
|
describe RemoveUnusedMethodsInHelpersReview do
|
6
6
|
let(:runner) { Core::Runner.new(
|
7
|
-
:
|
8
|
-
:
|
7
|
+
prepares: [Prepares::ControllerPrepare.new, Prepares::HelperPrepare.new],
|
8
|
+
reviews: RemoveUnusedMethodsInHelpersReview.new(except_methods: [])
|
9
9
|
) }
|
10
10
|
|
11
11
|
it "should remove unused methods" do
|
@@ -4,8 +4,8 @@ module RailsBestPractices
|
|
4
4
|
module Reviews
|
5
5
|
describe RemoveUnusedMethodsInModelsReview do
|
6
6
|
let(:runner) { Core::Runner.new(
|
7
|
-
:
|
8
|
-
:
|
7
|
+
prepares: Prepares::ModelPrepare.new,
|
8
|
+
reviews: RemoveUnusedMethodsInModelsReview.new({'except_methods' => ["*#set_cache"]})
|
9
9
|
) }
|
10
10
|
|
11
11
|
context "private" do
|
@@ -86,7 +86,7 @@ module RailsBestPractices
|
|
86
86
|
content =<<-EOF
|
87
87
|
class Post < ActiveRecord::Base
|
88
88
|
def fetch
|
89
|
-
get(:
|
89
|
+
get(position: 'first')
|
90
90
|
end
|
91
91
|
private
|
92
92
|
def get(options={}); end
|
@@ -159,7 +159,7 @@ module RailsBestPractices
|
|
159
159
|
it "should not remove unused method with validation condition" do
|
160
160
|
content =<<-EOF
|
161
161
|
class Post < ActiveRecord::Base
|
162
|
-
validates_uniqueness_of :login, :
|
162
|
+
validates_uniqueness_of :login, if: :email_blank?
|
163
163
|
private
|
164
164
|
def email_blank?; end
|
165
165
|
end
|
@@ -173,7 +173,7 @@ module RailsBestPractices
|
|
173
173
|
it "should not remove unused method with aasm" do
|
174
174
|
content =<<-EOF
|
175
175
|
class Post < ActiveRecord::Base
|
176
|
-
aasm_state :accepted, :
|
176
|
+
aasm_state :accepted, enter: [:update_datetime]
|
177
177
|
private
|
178
178
|
def update_datetime; end
|
179
179
|
end
|
@@ -383,7 +383,7 @@ module RailsBestPractices
|
|
383
383
|
it "should not remove unused named_scope" do
|
384
384
|
content =<<-EOF
|
385
385
|
class Post < ActiveRecord::Base
|
386
|
-
named_scope :active, :
|
386
|
+
named_scope :active, conditions: {active: true}
|
387
387
|
end
|
388
388
|
EOF
|
389
389
|
runner.prepare("app/models/post.rb", content)
|
@@ -403,7 +403,7 @@ module RailsBestPractices
|
|
403
403
|
it "should remove unused named_scope" do
|
404
404
|
content =<<-EOF
|
405
405
|
class Post < ActiveRecord::Base
|
406
|
-
named_scope :active, :
|
406
|
+
named_scope :active, conditions: {active: true}
|
407
407
|
end
|
408
408
|
EOF
|
409
409
|
runner.prepare("app/models/post.rb", content)
|
@@ -418,7 +418,7 @@ module RailsBestPractices
|
|
418
418
|
it "should not remove unused scope" do
|
419
419
|
content =<<-EOF
|
420
420
|
class Post < ActiveRecord::Base
|
421
|
-
scope :active, where(:
|
421
|
+
scope :active, where(active: true)
|
422
422
|
end
|
423
423
|
EOF
|
424
424
|
runner.prepare("app/models/post.rb", content)
|
@@ -438,7 +438,7 @@ module RailsBestPractices
|
|
438
438
|
it "should remove unused named_scope" do
|
439
439
|
content =<<-EOF
|
440
440
|
class Post < ActiveRecord::Base
|
441
|
-
scope :active, where(:
|
441
|
+
scope :active, where(active: true)
|
442
442
|
end
|
443
443
|
EOF
|
444
444
|
runner.prepare("app/models/post.rb", content)
|
@@ -540,7 +540,7 @@ module RailsBestPractices
|
|
540
540
|
content =<<-EOF
|
541
541
|
class Post < ActiveRecord::Base
|
542
542
|
def to_xml(options = {})
|
543
|
-
super options.merge(:
|
543
|
+
super options.merge(exclude: :visible, methods: [:is_discussion_conversation])
|
544
544
|
end
|
545
545
|
|
546
546
|
def is_discussion_conversation; end
|
@@ -669,6 +669,21 @@ module RailsBestPractices
|
|
669
669
|
runner.should have(0).errors
|
670
670
|
end
|
671
671
|
end
|
672
|
+
|
673
|
+
it "should not skip :call as call message" do
|
674
|
+
content =<<-EOF
|
675
|
+
module DateRange
|
676
|
+
RANGES = lambda {
|
677
|
+
last_month = {
|
678
|
+
'range' => lambda { [date_from_time.(31.days.ago), date_from_time.(Time.now)] },
|
679
|
+
'value' => 'last_month',
|
680
|
+
'label' => 'Last month'}
|
681
|
+
}[]
|
682
|
+
end
|
683
|
+
EOF
|
684
|
+
runner.prepare("app/mixins/date_range.rb", content)
|
685
|
+
runner.review("app/mixins/date_range.rb", content)
|
686
|
+
end
|
672
687
|
end
|
673
688
|
end
|
674
689
|
end
|
data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
module RailsBestPractices
|
4
4
|
module Reviews
|
5
5
|
describe ReplaceComplexCreationWithFactoryMethodReview do
|
6
|
-
let(:runner) { Core::Runner.new(:
|
6
|
+
let(:runner) { Core::Runner.new(reviews: ReplaceComplexCreationWithFactoryMethodReview.new) }
|
7
7
|
|
8
8
|
it "should replace complex creation with factory method" do
|
9
9
|
content = <<-EOF
|
@@ -26,7 +26,7 @@ module RailsBestPractices
|
|
26
26
|
EOF
|
27
27
|
runner.review('app/controllers/invoices_controller.rb', content)
|
28
28
|
runner.should have(1).errors
|
29
|
-
runner.errors[0].to_s.should == "app/controllers/invoices_controller.rb:
|
29
|
+
runner.errors[0].to_s.should == "app/controllers/invoices_controller.rb:2 - replace complex creation with factory method (@invoice attribute_assignment_count > 2)"
|
30
30
|
end
|
31
31
|
|
32
32
|
it "should not replace complex creation with factory method with simple creation" do
|
@@ -63,7 +63,7 @@ module RailsBestPractices
|
|
63
63
|
end
|
64
64
|
end
|
65
65
|
EOF
|
66
|
-
runner = Core::Runner.new(:
|
66
|
+
runner = Core::Runner.new(reviews: ReplaceComplexCreationWithFactoryMethodReview.new('attribute_assignment_count' => 5))
|
67
67
|
runner.review('app/controllers/invoices_controller.rb', content)
|
68
68
|
runner.should have(0).errors
|
69
69
|
end
|
data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
module RailsBestPractices
|
4
4
|
module Reviews
|
5
5
|
describe ReplaceInstanceVariableWithLocalVariableReview do
|
6
|
-
let(:runner) { Core::Runner.new(:
|
6
|
+
let(:runner) { Core::Runner.new(reviews: ReplaceInstanceVariableWithLocalVariableReview.new) }
|
7
7
|
|
8
8
|
it "should replace instance variable with local varialbe" do
|
9
9
|
content = <<-EOF
|
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
module RailsBestPractices
|
4
4
|
module Reviews
|
5
5
|
describe RestrictAutoGeneratedRoutesReview do
|
6
|
-
let(:runner) { Core::Runner.new(:
|
6
|
+
let(:runner) { Core::Runner.new(prepares: Prepares::ControllerPrepare.new, reviews: RestrictAutoGeneratedRoutesReview.new) }
|
7
7
|
|
8
8
|
describe "resources" do
|
9
9
|
before :each do
|
@@ -29,13 +29,13 @@ module RailsBestPractices
|
|
29
29
|
EOF
|
30
30
|
runner.review('config/routes.rb', content)
|
31
31
|
runner.should have(1).errors
|
32
|
-
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes (:
|
32
|
+
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes posts (only: [:show, :new, :create, :edit, :update, :destroy])"
|
33
33
|
end
|
34
34
|
|
35
35
|
it "should not restrict auto-generated routes with only" do
|
36
36
|
content =<<-EOF
|
37
37
|
ActionController::Routing::Routes.draw do |map|
|
38
|
-
map.resources :posts, :
|
38
|
+
map.resources :posts, only: [:show, :new, :create, :edit, :update, :destroy]
|
39
39
|
end
|
40
40
|
EOF
|
41
41
|
runner.review('config/routes.rb', content)
|
@@ -45,27 +45,27 @@ module RailsBestPractices
|
|
45
45
|
it "should not restrict auto-generated routes with except" do
|
46
46
|
content =<<-EOF
|
47
47
|
ActionController::Routing::Routes.draw do |map|
|
48
|
-
map.resources :posts, :
|
48
|
+
map.resources :posts, except: :index
|
49
49
|
end
|
50
50
|
EOF
|
51
51
|
runner.review('config/routes.rb', content)
|
52
52
|
runner.should have(0).errors
|
53
53
|
end
|
54
54
|
|
55
|
-
it "should not restrict auto-generated routes with :
|
55
|
+
it "should not restrict auto-generated routes with only: :none" do
|
56
56
|
content =<<-EOF
|
57
57
|
ActionController::Routing::Routes.draw do |map|
|
58
|
-
map.resources :posts, :
|
58
|
+
map.resources :posts, only: :none
|
59
59
|
end
|
60
60
|
EOF
|
61
61
|
runner.review('config/routes.rb', content)
|
62
62
|
runner.should have(0).errors
|
63
63
|
end
|
64
64
|
|
65
|
-
it "should not restrict auto-generated routes with :
|
65
|
+
it "should not restrict auto-generated routes with except: :all" do
|
66
66
|
content =<<-EOF
|
67
67
|
ActionController::Routing::Routes.draw do |map|
|
68
|
-
map.resources :posts, :
|
68
|
+
map.resources :posts, except: :all
|
69
69
|
end
|
70
70
|
EOF
|
71
71
|
runner.review('config/routes.rb', content)
|
@@ -76,12 +76,12 @@ module RailsBestPractices
|
|
76
76
|
it "should restrict auto-generated routes" do
|
77
77
|
content =<<-EOF
|
78
78
|
ActionController::Routing::Routes.draw do |map|
|
79
|
-
map.resources :articles, :
|
79
|
+
map.resources :articles, controller: "posts"
|
80
80
|
end
|
81
81
|
EOF
|
82
82
|
runner.review('config/routes.rb', content)
|
83
83
|
runner.should have(1).errors
|
84
|
-
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes (:
|
84
|
+
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes articles (only: [:show, :new, :create, :edit, :update, :destroy])"
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
@@ -111,13 +111,13 @@ module RailsBestPractices
|
|
111
111
|
EOF
|
112
112
|
runner.review('config/routes.rb', content)
|
113
113
|
runner.should have(1).errors
|
114
|
-
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes (:
|
114
|
+
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes posts (only: [:show, :new, :create, :edit, :update, :destroy])"
|
115
115
|
end
|
116
116
|
|
117
117
|
it "should not restrict auto-generated routes with only" do
|
118
118
|
content =<<-EOF
|
119
119
|
ActionController::Routing::Routes.draw do |map|
|
120
|
-
map.resources :posts, :
|
120
|
+
map.resources :posts, only: [:show, :new, :create, :edit, :update, :destroy] do |post|
|
121
121
|
post.resources :comments
|
122
122
|
end
|
123
123
|
end
|
@@ -129,7 +129,7 @@ module RailsBestPractices
|
|
129
129
|
it "should not restrict auto-generated routes with except" do
|
130
130
|
content =<<-EOF
|
131
131
|
ActionController::Routing::Routes.draw do |map|
|
132
|
-
map.resources :posts, :
|
132
|
+
map.resources :posts, except: :index do |post|
|
133
133
|
post.resources :comments
|
134
134
|
end
|
135
135
|
end
|
@@ -149,13 +149,13 @@ module RailsBestPractices
|
|
149
149
|
EOF
|
150
150
|
runner.review('config/routes.rb', content)
|
151
151
|
runner.should have(1).errors
|
152
|
-
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes (:
|
152
|
+
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes posts (only: [:show, :new, :create, :edit, :update, :destroy])"
|
153
153
|
end
|
154
154
|
|
155
155
|
it "should not restrict auto-generated routes with only" do
|
156
156
|
content =<<-EOF
|
157
157
|
RailsBestPracticesCom::Application.routes.draw do
|
158
|
-
resources :posts, :
|
158
|
+
resources :posts, only: %w(show new create edit update destroy)
|
159
159
|
end
|
160
160
|
EOF
|
161
161
|
runner.review('config/routes.rb', content)
|
@@ -165,7 +165,7 @@ module RailsBestPractices
|
|
165
165
|
it "should not restrict auto-generated routes with except" do
|
166
166
|
content =<<-EOF
|
167
167
|
RailsBestPracticesCom::Application.routes.draw do
|
168
|
-
resources :posts, :
|
168
|
+
resources :posts, except: :index
|
169
169
|
end
|
170
170
|
EOF
|
171
171
|
runner.review('config/routes.rb', content)
|
@@ -176,12 +176,12 @@ module RailsBestPractices
|
|
176
176
|
it "should restrict auto-generated routes" do
|
177
177
|
content =<<-EOF
|
178
178
|
RailsBestPracticesCom::Application.routes.draw do
|
179
|
-
resources :articles, :
|
179
|
+
resources :articles, controller: "posts"
|
180
180
|
end
|
181
181
|
EOF
|
182
182
|
runner.review('config/routes.rb', content)
|
183
183
|
runner.should have(1).errors
|
184
|
-
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes (:
|
184
|
+
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes articles (only: [:show, :new, :create, :edit, :update, :destroy])"
|
185
185
|
end
|
186
186
|
end
|
187
187
|
|
@@ -208,7 +208,7 @@ module RailsBestPractices
|
|
208
208
|
EOF
|
209
209
|
runner.review('config/routes.rb', content)
|
210
210
|
runner.should have(1).errors
|
211
|
-
runner.errors[0].to_s.should == "config/routes.rb:3 - restrict auto-generated routes (:
|
211
|
+
runner.errors[0].to_s.should == "config/routes.rb:3 - restrict auto-generated routes admin/comments (only: [:show, :new, :create, :edit, :update, :destroy])"
|
212
212
|
end
|
213
213
|
end
|
214
214
|
|
@@ -238,13 +238,13 @@ module RailsBestPractices
|
|
238
238
|
EOF
|
239
239
|
runner.review('config/routes.rb', content)
|
240
240
|
runner.should have(1).errors
|
241
|
-
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes (:
|
241
|
+
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes posts (only: [:show, :new, :create, :edit, :update, :destroy])"
|
242
242
|
end
|
243
243
|
|
244
244
|
it "should not restrict auto-generated routes with only" do
|
245
245
|
content =<<-EOF
|
246
246
|
RailsBestPracticesCom::Application.routes.draw do
|
247
|
-
resources :posts, :
|
247
|
+
resources :posts, only: %w(show new create edit update destroy) do
|
248
248
|
resources :comments
|
249
249
|
end
|
250
250
|
end
|
@@ -256,7 +256,7 @@ module RailsBestPractices
|
|
256
256
|
it "should not restrict auto-generated routes with except" do
|
257
257
|
content =<<-EOF
|
258
258
|
RailsBestPracticesCom::Application.routes.draw do
|
259
|
-
resources :posts, :
|
259
|
+
resources :posts, except: :index do
|
260
260
|
resources :comments
|
261
261
|
end
|
262
262
|
end
|
@@ -291,13 +291,13 @@ module RailsBestPractices
|
|
291
291
|
EOF
|
292
292
|
runner.review('config/routes.rb', content)
|
293
293
|
runner.should have(1).errors
|
294
|
-
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes (:
|
294
|
+
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes account (only: [:show, :new, :create, :edit, :update])"
|
295
295
|
end
|
296
296
|
|
297
297
|
it "should not restrict auto-generated routes with only" do
|
298
298
|
content =<<-EOF
|
299
299
|
ActionController::Routing::Routes.draw do |map|
|
300
|
-
map.resource :account, :
|
300
|
+
map.resource :account, only: %w(show new create edit update)
|
301
301
|
end
|
302
302
|
EOF
|
303
303
|
runner.review('config/routes.rb', content)
|
@@ -307,7 +307,7 @@ module RailsBestPractices
|
|
307
307
|
it "should not restrict auto-generated routes with except" do
|
308
308
|
content =<<-EOF
|
309
309
|
ActionController::Routing::Routes.draw do |map|
|
310
|
-
map.resource :account, :
|
310
|
+
map.resource :account, except: :destroy
|
311
311
|
end
|
312
312
|
EOF
|
313
313
|
runner.review('config/routes.rb', content)
|
@@ -324,13 +324,13 @@ module RailsBestPractices
|
|
324
324
|
EOF
|
325
325
|
runner.review('config/routes.rb', content)
|
326
326
|
runner.should have(1).errors
|
327
|
-
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes (:
|
327
|
+
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes account (only: [:show, :new, :create, :edit, :update])"
|
328
328
|
end
|
329
329
|
|
330
330
|
it "should not restrict auto-generated routes with only" do
|
331
331
|
content =<<-EOF
|
332
332
|
ActionController::Routing::Routes.draw do |map|
|
333
|
-
map.resource :account, :
|
333
|
+
map.resource :account, only: %w(show new create edit update)
|
334
334
|
end
|
335
335
|
EOF
|
336
336
|
runner.review('config/routes.rb', content)
|
@@ -340,7 +340,7 @@ module RailsBestPractices
|
|
340
340
|
it "should not restrict auto-generated routes with except" do
|
341
341
|
content =<<-EOF
|
342
342
|
ActionController::Routing::Routes.draw do |map|
|
343
|
-
map.resource :account, :
|
343
|
+
map.resource :account, except: :destroy
|
344
344
|
end
|
345
345
|
EOF
|
346
346
|
runner.review('config/routes.rb', content)
|
@@ -3,12 +3,12 @@ require 'spec_helper'
|
|
3
3
|
module RailsBestPractices
|
4
4
|
module Reviews
|
5
5
|
describe SimplifyRenderInControllersReview do
|
6
|
-
let(:runner) { Core::Runner.new(:
|
6
|
+
let(:runner) { Core::Runner.new(reviews: SimplifyRenderInControllersReview.new) }
|
7
7
|
|
8
8
|
it "should simplify render action view" do
|
9
9
|
content =<<-EOF
|
10
10
|
def edit
|
11
|
-
render :
|
11
|
+
render action: :edit
|
12
12
|
end
|
13
13
|
EOF
|
14
14
|
runner.review("app/controllers/posts_controller.rb", content)
|
@@ -19,7 +19,7 @@ module RailsBestPractices
|
|
19
19
|
it "should simplify render actions's template" do
|
20
20
|
content =<<-EOF
|
21
21
|
def edit
|
22
|
-
render :
|
22
|
+
render template: 'books/edit'
|
23
23
|
end
|
24
24
|
EOF
|
25
25
|
runner.review("app/controllers/posts_controller.rb", content)
|
@@ -30,7 +30,7 @@ module RailsBestPractices
|
|
30
30
|
it "should simplify render an arbitrary file" do
|
31
31
|
content =<<-EOF
|
32
32
|
def edit
|
33
|
-
render :
|
33
|
+
render file: '/path/to/rails/app/views/books/edit'
|
34
34
|
end
|
35
35
|
EOF
|
36
36
|
runner.review("app/controllers/posts_controller.rb", content)
|
@@ -3,11 +3,11 @@ require 'spec_helper'
|
|
3
3
|
module RailsBestPractices
|
4
4
|
module Reviews
|
5
5
|
describe SimplifyRenderInViewsReview do
|
6
|
-
let(:runner) { Core::Runner.new(:
|
6
|
+
let(:runner) { Core::Runner.new(reviews: SimplifyRenderInViewsReview.new) }
|
7
7
|
|
8
8
|
it "should simplify render simple partial" do
|
9
9
|
content =<<-EOF
|
10
|
-
<%= render :
|
10
|
+
<%= render partial: 'sidebar' %>
|
11
11
|
EOF
|
12
12
|
runner.review('app/views/posts/index.html.erb', content)
|
13
13
|
runner.should have(1).errors
|
@@ -16,7 +16,7 @@ module RailsBestPractices
|
|
16
16
|
|
17
17
|
it "should simplify render partial with object" do
|
18
18
|
content =<<-EOF
|
19
|
-
<%= render :
|
19
|
+
<%= render partial: 'post', object: @post %>
|
20
20
|
EOF
|
21
21
|
runner.review('app/views/posts/index.html.erb', content)
|
22
22
|
runner.should have(1).errors
|
@@ -25,7 +25,7 @@ module RailsBestPractices
|
|
25
25
|
|
26
26
|
it "should simplify render partial with collection" do
|
27
27
|
content =<<-EOF
|
28
|
-
<%= render :
|
28
|
+
<%= render partial: 'posts', collection: @posts %>
|
29
29
|
EOF
|
30
30
|
runner.review('app/views/posts/index.html.erb', content)
|
31
31
|
runner.should have(1).errors
|
@@ -34,7 +34,7 @@ module RailsBestPractices
|
|
34
34
|
|
35
35
|
it "should simplify render partial with local variables" do
|
36
36
|
content =<<-EOF
|
37
|
-
<%= render :
|
37
|
+
<%= render partial: 'comment', locals: { parent: post } %>
|
38
38
|
EOF
|
39
39
|
runner.review('app/views/posts/index.html.erb', content)
|
40
40
|
runner.should have(1).errors
|
@@ -68,7 +68,7 @@ module RailsBestPractices
|
|
68
68
|
|
69
69
|
it "should not simplify render partial with local variables" do
|
70
70
|
content =<<-EOF
|
71
|
-
<%= render 'comment', :
|
71
|
+
<%= render 'comment', parent: post %>
|
72
72
|
EOF
|
73
73
|
runner.review('app/views/posts/index.html.erb', content)
|
74
74
|
runner.should have(0).errors
|
@@ -76,7 +76,15 @@ module RailsBestPractices
|
|
76
76
|
|
77
77
|
it "should not simplify render partial with complex partial" do
|
78
78
|
content =<<-EOF
|
79
|
-
<%= render :
|
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
|
+
|
85
|
+
it "should not simplify render partial with layout option" do
|
86
|
+
content =<<-EOF
|
87
|
+
<%= render partial: 'post', layout: 'post' %>
|
80
88
|
EOF
|
81
89
|
runner.review('app/views/posts/index.html.erb', content)
|
82
90
|
runner.should have(0).errors
|