rails_best_practices 0.2.13 → 0.2.14

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
@@ -28,6 +28,8 @@ at the root directory of rails app
28
28
  rails_best_practices .
29
29
  </code></pre>
30
30
 
31
+ notice the period at the end, it can be the relative or absolute path of your rails app.
32
+
31
33
  *************************************************
32
34
 
33
35
  h2. Customize Configuration
@@ -54,6 +56,7 @@ UseObserverCheck: { }
54
56
  IsolateSeedDataCheck: { }
55
57
  AlwaysAddDbIndexCheck: { }
56
58
  UseFilterCheck: { }
59
+ MoveCodeIntoControllerCheck: { }
57
60
  </code></pre>
58
61
 
59
62
  *************************************************
@@ -95,7 +98,7 @@ h2. Progress
95
98
  ## DRY Controller
96
99
 
97
100
  * Lesson 6. View
98
- ## Move code into controller
101
+ ## [-Move code into controller-]
99
102
  ## Move code into model
100
103
  ## Move code into helper
101
104
  ## Replace instance variable with local variable
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.13
1
+ 0.2.14
@@ -13,7 +13,7 @@ module RailsBestPractices
13
13
  end
14
14
 
15
15
  def interesting_files
16
- /_controller.rb$/
16
+ CONTROLLER_FILES
17
17
  end
18
18
 
19
19
  def evaluate_start(node)
@@ -15,7 +15,7 @@ module RailsBestPractices
15
15
  end
16
16
 
17
17
  def interesting_files
18
- /db\/migrate\/.*rb/
18
+ MIGRATION_FILES
19
19
  end
20
20
 
21
21
  def initialize
@@ -4,6 +4,11 @@ module RailsBestPractices
4
4
  module Checks
5
5
  class Check
6
6
  NODE_TYPES = [:call, :defn, :defs, :if, :unless, :class, :lasgn]
7
+
8
+ CONTROLLER_FILES = /_controller.rb$/
9
+ MIGRATION_FILES = /db\/migrate\/.*rb/
10
+ MODLE_FILES = /models\/.*rb/
11
+ VIEW_FILES = /views\/.*erb/
7
12
 
8
13
  def initialize
9
14
  @errors = []
@@ -12,7 +12,7 @@ module RailsBestPractices
12
12
  end
13
13
 
14
14
  def interesting_files
15
- /db\/migrate\/.*rb/
15
+ MIGRATION_FILES
16
16
  end
17
17
 
18
18
  def initialize
@@ -12,7 +12,7 @@ module RailsBestPractices
12
12
  end
13
13
 
14
14
  def interesting_files
15
- /models\/.*rb/
15
+ MODLE_FILES
16
16
  end
17
17
 
18
18
  def evaluate_start(node)
@@ -0,0 +1,31 @@
1
+ require 'rails_best_practices/checks/check'
2
+
3
+ module RailsBestPractices
4
+ module Checks
5
+ # Check a view file to make sure there is no finder.
6
+ #
7
+ # Implementation: Check if view file contains finder, then the code should move to controller.
8
+ class MoveCodeIntoControllerCheck < Check
9
+
10
+ FINDER = [:find, :all, :first, :last]
11
+
12
+ def interesting_nodes
13
+ [:call]
14
+ end
15
+
16
+ def interesting_files
17
+ VIEW_FILES
18
+ end
19
+
20
+ def evaluate_start(node)
21
+ add_error "move code into controller" if finder?(node)
22
+ end
23
+
24
+ private
25
+
26
+ def finder?(node)
27
+ node.subject.node_type == :const && FINDER.include?(node.message)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -16,7 +16,7 @@ module RailsBestPractices
16
16
  end
17
17
 
18
18
  def interesting_files
19
- /_controller.rb$/
19
+ CONTROLLER_FILES
20
20
  end
21
21
 
22
22
  def evaluate_start(node)
@@ -13,7 +13,7 @@ module RailsBestPractices
13
13
  end
14
14
 
15
15
  def interesting_files
16
- /_controller.rb$/
16
+ CONTROLLER_FILES
17
17
  end
18
18
 
19
19
  def initialize(options = {})
@@ -13,7 +13,7 @@ module RailsBestPractices
13
13
  end
14
14
 
15
15
  def interesting_files
16
- /_controller.rb$/
16
+ CONTROLLER_FILES
17
17
  end
18
18
 
19
19
  def initialize(options = {})
@@ -12,7 +12,7 @@ module RailsBestPractices
12
12
  end
13
13
 
14
14
  def interesting_files
15
- /_controller.rb$/
15
+ CONTROLLER_FILES
16
16
  end
17
17
 
18
18
  def evaluate_start(node)
@@ -14,7 +14,7 @@ module RailsBestPractices
14
14
  end
15
15
 
16
16
  def interesting_files
17
- /models\/.*rb/
17
+ MODLE_FILES
18
18
  end
19
19
 
20
20
  def initialize
@@ -12,7 +12,7 @@ module RailsBestPractices
12
12
  end
13
13
 
14
14
  def interesting_files
15
- /_controller.rb$/
15
+ CONTROLLER_FILES
16
16
  end
17
17
 
18
18
  def evaluate_start(node)
@@ -16,3 +16,4 @@ require 'rails_best_practices/checks/use_observer_check'
16
16
  require 'rails_best_practices/checks/isolate_seed_data_check'
17
17
  require 'rails_best_practices/checks/always_add_db_index_check'
18
18
  require 'rails_best_practices/checks/use_filter_check'
19
+ require 'rails_best_practices/checks/move_code_into_controller_check'
@@ -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.13"
8
+ s.version = "0.2.14"
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"]
@@ -34,6 +34,7 @@ Gem::Specification.new do |s|
34
34
  "lib/rails_best_practices/checks/keep_finders_on_their_own_model_check.rb",
35
35
  "lib/rails_best_practices/checks/law_of_demeter_check.rb",
36
36
  "lib/rails_best_practices/checks/many_to_many_collection_check.rb",
37
+ "lib/rails_best_practices/checks/move_code_into_controller_check.rb",
37
38
  "lib/rails_best_practices/checks/move_finder_to_named_scope_check.rb",
38
39
  "lib/rails_best_practices/checks/move_model_logic_into_model_check.rb",
39
40
  "lib/rails_best_practices/checks/needless_deep_nesting_check.rb",
@@ -61,6 +62,7 @@ Gem::Specification.new do |s|
61
62
  "spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb",
62
63
  "spec/rails_best_practices/checks/law_of_demeter_check_spec.rb",
63
64
  "spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb",
65
+ "spec/rails_best_practices/checks/move_code_into_controller_check_spec.rb",
64
66
  "spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb",
65
67
  "spec/rails_best_practices/checks/move_model_logic_into_model_check_spec.rb",
66
68
  "spec/rails_best_practices/checks/needless_deep_nesting_check_spec.rb",
@@ -88,6 +90,7 @@ Gem::Specification.new do |s|
88
90
  "spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb",
89
91
  "spec/rails_best_practices/checks/law_of_demeter_check_spec.rb",
90
92
  "spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb",
93
+ "spec/rails_best_practices/checks/move_code_into_controller_check_spec.rb",
91
94
  "spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb",
92
95
  "spec/rails_best_practices/checks/move_model_logic_into_model_check_spec.rb",
93
96
  "spec/rails_best_practices/checks/needless_deep_nesting_check_spec.rb",
@@ -16,3 +16,4 @@ UseObserverCheck: { }
16
16
  IsolateSeedDataCheck: { }
17
17
  AlwaysAddDbIndexCheck: { }
18
18
  UseFilterCheck: { }
19
+ MoveCodeIntoControllerCheck: { }
@@ -0,0 +1,33 @@
1
+ require File.join(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe RailsBestPractices::Checks::MoveCodeIntoControllerCheck do
4
+ before(:each) do
5
+ @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::MoveCodeIntoControllerCheck.new)
6
+ end
7
+
8
+ it "should move code into controller" do
9
+ content = <<-EOF
10
+ <% @posts = Post.find(:all) %>
11
+ <% @posts.each do |post| %>
12
+ <%=h post.title %>
13
+ <%=h post.content %>
14
+ <% end %>
15
+ EOF
16
+ @runner.check('app/views/posts/index.html.erb', content)
17
+ errors = @runner.errors
18
+ errors.should_not be_empty
19
+ errors[0].to_s.should == "app/views/posts/index.html.erb:1 - move code into controller"
20
+ end
21
+
22
+ it "should not move code into controller" do
23
+ content = <<-EOF
24
+ <% @posts.each do |post| %>
25
+ <%=h post.title %>
26
+ <%=h post.content %>
27
+ <% end %>
28
+ EOF
29
+ @runner.check('app/views/posts/index.html.erb', content)
30
+ errors = @runner.errors
31
+ errors.should be_empty
32
+ end
33
+ 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.13
4
+ version: 0.2.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
@@ -57,6 +57,7 @@ files:
57
57
  - lib/rails_best_practices/checks/keep_finders_on_their_own_model_check.rb
58
58
  - lib/rails_best_practices/checks/law_of_demeter_check.rb
59
59
  - lib/rails_best_practices/checks/many_to_many_collection_check.rb
60
+ - lib/rails_best_practices/checks/move_code_into_controller_check.rb
60
61
  - lib/rails_best_practices/checks/move_finder_to_named_scope_check.rb
61
62
  - lib/rails_best_practices/checks/move_model_logic_into_model_check.rb
62
63
  - lib/rails_best_practices/checks/needless_deep_nesting_check.rb
@@ -84,6 +85,7 @@ files:
84
85
  - spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb
85
86
  - spec/rails_best_practices/checks/law_of_demeter_check_spec.rb
86
87
  - spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb
88
+ - spec/rails_best_practices/checks/move_code_into_controller_check_spec.rb
87
89
  - spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb
88
90
  - spec/rails_best_practices/checks/move_model_logic_into_model_check_spec.rb
89
91
  - spec/rails_best_practices/checks/needless_deep_nesting_check_spec.rb
@@ -133,6 +135,7 @@ test_files:
133
135
  - spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb
134
136
  - spec/rails_best_practices/checks/law_of_demeter_check_spec.rb
135
137
  - spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb
138
+ - spec/rails_best_practices/checks/move_code_into_controller_check_spec.rb
136
139
  - spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb
137
140
  - spec/rails_best_practices/checks/move_model_logic_into_model_check_spec.rb
138
141
  - spec/rails_best_practices/checks/needless_deep_nesting_check_spec.rb