rails_best_practices 1.14.4 → 1.15.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/.ruby-version +1 -1
  3. data/.travis.yml +1 -0
  4. data/README.md +26 -6
  5. data/lib/rails_best_practices/core/check.rb +15 -2
  6. data/lib/rails_best_practices/core/klasses.rb +13 -3
  7. data/lib/rails_best_practices/core/model_associations.rb +1 -1
  8. data/lib/rails_best_practices/core/runner.rb +2 -0
  9. data/lib/rails_best_practices/lexicals/long_line_check.rb +1 -1
  10. data/lib/rails_best_practices/prepares/route_prepare.rb +17 -1
  11. data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +2 -2
  12. data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +2 -2
  13. data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +1 -1
  14. data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +1 -1
  15. data/lib/rails_best_practices/reviews/move_code_into_model_review.rb +1 -1
  16. data/lib/rails_best_practices/reviews/move_model_logic_into_model_review.rb +1 -1
  17. data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +1 -1
  18. data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +1 -1
  19. data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +1 -2
  20. data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +1 -1
  21. data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +2 -1
  22. data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +15 -26
  23. data/lib/rails_best_practices/reviews/use_turbo_sprockets_rails3_review.rb +4 -2
  24. data/lib/rails_best_practices/version.rb +1 -1
  25. data/spec/rails_best_practices/analyzer_spec.rb +11 -11
  26. data/spec/rails_best_practices/core/check_spec.rb +1 -1
  27. data/spec/rails_best_practices/core/error_spec.rb +6 -6
  28. data/spec/rails_best_practices/core/klasses_spec.rb +26 -2
  29. data/spec/rails_best_practices/core/methods_spec.rb +18 -18
  30. data/spec/rails_best_practices/core/model_associations_spec.rb +6 -6
  31. data/spec/rails_best_practices/core/model_attributes_spec.rb +6 -6
  32. data/spec/rails_best_practices/core/modules_spec.rb +3 -3
  33. data/spec/rails_best_practices/core/routes_spec.rb +4 -4
  34. data/spec/rails_best_practices/core/runner_spec.rb +1 -1
  35. data/spec/rails_best_practices/lexicals/long_line_check_spec.rb +18 -6
  36. data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +18 -5
  37. data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +17 -5
  38. data/spec/rails_best_practices/prepares/config_prepare_spec.rb +1 -1
  39. data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +13 -13
  40. data/spec/rails_best_practices/prepares/gemfile_prepare_spec.rb +1 -1
  41. data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +2 -2
  42. data/spec/rails_best_practices/prepares/initializer_prepare_spec.rb +2 -2
  43. data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +1 -1
  44. data/spec/rails_best_practices/prepares/model_prepare_spec.rb +44 -44
  45. data/spec/rails_best_practices/prepares/route_prepare_spec.rb +102 -78
  46. data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +6 -6
  47. data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +25 -9
  48. data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +43 -23
  49. data/spec/rails_best_practices/reviews/check_save_return_value_spec.rb +34 -20
  50. data/spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb +16 -5
  51. data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +25 -3
  52. data/spec/rails_best_practices/reviews/hash_syntax_review_spec.rb +18 -7
  53. data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +30 -7
  54. data/spec/rails_best_practices/reviews/keep_finders_on_their_own_model_review_spec.rb +24 -8
  55. data/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb +24 -12
  56. data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +17 -5
  57. data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +14 -3
  58. data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +24 -13
  59. data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +25 -6
  60. data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +26 -4
  61. data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +31 -18
  62. data/spec/rails_best_practices/reviews/not_rescue_exception_spec.rb +23 -10
  63. data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +34 -7
  64. data/spec/rails_best_practices/reviews/not_use_times_ago_in_words_review_spec.rb +19 -8
  65. data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +33 -19
  66. data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +25 -13
  67. data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +14 -4
  68. data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +91 -23
  69. data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +20 -5
  70. data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +66 -41
  71. data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +28 -4
  72. data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +16 -7
  73. data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +45 -32
  74. data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +20 -9
  75. data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +23 -14
  76. data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +34 -7
  77. data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +21 -6
  78. data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +124 -79
  79. data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +12 -12
  80. data/spec/rails_best_practices/reviews/use_parenthesis_in_method_def_spec.rb +16 -4
  81. data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +39 -27
  82. data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +22 -8
  83. data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +39 -21
  84. data/spec/rails_best_practices/reviews/use_turbo_sprockets_rails3_review_spec.rb +66 -7
  85. metadata +36 -37
  86. data/.ruby-gemset +0 -1
