rails_best_practices 0.10.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/README.md +20 -0
  2. data/assets/result.html.haml +1 -1
  3. data/lib/rails_best_practices.rb +5 -3
  4. data/lib/rails_best_practices/core.rb +1 -1
  5. data/lib/rails_best_practices/core/check.rb +9 -12
  6. data/lib/rails_best_practices/core/checking_visitor.rb +9 -6
  7. data/lib/rails_best_practices/core/model_associations.rb +1 -1
  8. data/lib/rails_best_practices/core/nil.rb +5 -1
  9. data/lib/rails_best_practices/core/runner.rb +5 -5
  10. data/lib/rails_best_practices/core_ext/sexp.rb +688 -0
  11. data/lib/rails_best_practices/prepares/mailer_prepare.rb +4 -5
  12. data/lib/rails_best_practices/prepares/model_prepare.rb +16 -26
  13. data/lib/rails_best_practices/prepares/schema_prepare.rb +11 -17
  14. data/lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb +24 -75
  15. data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +39 -113
  16. data/lib/rails_best_practices/reviews/dry_bundler_in_capistrano_review.rb +6 -16
  17. data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +16 -32
  18. data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +11 -20
  19. data/lib/rails_best_practices/reviews/law_of_demeter_review.rb +7 -28
  20. data/lib/rails_best_practices/reviews/move_code_into_controller_review.rb +16 -14
  21. data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +10 -28
  22. data/lib/rails_best_practices/reviews/move_code_into_model_review.rb +12 -11
  23. data/lib/rails_best_practices/reviews/move_finder_to_named_scope_review.rb +13 -24
  24. data/lib/rails_best_practices/reviews/move_model_logic_into_model_review.rb +9 -9
  25. data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +24 -68
  26. data/lib/rails_best_practices/reviews/not_use_default_route_review.rb +15 -22
  27. data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +31 -91
  28. data/lib/rails_best_practices/reviews/remove_empty_helpers_review.rb +4 -2
  29. data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +20 -18
  30. data/lib/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review.rb +5 -3
  31. data/lib/rails_best_practices/reviews/review.rb +8 -37
  32. data/lib/rails_best_practices/reviews/simplify_render_in_controllers_review.rb +10 -6
  33. data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +9 -6
  34. data/lib/rails_best_practices/reviews/use_before_filter_review.rb +14 -72
  35. data/lib/rails_best_practices/reviews/use_model_association_review.rb +19 -31
  36. data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +5 -5
  37. data/lib/rails_best_practices/reviews/use_observer_review.rb +22 -40
  38. data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +34 -39
  39. data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +14 -38
  40. data/lib/rails_best_practices/reviews/use_scope_access_review.rb +13 -44
  41. data/lib/rails_best_practices/version.rb +1 -1
  42. data/spec/rails_best_practices/core/check_spec.rb +5 -5
  43. data/spec/rails_best_practices/core/checking_visitor_spec.rb +4 -4
  44. data/spec/rails_best_practices/core/model_associations_spec.rb +4 -4
  45. data/spec/rails_best_practices/core/nil_spec.rb +7 -1
  46. data/spec/rails_best_practices/core_ext/sexp_spec.rb +430 -0
  47. data/spec/rails_best_practices/prepares/model_prepare_spec.rb +12 -12
  48. data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +6 -6
  49. data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +14 -2
  50. data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +1 -1
  51. data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +3 -3
  52. data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +1 -1
  53. data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +15 -1
  54. data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +3 -3
  55. data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +1 -1
  56. data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +1 -1
  57. data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +4 -4
  58. data/spec/rails_best_practices_spec.rb +1 -3
  59. data/spec/spec_helper.rb +4 -0
  60. metadata +6 -8
  61. data/lib/rails_best_practices/core/visitable_sexp.rb +0 -444
  62. data/spec/rails_best_practices/core/visitable_sexp_spec.rb +0 -272
  63. data/spec/rails_best_practices/reviews/review_spec.rb +0 -11
