rails_best_practices 0.6.6 → 0.6.7

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 (79) hide show
  1. data/Gemfile +0 -1
  2. data/README.md +28 -24
  3. data/Rakefile +0 -8
  4. data/install_supported_rubies.sh +2 -3
  5. data/lib/rails_best_practices.rb +8 -7
  6. data/lib/rails_best_practices/core.rb +1 -0
  7. data/lib/rails_best_practices/core/check.rb +68 -0
  8. data/lib/rails_best_practices/core/checking_visitor.rb +18 -15
  9. data/lib/rails_best_practices/core/runner.rb +22 -12
  10. data/lib/rails_best_practices/core/visitable_sexp.rb +6 -2
  11. data/lib/rails_best_practices/prepares.rb +11 -0
  12. data/lib/rails_best_practices/prepares/mailer_prepare.rb +33 -0
  13. data/lib/rails_best_practices/prepares/model_prepare.rb +60 -0
  14. data/lib/rails_best_practices/reviews.rb +23 -0
  15. data/lib/rails_best_practices/{checks/add_model_virtual_attribute_check.rb → reviews/add_model_virtual_attribute_review.rb} +6 -9
  16. data/lib/rails_best_practices/{checks/always_add_db_index_check.rb → reviews/always_add_db_index_review.rb} +9 -12
  17. data/lib/rails_best_practices/{checks/dry_bundler_in_capistrano_check.rb → reviews/dry_bundler_in_capistrano_review.rb} +8 -11
  18. data/lib/rails_best_practices/{checks/isolate_seed_data_check.rb → reviews/isolate_seed_data_review.rb} +11 -14
  19. data/lib/rails_best_practices/{checks/keep_finders_on_their_own_model_check.rb → reviews/keep_finders_on_their_own_model_review.rb} +7 -10
  20. data/lib/rails_best_practices/{checks/law_of_demeter_check.rb → reviews/law_of_demeter_review.rb} +10 -14
  21. data/lib/rails_best_practices/{checks/move_code_into_controller_check.rb → reviews/move_code_into_controller_review.rb} +8 -11
  22. data/lib/rails_best_practices/{checks/move_code_into_helper_check.rb → reviews/move_code_into_helper_review.rb} +7 -10
  23. data/lib/rails_best_practices/{checks/move_code_into_model_check.rb → reviews/move_code_into_model_review.rb} +7 -10
  24. data/lib/rails_best_practices/{checks/move_finder_to_named_scope_check.rb → reviews/move_finder_to_named_scope_review.rb} +7 -10
  25. data/lib/rails_best_practices/{checks/move_model_logic_into_model_check.rb → reviews/move_model_logic_into_model_review.rb} +8 -11
  26. data/lib/rails_best_practices/{checks/needless_deep_nesting_check.rb → reviews/needless_deep_nesting_review.rb} +8 -11
  27. data/lib/rails_best_practices/{checks/not_use_default_route_check.rb → reviews/not_use_default_route_review.rb} +7 -10
  28. data/lib/rails_best_practices/{checks/overuse_route_customizations_check.rb → reviews/overuse_route_customizations_review.rb} +10 -13
  29. data/lib/rails_best_practices/{checks/replace_complex_creation_with_factory_method_check.rb → reviews/replace_complex_creation_with_factory_method_review.rb} +8 -11
  30. data/lib/rails_best_practices/{checks/replace_instance_variable_with_local_variable_check.rb → reviews/replace_instance_variable_with_local_variable_review.rb} +7 -10
  31. data/lib/rails_best_practices/reviews/review.rb +92 -0
  32. data/lib/rails_best_practices/{checks/use_before_filter_check.rb → reviews/use_before_filter_review.rb} +8 -11
  33. data/lib/rails_best_practices/{checks/use_model_association_check.rb → reviews/use_model_association_review.rb} +8 -11
  34. data/lib/rails_best_practices/{checks/use_observer_check.rb → reviews/use_observer_review.rb} +14 -41
  35. data/lib/rails_best_practices/{checks/use_query_attribute_check.rb → reviews/use_query_attribute_review.rb} +20 -16
  36. data/lib/rails_best_practices/{checks/use_say_with_time_in_migrations_check.rb → reviews/use_say_with_time_in_migrations_review.rb} +8 -11
  37. data/lib/rails_best_practices/{checks/use_scope_access_check.rb → reviews/use_scope_access_review.rb} +8 -11
  38. data/lib/rails_best_practices/version.rb +1 -1
  39. data/rails_best_practices.gemspec +3 -2
  40. data/rails_best_practices.yml +22 -22
  41. data/rake_rubies.sh +3 -2
  42. data/spec/rails_best_practices/core/check_spec.rb +41 -0
  43. data/spec/rails_best_practices/core/checking_visitor_spec.rb +78 -0
  44. data/spec/rails_best_practices/core/visitable_sexp_spec.rb +39 -36
  45. data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +14 -0
  46. data/spec/rails_best_practices/prepares/model_prepare_spec.rb +22 -0
  47. data/spec/rails_best_practices/{checks/add_model_virtual_attribute_check_spec.rb → reviews/add_model_virtual_attribute_review_spec.rb} +17 -25
  48. data/spec/rails_best_practices/{checks/always_add_db_index_check_spec.rb → reviews/always_add_db_index_review_spec.rb} +28 -41
  49. data/spec/rails_best_practices/{checks/dry_bundler_in_capistrano_check_spec.rb → reviews/dry_bundler_in_capistrano_review_spec.rb} +7 -11
  50. data/spec/rails_best_practices/{checks/isolate_seed_data_check_spec.rb → reviews/isolate_seed_data_review_spec.rb} +13 -20
  51. data/spec/rails_best_practices/{checks/keep_finders_on_their_own_model_check_spec.rb → reviews/keep_finders_on_their_own_model_review_spec.rb} +16 -24
  52. data/spec/rails_best_practices/{checks/law_of_demeter_check_spec.rb → reviews/law_of_demeter_review_spec.rb} +24 -26
  53. data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +29 -0
  54. data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +24 -0
  55. data/spec/rails_best_practices/{checks/move_code_into_model_check_spec.rb → reviews/move_code_into_model_review_spec.rb} +12 -18
  56. data/spec/rails_best_practices/{checks/move_finder_to_named_scope_check_spec.rb → reviews/move_finder_to_named_scope_review_spec.rb} +12 -16
  57. data/spec/rails_best_practices/{checks/move_model_logic_into_model_check_spec.rb → reviews/move_model_logic_into_model_review_spec.rb} +7 -11
  58. data/spec/rails_best_practices/{checks/needless_deep_nesting_check_spec.rb → reviews/needless_deep_nesting_review_spec.rb} +26 -37
  59. data/spec/rails_best_practices/{checks/not_use_default_route_check_spec.rb → reviews/not_use_default_route_review_spec.rb} +13 -19
  60. data/spec/rails_best_practices/{checks/overuse_route_customizations_check_spec.rb → reviews/overuse_route_customizations_review_spec.rb} +27 -39
  61. data/spec/rails_best_practices/{checks/replace_complex_creation_with_factory_method_check_spec.rb → reviews/replace_complex_creation_with_factory_method_review_spec.rb} +10 -15
  62. data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +31 -0
  63. data/spec/rails_best_practices/reviews/review_spec.rb +11 -0
  64. data/spec/rails_best_practices/{checks/use_before_filter_check_spec.rb → reviews/use_before_filter_review_spec.rb} +11 -17
  65. data/spec/rails_best_practices/{checks/use_model_association_check_spec.rb → reviews/use_model_association_review_spec.rb} +12 -18
  66. data/spec/rails_best_practices/{checks/use_observer_check_spec.rb → reviews/use_observer_review_spec.rb} +28 -29
  67. data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +190 -0
  68. data/spec/rails_best_practices/{checks/use_say_with_time_in_migrations_check_spec.rb → reviews/use_say_with_time_in_migrations_review_spec.rb} +14 -23
  69. data/spec/rails_best_practices/{checks/use_scope_access_check_spec.rb → reviews/use_scope_access_review_spec.rb} +28 -37
  70. data/spec/rails_best_practices_spec.rb +4 -4
  71. data/spec/spec_helper.rb +1 -0
  72. metadata +128 -102
  73. data/lib/rails_best_practices/checks.rb +0 -23
  74. data/lib/rails_best_practices/checks/check.rb +0 -203
  75. data/spec/rails_best_practices/checks/check_spec.rb +0 -57
  76. data/spec/rails_best_practices/checks/move_code_into_controller_check_spec.rb +0 -33
  77. data/spec/rails_best_practices/checks/move_code_into_helper_check_spec.rb +0 -28
  78. data/spec/rails_best_practices/checks/replace_instance_variable_with_local_variable_check_spec.rb +0 -36
  79. data/spec/rails_best_practices/checks/use_query_attribute_check_spec.rb +0 -192
