rails_best_practices 0.2.15 → 0.2.16

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 CHANGED
@@ -58,6 +58,7 @@ AlwaysAddDbIndexCheck: { }
58
58
  UseFilterCheck: { }
59
59
  MoveCodeIntoControllerCheck: { }
60
60
  MoveCodeIntoHelperCheck: { }
61
+ ReplaceInstanceVariableWithLocalVariableCheck: { }
61
62
  </code></pre>
62
63
 
63
64
  *************************************************
@@ -102,7 +103,7 @@ h2. Progress
102
103
  ## [-Move code into controller-]
103
104
  ## Move code into model
104
105
  ## [-Move code into helper-]
105
- ## Replace instance variable with local variable
106
+ ## [-Replace instance variable with local variable-]
106
107
  ## Use Form Builder
107
108
  ## Organize Helper files
108
109
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.15
1
+ 0.2.16
@@ -18,3 +18,4 @@ require 'rails_best_practices/checks/always_add_db_index_check'
18
18
  require 'rails_best_practices/checks/use_filter_check'
19
19
  require 'rails_best_practices/checks/move_code_into_controller_check'
20
20
  require 'rails_best_practices/checks/move_code_into_helper_check'
21
+ require 'rails_best_practices/checks/replace_instance_variable_with_local_variable_check'
@@ -3,12 +3,13 @@ require 'rails_best_practices/core/error'
3
3
  module RailsBestPractices
4
4
  module Checks
5
5
  class Check
6
- NODE_TYPES = [:call, :defn, :defs, :if, :unless, :class, :lasgn]
6
+ NODE_TYPES = [:call, :defn, :defs, :if, :unless, :class, :lasgn, :ivar]
7
7
 
8
8
  CONTROLLER_FILES = /_controller.rb$/
9
9
  MIGRATION_FILES = /db\/migrate\/.*rb/
10
10
  MODLE_FILES = /models\/.*rb/
11
11
  VIEW_FILES = /views\/.*erb/
12
+ PARTIAL_VIEW_FILES = /views\/.*\/_.*erb/
12
13
 
13
14
  def initialize
14
15
  @errors = []
@@ -0,0 +1,23 @@
1
+ require 'rails_best_practices/checks/check'
2
+
3
+ module RailsBestPractices
4
+ module Checks
5
+ # Check a partail view file to make sure there is no instance variable.
6
+ #
7
+ # Implementation: Check all instance variable, if exists, then it should be replaced with local variable
8
+ class ReplaceInstanceVariableWithLocalVariableCheck < Check
9
+
10
+ def interesting_nodes
11
+ [:ivar]
12
+ end
13
+
14
+ def interesting_files
15
+ PARTIAL_VIEW_FILES
16
+ end
17
+
18
+ def evaluate_start(node)
19
+ add_error "replace instance variable with local variable"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rails_best_practices}
8
- s.version = "0.2.15"
8
+ s.version = "0.2.16"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Richard Huang"]
@@ -43,6 +43,7 @@ Gem::Specification.new do |s|
43
43
  "lib/rails_best_practices/checks/not_use_default_route_check.rb",
44
44
  "lib/rails_best_practices/checks/overuse_route_customizations_check.rb",
45
45
  "lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb",
46
+ "lib/rails_best_practices/checks/replace_instance_variable_with_local_variable_check.rb",
46
47
  "lib/rails_best_practices/checks/use_filter_check.rb",
47
48
  "lib/rails_best_practices/checks/use_model_association_check.rb",
48
49
  "lib/rails_best_practices/checks/use_model_callback_check.rb",
@@ -72,6 +73,7 @@ Gem::Specification.new do |s|
72
73
  "spec/rails_best_practices/checks/not_use_default_route_check_spec.rb",
73
74
  "spec/rails_best_practices/checks/overuse_route_customizations_check_spec.rb",
74
75
  "spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb",
76
+ "spec/rails_best_practices/checks/replace_instance_variable_with_local_variable_check_spec.rb",
75
77
  "spec/rails_best_practices/checks/use_filter_check_spec.rb",
76
78
  "spec/rails_best_practices/checks/use_model_association_check_spec.rb",
77
79
  "spec/rails_best_practices/checks/use_model_callback_check_spec.rb",