@@ -13,7 +13,7 @@ module RailsBestPractices
13
13
  EOF
14
14
  runner.prepare("app/models/admin/post.rb", content)
15
15
  models = Prepares.models
16
- models.map(&:to_s).should == ["Blog::Post"]
16
+ expect(models.map(&:to_s)).to eq(["Blog::Post"])
17
17
  end
18
18
 
19
19
  it "class_name with modules" do
@@ -25,7 +25,7 @@ module RailsBestPractices
25
25
  EOF
26
26
  runner.prepare("app/models/admin/post.rb", content)
27
27
  models = Prepares.models
28
- models.map(&:to_s).should == ["Blog::Post"]
28
+ expect(models.map(&:to_s)).to eq(["Blog::Post"])
29
29
  end
30
30
  end
31
31
 
@@ -41,10 +41,10 @@ module RailsBestPractices
41
41
  EOF
42
42
  runner.prepare('app/models/project.rb', content)
43
43
  model_associations = Prepares.model_associations
44
- model_associations.get_association("Project", "portfolio").should == {"meta" => "belongs_to", "class_name" => "Portfolio"}
45
- model_associations.get_association("Project", "project_manager").should == {"meta" => "has_one", "class_name" => "ProjectManager"}
46
- model_associations.get_association("Project", "milestones").should == {"meta" => "has_many", "class_name" => "Milestone"}
47
- model_associations.get_association("Project", "categories").should == {"meta" => "has_and_belongs_to_many", "class_name" => "Category"}
44
+ expect(model_associations.get_association("Project", "portfolio")).to eq({"meta" => "belongs_to", "class_name" => "Portfolio"})
45
+ expect(model_associations.get_association("Project", "project_manager")).to eq({"meta" => "has_one", "class_name" => "ProjectManager"})
46
+ expect(model_associations.get_association("Project", "milestones")).to eq({"meta" => "has_many", "class_name" => "Milestone"})
47
+ expect(model_associations.get_association("Project", "categories")).to eq({"meta" => "has_and_belongs_to_many", "class_name" => "Category"})
48
48
  end
49
49
 
50
50
  context "with class_name option" do
@@ -56,7 +56,7 @@ module RailsBestPractices
56
56
  EOF
57
57
  runner.prepare("app/models/post.rb", content)
58
58
  model_associations = Prepares.model_associations
59
- model_associations.get_association("Post", "author").should == {"meta" => "belongs_to", "class_name" => "Person"}
59
+ expect(model_associations.get_association("Post", "author")).to eq({"meta" => "belongs_to", "class_name" => "Person"})
60
60
  end
61
61
 
62
62
  it "should parse has_one" do
@@ -67,7 +67,7 @@ module RailsBestPractices
67
67
  EOF
68
68
  runner.prepare("app/models/post.rb", content)
69
69
  model_associations = Prepares.model_associations
70
- model_associations.get_association("Project", "project_manager").should == {"meta" => "has_one", "class_name" => "Person"}
70
+ expect(model_associations.get_association("Project", "project_manager")).to eq({"meta" => "has_one", "class_name" => "Person"})
71
71
  end
72
72
 
73
73
  it "should parse has_many" do
@@ -78,7 +78,7 @@ module RailsBestPractices
78
78
  EOF
79
79
  runner.prepare("app/models/project.rb", content)
80
80
  model_associations = Prepares.model_associations
81
- model_associations.get_association("Project", "people").should == {"meta" => "has_many", "class_name" => "Person"}
81
+ expect(model_associations.get_association("Project", "people")).to eq({"meta" => "has_many", "class_name" => "Person"})
82
82
  end
83
83
 
84
84
  it "should parse has_and_belongs_to_many" do
@@ -89,7 +89,7 @@ module RailsBestPractices
89
89
  EOF
90
90
  runner.prepare("app/models/citizen.rb", content)
91
91
  model_associations = Prepares.model_associations
92
- model_associations.get_association("Citizen", "nations").should == {"meta" => "has_and_belongs_to_many", "class_name" => "Country"}
92
+ expect(model_associations.get_association("Citizen", "nations")).to eq({"meta" => "has_and_belongs_to_many", "class_name" => "Country"})
93
93
  end
94
94
 
95
95
  context "namespace" do
@@ -108,8 +108,8 @@ module RailsBestPractices
108
108
  runner.prepare("app/models/community/member.rb", content)
109
109
  runner.after_prepare
110
110
  model_associations = Prepares.model_associations
111
- model_associations.get_association("Community", "members").should == {"meta" => "has_many", "class_name" => "Community::Member"}
112
- model_associations.get_association("Community::Member", "community").should == {"meta" => "belongs_to", "class_name" => "Community"}
111
+ expect(model_associations.get_association("Community", "members")).to eq({"meta" => "has_many", "class_name" => "Community::Member"})
112
+ expect(model_associations.get_association("Community::Member", "community")).to eq({"meta" => "belongs_to", "class_name" => "Community"})
113
113
  end
114
114
 
115
115
  it "should parse without namespace" do
@@ -127,8 +127,8 @@ module RailsBestPractices
127
127
  runner.prepare("app/models/community/member.rb", content)
128
128
  runner.after_prepare
129
129
  model_associations = Prepares.model_associations
130
- model_associations.get_association("Community::Member::Rating", "member").should == {"meta" => "belongs_to", "class_name" => "Community::Member"}
131
- model_associations.get_association("Community::Member", "ratings").should == {"meta" => "has_many", "class_name" => "Community::Member::Rating"}
130
+ expect(model_associations.get_association("Community::Member::Rating", "member")).to eq({"meta" => "belongs_to", "class_name" => "Community::Member"})
131
+ expect(model_associations.get_association("Community::Member", "ratings")).to eq({"meta" => "has_many", "class_name" => "Community::Member::Rating"})
132
132
  end
133
133
  end
134
134
  end
@@ -143,7 +143,7 @@ module RailsBestPractices
143
143
  EOF
144
144
  runner.prepare("app/models/person.rb", content)
145
145
  model_associations = Prepares.model_associations
146
- model_associations.get_association("Person", "addresses").should == {"meta" => "embeds_many", "class_name" => "Address"}
146
+ expect(model_associations.get_association("Person", "addresses")).to eq({"meta" => "embeds_many", "class_name" => "Address"})
147
147
  end
148
148
 
149
149
  it "should parse embeds_one" do
@@ -155,7 +155,7 @@ module RailsBestPractices
155
155
  EOF
156
156
  runner.prepare("app/models/lush.rb", content)
157
157
  model_associations = Prepares.model_associations
158
- model_associations.get_association("Lush", "whiskey").should == {"meta" => "embeds_one", "class_name" => "Drink"}
158
+ expect(model_associations.get_association("Lush", "whiskey")).to eq({"meta" => "embeds_one", "class_name" => "Drink"})
159
159
  end
160
160
 
161
161
  it "should parse embedded_in" do
@@ -167,7 +167,7 @@ module RailsBestPractices
167
167
  EOF
168
168
  runner.prepare("app/models/drink.rb", content)
169
169
  model_associations = Prepares.model_associations
170
- model_associations.get_association("Drink", "alcoholic").should == {"meta" => "embedded_in", "class_name" => "Lush"}
170
+ expect(model_associations.get_association("Drink", "alcoholic")).to eq({"meta" => "embedded_in", "class_name" => "Lush"})
171
171
  end
172
172
  end
173
173
 
@@ -181,7 +181,7 @@ module RailsBestPractices
181
181
  EOF
182
182
  runner.prepare("app/models/employee.rb", content)
183
183
  model_associations = Prepares.model_associations
184
- model_associations.get_association("Employee", "desk").should == {"meta" => "one", "class_name" => "Desk"}
184
+ expect(model_associations.get_association("Employee", "desk")).to eq({"meta" => "one", "class_name" => "Desk"})
185
185
  end
186
186
 
187
187
  it "should parse many" do
@@ -193,7 +193,7 @@ module RailsBestPractices
193
193
  EOF
194
194
  runner.prepare("app/models/tree.rb", content)
195
195
  model_associations = Prepares.model_associations