@@ -4,6 +4,7 @@ gemset="rails_best_practices"
4
4
 
5
5
  for x in ${rubies[*]}
6
6
  do
7
- rvm use --create $x@$gemset && bundle install || exit $?
7
+ rvm use --create $x@$gemset
8
+ bundle install
9
+ rake spec:progress
8
10
  done
9
- rvm ree@$gemset,1.8.7@$gemset,1.9.2@$gemset rake spec:progress
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe RailsBestPractices::Core::Check do
4
+ let(:check) { RailsBestPractices::Core::Check.new }
5
+
6
+ it "should get empty interesting nodes" do
7
+ check.interesting_nodes.should == []
8
+ end
9
+
10
+ it "should match all files of interesting files" do
11
+ check.interesting_files.should == /.*/
12
+ end
13
+
14
+ context "node_start" do
15
+ it "should call start_if" do
16
+ node = stub(:node_type => :if)
17
+ check.should_receive(:send).with("start_if", node)
18
+ check.node_start(node)
19
+ end
20
+
21
+ it "should call start_call" do
22
+ node = stub(:node_type => :call)
23
+ check.should_receive(:send).with("start_call", node)
24
+ check.node_start(node)
25
+ end
26
+ end
27
+
28
+ context "node_end" do
29
+ it "should call end_if" do
30
+ node = stub(:node_type => :if)
31
+ check.should_receive(:send).with("end_if", node)
32
+ check.node_end(node)
33
+ end
34
+
35
+ it "should call end_call" do
36
+ node = stub(:node_type => :call)
37
+ check.should_receive(:send).with("end_call", node)
38
+ check.node_end(node)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,78 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ class TestPrepare1
5
+ def interesting_nodes
6
+ [:call]
7
+ end
8
+
9
+ def interesting_files
10
+ RailsBestPractices::Core::Check::MODEL_FILES
11
+ end
12
+ end
13
+
14
+ class TestPrepare2
15
+ def interesting_nodes
16
+ [:class]
17
+ end
18
+
19
+ def interesting_files
20
+ RailsBestPractices::Core::Check::MAILER_FILES
21
+ end
22
+ end
23
+
24
+ class TestReview1
25
+ def interesting_nodes
26
+ [:defn]
27
+ end
28
+
29
+ def interesting_files
30
+ RailsBestPractices::Core::Check::CONTROLLER_FILES
31
+ end
32
+ end
33
+
34
+ class TestReview2
35
+ def interesting_nodes
36
+ [:call]
37
+ end
38
+
39
+ def interesting_files
40
+ RailsBestPractices::Core::Check::VIEW_FILES
41
+ end
42
+ end
43
+
44
+ describe RailsBestPractices::Core::CheckingVisitor do
45
+ let(:prepare1) { TestPrepare1.new }
46
+ let(:prepare2) { TestPrepare2.new }
47
+ let(:review1) { TestReview1.new }
48
+ let(:review2) { TestReview2.new }
49
+ let(:visitor) { RailsBestPractices::Core::CheckingVisitor.new([prepare1, prepare2], [review1, review2]) }
50
+
51
+ it "should prepare model associations" do
52
+ node = stub(:node_type => :call, :children => [], :file => "app/models/user.rb")
53
+ prepare1.should_receive(:node_start).with(node)
54
+ prepare1.should_receive(:node_end).with(node)
55
+ visitor.prepare(node)
56
+ end
57
+
58
+ it "should prepare mailer names" do
59
+ node = stub(:node_type => :class, :children => [], :file => "app/mailers/user_mailer.rb")
60
+ prepare2.should_receive(:node_start).with(node)
61
+ prepare2.should_receive(:node_end).with(node)
62
+ visitor.prepare(node)
63
+ end
64
+
65
+ it "should review controller method definitions" do
66
+ node = stub(:node_type => :defn, :children => [], :file => "app/controllers/users_controller.rb")
67
+ review1.should_receive(:node_start).with(node)
68
+ review1.should_receive(:node_end).with(node)
69
+ visitor.review(node)
70
+ end
71
+
72
+ it "should review view calls" do
73
+ node = stub(:node_type => :call, :children => [], :file => "app/views/users/new.html.erb")
74
+ review2.should_receive(:node_start).with(node)
75
+ review2.should_receive(:node_end).with(node)
76
+ visitor.review(node)
77
+ end
78
+ end
@@ -1,20 +1,18 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Sexp do
4
- before :each do
5
- @parser = RubyParser.new
6
- end
4
+ let(:parser) { RubyParser.new }
7
5
 
8
6
  describe "children" do
9
7
  it "should get all child nodes" do
10
- node = @parser.parse("puts 'hello ', 'world'")
8
+ node = parser.parse("puts 'hello ', 'world'")
11
9
  node.children.should == [s(:arglist, s(:str, "hello "), s(:str, "world"))]
12
10
  end
13
11
  end
14
12
 
15
13
  describe "recursive_children" do
16
14
  it "should get all recursive child nodes" do
17
- node = @parser.parse("puts 'hello', dynamic_output")
15
+ node = parser.parse("puts 'hello', dynamic_output")
18
16
  children = []
19
17
  node.recursive_children { |child| children << child }
20
18
  children.should == [s(:arglist, s(:str, "hello"), s(:call, nil, :dynamic_output, s(:arglist))), s(:str, "hello"), s(:call, nil, :dynamic_output, s(:arglist)), s(:arglist)]
@@ -28,7 +26,7 @@ describe Sexp do
28
26
  current_user.posts.find(params[:id])
29
27
  end
30
28
  EOF
31
- @node = @parser.parse(content)
29
+ @node = parser.parse(content)
32
30
  end
33
31
 
34
32
  it "should get the call nodes with empty arguments" do
@@ -51,7 +49,7 @@ describe Sexp do
51
49
  current_user.posts.find(params[:id])
