rails_best_practices 0.2.0 → 0.2.6
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.
- data/README.textile +11 -6
- data/VERSION +1 -1
- data/lib/rails_best_practices/checks/always_add_db_index_check.rb +83 -0
- data/lib/rails_best_practices/checks/check.rb +1 -1
- data/lib/rails_best_practices/checks/isolate_seed_data_check.rb +46 -0
- data/lib/rails_best_practices/checks/keep_finders_on_their_own_model_check.rb +29 -0
- data/lib/rails_best_practices/checks/law_of_demeter_check.rb +47 -0
- data/lib/rails_best_practices/checks/use_observer_check.rb +49 -0
- data/lib/rails_best_practices/checks/use_scope_access_check.rb +2 -2
- data/lib/rails_best_practices/checks.rb +6 -1
- data/lib/rails_best_practices/command.rb +24 -4
- data/lib/rails_best_practices/core/core_ext.rb +12 -1
- data/lib/rails_best_practices/core/runner.rb +5 -1
- data/lib/rails_best_practices/core/visitable_sexp.rb +14 -5
- data/rails_best_practices.gemspec +17 -2
- data/rails_best_practices.yml +5 -0
- data/spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb +22 -4
- data/spec/rails_best_practices/checks/always_add_db_index_check_spec.rb +153 -0
- data/spec/rails_best_practices/checks/isolate_seed_data_check_spec.rb +75 -0
- data/spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb +71 -0
- data/spec/rails_best_practices/checks/law_of_demeter_check_spec.rb +37 -0
- data/spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb +6 -6
- data/spec/rails_best_practices/checks/move_model_logic_into_model_check_spec.rb +4 -4
- data/spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb +5 -5
- data/spec/rails_best_practices/checks/use_model_association_check_spec.rb +6 -6
- data/spec/rails_best_practices/checks/use_observer_check_spec.rb +67 -0
- data/spec/rails_best_practices/checks/use_scope_access_check_spec.rb +17 -17
- metadata +17 -2
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__) + '/../../spec_helper')
|
|
2
|
+
|
|
3
|
+
describe RailsBestPractices::Checks::AlwaysAddDbIndexCheck do
|
|
4
|
+
before(:each) do
|
|
5
|
+
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::AlwaysAddDbIndexCheck.new)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "should always add db index" do
|
|
9
|
+
content = <<-EOF
|
|
10
|
+
class CreateComments < ActiveRecord::Migration
|
|
11
|
+
def self.up
|
|
12
|
+
create_table "comments", :force => true do |t|
|
|
13
|
+
t.string :content
|
|
14
|
+
t.integer :post_id
|
|
15
|
+
t.integer :user_id
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.down
|
|
20
|
+
drop_table "comments"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
EOF
|
|
24
|
+
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
|
25
|
+
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
|
26
|
+
errors = @runner.errors
|
|
27
|
+
errors.should_not be_empty
|
|
28
|
+
errors[0].to_s.should == "db/migrate/20090918130258_create_comments.rb:2 - always add db index (comments => post_id, comments => user_id)"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "should always add db index with column has no id" do
|
|
32
|
+
content = <<-EOF
|
|
33
|
+
class CreateComments < ActiveRecord::Migration
|
|
34
|
+
def self.up
|
|
35
|
+
create_table "comments", :force => true do |t|
|
|
36
|
+
t.string :content
|
|
37
|
+
t.integer :position
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def self.down
|
|
42
|
+
drop_table "comments"
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
EOF
|
|
46
|
+
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
|
47
|
+
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
|
48
|
+
errors = @runner.errors
|
|
49
|
+
errors.should be_empty
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "should always add db index with references" do
|
|
53
|
+
content = <<-EOF
|
|
54
|
+
class CreateComments < ActiveRecord::Migration
|
|
55
|
+
def self.up
|
|
56
|
+
create_table "comments", :force => true do |t|
|
|
57
|
+
t.string :content
|
|
58
|
+
t.references :post
|
|
59
|
+
t.references :user
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def self.down
|
|
64
|
+
drop_table "comments"
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
EOF
|
|
68
|
+
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
|
69
|
+
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
|
70
|
+
errors = @runner.errors
|
|
71
|
+
errors.should_not be_empty
|
|
72
|
+
errors[0].to_s.should == "db/migrate/20090918130258_create_comments.rb:2 - always add db index (comments => post_id, comments => user_id)"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
it "should always add db index with column" do
|
|
76
|
+
content = <<-EOF
|
|
77
|
+
class CreateComments < ActiveRecord::Migration
|
|
78
|
+
def self.up
|
|
79
|
+
create_table "comments", :force => true do |t|
|
|
80
|
+
t.string :content
|
|
81
|
+
t.column :post_id, :integer
|
|
82
|
+
t.column :user_id, :integer
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def self.down
|
|
87
|
+
drop_table "comments"
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
EOF
|
|
91
|
+
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
|
92
|
+
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
|
93
|
+
errors = @runner.errors
|
|
94
|
+
errors.should_not be_empty
|
|
95
|
+
errors[0].to_s.should == "db/migrate/20090918130258_create_comments.rb:2 - always add db index (comments => post_id, comments => user_id)"
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
it "should not always add db index with add_index" do
|
|
99
|
+
content = <<-EOF
|
|
100
|
+
class CreateComments < ActiveRecord::Migration
|
|
101
|
+
def self.up
|
|
102
|
+
create_table "comments", :force => true do |t|
|
|
103
|
+
t.string :content
|
|
104
|
+
t.integer :post_id
|
|
105
|
+
t.integer :user_id
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
add_index :comments, :post_id
|
|
109
|
+
add_index :comments, :user_id
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def self.down
|
|
113
|
+
drop_table "comments"
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
EOF
|
|
117
|
+
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
|
118
|
+
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
|
119
|
+
errors = @runner.errors
|
|
120
|
+
errors.should be_empty
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
it "should not always add db index with add_index in another migration file" do
|
|
124
|
+
content = <<-EOF
|
|
125
|
+
class CreateComments < ActiveRecord::Migration
|
|
126
|
+
def self.up
|
|
127
|
+
create_table "comments", :force => true do |t|
|
|
128
|
+
t.string :content
|
|
129
|
+
t.integer :post_id
|
|
130
|
+
t.integer :user_id
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def self.down
|
|
135
|
+
drop_table "comments"
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
EOF
|
|
139
|
+
add_index_content = <<-EOF
|
|
140
|
+
class AddIndexesToComments < ActiveRecord::Migration
|
|
141
|
+
def self.up
|
|
142
|
+
add_index :comments, :post_id
|
|
143
|
+
add_index :comments, :user_id
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
EOF
|
|
147
|
+
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
|
148
|
+
@runner.check('db/migrate/20090919130258_add_indexes_to_comments.rb', content)
|
|
149
|
+
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
|
150
|
+
@runner.check('db/migrate/20090919130258_add_indexes_to_comments.rb', content)
|
|
151
|
+
errors = @runner.errors
|
|
152
|
+
end
|
|
153
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__) + '/../../spec_helper')
|
|
2
|
+
|
|
3
|
+
describe RailsBestPractices::Checks::IsolateSeedDataCheck do
|
|
4
|
+
before(:each) do
|
|
5
|
+
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::IsolateSeedDataCheck.new)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "should isolate seed data" do
|
|
9
|
+
content = <<-EOF
|
|
10
|
+
class CreateRoles < ActiveRecord::Migration
|
|
11
|
+
def self.up
|
|
12
|
+
create_table "roles", :force => true do |t|
|
|
13
|
+
t.string :name
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
["admin", "author", "editor", "account"].each do |name|
|
|
17
|
+
Role.create!(:name => name)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.down
|
|
22
|
+
drop_table "roles"
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
EOF
|
|
26
|
+
@runner.check('db/migrate/20090818130258_create_roles.rb', content)
|
|
27
|
+
errors = @runner.errors
|
|
28
|
+
errors.should_not be_empty
|
|
29
|
+
errors[0].to_s.should == "db/migrate/20090818130258_create_roles.rb:8 - isolate seed data"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it "should isolate seed data with new and save" 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
|
|
39
|
+
|
|
40
|
+
["admin", "author", "editor", "account"].each do |name|
|
|
41
|
+
role = Role.new(:name => name)
|
|
42
|
+
role.save!
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def self.down
|
|
47
|
+
drop_table "roles"
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
EOF
|
|
51
|
+
@runner.check('db/migrate/20090818130258_create_roles.rb', content)
|
|
52
|
+
errors = @runner.errors
|
|
53
|
+
errors.should_not be_empty
|
|
54
|
+
errors[0].to_s.should == "db/migrate/20090818130258_create_roles.rb:9 - isolate seed data"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it "should not isolate seed data without data insert" do
|
|
58
|
+
content = <<-EOF
|
|
59
|
+
class CreateRoles < ActiveRecord::Migration
|
|
60
|
+
def self.up
|
|
61
|
+
create_table "roles", :force => true do |t|
|
|
62
|
+
t.string :name
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def self.down
|
|
67
|
+
drop_table "roles"
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
EOF
|
|
71
|
+
@runner.check('db/migrate/20090818130258_create_roles.rb', content)
|
|
72
|
+
errors = @runner.errors
|
|
73
|
+
errors.should be_empty
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__) + '/../../spec_helper')
|
|
2
|
+
|
|
3
|
+
describe RailsBestPractices::Checks::KeepFindersOnTheirOwnModelCheck do
|
|
4
|
+
before(:each) do
|
|
5
|
+
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::KeepFindersOnTheirOwnModelCheck.new)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "should keep finders on thier own model" do
|
|
9
|
+
content = <<-EOF
|
|
10
|
+
class Post < ActiveRecord::Base
|
|
11
|
+
has_many :comments
|
|
12
|
+
|
|
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.check('app/models/post.rb', content)
|
|
20
|
+
errors = @runner.errors
|
|
21
|
+
errors.should_not be_empty
|
|
22
|
+
errors[0].to_s.should == "app/models/post.rb:5 - keep finders on their own model"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "should not keep finders on thier own model with self finder" do
|
|
26
|
+
content = <<-EOF
|
|
27
|
+
class Post < ActiveRecord::Base
|
|
28
|
+
has_many :comments
|
|
29
|
+
|
|
30
|
+
def find_valid_comments
|
|
31
|
+
self.find(:all, :conditions => { :is_spam => false },
|
|
32
|
+
:limit => 10)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
EOF
|
|
36
|
+
@runner.check('app/models/post.rb', content)
|
|
37
|
+
errors = @runner.errors
|
|
38
|
+
errors.should be_empty
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "should not keep finders on thier own model with own finder" do
|
|
42
|
+
content = <<-EOF
|
|
43
|
+
class Post < ActiveRecord::Base
|
|
44
|
+
has_many :comments
|
|
45
|
+
|
|
46
|
+
def find_valid_comments
|
|
47
|
+
Post.find(:all, :conditions => { :is_spam => false },
|
|
48
|
+
:limit => 10)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
EOF
|
|
52
|
+
@runner.check('app/models/post.rb', content)
|
|
53
|
+
errors = @runner.errors
|
|
54
|
+
errors.should be_empty
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it "should not keep finders on thier own model without finder" do
|
|
58
|
+
content = <<-EOF
|
|
59
|
+
class Post < ActiveRecord::Base
|
|
60
|
+
has_many :comments
|
|
61
|
+
|
|
62
|
+
def find_valid_comments
|
|
63
|
+
self.comments.destroy_all
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
EOF
|
|
67
|
+
@runner.check('app/models/post.rb', content)
|
|
68
|
+
errors = @runner.errors
|
|
69
|
+
errors.should be_empty
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__) + '/../../spec_helper')
|
|
2
|
+
|
|
3
|
+
describe RailsBestPractices::Checks::LawOfDemeterCheck do
|
|
4
|
+
before(:each) do
|
|
5
|
+
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::LawOfDemeterCheck.new)
|
|
6
|
+
|
|
7
|
+
content = <<-EOF
|
|
8
|
+
class Invoice < ActiveRecord::Base
|
|
9
|
+
belongs_to :user
|
|
10
|
+
end
|
|
11
|
+
EOF
|
|
12
|
+
@runner.check('app/models/invoice.rb', content)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "should law of demeter" do
|
|
16
|
+
content = <<-EOF
|
|
17
|
+
<%= @invoice.user.name %>
|
|
18
|
+
<%= @invoice.user.address %>
|
|
19
|
+
<%= @invoice.user.cellphone %>
|
|
20
|
+
EOF
|
|
21
|
+
@runner.check('app/views/invoices/show.html.erb', content)
|
|
22
|
+
errors = @runner.errors
|
|
23
|
+
errors.should_not be_empty
|
|
24
|
+
errors[0].to_s.should == "app/views/invoices/show.html.erb:1 - law of demeter"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "should no law of demeter" do
|
|
28
|
+
content = <<-EOF
|
|
29
|
+
<%= @invoice.user_name %>
|
|
30
|
+
<%= @invoice.user_address %>
|
|
31
|
+
<%= @invoice.user_cellphone %>
|
|
32
|
+
EOF
|
|
33
|
+
@runner.check('app/views/invoices/show.html.erb', content)
|
|
34
|
+
errors = @runner.errors
|
|
35
|
+
errors.should be_empty
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -20,11 +20,11 @@ describe RailsBestPractices::Checks::MoveFinderToNamedScopeCheck do
|
|
|
20
20
|
end
|
|
21
21
|
end
|
|
22
22
|
EOF
|
|
23
|
-
@runner.check('app/
|
|
23
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
24
24
|
errors = @runner.errors
|
|
25
25
|
errors.size.should == 2
|
|
26
|
-
errors[0].to_s.should == "app/
|
|
27
|
-
errors[1].to_s.should == "app/
|
|
26
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:4 - move finder to named_scope"
|
|
27
|
+
errors[1].to_s.should == "app/controllers/posts_controller.rb:8 - move finder to named_scope"
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
it "should not move simple finder" do
|
|
@@ -41,7 +41,7 @@ describe RailsBestPractices::Checks::MoveFinderToNamedScopeCheck do
|
|
|
41
41
|
end
|
|
42
42
|
end
|
|
43
43
|
EOF
|
|
44
|
-
@runner.check('app/
|
|
44
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
45
45
|
@runner.errors.should be_empty
|
|
46
46
|
end
|
|
47
47
|
|
|
@@ -55,7 +55,7 @@ describe RailsBestPractices::Checks::MoveFinderToNamedScopeCheck do
|
|
|
55
55
|
end
|
|
56
56
|
end
|
|
57
57
|
EOF
|
|
58
|
-
@runner.check('app/
|
|
58
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
59
59
|
@runner.errors.should be_empty
|
|
60
60
|
end
|
|
61
61
|
|
|
@@ -79,4 +79,4 @@ describe RailsBestPractices::Checks::MoveFinderToNamedScopeCheck do
|
|
|
79
79
|
@runner.errors.should be_empty
|
|
80
80
|
|
|
81
81
|
end
|
|
82
|
-
end
|
|
82
|
+
end
|
|
@@ -23,10 +23,10 @@ describe RailsBestPractices::Checks::MoveModelLogicIntoModelCheck do
|
|
|
23
23
|
redirect_to post_url(@post)
|
|
24
24
|
end
|
|
25
25
|
EOF
|
|
26
|
-
@runner.check('app/
|
|
26
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
27
27
|
errors = @runner.errors
|
|
28
28
|
errors.should_not be_empty
|
|
29
|
-
errors[0].to_s.should == "app/
|
|
29
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:3 - move model logic into model (@post called_count > 4)"
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
it "should not move model logic into model with simple model calling" do
|
|
@@ -42,8 +42,8 @@ describe RailsBestPractices::Checks::MoveModelLogicIntoModelCheck do
|
|
|
42
42
|
redirect_to post_url(@post)
|
|
43
43
|
end
|
|
44
44
|
EOF
|
|
45
|
-
@runner.check('app/
|
|
45
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
46
46
|
errors = @runner.errors
|
|
47
47
|
errors.should be_empty
|
|
48
48
|
end
|
|
49
|
-
end
|
|
49
|
+
end
|
data/spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb
CHANGED
|
@@ -25,10 +25,10 @@ describe RailsBestPractices::Checks::ReplaceComplexCreationWithFactoryMethodChec
|
|
|
25
25
|
end
|
|
26
26
|
end
|
|
27
27
|
EOF
|
|
28
|
-
@runner.check('app/
|
|
28
|
+
@runner.check('app/controllers/invoices_controller.rb', content)
|
|
29
29
|
errors = @runner.errors
|
|
30
30
|
errors.should_not be_empty
|
|
31
|
-
errors[0].to_s.should == "app/
|
|
31
|
+
errors[0].to_s.should == "app/controllers/invoices_controller.rb:3 - replace complex creation with factory method (@invoice attribute_assignment_count > 2)"
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
it "should not replace complex creation with factory method with simple creation" do
|
|
@@ -43,7 +43,7 @@ describe RailsBestPractices::Checks::ReplaceComplexCreationWithFactoryMethodChec
|
|
|
43
43
|
end
|
|
44
44
|
end
|
|
45
45
|
EOF
|
|
46
|
-
@runner.check('app/
|
|
46
|
+
@runner.check('app/controllers/invoices_controller.rb', content)
|
|
47
47
|
errors = @runner.errors
|
|
48
48
|
errors.should be_empty
|
|
49
49
|
end
|
|
@@ -69,8 +69,8 @@ describe RailsBestPractices::Checks::ReplaceComplexCreationWithFactoryMethodChec
|
|
|
69
69
|
end
|
|
70
70
|
EOF
|
|
71
71
|
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::ReplaceComplexCreationWithFactoryMethodCheck.new('attribute_assignment_count' => 5))
|
|
72
|
-
@runner.check('app/
|
|
72
|
+
@runner.check('app/controllers/invoices_controller.rb', content)
|
|
73
73
|
errors = @runner.errors
|
|
74
74
|
errors.should be_empty
|
|
75
75
|
end
|
|
76
|
-
end
|
|
76
|
+
end
|
|
@@ -16,10 +16,10 @@ describe RailsBestPractices::Checks::UseModelAssociationCheck do
|
|
|
16
16
|
end
|
|
17
17
|
end
|
|
18
18
|
EOF
|
|
19
|
-
@runner.check('app/
|
|
19
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
20
20
|
errors = @runner.errors
|
|
21
21
|
errors.should_not be_empty
|
|
22
|
-
errors[0].to_s.should == "app/
|
|
22
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:3 - use model association (for @post)"
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
it "should not use model association without association assign" do
|
|
@@ -32,7 +32,7 @@ describe RailsBestPractices::Checks::UseModelAssociationCheck do
|
|
|
32
32
|
end
|
|
33
33
|
end
|
|
34
34
|
EOF
|
|
35
|
-
@runner.check('app/
|
|
35
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
36
36
|
errors = @runner.errors
|
|
37
37
|
errors.should be_empty
|
|
38
38
|
end
|
|
@@ -48,10 +48,10 @@ describe RailsBestPractices::Checks::UseModelAssociationCheck do
|
|
|
48
48
|
end
|
|
49
49
|
end
|
|
50
50
|
EOF
|
|
51
|
-
@runner.check('app/
|
|
51
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
52
52
|
errors = @runner.errors
|
|
53
53
|
errors.should_not be_empty
|
|
54
|
-
errors[0].to_s.should == "app/
|
|
54
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:3 - use model association (for post)"
|
|
55
55
|
end
|
|
56
56
|
|
|
57
57
|
it "should not use model association" do
|
|
@@ -64,7 +64,7 @@ describe RailsBestPractices::Checks::UseModelAssociationCheck do
|
|
|
64
64
|
end
|
|
65
65
|
end
|
|
66
66
|
EOF
|
|
67
|
-
@runner.check('app/
|
|
67
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
68
68
|
errors = @runner.errors
|
|
69
69
|
errors.should be_empty
|
|
70
70
|
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__) + '/../../spec_helper')
|
|
2
|
+
|
|
3
|
+
describe RailsBestPractices::Checks::UseObserverCheck do
|
|
4
|
+
before(:each) do
|
|
5
|
+
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::UseObserverCheck.new)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "should use observer" do
|
|
9
|
+
content =<<-EOF
|
|
10
|
+
class Project < ActiveRecord::Base
|
|
11
|
+
after_create :send_create_notification
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
def send_create_notification
|
|
16
|
+
self.members.each do |member|
|
|
17
|
+
ProjectMailer.deliver_notification(self, member)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
EOF
|
|
22
|
+
@runner.check('app/models/project.rb', content)
|
|
23
|
+
errors = @runner.errors
|
|
24
|
+
errors.should_not be_empty
|
|
25
|
+
errors[0].to_s.should == "app/models/project.rb:6 - use observer"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it "should not use observer without callback" do
|
|
29
|
+
content =<<-EOF
|
|
30
|
+
class Project < ActiveRecord::Base
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def send_create_notification
|
|
34
|
+
self.members.each do |member|
|
|
35
|
+
ProjectMailer.deliver_notification(self, member)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
EOF
|
|
40
|
+
@runner.check('app/models/project.rb', content)
|
|
41
|
+
errors = @runner.errors
|
|
42
|
+
errors.should be_empty
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it "should use observer with two after_create" do
|
|
46
|
+
content =<<-EOF
|
|
47
|
+
class Project < ActiveRecord::Base
|
|
48
|
+
after_create :send_create_notification, :update_author
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def send_create_notification
|
|
53
|
+
self.members.each do |member|
|
|
54
|
+
ProjectMailer.deliver_notification(self, member)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def update_author
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
EOF
|
|
62
|
+
@runner.check('app/models/project.rb', content)
|
|
63
|
+
errors = @runner.errors
|
|
64
|
+
errors.should_not be_empty
|
|
65
|
+
errors[0].to_s.should == "app/models/project.rb:6 - use observer"
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -20,10 +20,10 @@ describe RailsBestPractices::Checks::UseScopeAccessCheck do
|
|
|
20
20
|
end
|
|
21
21
|
end
|
|
22
22
|
EOF
|
|
23
|
-
@runner.check('app/
|
|
23
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
24
24
|
errors = @runner.errors
|
|
25
25
|
errors.should_not be_empty
|
|
26
|
-
errors[0].to_s.should == "app/
|
|
26
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:7 - use scope access"
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
it "shoud use scope access by comparing with id" do
|
|
@@ -40,10 +40,10 @@ describe RailsBestPractices::Checks::UseScopeAccessCheck do
|
|
|
40
40
|
end
|
|
41
41
|
end
|
|
42
42
|
EOF
|
|
43
|
-
@runner.check('app/
|
|
43
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
44
44
|
errors = @runner.errors
|
|
45
45
|
errors.should_not be_empty
|
|
46
|
-
errors[0].to_s.should == "app/
|
|
46
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:7 - use scope access"
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
it "shoud use scope access with current_user ==" do
|
|
@@ -60,10 +60,10 @@ describe RailsBestPractices::Checks::UseScopeAccessCheck do
|
|
|
60
60
|
end
|
|
61
61
|
end
|
|
62
62
|
EOF
|
|
63
|
-
@runner.check('app/
|
|
63
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
64
64
|
errors = @runner.errors
|
|
65
65
|
errors.should_not be_empty
|
|
66
|
-
errors[0].to_s.should == "app/
|
|
66
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:7 - use scope access"
|
|
67
67
|
end
|
|
68
68
|
|
|
69
69
|
it "shoud use scope access by current_user.id ==" do
|
|
@@ -80,10 +80,10 @@ describe RailsBestPractices::Checks::UseScopeAccessCheck do
|
|
|
80
80
|
end
|
|
81
81
|
end
|
|
82
82
|
EOF
|
|
83
|
-
@runner.check('app/
|
|
83
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
84
84
|
errors = @runner.errors
|
|
85
85
|
errors.should_not be_empty
|
|
86
|
-
errors[0].to_s.should == "app/
|
|
86
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:7 - use scope access"
|
|
87
87
|
end
|
|
88
88
|
end
|
|
89
89
|
|
|
@@ -102,10 +102,10 @@ describe RailsBestPractices::Checks::UseScopeAccessCheck do
|
|
|
102
102
|
end
|
|
103
103
|
end
|
|
104
104
|
EOF
|
|
105
|
-
@runner.check('app/
|
|
105
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
106
106
|
errors = @runner.errors
|
|
107
107
|
errors.should_not be_empty
|
|
108
|
-
errors[0].to_s.should == "app/
|
|
108
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
|
109
109
|
end
|
|
110
110
|
|
|
111
111
|
it "shoud use scope access by comparing with id" do
|
|
@@ -122,10 +122,10 @@ describe RailsBestPractices::Checks::UseScopeAccessCheck do
|
|
|
122
122
|
end
|
|
123
123
|
end
|
|
124
124
|
EOF
|
|
125
|
-
@runner.check('app/
|
|
125
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
126
126
|
errors = @runner.errors
|
|
127
127
|
errors.should_not be_empty
|
|
128
|
-
errors[0].to_s.should == "app/
|
|
128
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
|
129
129
|
end
|
|
130
130
|
|
|
131
131
|
it "shoud use scope access with current_user ==" do
|
|
@@ -142,10 +142,10 @@ describe RailsBestPractices::Checks::UseScopeAccessCheck do
|
|
|
142
142
|
end
|
|
143
143
|
end
|
|
144
144
|
EOF
|
|
145
|
-
@runner.check('app/
|
|
145
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
146
146
|
errors = @runner.errors
|
|
147
147
|
errors.should_not be_empty
|
|
148
|
-
errors[0].to_s.should == "app/
|
|
148
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
|
149
149
|
end
|
|
150
150
|
|
|
151
151
|
it "shoud use scope access by current_user.id ==" do
|
|
@@ -162,10 +162,10 @@ describe RailsBestPractices::Checks::UseScopeAccessCheck do
|
|
|
162
162
|
end
|
|
163
163
|
end
|
|
164
164
|
EOF
|
|
165
|
-
@runner.check('app/
|
|
165
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
|
166
166
|
errors = @runner.errors
|
|
167
167
|
errors.should_not be_empty
|
|
168
|
-
errors[0].to_s.should == "app/
|
|
168
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
|
169
169
|
end
|
|
170
170
|
end
|
|
171
|
-
end
|
|
171
|
+
end
|