196
- model_associations.get_association("Tree", "birds").should == {"meta" => "many", "class_name" => "Bird"}
196
+ expect(model_associations.get_association("Tree", "birds")).to eq({"meta" => "many", "class_name" => "Bird"})
197
197
  end
198
198
  end
199
199
  end
@@ -208,7 +208,7 @@ module RailsBestPractices
208
208
  EOF
209
209
  runner.prepare("app/models/post.rb", content)
210
210
  methods = Prepares.model_methods
211
- methods.get_methods("Post").map(&:method_name).should == ["save", "find"]
211
+ expect(methods.get_methods("Post").map(&:method_name)).to eq(["save", "find"])
212
212
  end
213
213
 
214
214
  it "should parse model methods with access control" do
@@ -224,10 +224,10 @@ module RailsBestPractices
224
224
  EOF
225
225
  runner.prepare("app/models/post.rb", content)
226
226
  methods = Prepares.model_methods
227
- methods.get_methods("Post").map(&:method_name).should == ["save", "find", "create_or_update", "find_by_sql"]
228
- methods.get_methods("Post", "public").map(&:method_name).should == ["save", "find"]
229
- methods.get_methods("Post", "protected").map(&:method_name).should == ["create_or_update"]
230
- methods.get_methods("Post", "private").map(&:method_name).should == ["find_by_sql"]
227
+ expect(methods.get_methods("Post").map(&:method_name)).to eq(["save", "find", "create_or_update", "find_by_sql"])
228
+ expect(methods.get_methods("Post", "public").map(&:method_name)).to eq(["save", "find"])
229
+ expect(methods.get_methods("Post", "protected").map(&:method_name)).to eq(["create_or_update"])
230
+ expect(methods.get_methods("Post", "private").map(&:method_name)).to eq(["find_by_sql"])
231
231
  end
232
232
 
233
233
  it "should parse model methods with module ::" do
@@ -239,7 +239,7 @@ module RailsBestPractices
239
239
  EOF
240
240
  runner.prepare("app/models/admin/blog/post.rb", content)
241
241
  methods = Prepares.model_methods
242
- methods.get_methods("Admin::Blog::Post").map(&:method_name).should == ["save", "find"]
242
+ expect(methods.get_methods("Admin::Blog::Post").map(&:method_name)).to eq(["save", "find"])
243
243
  end
244
244
 
245
245
  it "should parse model methods with module" do
@@ -255,7 +255,7 @@ module RailsBestPractices
255
255
  EOF
256
256
  runner.prepare("app/models/admin/blog/post.rb", content)
257
257
  methods = Prepares.model_methods
258
- methods.get_methods("Admin::Blog::Post").map(&:method_name).should == ["save", "find"]
258
+ expect(methods.get_methods("Admin::Blog::Post").map(&:method_name)).to eq(["save", "find"])
259
259
  end
260
260
 
261
261
  it "should not add methods from module" do
@@ -272,7 +272,7 @@ module RailsBestPractices
272
272
  EOF
273
273
  runner.prepare("app/models/mixins/mixin.rb", content)
274
274
  methods = Prepares.model_methods
275
- methods.get_methods('Model').should be_empty
275
+ expect(methods.get_methods('Model')).to be_empty
276
276
  end
277
277
  end
278
278
 
@@ -285,7 +285,7 @@ module RailsBestPractices
285
285
  EOF
286
286
  runner.prepare("app/models/post.rb", content)
287
287
  methods = Prepares.model_methods
288
- methods.get_methods("Post").map(&:method_name).should == ["active"]
288
+ expect(methods.get_methods("Post").map(&:method_name)).to eq(["active"])
289
289
  end
290
290
 
291
291
  it "should treat scope as method" do
@@ -296,7 +296,7 @@ module RailsBestPractices
296
296
  EOF
297
297
  runner.prepare("app/models/post.rb", content)
298
298
  methods = Prepares.model_methods
299
- methods.get_methods("Post").map(&:method_name).should == ["active"]
299
+ expect(methods.get_methods("Post").map(&:method_name)).to eq(["active"])
300
300
  end
301
301
  end
302
302
 
@@ -309,7 +309,7 @@ module RailsBestPractices
309
309
  EOF