@@ -101,6 +103,7 @@ Gem::Specification.new do |s|
101
103
  "spec/rails_best_practices/checks/not_use_default_route_check_spec.rb",
102
104
  "spec/rails_best_practices/checks/overuse_route_customizations_check_spec.rb",
103
105
  "spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb",
106
+ "spec/rails_best_practices/checks/replace_instance_variable_with_local_variable_check_spec.rb",
104
107
  "spec/rails_best_practices/checks/use_filter_check_spec.rb",
105
108
  "spec/rails_best_practices/checks/use_model_association_check_spec.rb",
106
109
  "spec/rails_best_practices/checks/use_model_callback_check_spec.rb",
@@ -18,3 +18,4 @@ AlwaysAddDbIndexCheck: { }
18
18
  UseFilterCheck: { }
19
19
  MoveCodeIntoControllerCheck: { }
20
20
  MoveCodeIntoHelperCheck: { }
21
+ ReplaceInstanceVariableWithLocalVariableCheck: { }
@@ -15,6 +15,7 @@ describe RailsBestPractices::Checks::MoveCodeIntoHelperCheck do
15
15
  @runner.check('app/views/posts/show.html.erb', content)
16
16
  errors = @runner.errors
17
17
  errors.should_not be_empty
18
+ errors[0].to_s.should == "app/views/posts/show.html.erb:3 - move code into helper"
18
19
  end
19
20
 
20
21
  it "should not move code into helper with simple arguments" do
@@ -0,0 +1,26 @@
1
+ require File.join(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe RailsBestPractices::Checks::ReplaceInstanceVariableWithLocalVariableCheck do
4
+ before(:each) do
5
+ @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::ReplaceInstanceVariableWithLocalVariableCheck.new)
6
+ end
7
+
8
+ it "should replace instance variable with local varialbe" do
9
+ content = <<-EOF
10
+ <%= @post.title %>
11
+ EOF
12
+ @runner.check('app/views/posts/_post.html.erb', content)
13
+ errors = @runner.errors
14
+ errors.should_not be_empty
15
+ errors[0].to_s.should == "app/views/posts/_post.html.erb:1 - replace instance variable with local variable"
16
+ end
17
+
18
+ it "should not replace instance variable with local varialbe" do
19
+ content = <<-EOF
20
+ <%= post.title %>
21
+ EOF
22
+ @runner.check('app/views/posts/_post.html.erb', content)
23
+ errors = @runner.errors
24
+ errors.should be_empty
25
+ end
26
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_best_practices
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.15
4
+ version: 0.2.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
@@ -66,6 +66,7 @@ files:
66
66
  - lib/rails_best_practices/checks/not_use_default_route_check.rb
67
67
  - lib/rails_best_practices/checks/overuse_route_customizations_check.rb
68
68
  - lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
69
+ - lib/rails_best_practices/checks/replace_instance_variable_with_local_variable_check.rb
69
70
  - lib/rails_best_practices/checks/use_filter_check.rb
70
71
  - lib/rails_best_practices/checks/use_model_association_check.rb
71
72
  - lib/rails_best_practices/checks/use_model_callback_check.rb
@@ -95,6 +96,7 @@ files:
95
96
  - spec/rails_best_practices/checks/not_use_default_route_check_spec.rb
96
97
  - spec/rails_best_practices/checks/overuse_route_customizations_check_spec.rb
97
98
  - spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb
99
+ - spec/rails_best_practices/checks/replace_instance_variable_with_local_variable_check_spec.rb
98
100
  - spec/rails_best_practices/checks/use_filter_check_spec.rb
99
101
  - spec/rails_best_practices/checks/use_model_association_check_spec.rb
100
102
  - spec/rails_best_practices/checks/use_model_callback_check_spec.rb
@@ -146,6 +148,7 @@ test_files:
146
148
  - spec/rails_best_practices/checks/not_use_default_route_check_spec.rb
147
149
  - spec/rails_best_practices/checks/overuse_route_customizations_check_spec.rb
148
150
  - spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb
151
+ - spec/rails_best_practices/checks/replace_instance_variable_with_local_variable_check_spec.rb
149
152
  - spec/rails_best_practices/checks/use_filter_check_spec.rb
150
153
  - spec/rails_best_practices/checks/use_model_association_check_spec.rb
151
154
  - spec/rails_best_practices/checks/use_model_callback_check_spec.rb