52
50
  end
53
51
  EOF
54
- @node = @parser.parse(content)
52
+ @node = parser.parse(content)
55
53
  end
56
54
 
57
55
  it "should get first node with empty argument" do
@@ -67,7 +65,7 @@ describe Sexp do
67
65
  current_user.posts.find(params[:id])
68
66
  end
69
67
  EOF
70
- @node = @parser.parse(content)
68
+ @node = parser.parse(content)
71
69
  end
72
70
 
73
71
  it "should get the count of call nodes" do
@@ -77,86 +75,86 @@ describe Sexp do
77
75
 
78
76
  describe "subject" do
79
77
  it "should get subject of attrasgn node" do
80
- node = @parser.parse("user.name = params[:name]")
78
+ node = parser.parse("user.name = params[:name]")
81
79
  node.subject.should == s(:call, nil, :user, s(:arglist))
82
80
  end
83
81
 
84
82
  it "should get subject of call node" do
85
- node = @parser.parse("user.name")
83
+ node = parser.parse("user.name")
86
84
  node.subject.should == s(:call, nil, :user, s(:arglist))
87
85
  end
88
86
 
89
87
  it "should get subject of iter node" do
90
- node = @parser.parse("@users.each { |user| p user }")
88
+ node = parser.parse("@users.each { |user| p user }")
91
89
  node.subject.should == s(:call, s(:ivar, :@users), :each, s(:arglist))
92
90
  end
93
91
  end
94
92
 
95
93
  describe "class_name" do
96
94
  it "should get class name of class node" do
97
- node = @parser.parse("class User; end")
95
+ node = parser.parse("class User; end")
98
96
  node.class_name.should == :User
99
97
  end
100
98
  end
101
99
 
102
100
  describe "base_class" do
103
101
  it "should get base class of class node" do
