rails_best_practices 0.7.5 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -108,6 +108,8 @@ Now you can customize this configuration file, the default configuration is as f
108
108
  UseQueryAttributeCheck: { }
109
109
  RemoveTrailingWhitespaceCheck: { }
110
110
  UseMultipartAlternativeAsContentTypeOfEmailCheck: {}
111
+ SimplifyRenderInViewsCheck: {}
112
+ SimplifyRenderInControllersCheck: {}
111
113
 
112
114
  You can remove or comment one review to disable it, and you can change the options.
113
115
 
@@ -156,6 +158,7 @@ View
156
158
  2. Move code into model
157
159
  3. Move code into helper
158
160
  4. Replace instance variable with local variable
161
+ 5. Simplify render in views
159
162
 
160
163
  Deployment
161
164
 
@@ -126,7 +126,8 @@ module RailsBestPractices
126
126
  content = Haml::Engine.new(content).precompiled
127
127
  # remove \xxx characters
128
128
  content.gsub!(/\\\d{3}/, '')
129
- rescue Haml::SyntaxError
129
+ rescue Haml::Error
130
+ # do nothing, just ignore the wrong haml files.
130
131
  end
131
132
  end
132
133
  content
@@ -6,7 +6,15 @@ require 'rails_best_practices/prepares/schema_prepare'
6
6
  module RailsBestPractices
7
7
  module Prepares
8
8
  class <<self
9
- attr_accessor :models, :model_associations, :model_attributes, :mailer_names
9
+ attr_writer :models, :model_associations, :model_attributes, :mailer_names
10
+
11
+ [:models, :model_associations, :model_attributes, :mailer_names].each do |method_name|
12
+ class_eval <<-EOS
13
+ def #{method_name}
14
+ @#{method_name} ||= []
15
+ end
16
+ EOS
17
+ end
10
18
  end
11
19
  end
12
20
  end
@@ -22,3 +22,5 @@ require 'rails_best_practices/reviews/dry_bundler_in_capistrano_review'
22
22
  require 'rails_best_practices/reviews/use_say_with_time_in_migrations_review'
23
23
  require 'rails_best_practices/reviews/use_query_attribute_review'
24
24
  require 'rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review'
25
+ require 'rails_best_practices/reviews/simplify_render_in_views_review'
26
+ require 'rails_best_practices/reviews/simplify_render_in_controllers_review'
@@ -201,8 +201,8 @@ module RailsBestPractices
201
201
 
202
202
  # remove the non foreign keys with only _type column.
203
203
  def remove_only_type_foreign_keys
