rails_best_practices 1.19.0 → 1.19.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Guardfile +2 -2
- data/Rakefile +8 -8
- data/bin/rails_best_practices +1 -1
- data/lib/rails_best_practices/analyzer.rb +45 -45
- data/lib/rails_best_practices/command.rb +40 -40
- data/lib/rails_best_practices/core/check.rb +15 -15
- data/lib/rails_best_practices/core/klasses.rb +1 -1
- data/lib/rails_best_practices/core/methods.rb +7 -7
- data/lib/rails_best_practices/core/model_associations.rb +3 -3
- data/lib/rails_best_practices/core/modules.rb +1 -1
- data/lib/rails_best_practices/core/routes.rb +2 -2
- data/lib/rails_best_practices/core/runner.rb +3 -3
- data/lib/rails_best_practices/core_ext/erubis.rb +4 -4
- data/lib/rails_best_practices/lexicals/remove_tab_check.rb +2 -2
- data/lib/rails_best_practices/lexicals/remove_trailing_whitespace_check.rb +2 -2
- data/lib/rails_best_practices/prepares/config_prepare.rb +1 -1
- data/lib/rails_best_practices/prepares/controller_prepare.rb +8 -8
- data/lib/rails_best_practices/prepares/helper_prepare.rb +1 -1
- data/lib/rails_best_practices/prepares/initializer_prepare.rb +4 -4
- data/lib/rails_best_practices/prepares/mailer_prepare.rb +1 -1
- data/lib/rails_best_practices/prepares/model_prepare.rb +19 -19
- data/lib/rails_best_practices/prepares/route_prepare.rb +34 -34
- data/lib/rails_best_practices/prepares/schema_prepare.rb +1 -1
- 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 +7 -7
- data/lib/rails_best_practices/reviews/check_save_return_value_review.rb +1 -1
- data/lib/rails_best_practices/reviews/default_scope_is_evil_review.rb +3 -3
- data/lib/rails_best_practices/reviews/dry_bundler_in_capistrano_review.rb +3 -3
- data/lib/rails_best_practices/reviews/hash_syntax_review.rb +1 -1
- data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +6 -6
- data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +2 -2
- data/lib/rails_best_practices/reviews/law_of_demeter_review.rb +4 -4
- data/lib/rails_best_practices/reviews/move_code_into_controller_review.rb +3 -3
- data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +2 -2
- 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 +2 -2
- 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 +4 -4
- data/lib/rails_best_practices/reviews/not_rescue_exception_review.rb +2 -2
- data/lib/rails_best_practices/reviews/not_use_default_route_review.rb +4 -4
- data/lib/rails_best_practices/reviews/not_use_time_ago_in_words_review.rb +3 -3
- data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +2 -2
- data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +9 -9
- data/lib/rails_best_practices/reviews/remove_empty_helpers_review.rb +3 -3
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb +11 -11
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_helpers_review.rb +1 -1
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +8 -7
- data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +2 -2
- data/lib/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review.rb +2 -2
- data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +22 -22
- data/lib/rails_best_practices/reviews/review.rb +2 -2
- data/lib/rails_best_practices/reviews/simplify_render_in_controllers_review.rb +3 -3
- data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +4 -4
- data/lib/rails_best_practices/reviews/use_before_filter_review.rb +2 -2
- data/lib/rails_best_practices/reviews/use_model_association_review.rb +2 -2
- data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +2 -2
- data/lib/rails_best_practices/reviews/use_observer_review.rb +3 -3
- data/lib/rails_best_practices/reviews/use_parentheses_in_method_def_review.rb +1 -1
- data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +3 -3
- data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +2 -2
- data/lib/rails_best_practices/reviews/use_scope_access_review.rb +5 -5
- data/lib/rails_best_practices/reviews/use_turbo_sprockets_rails3_review.rb +4 -4
- data/lib/rails_best_practices/version.rb +1 -1
- data/rails_best_practices.gemspec +24 -24
- data/spec/rails_best_practices/analyzer_spec.rb +43 -43
- data/spec/rails_best_practices/core/check_spec.rb +2 -2
- data/spec/rails_best_practices/core/checks_loader_spec.rb +4 -4
- data/spec/rails_best_practices/core/error_spec.rb +16 -16
- data/spec/rails_best_practices/core/except_methods_spec.rb +15 -15
- data/spec/rails_best_practices/core/gems_spec.rb +9 -9
- data/spec/rails_best_practices/core/klasses_spec.rb +17 -17
- data/spec/rails_best_practices/core/methods_spec.rb +28 -28
- data/spec/rails_best_practices/core/model_associations_spec.rb +10 -10
- data/spec/rails_best_practices/core/model_attributes_spec.rb +10 -10
- data/spec/rails_best_practices/core/modules_spec.rb +10 -10
- data/spec/rails_best_practices/core/routes_spec.rb +10 -10
- data/spec/rails_best_practices/core/runner_spec.rb +4 -4
- data/spec/rails_best_practices/core_ext/erubis_spec.rb +7 -7
- data/spec/rails_best_practices/lexicals/long_line_check_spec.rb +7 -7
- data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +6 -6
- data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +6 -6
- data/spec/rails_best_practices/prepares/config_prepare_spec.rb +3 -3
- data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +26 -26
- data/spec/rails_best_practices/prepares/gemfile_prepare_spec.rb +3 -3
- data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +6 -6
- data/spec/rails_best_practices/prepares/initializer_prepare_spec.rb +5 -5
- data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +2 -2
- data/spec/rails_best_practices/prepares/model_prepare_spec.rb +108 -108
- data/spec/rails_best_practices/prepares/route_prepare_spec.rb +77 -77
- data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +8 -8
- data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +10 -10
- data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +27 -27
- data/spec/rails_best_practices/reviews/check_destroy_return_value_review_spec.rb +11 -11
- data/spec/rails_best_practices/reviews/check_save_return_value_review_spec.rb +17 -17
- data/spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +4 -4
- data/spec/rails_best_practices/reviews/hash_syntax_review_spec.rb +8 -8
- data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +10 -10
- 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 +16 -16
- data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +4 -4
- data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +13 -13
- data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +5 -5
- data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +10 -10
- data/spec/rails_best_practices/reviews/not_rescue_exception_review_spec.rb +8 -8
- data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +4 -4
- data/spec/rails_best_practices/reviews/not_use_time_ago_in_words_review_spec.rb +11 -11
- data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +16 -16
- data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +5 -5
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +33 -33
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +106 -106
- data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +5 -5
- data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +8 -8
- data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +29 -29
- data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +15 -15
- data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +15 -15
- data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +29 -29
- data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/use_parentheses_in_method_def_review_spec.rb +5 -5
- data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +28 -28
- data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +9 -9
- data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +23 -23
- data/spec/rails_best_practices/reviews/use_turbo_sprockets_rails3_review_spec.rb +6 -6
- data/spec/spec_helper.rb +1 -1
- metadata +7 -7
@@ -5,7 +5,7 @@ module RailsBestPractices
|
|
5
5
|
describe SchemaPrepare do
|
6
6
|
let(:runner) { Core::Runner.new(prepares: SchemaPrepare.new) }
|
7
7
|
|
8
|
-
it
|
8
|
+
it 'should parse model attributes' do
|
9
9
|
content =<<-EOF
|
10
10
|
ActiveRecord::Schema.define(version: 20110319172136) do
|
11
11
|
create_table "posts", force: true do |t|
|
@@ -18,14 +18,14 @@ module RailsBestPractices
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
EOF
|
21
|
-
runner.prepare(
|
21
|
+
runner.prepare('db/schema.rb', content)
|
22
22
|
model_attributes = Prepares.model_attributes
|
23
|
-
expect(model_attributes.get_attribute_type(
|
24
|
-
expect(model_attributes.get_attribute_type(
|
25
|
-
expect(model_attributes.get_attribute_type(
|
26
|
-
expect(model_attributes.get_attribute_type(
|
27
|
-
expect(model_attributes.get_attribute_type(
|
28
|
-
expect(model_attributes.get_attribute_type(
|
23
|
+
expect(model_attributes.get_attribute_type('Post', 'title')).to eq('string')
|
24
|
+
expect(model_attributes.get_attribute_type('Post', 'body')).to eq('text')
|
25
|
+
expect(model_attributes.get_attribute_type('Post', 'created_at')).to eq('datetime')
|
26
|
+
expect(model_attributes.get_attribute_type('Post', 'user_id')).to eq('integer')
|
27
|
+
expect(model_attributes.get_attribute_type('Post', 'comments_count')).to eq('integer')
|
28
|
+
expect(model_attributes.get_attribute_type('Post', 'published')).to eq('boolean')
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
@@ -5,7 +5,7 @@ module RailsBestPractices
|
|
5
5
|
describe AddModelVirtualAttributeReview do
|
6
6
|
let(:runner) { Core::Runner.new(reviews: AddModelVirtualAttributeReview.new) }
|
7
7
|
|
8
|
-
it
|
8
|
+
it 'should add model virtual attribute' do
|
9
9
|
content = <<-EOF
|
10
10
|
class UsersController < ApplicationController
|
11
11
|
def create
|
@@ -18,10 +18,10 @@ module RailsBestPractices
|
|
18
18
|
EOF
|
19
19
|
runner.review('app/controllers/users_controller.rb', content)
|
20
20
|
expect(runner.errors.size).to eq(1)
|
21
|
-
expect(runner.errors[0].to_s).to eq(
|
21
|
+
expect(runner.errors[0].to_s).to eq('app/controllers/users_controller.rb:2 - add model virtual attribute (for @user)')
|
22
22
|
end
|
23
23
|
|
24
|
-
it
|
24
|
+
it 'should add model virtual attribute with local assignment' do
|
25
25
|
content = <<-EOF
|
26
26
|
class UsersController < ApplicationController
|
27
27
|
def create
|
@@ -34,10 +34,10 @@ module RailsBestPractices
|
|
34
34
|
EOF
|
35
35
|
runner.review('app/controllers/users_controller.rb', content)
|
36
36
|
expect(runner.errors.size).to eq(1)
|
37
|
-
expect(runner.errors[0].to_s).to eq(
|
37
|
+
expect(runner.errors[0].to_s).to eq('app/controllers/users_controller.rb:2 - add model virtual attribute (for user)')
|
38
38
|
end
|
39
39
|
|
40
|
-
it
|
40
|
+
it 'should not add model virtual attribute with differen param' do
|
41
41
|
content = <<-EOF
|
42
42
|
class UsersController < ApplicationController
|
43
43
|
def create
|
@@ -52,7 +52,7 @@ module RailsBestPractices
|
|
52
52
|
expect(runner.errors.size).to eq(0)
|
53
53
|
end
|
54
54
|
|
55
|
-
it
|
55
|
+
it 'should not add model virtual attribute with read' do
|
56
56
|
content = <<-EOF
|
57
57
|
class UsersController < ApplicationController
|
58
58
|
def show
|
@@ -68,7 +68,7 @@ module RailsBestPractices
|
|
68
68
|
expect(runner.errors.size).to eq(0)
|
69
69
|
end
|
70
70
|
|
71
|
-
it
|
71
|
+
it 'should add model virtual attribute with two dimension params' do
|
72
72
|
content = <<-EOF
|
73
73
|
class UsersController < ApplicationController
|
74
74
|
def create
|
@@ -81,10 +81,10 @@ module RailsBestPractices
|
|
81
81
|
EOF
|
82
82
|
runner.review('app/controllers/users_controller.rb', content)
|
83
83
|
expect(runner.errors.size).to eq(1)
|
84
|
-
expect(runner.errors[0].to_s).to eq(
|
84
|
+
expect(runner.errors[0].to_s).to eq('app/controllers/users_controller.rb:2 - add model virtual attribute (for @user)')
|
85
85
|
end
|
86
86
|
|
87
|
-
it
|
87
|
+
it 'should no add model virtual attribute with two dimension params' do
|
88
88
|
content = <<-EOF
|
89
89
|
class UsersController < ApplicationController
|
90
90
|
def create
|
@@ -99,7 +99,7 @@ module RailsBestPractices
|
|
99
99
|
expect(runner.errors.size).to eq(0)
|
100
100
|
end
|
101
101
|
|
102
|
-
it
|
102
|
+
it 'should not check ignored files' do
|
103
103
|
runner = Core::Runner.new(reviews: AddModelVirtualAttributeReview.new(ignored_files: /user/))
|
104
104
|
content = <<-EOF
|
105
105
|
class UsersController < ApplicationController
|
@@ -5,7 +5,7 @@ module RailsBestPractices
|
|
5
5
|
describe AlwaysAddDbIndexReview do
|
6
6
|
let(:runner) { Core::Runner.new(reviews: AlwaysAddDbIndexReview.new) }
|
7
7
|
|
8
|
-
it
|
8
|
+
it 'should always add db index' do
|
9
9
|
content = <<-EOF
|
10
10
|
ActiveRecord::Schema.define(version: 20100603080629) do
|
11
11
|
create_table "comments", force: true do |t|
|
@@ -22,11 +22,11 @@ module RailsBestPractices
|
|
22
22
|
runner.review('db/schema.rb', content)
|
23
23
|
runner.after_review
|
24
24
|
expect(runner.errors.size).to eq(2)
|
25
|
-
expect(runner.errors[0].to_s).to eq(
|
26
|
-
expect(runner.errors[1].to_s).to eq(
|
25
|
+
expect(runner.errors[0].to_s).to eq('db/schema.rb:2 - always add db index (comments => [post_id])')
|
26
|
+
expect(runner.errors[1].to_s).to eq('db/schema.rb:2 - always add db index (comments => [user_id])')
|
27
27
|
end
|
28
28
|
|
29
|
-
it
|
29
|
+
it 'should always add db index with polymorphic foreign key' do
|
30
30
|
content = <<-EOF
|
31
31
|
ActiveRecord::Schema.define(version: 20100603080629) do
|
32
32
|
create_table "versions", force: true do |t|
|
@@ -39,10 +39,10 @@ module RailsBestPractices
|
|
39
39
|
runner.review('db/schema.rb', content)
|
40
40
|
runner.after_review
|
41
41
|
expect(runner.errors.size).to eq(1)
|
42
|
-
expect(runner.errors[0].to_s).to eq(
|
42
|
+
expect(runner.errors[0].to_s).to eq('db/schema.rb:2 - always add db index (versions => [versioned_id, versioned_type])')
|
43
43
|
end
|
44
44
|
|
45
|
-
it
|
45
|
+
it 'should always add db index with polymorphic foreign key and _type is defined before _id' do
|
46
46
|
content = <<-EOF
|
47
47
|
ActiveRecord::Schema.define(version: 20100603080629) do
|
48
48
|
create_table "versions", force: true do |t|
|
@@ -55,10 +55,10 @@ module RailsBestPractices
|
|
55
55
|
runner.review('db/schema.rb', content)
|
56
56
|
runner.after_review
|
57
57
|
expect(runner.errors.size).to eq(1)
|
58
|
-
expect(runner.errors[0].to_s).to eq(
|
58
|
+
expect(runner.errors[0].to_s).to eq('db/schema.rb:2 - always add db index (versions => [versioned_id, versioned_type])')
|
59
59
|
end
|
60
60
|
|
61
|
-
it
|
61
|
+
it 'should always add db index with single index, but without polymorphic foreign key' do
|
62
62
|
content = <<-EOF
|
63
63
|
ActiveRecord::Schema.define(version: 20100603080629) do
|
64
64
|
create_table "taggings", force: true do |t|
|
@@ -75,10 +75,10 @@ module RailsBestPractices
|
|
75
75
|
runner.review('db/schema.rb', content)
|
76
76
|
runner.after_review
|
77
77
|
expect(runner.errors.size).to eq(1)
|
78
|
-
expect(runner.errors[0].to_s).to eq(
|
78
|
+
expect(runner.errors[0].to_s).to eq('db/schema.rb:2 - always add db index (taggings => [taggable_id, taggable_type])')
|
79
79
|
end
|
80
80
|
|
81
|
-
it
|
81
|
+
it 'should always add db index with polymorphic foreign key, but without single index' do
|
82
82
|
content = <<-EOF
|
83
83
|
ActiveRecord::Schema.define(version: 20100603080629) do
|
84
84
|
create_table "taggings", force: true do |t|
|
@@ -95,10 +95,10 @@ module RailsBestPractices
|
|
95
95
|
runner.review('db/schema.rb', content)
|
96
96
|
runner.after_review
|
97
97
|
expect(runner.errors.size).to eq(1)
|
98
|
-
expect(runner.errors[0].to_s).to eq(
|
98
|
+
expect(runner.errors[0].to_s).to eq('db/schema.rb:2 - always add db index (taggings => [tag_id])')
|
99
99
|
end
|
100
100
|
|
101
|
-
it
|
101
|
+
it 'should always add db index only _id without non related _type column' do
|
102
102
|
content = <<-EOF
|
103
103
|
ActiveRecord::Schema.define(version: 20100603080629) do
|
104
104
|
create_table "websites", force: true do |t|
|
@@ -114,10 +114,10 @@ module RailsBestPractices
|
|
114
114
|
runner.review('db/schema.rb', content)
|
115
115
|
runner.after_review
|
116
116
|
expect(runner.errors.size).to eq(1)
|
117
|
-
expect(runner.errors[0].to_s).to eq(
|
117
|
+
expect(runner.errors[0].to_s).to eq('db/schema.rb:2 - always add db index (websites => [user_id])')
|
118
118
|
end
|
119
119
|
|
120
|
-
it
|
120
|
+
it 'should not always add db index with column has no id' do
|
121
121
|
content = <<-EOF
|
122
122
|
ActiveRecord::Schema.define(version: 20100603080629) do
|
123
123
|
create_table "comments", force: true do |t|
|
@@ -131,7 +131,7 @@ module RailsBestPractices
|
|
131
131
|
expect(runner.errors.size).to eq(0)
|
132
132
|
end
|
133
133
|
|
134
|
-
it
|
134
|
+
it 'should not always add db index with add_index' do
|
135
135
|
content = <<-EOF
|
136
136
|
ActiveRecord::Schema.define(version: 20100603080629) do
|
137
137
|
create_table "comments", force: true do |t|
|
@@ -153,7 +153,7 @@ module RailsBestPractices
|
|
153
153
|
expect(runner.errors.size).to eq(0)
|
154
154
|
end
|
155
155
|
|
156
|
-
it
|
156
|
+
it 'should not always add db index with t.index' do
|
157
157
|
# e.g. schema_plus creates indices like this https://github.com/lomba/schema_plus
|
158
158
|
content = <<-EOF
|
159
159
|
ActiveRecord::Schema.define(version: 20100603080629) do
|
@@ -171,7 +171,7 @@ module RailsBestPractices
|
|
171
171
|
expect(runner.errors.size).to eq(0)
|
172
172
|
end
|
173
173
|
|
174
|
-
it
|
174
|
+
it 'should not always add db index with only _type column' do
|
175
175
|
content = <<-EOF
|
176
176
|
ActiveRecord::Schema.define(version: 20100603080629) do
|
177
177
|
create_table "versions", force: true do |t|
|
@@ -184,7 +184,7 @@ module RailsBestPractices
|
|
184
184
|
expect(runner.errors.size).to eq(0)
|
185
185
|
end
|
186
186
|
|
187
|
-
it
|
187
|
+
it 'should not always add db index with multi-column index' do
|
188
188
|
content = <<-EOF
|
189
189
|
ActiveRecord::Schema.define(version: 20100603080629) do
|
190
190
|
create_table "versions", force: true do |t|
|
@@ -201,7 +201,7 @@ module RailsBestPractices
|
|
201
201
|
expect(runner.errors.size).to eq(0)
|
202
202
|
end
|
203
203
|
|
204
|
-
it
|
204
|
+
it 'should not always add db index if there is an index contains more columns' do
|
205
205
|
content = <<-EOF
|
206
206
|
ActiveRecord::Schema.define(version: 20100603080629) do
|
207
207
|
create_table "taggings", force: true do |t|
|
@@ -218,7 +218,7 @@ module RailsBestPractices
|
|
218
218
|
expect(runner.errors.size).to eq(0)
|
219
219
|
end
|
220
220
|
|
221
|
-
it
|
221
|
+
it 'should not always add db index if two indexes for polymorphic association' do
|
222
222
|
content =<<-EOF
|
223
223
|
ActiveRecord::Schema.define(version: 20100603080629) do
|
224
224
|
create_table "taggings", force: true do |t|
|
@@ -236,7 +236,7 @@ module RailsBestPractices
|
|
236
236
|
expect(runner.errors.size).to eq(0)
|
237
237
|
end
|
238
238
|
|
239
|
-
it
|
239
|
+
it 'should not always add db index if table does not exist' do
|
240
240
|
content =<<-EOF
|
241
241
|
ActiveRecord::Schema.define(version: 20100603080629) do
|
242
242
|
create_table "comments", force: true do |t|
|
@@ -249,7 +249,7 @@ module RailsBestPractices
|
|
249
249
|
expect(runner.errors.size).to eq(0)
|
250
250
|
end
|
251
251
|
|
252
|
-
it
|
252
|
+
it 'should always add db index if association_name is different to foreign_key' do
|
253
253
|
content =<<-EOF
|
254
254
|
class Comment < ActiveRecord::Base
|
255
255
|
belongs_to :commentor, class_name: "User"
|
@@ -273,10 +273,10 @@ module RailsBestPractices
|
|
273
273
|
runner.review('db/schema.rb', content)
|
274
274
|
runner.after_review
|
275
275
|
expect(runner.errors.size).to eq(1)
|
276
|
-
expect(runner.errors[0].to_s).to eq(
|
276
|
+
expect(runner.errors[0].to_s).to eq('db/schema.rb:2 - always add db index (comments => [commentor_id])')
|
277
277
|
end
|
278
278
|
|
279
|
-
it
|
279
|
+
it 'should not check ignored files' do
|
280
280
|
runner = Core::Runner.new(reviews: AlwaysAddDbIndexReview.new(ignored_files: /db\/schema/))
|
281
281
|
content = <<-EOF
|
282
282
|
ActiveRecord::Schema.define(version: 20100603080629) do
|
@@ -296,7 +296,7 @@ module RailsBestPractices
|
|
296
296
|
expect(runner.errors.size).to eq(0)
|
297
297
|
end
|
298
298
|
|
299
|
-
it
|
299
|
+
it 'should detect index option in column creation' do
|
300
300
|
content = <<-EOF
|
301
301
|
ActiveRecord::Schema.define(version: 20100603080629) do
|
302
302
|
create_table "comments", force: true do |t|
|
@@ -319,8 +319,8 @@ module RailsBestPractices
|
|
319
319
|
runner.review('db/schema.rb', content)
|
320
320
|
runner.after_review
|
321
321
|
expect(runner.errors.size).to eq(2)
|
322
|
-
expect(runner.errors[0].to_s).to eq(
|
323
|
-
expect(runner.errors[1].to_s).to eq(
|
322
|
+
expect(runner.errors[0].to_s).to eq('db/schema.rb:2 - always add db index (comments => [image_id])')
|
323
|
+
expect(runner.errors[1].to_s).to eq('db/schema.rb:2 - always add db index (comments => [link_id])')
|
324
324
|
end
|
325
325
|
end
|
326
326
|
end
|
@@ -5,8 +5,8 @@ module RailsBestPractices
|
|
5
5
|
describe CheckDestroyReturnValueReview do
|
6
6
|
let(:runner) { Core::Runner.new(reviews: CheckDestroyReturnValueReview.new) }
|
7
7
|
|
8
|
-
describe
|
9
|
-
it
|
8
|
+
describe 'check_destroy_return_value' do
|
9
|
+
it 'should warn you if you fail to check the destroy return value' do
|
10
10
|
content =<<-EOF
|
11
11
|
def my_method
|
12
12
|
post = Posts.create do |p|
|
@@ -20,7 +20,7 @@ module RailsBestPractices
|
|
20
20
|
expect(runner.errors[0].to_s).to eq("app/helpers/posts_helper.rb:5 - check 'destroy' return value or use 'destroy!'")
|
21
21
|
end
|
22
22
|
|
23
|
-
it
|
23
|
+
it 'should allow destroy return value if assigned to a var' do
|
24
24
|
content =<<-EOF
|
25
25
|
def my_method
|
26
26
|
post = Posts.create do |p|
|
@@ -33,7 +33,7 @@ module RailsBestPractices
|
|
33
33
|
expect(runner.errors.size).to eq(0)
|
34
34
|
end
|
35
35
|
|
36
|
-
it
|
36
|
+
it 'should allow destroy return value used in if' do
|
37
37
|
content =<<-EOF
|
38
38
|
def my_method
|
39
39
|
post = Posts.create do |p|
|
@@ -50,7 +50,7 @@ module RailsBestPractices
|
|
50
50
|
expect(runner.errors.size).to eq(0)
|
51
51
|
end
|
52
52
|
|
53
|
-
it
|
53
|
+
it 'should allow destroy return value used in elsif' do
|
54
54
|
content =<<-EOF
|
55
55
|
def my_method
|
56
56
|
post = Posts.create do |p|
|
@@ -69,7 +69,7 @@ module RailsBestPractices
|
|
69
69
|
expect(runner.errors.size).to eq(0)
|
70
70
|
end
|
71
71
|
|
72
|
-
it
|
72
|
+
it 'should allow destroy return value used in unless' do
|
73
73
|
content =<<-EOF
|
74
74
|
def my_method
|
75
75
|
unless @post.destroy
|
@@ -81,7 +81,7 @@ module RailsBestPractices
|
|
81
81
|
expect(runner.errors.size).to eq(0)
|
82
82
|
end
|
83
83
|
|
84
|
-
it
|
84
|
+
it 'should allow destroy return value used in if_mod' do
|
85
85
|
content =<<-EOF
|
86
86
|
def my_method
|
87
87
|
post = Posts.create do |p|
|
@@ -94,7 +94,7 @@ module RailsBestPractices
|
|
94
94
|
expect(runner.errors.size).to eq(0)
|
95
95
|
end
|
96
96
|
|
97
|
-
it
|
97
|
+
it 'should allow destroy return value used in unless_mod' do
|
98
98
|
content =<<-EOF
|
99
99
|
def my_method
|
100
100
|
post = Posts.create do |p|
|
@@ -107,7 +107,7 @@ module RailsBestPractices
|
|
107
107
|
expect(runner.errors.size).to eq(0)
|
108
108
|
end
|
109
109
|
|
110
|
-
it
|
110
|
+
it 'should allow destroy return value used in unless with &&' do
|
111
111
|
content =<<-EOF
|
112
112
|
def my_method
|
113
113
|
unless some_method(1) && other_method(2) && @post.destroy
|
@@ -119,7 +119,7 @@ module RailsBestPractices
|
|
119
119
|
expect(runner.errors.size).to eq(0)
|
120
120
|
end
|
121
121
|
|
122
|
-
it
|
122
|
+
it 'should allow destroy!' do
|
123
123
|
content =<<-EOF
|
124
124
|
def my_method
|
125
125
|
post = Posts.create do |p|
|
@@ -133,7 +133,7 @@ module RailsBestPractices
|
|
133
133
|
end
|
134
134
|
end
|
135
135
|
|
136
|
-
it
|
136
|
+
it 'should not check ignored files' do
|
137
137
|
runner = Core::Runner.new(reviews: CheckDestroyReturnValueReview.new(ignored_files: /helpers/))
|
138
138
|
content =<<-EOF
|
139
139
|
def my_method
|
@@ -5,8 +5,8 @@ module RailsBestPractices
|
|
5
5
|
describe CheckSaveReturnValueReview do
|
6
6
|
let(:runner) { Core::Runner.new(reviews: CheckSaveReturnValueReview.new) }
|
7
7
|
|
8
|
-
describe
|
9
|
-
it
|
8
|
+
describe 'check_save_return_value' do
|
9
|
+
it 'should warn you if you fail to check save return value' do
|
10
10
|
content =<<-EOF
|
11
11
|
def my_method
|
12
12
|
post = Posts.new do |p|
|
@@ -20,7 +20,7 @@ module RailsBestPractices
|
|
20
20
|
expect(runner.errors[0].to_s).to eq("app/helpers/posts_helper.rb:5 - check 'save' return value or use 'save!'")
|
21
21
|
end
|
22
22
|
|
23
|
-
it
|
23
|
+
it 'should allow save return value assigned to var' do
|
24
24
|
content =<<-EOF
|
25
25
|
def my_method
|
26
26
|
post = Posts.new do |p|
|
@@ -33,7 +33,7 @@ module RailsBestPractices
|
|
33
33
|
expect(runner.errors.size).to eq(0)
|
34
34
|
end
|
35
35
|
|
36
|
-
it
|
36
|
+
it 'should allow save return value used in if' do
|
37
37
|
content =<<-EOF
|
38
38
|
def my_method
|
39
39
|
post = Posts.new do |p|
|
@@ -50,7 +50,7 @@ module RailsBestPractices
|
|
50
50
|
expect(runner.errors.size).to eq(0)
|
51
51
|
end
|
52
52
|
|
53
|
-
it
|
53
|
+
it 'should allow save return value used in elsif' do
|
54
54
|
content =<<-EOF
|
55
55
|
def my_method
|
56
56
|
post = Posts.new do |p|
|
@@ -69,7 +69,7 @@ module RailsBestPractices
|
|
69
69
|
expect(runner.errors.size).to eq(0)
|
70
70
|
end
|
71
71
|
|
72
|
-
it
|
72
|
+
it 'should allow save return value used in unless' do
|
73
73
|
content =<<-EOF
|
74
74
|
def my_method
|
75
75
|
unless @post.save
|
@@ -81,7 +81,7 @@ module RailsBestPractices
|
|
81
81
|
expect(runner.errors.size).to eq(0)
|
82
82
|
end
|
83
83
|
|
84
|
-
it
|
84
|
+
it 'should allow save return value used in if_mod' do
|
85
85
|
content =<<-EOF
|
86
86
|
def my_method
|
87
87
|
post = Posts.new do |p|
|
@@ -94,7 +94,7 @@ module RailsBestPractices
|
|
94
94
|
expect(runner.errors.size).to eq(0)
|
95
95
|
end
|
96
96
|
|
97
|
-
it
|
97
|
+
it 'should allow save return value used in unless_mod' do
|
98
98
|
content =<<-EOF
|
99
99
|
def my_method
|
100
100
|
post = Posts.new do |p|
|
@@ -107,7 +107,7 @@ module RailsBestPractices
|
|
107
107
|
expect(runner.errors.size).to eq(0)
|
108
108
|
end
|
109
109
|
|
110
|
-
it
|
110
|
+
it 'should allow save return value used in unless with &&' do
|
111
111
|
content =<<-EOF
|
112
112
|
def my_method
|
113
113
|
unless some_method(1) && other_method(2) && @post.save
|
@@ -119,7 +119,7 @@ module RailsBestPractices
|
|
119
119
|
expect(runner.errors.size).to eq(0)
|
120
120
|
end
|
121
121
|
|
122
|
-
it
|
122
|
+
it 'should allow save!' do
|
123
123
|
content =<<-EOF
|
124
124
|
def my_method
|
125
125
|
post = Posts.new do |p|
|
@@ -132,7 +132,7 @@ module RailsBestPractices
|
|
132
132
|
expect(runner.errors.size).to eq(0)
|
133
133
|
end
|
134
134
|
|
135
|
-
it
|
135
|
+
it 'should warn you if you fail to check update_attributes return value' do
|
136
136
|
content =<<-EOF
|
137
137
|
def my_method
|
138
138
|
@post.update_attributes params
|
@@ -143,7 +143,7 @@ module RailsBestPractices
|
|
143
143
|
expect(runner.errors[0].to_s).to eq("app/helpers/posts_helper.rb:2 - check 'update_attributes' return value or use 'update_attributes!'")
|
144
144
|
end
|
145
145
|
|
146
|
-
it
|
146
|
+
it 'should allow update_attributes if return value is checked' do
|
147
147
|
content =<<-EOF
|
148
148
|
def my_method
|
149
149
|
@post.update_attributes(params) or raise "failed to save"
|
@@ -153,7 +153,7 @@ module RailsBestPractices
|
|
153
153
|
expect(runner.errors.size).to eq(0)
|
154
154
|
end
|
155
155
|
|
156
|
-
it
|
156
|
+
it 'is not clever enough to allow update_attributes if value is returned from method' do
|
157
157
|
# This review is not clever enough to do a full liveness analysis
|
158
158
|
# of whether the returned value is used in all cases.
|
159
159
|
content =<<-EOF
|
@@ -177,7 +177,7 @@ module RailsBestPractices
|
|
177
177
|
expect(runner.errors[0].to_s).to eq("app/controllers/posts_controller.rb:12 - check 'update_attributes' return value or use 'update_attributes!'")
|
178
178
|
end
|
179
179
|
|
180
|
-
it
|
180
|
+
it 'should warn you if you use create which is always unsafe' do
|
181
181
|
content =<<-EOF
|
182
182
|
class Post < ActiveRecord::Base
|
183
183
|
end
|
@@ -196,7 +196,7 @@ module RailsBestPractices
|
|
196
196
|
expect(runner.errors[0].to_s).to eq("app/helpers/posts_helper.rb:2 - use 'create!' instead of 'create' as the latter may not always save")
|
197
197
|
end
|
198
198
|
|
199
|
-
it
|
199
|
+
it 'should warn you if you use create with a block which is always unsafe' do
|
200
200
|
content =<<-EOF
|
201
201
|
module Blog
|
202
202
|
class Post < ActiveRecord::Base
|
@@ -224,7 +224,7 @@ module RailsBestPractices
|
|
224
224
|
expect(runner.errors[0].to_s).to eq("app/helpers/blog/posts_helper.rb:4 - use 'create!' instead of 'create' as the latter may not always save")
|
225
225
|
end
|
226
226
|
|
227
|
-
it
|
227
|
+
it 'allows create called on non-model classes' do
|
228
228
|
content =<<-EOF
|
229
229
|
def my_method
|
230
230
|
pk12 = OpenSSL::PKCS12.create(
|
@@ -239,7 +239,7 @@ module RailsBestPractices
|
|
239
239
|
end
|
240
240
|
end
|
241
241
|
|
242
|
-
it
|
242
|
+
it 'should not check ignored files' do
|
243
243
|
runner = Core::Runner.new(reviews: CheckSaveReturnValueReview.new(ignored_files: /helpers/))
|
244
244
|
content =<<-EOF
|
245
245
|
def my_method
|