104
- node = @parser.parse("class User < ActiveRecord::Base; end")
102
+ node = parser.parse("class User < ActiveRecord::Base; end")
105
103
  node.base_class.should == s(:colon2, s(:const, :ActiveRecord), :Base)
106
104
  end
107
105
  end
108
106
 
109
107
  describe "left_value" do
110
108
  it "should get the left value of lasgn" do
111
- node = @parser.parse("user = params[:user]")
109
+ node = parser.parse("user = params[:user]")
112
110
  node.left_value.should == :user
113
111
  end
114
112
 
115
113
  it "should get the left value of iasgn" do
116
- node = @parser.parse("@user = params[:user]")
114
+ node = parser.parse("@user = params[:user]")
117
115
  node.left_value.should == :@user
118
116
  end
119
117
  end
120
118
 
121
119
  describe "right_value" do
122
120
  it "should get the right value of lasgn" do
123
- node = @parser.parse("user = current_user")
121
+ node = parser.parse("user = current_user")
124
122
  node.right_value.should == s(:call, nil, :current_user, s(:arglist))
125
123
  end
126
124
 
127
125
  it "should get the right value of iasgn" do
128
- node = @parser.parse("@user = current_user")
126
+ node = parser.parse("@user = current_user")
129
127
  node.right_value.should == s(:call, nil, :current_user, s(:arglist))
130
128
  end
131
129
  end
132
130
 
133
131
  describe "message" do
134
132
  it "should get the message of attrasgn" do
135
- node = @parser.parse("user.name = params[:name]")
133
+ node = parser.parse("user.name = params[:name]")
136
134
  node.message.should == :name=
137
135
  end
138
136
 
139
137
  it "should get the message of call" do
140
- node = @parser.parse("has_many :projects")
138
+ node = parser.parse("has_many :projects")
141
139
  node.message.should == :has_many
142
140
  end
143
141
  end
144
142
 
145
143
  describe "arguments" do
146
144
  it "should get the arguments of attrasgn" do
147
- node = @parser.parse("post.user = current_user")
145
+ node = parser.parse("post.user = current_user")
148
146
  node.arguments.should == s(:arglist, s(:call, nil, :current_user, s(:arglist)))
149
147
  end
150
148
 
151
149
  it "should get the arguments of call" do
152
- node = @parser.parse("username == ''")
150
+ node = parser.parse("username == ''")
153
151
  node.arguments.should == s(:arglist, s(:str, ""))
154
152
  end
155
153
  end
156
154
 
157
155
  describe "conditional_statement" do
158
156
  it "should get conditional statement of if" do
159
- node = @parser.parse("if current_user.present?; puts current_user.login; end")
157
+ node = parser.parse("if current_user.present?; puts current_user.login; end")
160
158
  node.conditional_statement.should == s(:call, s(:call, nil, :current_user, s(:arglist)), :present?, s(:arglist))
161
159
  end
162
160
  end
@@ -170,7 +168,7 @@ describe Sexp do
170
168
  current_user.email
171
169
  end
172
170
  EOF
173
- node = @parser.parse(content)
171
+ node = parser.parse(content)
174
172
  node.true_node.should == s(:call, s(:call, nil, :current_user, s(:arglist)), :login, s(:arglist))
175
173
  end
176
174
  end
@@ -184,75 +182,80 @@ describe Sexp do
184
182
  current_user.email
185
183
  end
186
184
  EOF
187
- node = @parser.parse(content)
185
+ node = parser.parse(content)
188
186
  node.false_node.should == s(:call, s(:call, nil, :current_user, s(:arglist)), :email, s(:arglist))
189
187
  end
190
188
  end
191
189
 
192
190
  describe "method_name" do
193
191
  it "should get the method name of defn" do
194
- node = @parser.parse("def show; end")
192
+ node = parser.parse("def show; end")
195
193
  node.method_name.should == :show
196
194
  end
197
195
  end
198
196
 
199
197
  describe "body" do
200
198
  it "should get body of iter" do
201
- node = @parser.parse("resources :posts do; resources :comments; end")
199
+ node = parser.parse("resources :posts do; resources :comments; end")
202
200
  node.body.should == s(:call, nil, :resources, s(:arglist, s(:lit, :comments)))
203
201
  end
