rails_best_practices 1.9.0 → 1.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. data/.travis.yml +4 -1
  2. data/Gemfile +0 -3
  3. data/Gemfile.lock +10 -9
  4. data/README.md +2 -0
  5. data/lib/rails_best_practices/analyzer.rb +40 -21
  6. data/lib/rails_best_practices/core.rb +1 -0
  7. data/lib/rails_best_practices/core/check.rb +12 -0
  8. data/lib/rails_best_practices/core/configs.rb +7 -0
  9. data/lib/rails_best_practices/core/error.rb +2 -1
  10. data/lib/rails_best_practices/core/routes.rb +11 -5
  11. data/lib/rails_best_practices/core/runner.rb +6 -2
  12. data/lib/rails_best_practices/core_ext/sexp.rb +3 -1
  13. data/lib/rails_best_practices/prepares.rb +5 -0
  14. data/lib/rails_best_practices/prepares/config_prepare.rb +21 -0
  15. data/lib/rails_best_practices/prepares/route_prepare.rb +24 -7
  16. data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +11 -3
  17. data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +7 -3
  18. data/lib/rails_best_practices/version.rb +1 -1
  19. data/rails_best_practices.gemspec +1 -0
  20. data/spec/rails_best_practices/analyzer_spec.rb +52 -32
  21. data/spec/rails_best_practices/core/check_spec.rb +64 -45
  22. data/spec/rails_best_practices/core/checking_visitor_spec.rb +65 -64
  23. data/spec/rails_best_practices/core/configs_spec.rb +7 -0
  24. data/spec/rails_best_practices/core/controllers_spec.rb +4 -2
  25. data/spec/rails_best_practices/core/error_spec.rb +28 -22
  26. data/spec/rails_best_practices/core/helpers_spec.rb +4 -2
  27. data/spec/rails_best_practices/core/klasses_spec.rb +8 -6
  28. data/spec/rails_best_practices/core/mailers_spec.rb +4 -2
  29. data/spec/rails_best_practices/core/methods_spec.rb +37 -35
  30. data/spec/rails_best_practices/core/model_associations_spec.rb +17 -15
  31. data/spec/rails_best_practices/core/model_attributes_spec.rb +17 -15
  32. data/spec/rails_best_practices/core/models_spec.rb +4 -2
  33. data/spec/rails_best_practices/core/modules_spec.rb +21 -19
  34. data/spec/rails_best_practices/core/nil_spec.rb +23 -21
  35. data/spec/rails_best_practices/core/routes_spec.rb +21 -14
  36. data/spec/rails_best_practices/core/runner_spec.rb +17 -15
  37. data/spec/rails_best_practices/core_ext/sexp_spec.rb +32 -7
  38. data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +35 -31
  39. data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +35 -31
  40. data/spec/rails_best_practices/prepares/config_prepare_spec.rb +24 -0
  41. data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +122 -123
  42. data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +36 -32
  43. data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +12 -8
  44. data/spec/rails_best_practices/prepares/model_prepare_spec.rb +326 -322
  45. data/spec/rails_best_practices/prepares/route_prepare_spec.rb +619 -573
  46. data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +25 -21
  47. data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +85 -87
  48. data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +224 -222
  49. data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +30 -26
  50. data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +79 -75
  51. data/spec/rails_best_practices/reviews/keep_finders_on_their_own_model_review_spec.rb +76 -72
  52. data/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb +138 -140
  53. data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +38 -34
  54. data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +22 -18
  55. data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +57 -53
  56. data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +62 -63
  57. data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +51 -50
  58. data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +140 -136
  59. data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +48 -44
  60. data/spec/rails_best_practices/reviews/not_use_times_ago_in_words_review_spec.rb +42 -38
  61. data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +151 -149
  62. data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +86 -55
  63. data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +23 -19
  64. data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +290 -286
  65. data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +76 -72
  66. data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +601 -597
  67. data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +55 -54
  68. data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +34 -30
  69. data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +288 -289
  70. data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +59 -55
  71. data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +73 -69
  72. data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +89 -98
  73. data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +52 -52
  74. data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +192 -190
  75. data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +114 -121
  76. data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +189 -190
  77. data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +97 -93
  78. data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +146 -151
  79. data/spec/spec_helper.rb +11 -20
  80. metadata +42 -26
  81. data/.watchr.example +0 -65