310
310
  runner.prepare("app/models/post.rb", content)
311
311
  methods = Prepares.model_methods
312
- methods.get_methods("Post").map(&:method_name).should == ["new"]
312
+ expect(methods.get_methods("Post").map(&:method_name)).to eq(["new"])
313
313
  end
314
314
 
315
315
  it "should treat alias_method as method" do
@@ -320,7 +320,7 @@ module RailsBestPractices
320
320
  EOF
321
321
  runner.prepare("app/models/post.rb", content)
322
322
  methods = Prepares.model_methods
323
- methods.get_methods("Post").map(&:method_name).should == ["new"]
323
+ expect(methods.get_methods("Post").map(&:method_name)).to eq(["new"])
324
324
  end
325
325
 
326
326
  it "should treat alias_method_chain as method" do
@@ -331,7 +331,7 @@ module RailsBestPractices
331
331
  EOF
332
332
  runner.prepare("app/models/post.rb", content)
333
333
  methods = Prepares.model_methods
334
- methods.get_methods("Post").map(&:method_name).should == ["method_with_feature", "method"]
334
+ expect(methods.get_methods("Post").map(&:method_name)).to eq(["method_with_feature", "method"])
335
335
  end
336
336
  end
337
337
 
@@ -349,11 +349,11 @@ module RailsBestPractices
349
349
  EOF
350
350
  runner.prepare("app/models/post.rb", content)
351
351
  model_attributes = Prepares.model_attributes
352
- model_attributes.get_attribute_type("Post", "title").should == "String"
353
- model_attributes.get_attribute_type("Post", "tags").should == "Array"
354
- model_attributes.get_attribute_type("Post", "comments_count").should == "Integer"
355
- model_attributes.get_attribute_type("Post", "deleted_at").should == "DateTime"
356
- model_attributes.get_attribute_type("Post", "active").should == "Boolean"
352
+ expect(model_attributes.get_attribute_type("Post", "title")).to eq("String")
353
+ expect(model_attributes.get_attribute_type("Post", "tags")).to eq("Array")
354
+ expect(model_attributes.get_attribute_type("Post", "comments_count")).to eq("Integer")
355
+ expect(model_attributes.get_attribute_type("Post", "deleted_at")).to eq("DateTime")
356
+ expect(model_attributes.get_attribute_type("Post", "active")).to eq("Boolean")
357
357
  end
358
358
 
359
359
  it "should parse mongomapper field" do
@@ -370,12 +370,12 @@ module RailsBestPractices
370
370
  EOF
371
371
  runner.prepare("app/models/post.rb", content)
372
372
  model_attributes = Prepares.model_attributes
373
- model_attributes.get_attribute_type("Post", "first_name").should == "String"
374
- model_attributes.get_attribute_type("Post", "last_name").should == "String"
375
- model_attributes.get_attribute_type("Post", "age").should == "Integer"
376
- model_attributes.get_attribute_type("Post", "born_at").should == "Time"
377
- model_attributes.get_attribute_type("Post", "active").should == "Boolean"
378
- model_attributes.get_attribute_type("Post", "fav_colors").should == "Array"
373
+ expect(model_attributes.get_attribute_type("Post", "first_name")).to eq("String")
374
+ expect(model_attributes.get_attribute_type("Post", "last_name")).to eq("String")
375
+ expect(model_attributes.get_attribute_type("Post", "age")).to eq("Integer")
376
+ expect(model_attributes.get_attribute_type("Post", "born_at")).to eq("Time")
377
+ expect(model_attributes.get_attribute_type("Post", "active")).to eq("Boolean")
378
+ expect(model_attributes.get_attribute_type("Post", "fav_colors")).to eq("Array")
379
379
  end
380
380
  end
381
381
 
@@ -387,7 +387,7 @@ module RailsBestPractices
387
387
  end
388
388
  EOF
389
389
  content.sub!('?', '\'SELECT event_notification_templates.* from event_notification_templates where event_type_id=#{event_type_id} and delivery_method_id=#{delivery_method_id}\'')
390
- lambda { runner.prepare('app/models/event_subscription.rb', content) }.should_not raise_error
390
+ expect { runner.prepare('app/models/event_subscription.rb', content) }.not_to raise_error
391
391
  end
392
392
  end
393
393
  end