rails_best_practices 1.9.0 → 1.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +4 -1
- data/Gemfile +0 -3
- data/Gemfile.lock +10 -9
- data/README.md +2 -0
- data/lib/rails_best_practices/analyzer.rb +40 -21
- data/lib/rails_best_practices/core.rb +1 -0
- data/lib/rails_best_practices/core/check.rb +12 -0
- data/lib/rails_best_practices/core/configs.rb +7 -0
- data/lib/rails_best_practices/core/error.rb +2 -1
- data/lib/rails_best_practices/core/routes.rb +11 -5
- data/lib/rails_best_practices/core/runner.rb +6 -2
- data/lib/rails_best_practices/core_ext/sexp.rb +3 -1
- data/lib/rails_best_practices/prepares.rb +5 -0
- data/lib/rails_best_practices/prepares/config_prepare.rb +21 -0
- data/lib/rails_best_practices/prepares/route_prepare.rb +24 -7
- data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +11 -3
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +7 -3
- data/lib/rails_best_practices/version.rb +1 -1
- data/rails_best_practices.gemspec +1 -0
- data/spec/rails_best_practices/analyzer_spec.rb +52 -32
- data/spec/rails_best_practices/core/check_spec.rb +64 -45
- data/spec/rails_best_practices/core/checking_visitor_spec.rb +65 -64
- data/spec/rails_best_practices/core/configs_spec.rb +7 -0
- data/spec/rails_best_practices/core/controllers_spec.rb +4 -2
- data/spec/rails_best_practices/core/error_spec.rb +28 -22
- data/spec/rails_best_practices/core/helpers_spec.rb +4 -2
- data/spec/rails_best_practices/core/klasses_spec.rb +8 -6
- data/spec/rails_best_practices/core/mailers_spec.rb +4 -2
- data/spec/rails_best_practices/core/methods_spec.rb +37 -35
- data/spec/rails_best_practices/core/model_associations_spec.rb +17 -15
- data/spec/rails_best_practices/core/model_attributes_spec.rb +17 -15
- data/spec/rails_best_practices/core/models_spec.rb +4 -2
- data/spec/rails_best_practices/core/modules_spec.rb +21 -19
- data/spec/rails_best_practices/core/nil_spec.rb +23 -21
- data/spec/rails_best_practices/core/routes_spec.rb +21 -14
- data/spec/rails_best_practices/core/runner_spec.rb +17 -15
- data/spec/rails_best_practices/core_ext/sexp_spec.rb +32 -7
- data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +35 -31
- data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +35 -31
- data/spec/rails_best_practices/prepares/config_prepare_spec.rb +24 -0
- data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +122 -123
- data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +36 -32
- data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +12 -8
- data/spec/rails_best_practices/prepares/model_prepare_spec.rb +326 -322
- data/spec/rails_best_practices/prepares/route_prepare_spec.rb +619 -573
- data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +25 -21
- data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +85 -87
- data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +224 -222
- data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +30 -26
- data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +79 -75
- data/spec/rails_best_practices/reviews/keep_finders_on_their_own_model_review_spec.rb +76 -72
- data/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb +138 -140
- data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +38 -34
- data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +22 -18
- data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +57 -53
- data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +62 -63
- data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +51 -50
- data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +140 -136
- data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +48 -44
- data/spec/rails_best_practices/reviews/not_use_times_ago_in_words_review_spec.rb +42 -38
- data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +151 -149
- data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +86 -55
- data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +23 -19
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +290 -286
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +76 -72
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +601 -597
- data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +55 -54
- data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +34 -30
- data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +288 -289
- data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +59 -55
- data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +73 -69
- data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +89 -98
- data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +52 -52
- data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +192 -190
- data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +114 -121
- data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +189 -190
- data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +97 -93
- data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +146 -151
- data/spec/spec_helper.rb +11 -20
- metadata +42 -26
- data/.watchr.example +0 -65
@@ -1,28 +1,32 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module RailsBestPractices
|
4
|
+
module Prepares
|
5
|
+
describe SchemaPrepare do
|
6
|
+
let(:runner) { Core::Runner.new(:prepares => SchemaPrepare.new) }
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
8
|
+
it "should parse model attributes" do
|
9
|
+
content =<<-EOF
|
10
|
+
ActiveRecord::Schema.define(:version => 20110319172136) do
|
11
|
+
create_table "posts", :force => true do |t|
|
12
|
+
t.string "title"
|
13
|
+
t.text "body", :limit => 16777215
|
14
|
+
t.datetime "created_at"
|
15
|
+
t.integer "user_id"
|
16
|
+
t.integer "comments_count", :default => 0
|
17
|
+
t.boolean "published", :default => false, :null => false
|
18
|
+
end
|
19
|
+
end
|
20
|
+
EOF
|
21
|
+
runner.prepare("db/schema.rb", content)
|
22
|
+
model_attributes = Prepares.model_attributes
|
23
|
+
model_attributes.get_attribute_type("Post", "title").should == "string"
|
24
|
+
model_attributes.get_attribute_type("Post", "body").should == "text"
|
25
|
+
model_attributes.get_attribute_type("Post", "created_at").should == "datetime"
|
26
|
+
model_attributes.get_attribute_type("Post", "user_id").should == "integer"
|
27
|
+
model_attributes.get_attribute_type("Post", "comments_count").should == "integer"
|
28
|
+
model_attributes.get_attribute_type("Post", "published").should == "boolean"
|
16
29
|
end
|
17
30
|
end
|
18
|
-
EOF
|
19
|
-
runner.prepare("db/schema.rb", content)
|
20
|
-
model_attributes = RailsBestPractices::Prepares.model_attributes
|
21
|
-
model_attributes.get_attribute_type("Post", "title").should == "string"
|
22
|
-
model_attributes.get_attribute_type("Post", "body").should == "text"
|
23
|
-
model_attributes.get_attribute_type("Post", "created_at").should == "datetime"
|
24
|
-
model_attributes.get_attribute_type("Post", "user_id").should == "integer"
|
25
|
-
model_attributes.get_attribute_type("Post", "comments_count").should == "integer"
|
26
|
-
model_attributes.get_attribute_type("Post", "published").should == "boolean"
|
27
31
|
end
|
28
32
|
end
|
@@ -1,105 +1,103 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module RailsBestPractices
|
4
|
+
module Reviews
|
5
|
+
describe AddModelVirtualAttributeReview do
|
6
|
+
let(:runner) { Core::Runner.new(:reviews => AddModelVirtualAttributeReview.new) }
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
8
|
+
it "should add model virtual attribute" do
|
9
|
+
content = <<-EOF
|
10
|
+
class UsersController < ApplicationController
|
11
|
+
def create
|
12
|
+
@user = User.new(params[:user])
|
13
|
+
@user.first_name = params[:full_name].split(' ', 2).first
|
14
|
+
@user.last_name = params[:full_name].split(' ', 2).last
|
15
|
+
@user.save
|
16
|
+
end
|
17
|
+
end
|
18
|
+
EOF
|
19
|
+
runner.review('app/controllers/users_controller.rb', content)
|
20
|
+
runner.should have(1).errors
|
21
|
+
runner.errors[0].to_s.should == "app/controllers/users_controller.rb:3 - add model virtual attribute (for @user)"
|
15
22
|
end
|
16
|
-
end
|
17
|
-
EOF
|
18
|
-
runner.review('app/controllers/users_controller.rb', content)
|
19
|
-
runner.should have(1).errors
|
20
|
-
runner.errors[0].to_s.should == "app/controllers/users_controller.rb:3 - add model virtual attribute (for @user)"
|
21
|
-
end
|
22
|
-
|
23
|
-
it "should add model virtual attribute with local assignment" do
|
24
|
-
content = <<-EOF
|
25
|
-
class UsersController < ApplicationController
|
26
23
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
24
|
+
it "should add model virtual attribute with local assignment" do
|
25
|
+
content = <<-EOF
|
26
|
+
class UsersController < ApplicationController
|
27
|
+
def create
|
28
|
+
user = User.new(params[:user])
|
29
|
+
user.first_name = params[:full_name].split(' ', 2).first
|
30
|
+
user.last_name = params[:full_name].split(' ', 2).last
|
31
|
+
user.save
|
32
|
+
end
|
33
|
+
end
|
34
|
+
EOF
|
35
|
+
runner.review('app/controllers/users_controller.rb', content)
|
36
|
+
runner.should have(1).errors
|
37
|
+
runner.errors[0].to_s.should == "app/controllers/users_controller.rb:3 - add model virtual attribute (for user)"
|
32
38
|
end
|
33
|
-
end
|
34
|
-
EOF
|
35
|
-
runner.review('app/controllers/users_controller.rb', content)
|
36
|
-
runner.should have(1).errors
|
37
|
-
runner.errors[0].to_s.should == "app/controllers/users_controller.rb:3 - add model virtual attribute (for user)"
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should not add model virtual attribute with differen param" do
|
41
|
-
content = <<-EOF
|
42
|
-
class UsersController < ApplicationController
|
43
39
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
40
|
+
it "should not add model virtual attribute with differen param" do
|
41
|
+
content = <<-EOF
|
42
|
+
class UsersController < ApplicationController
|
43
|
+
def create
|
44
|
+
@user = User.new(params[:user])
|
45
|
+
@user.first_name = params[:first_name]
|
46
|
+
@user.last_name = params[:last_name]
|
47
|
+
@user.save
|
48
|
+
end
|
49
|
+
end
|
50
|
+
EOF
|
51
|
+
runner.review('app/controllers/users_controller.rb', content)
|
52
|
+
runner.should have(0).errors
|
49
53
|
end
|
50
|
-
end
|
51
|
-
EOF
|
52
|
-
runner.review('app/controllers/users_controller.rb', content)
|
53
|
-
runner.should have(0).errors
|
54
|
-
end
|
55
|
-
|
56
|
-
it "should not add model virtual attribute with read" do
|
57
|
-
content = <<-EOF
|
58
|
-
class UsersController < ApplicationController
|
59
54
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
55
|
+
it "should not add model virtual attribute with read" do
|
56
|
+
content = <<-EOF
|
57
|
+
class UsersController < ApplicationController
|
58
|
+
def show
|
59
|
+
if params[:id]
|
60
|
+
@user = User.find(params[:id])
|
61
|
+
else
|
62
|
+
@user = current_user
|
63
|
+
end
|
64
|
+
end
|
65
65
|
end
|
66
|
+
EOF
|
67
|
+
runner.review('app/controllers/users_controller.rb', content)
|
68
|
+
runner.should have(0).errors
|
66
69
|
end
|
67
|
-
end
|
68
|
-
EOF
|
69
|
-
runner.review('app/controllers/users_controller.rb', content)
|
70
|
-
runner.should have(0).errors
|
71
|
-
end
|
72
|
-
|
73
|
-
it "should add model virtual attribute with two dimension params" do
|
74
|
-
content = <<-EOF
|
75
|
-
class UsersController < ApplicationController
|
76
70
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
71
|
+
it "should add model virtual attribute with two dimension params" do
|
72
|
+
content = <<-EOF
|
73
|
+
class UsersController < ApplicationController
|
74
|
+
def create
|
75
|
+
@user = User.new(params[:user])
|
76
|
+
@user.first_name = params[:user][:full_name].split(' ', 2).first
|
77
|
+
@user.last_name = params[:user][:full_name].split(' ', 2).last
|
78
|
+
@user.save
|
79
|
+
end
|
80
|
+
end
|
81
|
+
EOF
|
82
|
+
runner.review('app/controllers/users_controller.rb', content)
|
83
|
+
runner.should have(1).errors
|
84
|
+
runner.errors[0].to_s.should == "app/controllers/users_controller.rb:3 - add model virtual attribute (for @user)"
|
82
85
|
end
|
83
|
-
end
|
84
|
-
EOF
|
85
|
-
runner.review('app/controllers/users_controller.rb', content)
|
86
|
-
runner.should have(1).errors
|
87
|
-
runner.errors[0].to_s.should == "app/controllers/users_controller.rb:3 - add model virtual attribute (for @user)"
|
88
|
-
end
|
89
86
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
87
|
+
it "should no add model virtual attribute with two dimension params" do
|
88
|
+
content = <<-EOF
|
89
|
+
class UsersController < ApplicationController
|
90
|
+
def create
|
91
|
+
@user = User.new(params[:user])
|
92
|
+
@user.first_name = params[:user][:first_name]
|
93
|
+
@user.last_name = params[:user][:last_name]
|
94
|
+
@user.save
|
95
|
+
end
|
96
|
+
end
|
97
|
+
EOF
|
98
|
+
runner.review('app/controllers/users_controller.rb', content)
|
99
|
+
runner.should have(0).errors
|
99
100
|
end
|
100
101
|
end
|
101
|
-
EOF
|
102
|
-
runner.review('app/controllers/users_controller.rb', content)
|
103
|
-
runner.should have(0).errors
|
104
102
|
end
|
105
103
|
end
|
@@ -1,260 +1,262 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
module RailsBestPractices
|
4
|
+
module Reviews
|
5
|
+
describe AlwaysAddDbIndexReview do
|
6
|
+
let(:runner) { Core::Runner.new(:reviews => AlwaysAddDbIndexReview.new) }
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
8
|
+
it "should always add db index" do
|
9
|
+
content = <<-EOF
|
10
|
+
ActiveRecord::Schema.define(:version => 20100603080629) do
|
11
|
+
create_table "comments", :force => true do |t|
|
12
|
+
t.string "content"
|
13
|
+
t.integer "post_id"
|
14
|
+
t.integer "user_id"
|
15
|
+
end
|
16
|
+
create_table "posts", :force => true do |t|
|
17
|
+
end
|
18
|
+
create_table "users", :force => true do |t|
|
19
|
+
end
|
20
|
+
end
|
21
|
+
EOF
|
22
|
+
runner.review('db/schema.rb', content)
|
23
|
+
runner.after_review
|
24
|
+
runner.should have(2).errors
|
25
|
+
runner.errors[0].to_s.should == "db/schema.rb:2 - always add db index (comments => [post_id])"
|
26
|
+
runner.errors[1].to_s.should == "db/schema.rb:2 - always add db index (comments => [user_id])"
|
15
27
|
end
|
16
|
-
create_table "posts", :force => true do |t|
|
17
|
-
end
|
18
|
-
create_table "users", :force => true do |t|
|
19
|
-
end
|
20
|
-
end
|
21
|
-
EOF
|
22
|
-
runner.review('db/schema.rb', content)
|
23
|
-
runner.after_review
|
24
|
-
runner.should have(2).errors
|
25
|
-
runner.errors[0].to_s.should == "db/schema.rb:2 - always add db index (comments => [post_id])"
|
26
|
-
runner.errors[1].to_s.should == "db/schema.rb:2 - always add db index (comments => [user_id])"
|
27
|
-
end
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
29
|
+
it "should always add db index with polymorphic foreign key" do
|
30
|
+
content = <<-EOF
|
31
|
+
ActiveRecord::Schema.define(:version => 20100603080629) do
|
32
|
+
create_table "versions", :force => true do |t|
|
33
|
+
t.integer "versioned_id"
|
34
|
+
t.string "versioned_type"
|
35
|
+
t.string "tag"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
EOF
|
39
|
+
runner.review('db/schema.rb', content)
|
40
|
+
runner.after_review
|
41
|
+
runner.should have(1).errors
|
42
|
+
runner.errors[0].to_s.should == "db/schema.rb:2 - always add db index (versions => [versioned_id, versioned_type])"
|
36
43
|
end
|
37
|
-
end
|
38
|
-
EOF
|
39
|
-
runner.review('db/schema.rb', content)
|
40
|
-
runner.after_review
|
41
|
-
runner.should have(1).errors
|
42
|
-
runner.errors[0].to_s.should == "db/schema.rb:2 - always add db index (versions => [versioned_id, versioned_type])"
|
43
|
-
end
|
44
44
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
45
|
+
it "should always add db index with polymorphic foreign key and _type is defined before _id" do
|
46
|
+
content = <<-EOF
|
47
|
+
ActiveRecord::Schema.define(:version => 20100603080629) do
|
48
|
+
create_table "versions", :force => true do |t|
|
49
|
+
t.string "versioned_type"
|
50
|
+
t.integer "versioned_id"
|
51
|
+
t.string "tag"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
EOF
|
55
|
+
runner.review('db/schema.rb', content)
|
56
|
+
runner.after_review
|
57
|
+
runner.should have(1).errors
|
58
|
+
runner.errors[0].to_s.should == "db/schema.rb:2 - always add db index (versions => [versioned_id, versioned_type])"
|
52
59
|
end
|
53
|
-
end
|
54
|
-
EOF
|
55
|
-
runner.review('db/schema.rb', content)
|
56
|
-
runner.after_review
|
57
|
-
runner.should have(1).errors
|
58
|
-
runner.errors[0].to_s.should == "db/schema.rb:2 - always add db index (versions => [versioned_id, versioned_type])"
|
59
|
-
end
|
60
60
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
61
|
+
it "should always add db index with single index, but without polymorphic foreign key" do
|
62
|
+
content = <<-EOF
|
63
|
+
ActiveRecord::Schema.define(:version => 20100603080629) do
|
64
|
+
create_table "taggings", :force => true do |t|
|
65
|
+
t.integer "tag_id"
|
66
|
+
t.integer "taggable_id"
|
67
|
+
t.string "taggable_type"
|
68
|
+
end
|
69
|
+
create_table "tags", :force => true do |t|
|
70
|
+
end
|
71
71
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
end
|
80
|
-
|
81
|
-
it "should always add db index with polymorphic foreign key, but without single index" do
|
82
|
-
content = <<-EOF
|
83
|
-
ActiveRecord::Schema.define(:version => 20100603080629) do
|
84
|
-
create_table "taggings", :force => true do |t|
|
85
|
-
t.integer "tag_id"
|
86
|
-
t.integer "taggable_id"
|
87
|
-
t.string "taggable_type"
|
88
|
-
end
|
89
|
-
create_table "tags", :force => true do |t|
|
72
|
+
add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
|
73
|
+
end
|
74
|
+
EOF
|
75
|
+
runner.review('db/schema.rb', content)
|
76
|
+
runner.after_review
|
77
|
+
runner.should have(1).errors
|
78
|
+
runner.errors[0].to_s.should == "db/schema.rb:2 - always add db index (taggings => [taggable_id, taggable_type])"
|
90
79
|
end
|
91
80
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
81
|
+
it "should always add db index with polymorphic foreign key, but without single index" do
|
82
|
+
content = <<-EOF
|
83
|
+
ActiveRecord::Schema.define(:version => 20100603080629) do
|
84
|
+
create_table "taggings", :force => true do |t|
|
85
|
+
t.integer "tag_id"
|
86
|
+
t.integer "taggable_id"
|
87
|
+
t.string "taggable_type"
|
88
|
+
end
|
89
|
+
create_table "tags", :force => true do |t|
|
90
|
+
end
|
100
91
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
t.string "icon_content_type"
|
109
|
-
end
|
110
|
-
create_table "users", :force => true do |t|
|
92
|
+
add_index "taggings", ["taggable_id", "taggable_type"], :name => "index_taggings_on_taggable_id_and_taggable_type"
|
93
|
+
end
|
94
|
+
EOF
|
95
|
+
runner.review('db/schema.rb', content)
|
96
|
+
runner.after_review
|
97
|
+
runner.should have(1).errors
|
98
|
+
runner.errors[0].to_s.should == "db/schema.rb:2 - always add db index (taggings => [tag_id])"
|
111
99
|
end
|
112
|
-
end
|
113
|
-
EOF
|
114
|
-
runner.review('db/schema.rb', content)
|
115
|
-
runner.after_review
|
116
|
-
runner.should have(1).errors
|
117
|
-
runner.errors[0].to_s.should == "db/schema.rb:2 - always add db index (websites => [user_id])"
|
118
|
-
end
|
119
100
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
101
|
+
it "should always add db index only _id without non related _type column" do
|
102
|
+
content = <<-EOF
|
103
|
+
ActiveRecord::Schema.define(:version => 20100603080629) do
|
104
|
+
create_table "websites", :force => true do |t|
|
105
|
+
t.integer "user_id"
|
106
|
+
t.string "icon_file_name"
|
107
|
+
t.integer "icon_file_size"
|
108
|
+
t.string "icon_content_type"
|
109
|
+
end
|
110
|
+
create_table "users", :force => true do |t|
|
111
|
+
end
|
112
|
+
end
|
113
|
+
EOF
|
114
|
+
runner.review('db/schema.rb', content)
|
115
|
+
runner.after_review
|
116
|
+
runner.should have(1).errors
|
117
|
+
runner.errors[0].to_s.should == "db/schema.rb:2 - always add db index (websites => [user_id])"
|
126
118
|
end
|
127
|
-
end
|
128
|
-
EOF
|
129
|
-
runner.review('db/schema.rb', content)
|
130
|
-
runner.after_review
|
131
|
-
runner.should have(0).errors
|
132
|
-
end
|
133
119
|
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
120
|
+
it "should not always add db index with column has no id" do
|
121
|
+
content = <<-EOF
|
122
|
+
ActiveRecord::Schema.define(:version => 20100603080629) do
|
123
|
+
create_table "comments", :force => true do |t|
|
124
|
+
t.string "content"
|
125
|
+
t.integer "position"
|
126
|
+
end
|
127
|
+
end
|
128
|
+
EOF
|
129
|
+
runner.review('db/schema.rb', content)
|
130
|
+
runner.after_review
|
131
|
+
runner.should have(0).errors
|
145
132
|
end
|
146
133
|
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
134
|
+
it "should not always add db index with add_index" do
|
135
|
+
content = <<-EOF
|
136
|
+
ActiveRecord::Schema.define(:version => 20100603080629) do
|
137
|
+
create_table "comments", :force => true do |t|
|
138
|
+
t.string "content"
|
139
|
+
t.integer "post_id"
|
140
|
+
t.integer "user_id"
|
141
|
+
end
|
142
|
+
create_table "posts", :force => true do |t|
|
143
|
+
end
|
144
|
+
create_table "users", :force => true do |t|
|
145
|
+
end
|
155
146
|
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
147
|
+
add_index "comments", ["post_id"], :name => "index_comments_on_post_id"
|
148
|
+
add_index "comments", ["user_id"], :name => "index_comments_on_user_id"
|
149
|
+
end
|
150
|
+
EOF
|
151
|
+
runner.review('db/schema.rb', content)
|
152
|
+
runner.after_review
|
153
|
+
runner.should have(0).errors
|
161
154
|
end
|
162
|
-
end
|
163
|
-
EOF
|
164
|
-
runner.review('db/schema.rb', content)
|
165
|
-
runner.after_review
|
166
|
-
runner.should have(0).errors
|
167
|
-
end
|
168
155
|
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
156
|
+
it "should not always add db index with only _type column" do
|
157
|
+
content = <<-EOF
|
158
|
+
ActiveRecord::Schema.define(:version => 20100603080629) do
|
159
|
+
create_table "versions", :force => true do |t|
|
160
|
+
t.string "versioned_type"
|
161
|
+
end
|
162
|
+
end
|
163
|
+
EOF
|
164
|
+
runner.review('db/schema.rb', content)
|
165
|
+
runner.after_review
|
166
|
+
runner.should have(0).errors
|
176
167
|
end
|
177
168
|
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
169
|
+
it "should not always add db index with multi-column index" do
|
170
|
+
content = <<-EOF
|
171
|
+
ActiveRecord::Schema.define(:version => 20100603080629) do
|
172
|
+
create_table "versions", :force => true do |t|
|
173
|
+
t.integer "versioned_id"
|
174
|
+
t.string "versioned_type"
|
175
|
+
t.string "tag"
|
176
|
+
end
|
185
177
|
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
t.string "context"
|
178
|
+
add_index "versions", ["versioned_id", "versioned_type"], :name => "index_versions_on_versioned_id_and_versioned_type"
|
179
|
+
end
|
180
|
+
EOF
|
181
|
+
runner.review('db/schema.rb', content)
|
182
|
+
runner.after_review
|
183
|
+
runner.should have(0).errors
|
193
184
|
end
|
194
185
|
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
186
|
+
it "should not always add db index if there is an index contains more columns" do
|
187
|
+
content = <<-EOF
|
188
|
+
ActiveRecord::Schema.define(:version => 20100603080629) do
|
189
|
+
create_table "taggings", :force => true do |t|
|
190
|
+
t.integer "taggable_id"
|
191
|
+
t.string "taggable_type"
|
192
|
+
t.string "context"
|
193
|
+
end
|
202
194
|
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
t.datetime "created_at"
|
195
|
+
add_index "taggings", ["taggable_id", "taggable_type", "context"], :name => "index_taggings_on_taggable_id_and_taggable_type_and_context"
|
196
|
+
end
|
197
|
+
EOF
|
198
|
+
runner.review('db/schema.rb', content)
|
199
|
+
runner.after_review
|
200
|
+
runner.should have(0).errors
|
210
201
|
end
|
211
202
|
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
203
|
+
it "should not always add db index if two indexes for polymorphic association" do
|
204
|
+
content =<<-EOF
|
205
|
+
ActiveRecord::Schema.define(:version => 20100603080629) do
|
206
|
+
create_table "taggings", :force => true do |t|
|
207
|
+
t.integer "tagger_id"
|
208
|
+
t.string "tagger_type"
|
209
|
+
t.datetime "created_at"
|
210
|
+
end
|
220
211
|
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
212
|
+
add_index "taggings", ["tagger_id"], :name => "index_taggings_on_tagger_id"
|
213
|
+
add_index "taggings", ["tagger_type"], :name => "index_taggings_on_tagger_type"
|
214
|
+
end
|
215
|
+
EOF
|
216
|
+
runner.review('db/schema.rb', content)
|
217
|
+
runner.after_review
|
218
|
+
runner.should have(0).errors
|
226
219
|
end
|
227
|
-
end
|
228
|
-
EOF
|
229
|
-
runner.review('db/schema.rb', content)
|
230
|
-
runner.after_review
|
231
|
-
runner.should have(0).errors
|
232
|
-
end
|
233
220
|
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
runner.prepare('app/models/user.rb', content)
|
246
|
-
content =<<-EOF
|
247
|
-
ActiveRecord::Schema.define(:version => 20100603080629) do
|
248
|
-
create_table "comments", :force => true do |t|
|
249
|
-
t.integer "commentor_id"
|
221
|
+
it "should not always add db index if table does not exist" do
|
222
|
+
content =<<-EOF
|
223
|
+
ActiveRecord::Schema.define(:version => 20100603080629) do
|
224
|
+
create_table "comments", :force => true do |t|
|
225
|
+
t.integer "post_id"
|
226
|
+
end
|
227
|
+
end
|
228
|
+
EOF
|
229
|
+
runner.review('db/schema.rb', content)
|
230
|
+
runner.after_review
|
231
|
+
runner.should have(0).errors
|
250
232
|
end
|
251
|
-
|
233
|
+
|
234
|
+
it "should always add db index if association_name is different to foreign_key" do
|
235
|
+
content =<<-EOF
|
236
|
+
class Comment < ActiveRecord::Base
|
237
|
+
belongs_to :commentor, :class_name => "User"
|
238
|
+
end
|
239
|
+
EOF
|
240
|
+
runner.prepare('app/models/comment.rb', content)
|
241
|
+
content =<<-EOF
|
242
|
+
class User < ActiveRecord::Base
|
243
|
+
end
|
244
|
+
EOF
|
245
|
+
runner.prepare('app/models/user.rb', content)
|
246
|
+
content =<<-EOF
|
247
|
+
ActiveRecord::Schema.define(:version => 20100603080629) do
|
248
|
+
create_table "comments", :force => true do |t|
|
249
|
+
t.integer "commentor_id"
|
250
|
+
end
|
251
|
+
create_table "users", :force => true do |t|
|
252
|
+
end
|
253
|
+
end
|
254
|
+
EOF
|
255
|
+
runner.review('db/schema.rb', content)
|
256
|
+
runner.after_review
|
257
|
+
runner.should have(1).errors
|
258
|
+
runner.errors[0].to_s.should == "db/schema.rb:2 - always add db index (comments => [commentor_id])"
|
252
259
|
end
|
253
260
|
end
|
254
|
-
EOF
|
255
|
-
runner.review('db/schema.rb', content)
|
256
|
-
runner.after_review
|
257
|
-
runner.should have(1).errors
|
258
|
-
runner.errors[0].to_s.should == "db/schema.rb:2 - always add db index (comments => [commentor_id])"
|
259
261
|
end
|
260
262
|
end
|