@@ -1,28 +1,32 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe RailsBestPractices::Prepares::SchemaPrepare do
4
- let(:runner) { RailsBestPractices::Core::Runner.new(:prepares => RailsBestPractices::Prepares::SchemaPrepare.new) }
3
+ module RailsBestPractices
4
+ module Prepares
5
+ describe SchemaPrepare do
6
+ let(:runner) { Core::Runner.new(:prepares => SchemaPrepare.new) }
5
7
 
6
- it "should parse model attributes" do
7
- content =<<-EOF
8
- ActiveRecord::Schema.define(:version => 20110319172136) do
9
- create_table "posts", :force => true do |t|
10
- t.string "title"
11
- t.text "body", :limit => 16777215
12
- t.datetime "created_at"
13
- t.integer "user_id"
14
- t.integer "comments_count", :default => 0
15
- t.boolean "published", :default => false, :null => false
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
- describe RailsBestPractices::Reviews::AddModelVirtualAttributeReview do
4
- let(:runner) { RailsBestPractices::Core::Runner.new(:reviews => RailsBestPractices::Reviews::AddModelVirtualAttributeReview.new) }
3
+ module RailsBestPractices
4
+ module Reviews
5
+ describe AddModelVirtualAttributeReview do
6
+ let(:runner) { Core::Runner.new(:reviews => AddModelVirtualAttributeReview.new) }
5
7
 
6
- it "should add model virtual attribute" do
7
- content = <<-EOF
8
- class UsersController < ApplicationController
9
-
10
- def create
11
- @user = User.new(params[:user])
12
- @user.first_name = params[:full_name].split(' ', 2).first
13
- @user.last_name = params[:full_name].split(' ', 2).last
14
- @user.save
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
- 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
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
- def create
45
- @user = User.new(params[:user])
46
- @user.first_name = params[:first_name]
47
- @user.last_name = params[:last_name]
48
- @user.save
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
- def show
61
- if params[:id]
62
- @user = User.find(params[:id])
63
- else
64
- @user = current_user
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
- def create
78
- @user = User.new(params[:user])
79
- @user.first_name = params[:user][:full_name].split(' ', 2).first
80
- @user.last_name = params[:user][:full_name].split(' ', 2).last
81
- @user.save
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
- it "should no add model virtual attribute with two dimension params" do
91
- content = <<-EOF
92
- class UsersController < ApplicationController
93
-
94
- def create
95
- @user = User.new(params[:user])
96
- @user.first_name = params[:user][:first_name]
97
- @user.last_name = params[:user][:last_name]
98
- @user.save
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
- describe RailsBestPractices::Reviews::AlwaysAddDbIndexReview do
4
- let(:runner) { RailsBestPractices::Core::Runner.new(
5
- :reviews => RailsBestPractices::Reviews::AlwaysAddDbIndexReview.new
6
- ) }
3
+ module RailsBestPractices
4
+ module Reviews
5
+ describe AlwaysAddDbIndexReview do
6
+ let(:runner) { Core::Runner.new(:reviews => AlwaysAddDbIndexReview.new) }
7
7
 
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"
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
- 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"
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
- 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"
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
- 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
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
- 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])"
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
- 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])"
99
- end
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
- 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|
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
- 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"
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
- 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|
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
- 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
154
- end
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
- 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"
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
- 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"
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
- 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
184
- end
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
- 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"
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
- 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
201
- end
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
- 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"
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
- 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
219
- end
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
- 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"
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
- 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"
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
- create_table "users", :force => true do |t|
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