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.
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,35 +1,39 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe RailsBestPractices::Reviews::DryBundlerInCapistranoReview do
4
- let(:runner) { runner = RailsBestPractices::Core::Runner.new(:reviews => RailsBestPractices::Reviews::DryBundlerInCapistranoReview.new) }
3
+ module RailsBestPractices
4
+ module Reviews
5
+ describe DryBundlerInCapistranoReview do
6
+ let(:runner) { runner = Core::Runner.new(:reviews => DryBundlerInCapistranoReview.new) }
5
7
 
6
- it "should dry bundler in capistrno" do
7
- content = <<-EOF
8
- namespace :bundler do
9
- task :create_symlink, :roles => :app do
10
- shared_dir = File.join(shared_path, 'bundle')
11
- release_dir = File.join(current_release, '.bundle')
12
- run("mkdir -p \#{shared_dir} && ln -s \#{shared_dir} \#{release_dir}")
8
+ it "should dry bundler in capistrno" do
9
+ content = <<-EOF
10
+ namespace :bundler do
11
+ task :create_symlink, :roles => :app do
12
+ shared_dir = File.join(shared_path, 'bundle')
13
+ release_dir = File.join(current_release, '.bundle')
14
+ run("mkdir -p \#{shared_dir} && ln -s \#{shared_dir} \#{release_dir}")
15
+ end
16
+
17
+ task :bundle_new_release, :roles => :app do
18
+ bundler.create_symlink
19
+ run "cd \#{release_path} && bundle install --without development test"
20
+ end
21
+ end
22
+
23
+ after 'deploy:update_code', 'bundler:bundle_new_release'
24
+ EOF
25
+ runner.review('config/deploy.rb', content)
26
+ runner.should have(1).errors
27
+ runner.errors[0].to_s.should == "config/deploy.rb:1 - dry bundler in capistrano"
13
28
  end
14
29
 
15
- task :bundle_new_release, :roles => :app do
16
- bundler.create_symlink
17
- run "cd \#{release_path} && bundle install --without development test"
30
+ it "should not dry bundler in capistrano" do
31
+ content = <<-EOF
32
+ require 'bundler/capistrano'
33
+ EOF
34
+ runner.review('config/deploy.rb', content)
35
+ runner.should have(0).errors
18
36
  end
19
37
  end
20
-
21
- after 'deploy:update_code', 'bundler:bundle_new_release'
22
- EOF
23
- runner.review('config/deploy.rb', content)
24
- runner.should have(1).errors
25
- runner.errors[0].to_s.should == "config/deploy.rb:1 - dry bundler in capistrano"
26
- end
27
-
28
- it "should not dry bundler in capistrano" do
29
- content = <<-EOF
30
- require 'bundler/capistrano'
31
- EOF
32
- runner.review('config/deploy.rb', content)
33
- runner.should have(0).errors
34
38
  end
35
39
  end
@@ -1,98 +1,102 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe RailsBestPractices::Reviews::IsolateSeedDataReview do
4
- let(:runner) { RailsBestPractices::Core::Runner.new(:reviews => RailsBestPractices::Reviews::IsolateSeedDataReview.new) }
3
+ module RailsBestPractices
4
+ module Reviews
5
+ describe IsolateSeedDataReview do
6
+ let(:runner) { Core::Runner.new(:reviews => IsolateSeedDataReview.new) }
5
7
 
6
- context "create" do
7
- it "should isolate seed data" do
8
- content = <<-EOF
9
- class CreateRoles < ActiveRecord::Migration
10
- def self.up
11
- create_table "roles", :force => true do |t|
12
- t.string :name
13
- end
8
+ context "create" do
9
+ it "should isolate seed data" do
10
+ content = <<-EOF
11
+ class CreateRoles < ActiveRecord::Migration
12
+ def self.up
13
+ create_table "roles", :force => true do |t|
14
+ t.string :name
15
+ end
14
16
 
15
- ["admin", "author", "editor", "account"].each do |name|
16
- Role.create!(:name => name)
17
- end
18
- end
17
+ ["admin", "author", "editor", "account"].each do |name|
18
+ Role.create!(:name => name)
19
+ end
20
+ end
19
21
 
20
- def self.down
21
- drop_table "roles"
22
+ def self.down
23
+ drop_table "roles"
24
+ end
25
+ end
26
+ EOF
27
+ runner.review('db/migrate/20090818130258_create_roles.rb', content)
28
+ runner.should have(1).errors
29
+ runner.errors[0].to_s.should == "db/migrate/20090818130258_create_roles.rb:8 - isolate seed data"
22
30
  end
23
31
  end
24
- EOF
25
- runner.review('db/migrate/20090818130258_create_roles.rb', content)
26
- runner.should have(1).errors
27
- runner.errors[0].to_s.should == "db/migrate/20090818130258_create_roles.rb:8 - isolate seed data"
28
- end
29
- end
30
32
 
31
- context "new and save" do
32
- it "should isolate seed data for local variable" do
33
- content = <<-EOF
34
- class CreateRoles < ActiveRecord::Migration
35
- def self.up
36
- create_table "roles", :force => true do |t|
37
- t.string :name
38
- end
33
+ context "new and save" do
34
+ it "should isolate seed data for local variable" do
35
+ content = <<-EOF
36
+ class CreateRoles < ActiveRecord::Migration
37
+ def self.up
38
+ create_table "roles", :force => true do |t|
39
+ t.string :name
40
+ end
41
+
42
+ ["admin", "author", "editor", "account"].each do |name|
43
+ role = Role.new(:name => name)
44
+ role.save!
45
+ end
46
+ end
39
47
 
40
- ["admin", "author", "editor", "account"].each do |name|
41
- role = Role.new(:name => name)
42
- role.save!
48
+ def self.down
49
+ drop_table "roles"
50
+ end
43
51
  end
52
+ EOF
53
+ runner.review('db/migrate/20090818130258_create_roles.rb', content)
54
+ runner.should have(1).errors
55
+ runner.errors[0].to_s.should == "db/migrate/20090818130258_create_roles.rb:9 - isolate seed data"
44
56
  end
45
57
 
46
- def self.down
47
- drop_table "roles"
48
- end
49
- end
50
- EOF
51
- runner.review('db/migrate/20090818130258_create_roles.rb', content)
52
- runner.should have(1).errors
53
- runner.errors[0].to_s.should == "db/migrate/20090818130258_create_roles.rb:9 - isolate seed data"
54
- end
58
+ it "should isolate seed data for instance variable" do
59
+ content = <<-EOF
60
+ class CreateRoles < ActiveRecord::Migration
61
+ def self.up
62
+ create_table "roles", :force => true do |t|
63
+ t.string :name
64
+ end
55
65
 
56
- it "should isolate seed data for instance variable" do
57
- content = <<-EOF
58
- class CreateRoles < ActiveRecord::Migration
59
- def self.up
60
- create_table "roles", :force => true do |t|
61
- t.string :name
62
- end
66
+ ["admin", "author", "editor", "account"].each do |name|
67
+ @role = Role.new(:name => name)
68
+ @role.save!
69
+ end
70
+ end
63
71
 
64
- ["admin", "author", "editor", "account"].each do |name|
65
- @role = Role.new(:name => name)
66
- @role.save!
72
+ def self.down
73
+ drop_table "roles"
74
+ end
67
75
  end
68
- end
69
-
70
- def self.down
71
- drop_table "roles"
76
+ EOF
77
+ runner.review('db/migrate/20090818130258_create_roles.rb', content)
78
+ runner.should have(1).errors
79
+ runner.errors[0].to_s.should == "db/migrate/20090818130258_create_roles.rb:9 - isolate seed data"
72
80
  end
73
81
  end
74
- EOF
75
- runner.review('db/migrate/20090818130258_create_roles.rb', content)
76
- runner.should have(1).errors
77
- runner.errors[0].to_s.should == "db/migrate/20090818130258_create_roles.rb:9 - isolate seed data"
78
- end
79
- end
80
82
 
81
- it "should not isolate seed data without data insert" do
82
- content = <<-EOF
83
- class CreateRoles < ActiveRecord::Migration
84
- def self.up
85
- create_table "roles", :force => true do |t|
86
- t.string :name
87
- end
88
- end
83
+ it "should not isolate seed data without data insert" do
84
+ content = <<-EOF
85
+ class CreateRoles < ActiveRecord::Migration
86
+ def self.up
87
+ create_table "roles", :force => true do |t|
88
+ t.string :name
89
+ end
90
+ end
89
91
 
90
- def self.down
91
- drop_table "roles"
92
+ def self.down
93
+ drop_table "roles"
94
+ end
95
+ end
96
+ EOF
97
+ runner.review('db/migrate/20090818130258_create_roles.rb', content)
98
+ runner.should have(0).errors
92
99
  end
93
100
  end
94
- EOF
95
- runner.review('db/migrate/20090818130258_create_roles.rb', content)
96
- runner.should have(0).errors
97
101
  end
98
102
  end
@@ -1,95 +1,99 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe RailsBestPractices::Reviews::KeepFindersOnTheirOwnModelReview do
4
- let(:runner) { RailsBestPractices::Core::Runner.new(:reviews => RailsBestPractices::Reviews::KeepFindersOnTheirOwnModelReview.new) }
3
+ module RailsBestPractices
4
+ module Reviews
5
+ describe KeepFindersOnTheirOwnModelReview do
6
+ let(:runner) { Core::Runner.new(:reviews => KeepFindersOnTheirOwnModelReview.new) }
5
7
 
6
- it "should keep finders on thier own model" do
7
- content = <<-EOF
8
- class Post < ActiveRecord::Base
9
- has_many :comments
8
+ it "should keep finders on thier own model" do
9
+ content = <<-EOF
10
+ class Post < ActiveRecord::Base
11
+ has_many :comments
10
12
 
11
- def find_valid_comments
12
- self.comment.find(:all, :conditions => { :is_spam => false },
13
- :limit => 10)
13
+ def find_valid_comments
14
+ self.comment.find(:all, :conditions => { :is_spam => false },
15
+ :limit => 10)
16
+ end
17
+ end
18
+ EOF
19
+ runner.review('app/models/post.rb', content)
20
+ runner.should have(1).errors
21
+ runner.errors[0].to_s.should == "app/models/post.rb:5 - keep finders on their own model"
14
22
  end
15
- end
16
- EOF
17
- runner.review('app/models/post.rb', content)
18
- runner.should have(1).errors
19
- runner.errors[0].to_s.should == "app/models/post.rb:5 - keep finders on their own model"
20
- end
21
23
 
22
- it "should keep finders on thier own model with all method" do
23
- content = <<-EOF
24
- class Post < ActiveRecord::Base
25
- has_many :comments
24
+ it "should keep finders on thier own model with all method" do
25
+ content = <<-EOF
26
+ class Post < ActiveRecord::Base
27
+ has_many :comments
26
28
 
27
- def find_valid_comments
28
- self.comment.all(:conditions => { :is_spam => false },
29
- :limit => 10)
29
+ def find_valid_comments
30
+ self.comment.all(:conditions => { :is_spam => false },
31
+ :limit => 10)
32
+ end
33
+ end
34
+ EOF
35
+ runner.review('app/models/post.rb', content)
36
+ runner.should have(1).errors
37
+ runner.errors[0].to_s.should == "app/models/post.rb:5 - keep finders on their own model"
30
38
  end
31
- end
32
- EOF
33
- runner.review('app/models/post.rb', content)
34
- runner.should have(1).errors
35
- runner.errors[0].to_s.should == "app/models/post.rb:5 - keep finders on their own model"
36
- end
37
39
 
38
- it "should not keep finders on thier own model with self finder" do
39
- content = <<-EOF
40
- class Post < ActiveRecord::Base
41
- has_many :comments
40
+ it "should not keep finders on thier own model with self finder" do
41
+ content = <<-EOF
42
+ class Post < ActiveRecord::Base
43
+ has_many :comments
42
44
 
43
- def find_valid_comments
44
- self.find(:all, :conditions => { :is_spam => false },
45
- :limit => 10)
45
+ def find_valid_comments
46
+ self.find(:all, :conditions => { :is_spam => false },
47
+ :limit => 10)
48
+ end
49
+ end
50
+ EOF
51
+ runner.review('app/models/post.rb', content)
52
+ runner.should have(0).errors
46
53
  end
47
- end
48
- EOF
49
- runner.review('app/models/post.rb', content)
50
- runner.should have(0).errors
51
- end
52
54
 
53
- it "should not keep finders on thier own model with own finder" do
54
- content = <<-EOF
55
- class Post < ActiveRecord::Base
56
- has_many :comments
55
+ it "should not keep finders on thier own model with own finder" do
56
+ content = <<-EOF
57
+ class Post < ActiveRecord::Base
58
+ has_many :comments
57
59
 
58
- def find_valid_comments
59
- Post.find(:all, :conditions => { :is_spam => false },
60
- :limit => 10)
60
+ def find_valid_comments
61
+ Post.find(:all, :conditions => { :is_spam => false },
62
+ :limit => 10)
63
+ end
64
+ end
65
+ EOF
66
+ runner.review('app/models/post.rb', content)
67
+ runner.should have(0).errors
61
68
  end
62
- end
63
- EOF
64
- runner.review('app/models/post.rb', content)
65
- runner.should have(0).errors
66
- end
67
69
 
68
- it "should not keep finders on their own model without finder" do
69
- content = <<-EOF
70
- class Post < ActiveRecord::Base
71
- has_many :comments
70
+ it "should not keep finders on their own model without finder" do
71
+ content = <<-EOF
72
+ class Post < ActiveRecord::Base
73
+ has_many :comments
72
74
 
73
- def find_valid_comments
74
- self.comments.destroy_all
75
+ def find_valid_comments
76
+ self.comments.destroy_all
77
+ end
78
+ end
79
+ EOF
80
+ runner.review('app/models/post.rb', content)
81
+ runner.should have(0).errors
75
82
  end
76
- end
77
- EOF
78
- runner.review('app/models/post.rb', content)
79
- runner.should have(0).errors
80
- end
81
83
 
82
- it "should not keep finders on their own model with ruby Array#find" do
83
- content = <<-EOF
84
- class Post < ActiveRecord::Base
85
- has_many :comments
84
+ it "should not keep finders on their own model with ruby Array#find" do
85
+ content = <<-EOF
86
+ class Post < ActiveRecord::Base
87
+ has_many :comments
86
88
 
87
- def active_comments
88
- self.comments.find {|comment| comment.status == 'active'}
89
+ def active_comments
90
+ self.comments.find {|comment| comment.status == 'active'}
91
+ end
92
+ end
93
+ EOF
94
+ runner.review('app/models/post.rb', content)
95
+ runner.should have(0).errors
89
96
  end
90
97
  end
91
- EOF
92
- runner.review('app/models/post.rb', content)
93
- runner.should have(0).errors
94
98
  end
95
99
  end
@@ -1,167 +1,165 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe RailsBestPractices::Reviews::LawOfDemeterReview do
4
-
5
- let(:runner) {
6
- RailsBestPractices::Core::Runner.new(
7
- :prepares => [RailsBestPractices::Prepares::ModelPrepare.new, RailsBestPractices::Prepares::SchemaPrepare.new],
8
- :reviews => RailsBestPractices::Reviews::LawOfDemeterReview.new
9
- )
10
- }
11
-
12
- describe "belongs_to" do
13
- before(:each) do
14
- content = <<-EOF
15
- class Invoice < ActiveRecord::Base
16
- belongs_to :user
17
- end
18
- EOF
19
- runner.prepare('app/models/invoice.rb', content)
20
-
21
- content = <<-EOF
22
- ActiveRecord::Schema.define(:version => 20110216150853) do
23
- create_table "users", force => true do |t|
24
- t.string :name
25
- t.string :address
26
- t.string :cellphone
3
+ module RailsBestPractices
4
+ module Reviews
5
+ describe LawOfDemeterReview do
6
+ let(:runner) { Core::Runner.new(:prepares => [Prepares::ModelPrepare.new, Prepares::SchemaPrepare.new], :reviews => LawOfDemeterReview.new) }
7
+
8
+ describe "belongs_to" do
9
+ before(:each) do
10
+ content = <<-EOF
11
+ class Invoice < ActiveRecord::Base
12
+ belongs_to :user
13
+ end
14
+ EOF
15
+ runner.prepare('app/models/invoice.rb', content)
16
+
17
+ content = <<-EOF
18
+ ActiveRecord::Schema.define(:version => 20110216150853) do
19
+ create_table "users", force => true do |t|
20
+ t.string :name
21
+ t.string :address
22
+ t.string :cellphone
23
+ end
24
+ end
25
+ EOF
26
+ runner.prepare('db/schema.rb', content)
27
27
  end
28
- end
29
- EOF
30
- runner.prepare('db/schema.rb', content)
31
- end
32
28
 
33
- it "should law of demeter with erb" do
34
- content = <<-EOF
35
- <%= @invoice.user.name %>
36
- <%= @invoice.user.address %>
37
- <%= @invoice.user.cellphone %>
38
- EOF
39
- runner.review('app/views/invoices/show.html.erb', content)
40
- runner.should have(3).errors
41
- runner.errors[0].to_s.should == "app/views/invoices/show.html.erb:1 - law of demeter"
42
- end
29
+ it "should law of demeter with erb" do
30
+ content = <<-EOF
31
+ <%= @invoice.user.name %>
32
+ <%= @invoice.user.address %>
33
+ <%= @invoice.user.cellphone %>
34
+ EOF
35
+ runner.review('app/views/invoices/show.html.erb', content)
36
+ runner.should have(3).errors
37
+ runner.errors[0].to_s.should == "app/views/invoices/show.html.erb:1 - law of demeter"
38
+ end
43
39
 
44
- it "should law of demeter with haml" do
45
- content = <<-EOF
40
+ it "should law of demeter with haml" do
41
+ content = <<-EOF
46
42
  = @invoice.user.name
47
43
  = @invoice.user.address
48
44
  = @invoice.user.cellphone
49
- EOF
50
- runner.review('app/views/invoices/show.html.haml', content)
51
- runner.should have(3).errors
52
- runner.errors[0].to_s.should == "app/views/invoices/show.html.haml:1 - law of demeter"
53
- end
45
+ EOF
46
+ runner.review('app/views/invoices/show.html.haml', content)
47
+ runner.should have(3).errors
48
+ runner.errors[0].to_s.should == "app/views/invoices/show.html.haml:1 - law of demeter"
49
+ end
54
50
 
55
- it "should law of demeter with slim" do
56
- content = <<-EOF
51
+ it "should law of demeter with slim" do
52
+ content = <<-EOF
57
53
  = @invoice.user.name
58
54
  = @invoice.user.address
59
55
  = @invoice.user.cellphone
60
- EOF
61
- runner.review('app/views/invoices/show.html.slim', content)
62
- runner.should have(3).errors
63
- runner.errors[0].to_s.should == "app/views/invoices/show.html.slim:1 - law of demeter"
64
- end
65
-
66
- it "should no law of demeter" do
67
- content = <<-EOF
68
- <%= @invoice.user_name %>
69
- <%= @invoice.user_address %>
70
- <%= @invoice.user_cellphone %>
71
- EOF
72
- runner.review('app/views/invoices/show.html.erb', content)
73
- runner.should have(0).errors
74
- end
75
- end
76
-
77
- describe "has_one" do
78
- before(:each) do
79
- content = <<-EOF
80
- class Invoice < ActiveRecord::Base
81
- has_one :price
82
- end
83
- EOF
84
- runner.prepare('app/models/invoice.rb', content)
56
+ EOF
57
+ runner.review('app/views/invoices/show.html.slim', content)
58
+ runner.should have(3).errors
59
+ runner.errors[0].to_s.should == "app/views/invoices/show.html.slim:1 - law of demeter"
60
+ end
85
61
 
86
- content = <<-EOF
87
- ActiveRecord::Schema.define(:version => 20110216150853) do
88
- create_table "prices", force => true do |t|
89
- t.string :currency
90
- t.integer :number
62
+ it "should no law of demeter" do
63
+ content = <<-EOF
64
+ <%= @invoice.user_name %>
65
+ <%= @invoice.user_address %>
66
+ <%= @invoice.user_cellphone %>
67
+ EOF
68
+ runner.review('app/views/invoices/show.html.erb', content)
69
+ runner.should have(0).errors
91
70
  end
92
71
  end
93
- EOF
94
- runner.prepare('db/schema.rb', content)
95
- end
96
72
 
97
- it "should law of demeter" do
98
- content = <<-EOF
99
- <%= @invoice.price.currency %>
100
- <%= @invoice.price.number %>
101
- EOF
102
- runner.review('app/views/invoices/show.html.erb', content)
103
- runner.should have(2).errors
104
- runner.errors[0].to_s.should == "app/views/invoices/show.html.erb:1 - law of demeter"
105
- end
106
- end
73
+ describe "has_one" do
74
+ before(:each) do
75
+ content = <<-EOF
76
+ class Invoice < ActiveRecord::Base
77
+ has_one :price
78
+ end
79
+ EOF
80
+ runner.prepare('app/models/invoice.rb', content)
81
+
82
+ content = <<-EOF
83
+ ActiveRecord::Schema.define(:version => 20110216150853) do
84
+ create_table "prices", force => true do |t|
85
+ t.string :currency
86
+ t.integer :number
87
+ end
88
+ end
89
+ EOF
90
+ runner.prepare('db/schema.rb', content)
91
+ end
107
92
 
108
- context "polymorphic association" do
109
- before :each do
110
- content = <<-EOF
111
- class Comment < ActiveRecord::Base
112
- belongs_to :commentable, :polymorphic => true
93
+ it "should law of demeter" do
94
+ content = <<-EOF
95
+ <%= @invoice.price.currency %>
96
+ <%= @invoice.price.number %>
97
+ EOF
98
+ runner.review('app/views/invoices/show.html.erb', content)
99
+ runner.should have(2).errors
100
+ runner.errors[0].to_s.should == "app/views/invoices/show.html.erb:1 - law of demeter"
101
+ end
113
102
  end
114
- EOF
115
- runner.prepare('app/models/comment.rb', content)
116
103
 
117
- content = <<-EOF
118
- class Post < ActiveRecord::Base
119
- has_many :comments
120
- end
121
- EOF
122
- runner.prepare('app/models/comment.rb', content)
104
+ context "polymorphic association" do
105
+ before :each do
106
+ content = <<-EOF
107
+ class Comment < ActiveRecord::Base
108
+ belongs_to :commentable, :polymorphic => true
109
+ end
110
+ EOF
111
+ runner.prepare('app/models/comment.rb', content)
112
+
113
+ content = <<-EOF
114
+ class Post < ActiveRecord::Base
115
+ has_many :comments
116
+ end
117
+ EOF
118
+ runner.prepare('app/models/comment.rb', content)
119
+
120
+ content = <<-EOF
121
+ ActiveRecord::Schema.define(:version => 20110216150853) do
122
+ create_table "posts", force => true do |t|
123
+ t.string :title
124
+ end
125
+ end
126
+ EOF
127
+ runner.prepare('db/schema.rb', content)
128
+ end
123
129
 
124
- content = <<-EOF
125
- ActiveRecord::Schema.define(:version => 20110216150853) do
126
- create_table "posts", force => true do |t|
127
- t.string :title
130
+ it "should law of demeter" do
131
+ content = <<-EOF
132
+ <%= @comment.commentable.title %>
133
+ EOF
134
+ runner.review('app/views/comments/index.html.erb', content)
135
+ runner.should have(1).errors
136
+ runner.errors[0].to_s.should == "app/views/comments/index.html.erb:1 - law of demeter"
128
137
  end
129
138
  end
130
- EOF
131
- runner.prepare('db/schema.rb', content)
132
- end
133
139
 
134
- it "should law of demeter" do
135
- content = <<-EOF
136
- <%= @comment.commentable.title %>
137
- EOF
138
- runner.review('app/views/comments/index.html.erb', content)
139
- runner.should have(1).errors
140
- runner.errors[0].to_s.should == "app/views/comments/index.html.erb:1 - law of demeter"
141
- end
142
- end
143
-
144
- it "should no law of demeter with method call" do
145
- content = <<-EOF
146
- class Question < ActiveRecord::Base
147
- has_many :answers, :dependent => :destroy
148
- end
149
- EOF
150
- runner.prepare('app/models/question.rb', content)
151
- content = <<-EOF
152
- class Answer < ActiveRecord::Base
153
- belongs_to :question, :counter_cache => true, :touch => true
154
- end
155
- EOF
156
- runner.prepare('app/models/answer.rb', content)
157
- content = <<-EOF
158
- class CommentsController < ApplicationController
159
- def comment_url
160
- question_path(@answer.question)
140
+ it "should no law of demeter with method call" do
141
+ content = <<-EOF
142
+ class Question < ActiveRecord::Base
143
+ has_many :answers, :dependent => :destroy
144
+ end
145
+ EOF
146
+ runner.prepare('app/models/question.rb', content)
147
+ content = <<-EOF
148
+ class Answer < ActiveRecord::Base
149
+ belongs_to :question, :counter_cache => true, :touch => true
150
+ end
151
+ EOF
152
+ runner.prepare('app/models/answer.rb', content)
153
+ content = <<-EOF
154
+ class CommentsController < ApplicationController
155
+ def comment_url
156
+ question_path(@answer.question)
157
+ end
158
+ end
159
+ EOF
160
+ runner.review('app/controllers/comments_controller.rb', content)
161
+ runner.should have(0).errors
161
162
  end
162
163
  end
163
- EOF
164
- runner.review('app/controllers/comments_controller.rb', content)
165
- runner.should have(0).errors
166
164
  end
167
165
  end