@@ -1,272 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Sexp do
4
- let(:parser) { RubyParser.new }
5
-
6
- describe "children" do
7
- it "should get all child nodes" do
8
- node = parser.parse("puts 'hello ', 'world'")
9
- node.children.should == [s(:arglist, s(:str, "hello "), s(:str, "world"))]
10
- end
11
- end
12
-
13
- describe "recursive_children" do
14
- it "should get all recursive child nodes" do
15
- node = parser.parse("puts 'hello', dynamic_output")
16
- children = []
17
- node.recursive_children { |child| children << child }
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)]
19
- end
20
- end
21
-
22
- describe "grep_nodes" do
23
- before :each do
24
- content = <<-EOF
25
- def show
26
- current_user.posts.find(params[:id])
27
- end
28
- EOF
29
- @node = parser.parse(content)
30
- end
31
-
32
- it "should get the call nodes with empty arguments" do
33
- nodes = []
34
- @node.grep_nodes(:node_type => :call, :arguments => s(:arglist)) { |node| nodes << node }
35
- nodes.should == [s(:call, s(:call, nil, :current_user, s(:arglist)), :posts, s(:arglist)), s(:call, nil, :current_user, s(:arglist)), s(:call, nil, :params, s(:arglist))]
36
- end
37
-
38
- it "should get the call nodes with different messages" do
39
- nodes = []
40
- @node.grep_nodes(:node_type => :call, :message => [:current_user, :params]) { |node| nodes << node }
41
- nodes.should == [s(:call, nil, :current_user, s(:arglist)), s(:call, nil, :params, s(:arglist))]
42
- end
43
- end
44
-
45
- describe "grep_node" do
46
- before :each do
47
- content = <<-EOF
48
- def show
49
- current_user.posts.find(params[:id])
50
- end
51
- EOF
52
- @node = parser.parse(content)
53
- end
54
-
55
- it "should get first node with empty argument" do
56
- node = @node.grep_node(:node_type => :call, :arguments => s(:arglist))
57
- node.should == s(:call, s(:call, nil, :current_user, s(:arglist)), :posts, s(:arglist))
58
- end
59
- end
60
-
61
- describe "grep_nodes_count" do
62
- before :each do
63
- content = <<-EOF
64
- def show
65
- current_user.posts.find(params[:id])
66
- end
67
- EOF
68
- @node = parser.parse(content)
69
- end
70
-
71
- it "should get the count of call nodes" do
72
- @node.grep_nodes_count(:node_type => :call).should == 5
73
- end
74
- end
75
-
76
- describe "subject" do
77
- it "should get subject of attrasgn node" do
78
- node = parser.parse("user.name = params[:name]")
79
- node.subject.should == s(:call, nil, :user, s(:arglist))
80
- end
81
-
82
- it "should get subject of call node" do
83
- node = parser.parse("user.name")
84
- node.subject.should == s(:call, nil, :user, s(:arglist))
85
- end
86
-
87
- it "should get subject of iter node" do
88
- node = parser.parse("@users.each { |user| p user }")
89
- node.subject.should == s(:call, s(:ivar, :@users), :each, s(:arglist))
90
- end
91
- end
92
-
93
- describe "class_name" do
94
- it "should get class name of class node" do
95
- node = parser.parse("class User; end")
96
- node.class_name.should == :User
97
- end
98
- end
99
-
100
- describe "base_class" do
101
- it "should get base class of class node" do
102
- node = parser.parse("class User < ActiveRecord::Base; end")
103
- node.base_class.should == s(:colon2, s(:const, :ActiveRecord), :Base)
104
- end
105
- end
106
-
107
- describe "left_value" do
108
- it "should get the left value of lasgn" do
109
- node = parser.parse("user = params[:user]")
110
- node.left_value.should == :user
111
- end
112
-
113
- it "should get the left value of iasgn" do
114
- node = parser.parse("@user = params[:user]")
115
- node.left_value.should == :@user
116
- end
117
- end
118
-
119
- describe "right_value" do
120
- it "should get the right value of lasgn" do
121
- node = parser.parse("user = current_user")
122
- node.right_value.should == s(:call, nil, :current_user, s(:arglist))
123
- end
124
-
125
- it "should get the right value of iasgn" do
126
- node = parser.parse("@user = current_user")
127
- node.right_value.should == s(:call, nil, :current_user, s(:arglist))
128
- end
129
- end
130
-
131
- describe "message" do
132
- it "should get the message of attrasgn" do
133
- node = parser.parse("user.name = params[:name]")
134
- node.message.should == :name=
135
- end
136
-
137
- it "should get the message of call" do
138
- node = parser.parse("has_many :projects")
139
- node.message.should == :has_many
140
- end
141
- end
142
-
143
- describe "arguments" do
144
- it "should get the arguments of attrasgn" do
145
- node = parser.parse("post.user = current_user")
146
- node.arguments.should == s(:arglist, s(:call, nil, :current_user, s(:arglist)))
147
- end
148
-
149
- it "should get the arguments of call" do
150
- node = parser.parse("username == ''")
151
- node.arguments.should == s(:arglist, s(:str, ""))
152
- end
153
- end
154
-
155
- describe "conditional_statement" do
156
- it "should get conditional statement of if" do
157
- node = parser.parse("if current_user.present?; puts current_user.login; end")
158
- node.conditional_statement.should == s(:call, s(:call, nil, :current_user, s(:arglist)), :present?, s(:arglist))
159
- end
160
- end
161
-
162
- describe "true_node" do
163
- it "should get the true node of if" do
164
- content = <<-EOF
165
- if current_user.login?
166
- current_user.login
167
- else
168
- current_user.email
169
- end
170
- EOF
171
- node = parser.parse(content)
172
- node.true_node.should == s(:call, s(:call, nil, :current_user, s(:arglist)), :login, s(:arglist))
173
- end
174
- end
175
-
176
- describe "false_node" do
177
- it "should get the false node of if" do
178
- content = <<-EOF
179
- if current_user.login?
180
- current_user.login
181
- else
182
- current_user.email
183
- end
184
- EOF
185
- node = parser.parse(content)
186
- node.false_node.should == s(:call, s(:call, nil, :current_user, s(:arglist)), :email, s(:arglist))
187
- end
188
- end
189
-
190
- describe "method_name" do
191
- it "should get the method name of defn" do
192
- node = parser.parse("def show; end")
193
- node.method_name.should == :show
194
- end
195
- end
196
-
197
- describe "body" do
198
- it "should get body of iter" do
199
- node = parser.parse("resources :posts do; resources :comments; end")
200
- node.body.should == s(:call, nil, :resources, s(:arglist, s(:lit, :comments)))
201
- end
202
-
203
- it "should get body of class" do
204
- node = parser.parse("class User; def login; end; def email; end; end")
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)))))
206
- end
207
-
208
- it "should get body of defn" do
209
- node = parser.parse("def fullname; first_name + last+name; end")
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)))))
211
- end
212
- end
213
-
214
- describe "to_s" do
215
- it "should get to_s for lvar" do
216
- node = parser.parse("user = current_user; user")
217
- node.children[1].node_type.should == :lvar
218
- node.children[1].to_s.should == "user"
219
- end
220
-
221
- it "should get to_s for ivar" do
222
- node = parser.parse("@user")
223
- node.to_s.should == "@user"
224
- end
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
-
231
- it "should get to_s for str" do
232
- node = parser.parse("'user'")
233
- node.to_s.should == "user"
234
- end
235
-
236
- it "should get to_s for lit" do
237
- node = parser.parse("{:user => 'Richard'}")
238
- node[1].to_s.should == "user"
239
- end
240
-
241
-
242
- it "should get to_s for const" do
243
- node = parser.parse("User")
244
- node.to_s.should == "User"
245
- end
246
-
247
- it "should get to_s for array" do
248
- node = parser.parse("[@user1, @user2]")
249
- node.to_s.should == '["@user1", "@user2"]'
250
- end
251
-
252
- it "should get to_s for hash" do
253
- node = parser.parse("{:first_name => 'Richard', :last_name => 'Huang'}")
254
- node.to_s.should == '{"first_name" => "Richard", "last_name" => "Huang"}'
255
- end
256
-
257
- it "should get to_s for hash with true/false" do
258
- node = parser.parse("{:shallow => true, :index => false}")
259
- node.to_s.should == '{"shallow" => true, "index" => false}'
260
- end
261
-
262
- it "should get to_s for array and hash mixed" do
263
- node = parser.parse("{:only => [:show, :index]}")
264
- node.to_s.should == '{"only" => ["show", "index"]}'
265
- end
266
-
267
- it "should get to_s for colon2" do
268
- node = parser.parse("RailsBestPractices::Core")
269
- node.to_s.should == "RailsBestPractices::Core"
270
- end
271
- end
272
- end
@@ -1,11 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe RailsBestPractices::Reviews::Review do
4
- let(:check) { RailsBestPractices::Reviews::Review.new }
5
-
6
- context "equal?" do
7
- it "should be true when compare symbol string with symbol" do
8
- check.equal?(":test", :test).should be_true
9
- end
10
- end
11
- end