204
202
 
205
203
  it "should get body of class" do
206
- node = @parser.parse("class User; def login; end; def email; end; end")
204
+ node = parser.parse("class User; def login; end; def email; end; end")
207
205
  node.body.should == s(:block, s(:defn, :login, s(:args), s(:scope, s(:block, s(:nil)))), s(:defn, :email, s(:args), s(:scope, s(:block, s(:nil)))))
208
206
  end
209
207
 
210
208
  it "should get body of defn" do
211
- node = @parser.parse("def fullname; first_name + last+name; end")
209
+ node = parser.parse("def fullname; first_name + last+name; end")
212
210
  node.body.should == s(:block, s(:call, s(:call, s(:call, nil, :first_name, s(:arglist)), :+, s(:arglist, s(:call, nil, :last, s(:arglist)))), :+, s(:arglist, s(:call, nil, :name, s(:arglist)))))
213
211
  end
214
212
  end
215
213
 
216
214
  describe "to_s" do
217
215
  it "should get to_s for lvar" do
218
- node = @parser.parse("user = current_user; user")
216
+ node = parser.parse("user = current_user; user")
219
217
  node.children[1].node_type.should == :lvar
220
218
  node.children[1].to_s.should == "user"
221
219
  end
222
220
 
223
221
  it "should get to_s for ivar" do
224
- node = @parser.parse("@user")
222
+ node = parser.parse("@user")
225
223
  node.to_s.should == "@user"
226
224
  end
227
225
 
226
+ it "should get to_s for ivar with remove_at" do
227
+ node = parser.parse("@user")
228
+ node.to_s(:remove_at => true).should == "user"
229
+ end
230
+
228
231
  it "should get to_s for str" do
229
- node = @parser.parse("'user'")
232
+ node = parser.parse("'user'")
230
233
  node.to_s.should == "user"
231
234
  end
232
235
 
233
236
  it "should get to_s for lit" do
234
- node = @parser.parse("{:user => 'Richard'}")
237
+ node = parser.parse("{:user => 'Richard'}")
235
238
  node[1].to_s.should == "user"
236
239
  end
237
240
 
238
241
 
239
242
  it "should get to_s for const" do
240
- node = @parser.parse("User")
243
+ node = parser.parse("User")
241
244
  node.to_s.should == "User"
242
245
  end
243
246
 
244
247
  it "should get to_s for array" do
245
- node = @parser.parse("[@user1, @user2]")
248
+ node = parser.parse("[@user1, @user2]")
246
249
  node.to_s.should == '["@user1", "@user2"]'
247
250
  end
248
251
 
249
252
  it "should get to_s for hash" do
250
- node = @parser.parse("{:first_name => 'Richard', :last_name => 'Huang'}")
253
+ node = parser.parse("{:first_name => 'Richard', :last_name => 'Huang'}")
251
254
  node.to_s.should == '{"first_name" => "Richard", "last_name" => "Huang"}'
252
255
  end
253
256
 
254
257
  it "should get to_s for colon2" do
255
- node = @parser.parse("RailsBestPractices::Core")
258
+ node = parser.parse("RailsBestPractices::Core")
256
259
  node.to_s.should == "RailsBestPractices::Core"
257
260
  end
258
261
  end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe RailsBestPractices::Prepares::MailerPrepare do
4
+ let(:runner) { RailsBestPractices::Core::Runner.new(:prepares => RailsBestPractices::Prepares::MailerPrepare.new) }
5
+
6
+ it "should parse mailer names" do
7
+ content =<<-EOF
8
+ class ProjectMailer < ActionMailer::Base
9
+ end
10
+ EOF
11
+ runner.prepare('app/mailers/project_mailer.rb', content)
12
+ RailsBestPractices::Prepares.mailer_names.should == [:ProjectMailer]
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe RailsBestPractices::Prepares::ModelPrepare do
4
+ let(:runner) { RailsBestPractices::Core::Runner.new(:prepares => RailsBestPractices::Prepares::ModelPrepare.new) }
5
+
6
+ it "should parse model associations" do
7
+ content =<<-EOF
8
+ class Project < ActiveRecord::Base
9
+ belongs_to :portfolio
10
+ has_one :project_manager
11
+ has_many :milestones
12
+ has_and_belongs_to_many :categories
13
+ end
14
+ EOF
15
+ runner.prepare('app/models/project.rb', content)
16
+ model_associations = RailsBestPractices::Prepares.model_associations
17
+ model_associations["Project"]["portfolio"].should == :belongs_to
18
+ model_associations["Project"]["project_manager"].should == :has_one
19
+ model_associations["Project"]["milestones"].should == :has_many
20
+ model_associations["Project"]["categories"].should == :has_and_belongs_to_many
21
+ end
22
+ end
@@ -1,9 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe RailsBestPractices::Checks::AddModelVirtualAttributeCheck do
4
- before(:each) do
5
- @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::AddModelVirtualAttributeCheck.new)
6
- end
3
+ describe RailsBestPractices::Reviews::AddModelVirtualAttributeReview do
4
+ let(:runner) { RailsBestPractices::Core::Runner.new(:reviews => RailsBestPractices::Reviews::AddModelVirtualAttributeReview.new) }
7
5
 
8
6
  it "should add model virtual attribute" do
9
7
  content = <<-EOF
@@ -17,10 +15,9 @@ describe RailsBestPractices::Checks::AddModelVirtualAttributeCheck do
17
15
  end
18
16
  end
19
17
  EOF
20
- @runner.review('app/controllers/users_controller.rb', content)
21
- errors = @runner.errors
22
- errors.should_not be_empty
23
- errors[0].to_s.should == "app/controllers/users_controller.rb:3 - add model virtual attribute (for @user)"
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)"
24
21
  end
25
22
 
26
23
  it "should add model virtual attribute with local assignment" do
@@ -35,10 +32,9 @@ describe RailsBestPractices::Checks::AddModelVirtualAttributeCheck do
35
32
  end
36
33
  end
37
34
  EOF
38
- @runner.review('app/controllers/users_controller.rb', content)
39
- errors = @runner.errors
40
- errors.should_not be_empty
41
- errors[0].to_s.should == "app/controllers/users_controller.rb:3 - add model virtual attribute (for user)"
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)"
42
38
  end
43
39
 
44
40
  it "should not add model virtual attribute with differen param" do
@@ -53,9 +49,8 @@ describe RailsBestPractices::Checks::AddModelVirtualAttributeCheck do
53
49
  end
54
50
  end
55
51
  EOF
56
- @runner.review('app/controllers/users_controller.rb', content)
57
- errors = @runner.errors
58
- errors.should be_empty
52
+ runner.review('app/controllers/users_controller.rb', content)
53
+ runner.should have(0).errors
59
54
  end
60
55
 
61
56
  it "should not add model virtual attribute with read" do
@@ -71,9 +66,8 @@ describe RailsBestPractices::Checks::AddModelVirtualAttributeCheck do
71
66
  end
72
67
  end
73
68
  EOF
74
- @runner.review('app/controllers/users_controller.rb', content)
75
- errors = @runner.errors
76
- errors.should be_empty
69
+ runner.review('app/controllers/users_controller.rb', content)
70
+ runner.should have(0).errors
77
71
  end
78
72
 
79
73
  it "should add model virtual attribute with two dimension params" do
@@ -88,10 +82,9 @@ describe RailsBestPractices::Checks::AddModelVirtualAttributeCheck do
88
82
  end
89
83
  end
90
84
  EOF
91
- @runner.review('app/controllers/users_controller.rb', content)
92
- errors = @runner.errors
93
- errors.should_not be_empty
94
- errors[0].to_s.should == "app/controllers/users_controller.rb:3 - add model virtual attribute (for @user)"
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)"
95
88
  end
96
89
 
97
90
  it "should no add model virtual attribute with two dimension params" do
@@ -106,8 +99,7 @@ describe RailsBestPractices::Checks::AddModelVirtualAttributeCheck do
106
99
  end
107
100
  end
108
101
  EOF
109
- @runner.review('app/controllers/users_controller.rb', content)
110
- errors = @runner.errors
111
- errors.should be_empty
102
+ runner.review('app/controllers/users_controller.rb', content)
103
+ runner.should have(0).errors
112
104
  end
113
105
  end