204
- @foreign_keys.delete_if { |table, foreign_key|
205
- foreign_key.size == 1 && foreign_key[0] =~ /_type$/
204
+ @foreign_keys.each { |table, foreign_keys|
205
+ foreign_keys.delete_if { |key| key =~ /_type$/ }
206
206
  }
207
207
  end
208
208
 
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+ require 'rails_best_practices/reviews/review'
3
+
4
+ module RailsBestPractices
5
+ module Reviews
6
+ # Review a controller file to make sure using simplified syntax for render.
7
+ #
8
+ # See the best practice details here http://rails-bestpractices.com/posts/62-simplify-render-in-controllers.
9
+ #
10
+ # Implementation:
11
+ #
12
+ # Review process:
13
+ # check all render method calls in controller files,
14
+ # if there is a key 'action', 'template' or 'file' in the argument,
15
+ # then they should be replaced by simplified syntax.
16
+ class SimplifyRenderInControllersReview < Review
17
+ def url
18
+ "http://rails-bestpractices.com/posts/62-simplify-render-in-controllers"
19
+ end
20
+
21
+ def interesting_nodes
22
+ [:call]
23
+ end
24
+
25
+ def interesting_files
26
+ CONTROLLER_FILES
27
+ end
28
+
29
+ # check call node in the controller file,
30
+ # if its message is render and the arguments contain a key action, template or file,
31
+ # then it should be replaced by simplified syntax.
32
+ def start_call(call_node)
33
+ if :render == call_node.message && call_node.arguments[1].to_s =~ /"(action|template|file)" =>/
34
+ add_error 'simplify render in controllers'
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+ require 'rails_best_practices/reviews/review'
3
+
4
+ module RailsBestPractices
5
+ module Reviews
6
+ # Review a view file to make sure using simplified syntax for render.
7
+ #
8
+ # See the best practice details here http://rails-bestpractices.com/posts/61-simplify-render-in-views.
9
+ #
10
+ # Implementation:
11
+ #
12
+ # Review process:
13
+ # check all render method calls in view files,
14
+ # if there is a key 'partial' in the argument, then they should be replaced by simplified syntax.
15
+ class SimplifyRenderInViewsReview < Review
16
+ def url
17
+ "http://rails-bestpractices.com/posts/61-simplify-render-in-views"
18
+ end
19
+
20
+ def interesting_nodes
21
+ [:call]
22
+ end
23
+
24
+ def interesting_files
25
+ VIEW_FILES
26
+ end
27
+
28
+ # check call node in view file,
29
+ # if its message is render and the arguments contain a key partial,
30
+ # then it should be replaced by simplified syntax.
31
+ def start_call(call_node)
32
+ if :render == call_node.message && call_node.arguments[1].to_s =~ /"partial" =>/
33
+ add_error 'simplify render in views'
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
  module RailsBestPractices
3
- VERSION = "0.7.5"
3
+ VERSION = "0.8.0"
4
4
  end
5
5
 
@@ -22,3 +22,5 @@ UseSayWithTimeInMigrationsCheck: { }
22
22
  UseQueryAttributeCheck: { }
23
23
  RemoveTrailingWhitespaceCheck: { }
24
24
  UseMultipartAlternativeAsContentTypeOfEmailCheck: {}
25
+ SimplifyRenderInViewsCheck: {}
26
+ SimplifyRenderInControllersCheck: {}
@@ -83,6 +83,22 @@ describe RailsBestPractices::Reviews::AlwaysAddDbIndexReview do
83
83
  runner.errors[0].to_s.should == "db/schema.rb:2 - always add db index (taggings => [tag_id])"
84
84
  end
85
85
 
86
+ it "should always add db index only _id without non related _type column" do
87
+ content = <<-EOF
88
+ ActiveRecord::Schema.define(:version => 20100603080629) do
89
+ create_table "websites", :force => true do |t|
90
+ t.integer "user_id"
91
+ t.string "icon_file_name"
92
+ t.integer "icon_file_size"
93
+ t.string "icon_content_type"
94
+ end
95
+ end
96
+ EOF
97
+ runner.review('db/schema.rb', content)
98
+ runner.should have(1).errors
99
+ runner.errors[0].to_s.should == "db/schema.rb:2 - always add db index (websites => [user_id])"
100
+ end
101
+
86
102
  it "should not always add db index with column has no id" do
87
103
  content = <<-EOF
88
104
  ActiveRecord::Schema.define(:version => 20100603080629) do
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe RailsBestPractices::Reviews::SimplifyRenderInControllersReview do
4
+ let(:runner) { RailsBestPractices::Core::Runner.new(:reviews => RailsBestPractices::Reviews::SimplifyRenderInControllersReview.new) }
5
+
6
+ it "should simplify render action view" do
7
+ content =<<-EOF
8
+ def edit
9
+ render :action => :edit
10
+ end
11
+ EOF
12
+ runner.review("app/controllers/posts_controller.rb", content)
13
+ runner.should have(1).errors
14
+ runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:3 - simplify render in controllers"
15
+ end
16
+
17
+ it "should simplify render actions's template" do
18
+ content =<<-EOF
19
+ def edit
20
+ render :template => 'books/edit'
21
+ end
22
+ EOF
23
+ runner.review("app/controllers/posts_controller.rb", content)
24
+ runner.should have(1).errors
25
+ runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:3 - simplify render in controllers"
26
+ end
27
+
28
+ it "should simplify render an arbitrary file" do
29
+ content =<<-EOF
30
+ def edit
31
+ render :file => '/path/to/rails/app/views/books/edit'
32
+ end
33
+ EOF
34
+ runner.review("app/controllers/posts_controller.rb", content)
35
+ runner.should have(1).errors
36
+ runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:3 - simplify render in controllers"
37
+ end
38
+
39
+ it "should not simplify render action view" do
40
+ content =<<-EOF
41
+ render :edit
42
+ EOF
43
+ runner.review("app/controllers/posts_controller", content)
44
+ runner.should have(0).errors
45
+ end
46
+
47
+ it "should not simplify render actions's template" do
48
+ content =<<-EOF
49
+ def edit
50
+ render 'books/edit'
51
+ end
52
+ EOF
53
+ runner.review("app/controllers/posts_controller.rb", content)
54
+ runner.should have(0).errors
55
+ end
56
+
57
+ it "should not simplify render an arbitrary file" do
58
+ content =<<-EOF
59
+ def edit
60
+ render '/path/to/rails/app/views/books/edit'
61
+ end
62
+ EOF
63
+ runner.review("app/controllers/posts_controller.rb", content)
64
+ runner.should have(0).errors
65
+ end
66
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ describe RailsBestPractices::Reviews::SimplifyRenderInViewsReview do
4
+ let(:runner) { RailsBestPractices::Core::Runner.new(:reviews => RailsBestPractices::Reviews::SimplifyRenderInViewsReview.new) }
5
+
6
+ it "should simplify render simple partial" do
7
+ content =<<-EOF
8
+ <%= render :partial => 'sidebar' %>
9
+ EOF
10
+ runner.review('app/views/posts/index.html.erb', content)
11
+ runner.should have(1).errors
12
+ runner.errors[0].to_s.should == "app/views/posts/index.html.erb:1 - simplify render in views"
13
+ end
14
+
15
+ it "should simplify render partial with object" do
16
+ content =<<-EOF
17
+ <%= render :partial => 'posts/post', :object => @post %>
18
+ EOF
19
+ runner.review('app/views/posts/index.html.erb', content)
20
+ runner.should have(1).errors
21
+ runner.errors[0].to_s.should == "app/views/posts/index.html.erb:1 - simplify render in views"
22
+ end
23
+
24
+ it "should simplify render partial with collection" do
25
+ content =<<-EOF
26
+ <%= render :partial => 'posts', :collection => @posts %>
27
+ EOF
28
+ runner.review('app/views/posts/index.html.erb', content)
29
+ runner.should have(1).errors
30
+ runner.errors[0].to_s.should == "app/views/posts/index.html.erb:1 - simplify render in views"
31
+ end
32
+
33
+ it "should simplify render partial with local variables" do
34
+ content =<<-EOF
35
+ <%= render :partial => 'comments/comment', :locals => { :parent => post } %>
36
+ EOF
37
+ runner.review('app/views/posts/index.html.erb', content)
38
+ runner.should have(1).errors
39
+ runner.errors[0].to_s.should == "app/views/posts/index.html.erb:1 - simplify render in views"
40
+ end
41
+
42
+ it "should not simplify render simple partial" do
43
+ content =<<-EOF
44
+ <%= render 'sidebar' %>
45
+ <%= render 'shared/sidebar' %>
46
+ EOF
47
+ runner.review('app/views/posts/index.html.erb', content)
48
+ runner.should have(0).errors
49
+ end
50
+
51
+ it "should not simplify render partial with object" do
52
+ content =<<-EOF
53
+ <%= render @post %>
54
+ EOF
55
+ runner.review('app/views/posts/index.html.erb', content)
56
+ runner.should have(0).errors
57
+ end
58
+
59
+ it "should not simplify render partial with collection" do
60
+ content =<<-EOF
61
+ <%= render @posts %>
62
+ EOF
63
+ runner.review('app/views/posts/index.html.erb', content)
64
+ runner.should have(0).errors
65
+ end
66
+
67
+ it "should not simplify render partial with local variables" do
68
+ content =<<-EOF
69
+ <%= render 'comments/comment', :parent => post %>
70
+ EOF
71
+ runner.review('app/views/posts/index.html.erb', content)
72
+ runner.should have(0).errors
73
+ end
74
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_best_practices
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 63
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 7
9
- - 5
10
- version: 0.7.5
8
+ - 8
9
+ - 0
10
+ version: 0.8.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Richard Huang
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-28 00:00:00 +08:00
18
+ date: 2011-04-23 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -247,6 +247,8 @@ files:
247
247
  - lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb
248
248
  - lib/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review.rb
249
249
  - lib/rails_best_practices/reviews/review.rb
250
+ - lib/rails_best_practices/reviews/simplify_render_in_controllers_review.rb
251
+ - lib/rails_best_practices/reviews/simplify_render_in_views_review.rb
250
252
  - lib/rails_best_practices/reviews/use_before_filter_review.rb
251
253
  - lib/rails_best_practices/reviews/use_model_association_review.rb
252
254
  - lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb
@@ -288,6 +290,8 @@ files:
288
290
  - spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb
289
291
  - spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb
290
292
  - spec/rails_best_practices/reviews/review_spec.rb
293
+ - spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb
294
+ - spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb
291
295
  - spec/rails_best_practices/reviews/use_before_filter_review_spec.rb
292
296
  - spec/rails_best_practices/reviews/use_model_association_review_spec.rb
293
297
  - spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb
@@ -378,6 +382,8 @@ test_files:
378
382
  - spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb
379
383
  - spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb
380
384
  - spec/rails_best_practices/reviews/review_spec.rb
385
+ - spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb
386
+ - spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb
381
387
  - spec/rails_best_practices/reviews/use_before_filter_review_spec.rb
382
388
  - spec/rails_best_practices/reviews/use_model_association_review_spec.rb
383
389
  - spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb