rails_best_practices 0.2.11 → 0.2.12
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +2 -2
- data/VERSION +1 -1
- data/lib/rails_best_practices/checks.rb +1 -0
- data/lib/rails_best_practices/checks/use_filter_check.rb +36 -0
- data/lib/rails_best_practices/core/visitable_sexp.rb +3 -1
- data/rails_best_practices.gemspec +4 -1
- data/rails_best_practices.yml +1 -0
- data/spec/rails_best_practices/checks/use_filter_check_spec.rb +62 -0
- metadata +4 -1
data/README.textile
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
h1. rails_best_practices
|
2
|
-
|
3
2
|
rails_best_practices is a gem to check quality of rails app files according to ihower's presentation Rails Best Practices.
|
4
3
|
rails_best_practices is a code static parser tool based on ruby_parser.
|
5
4
|
|
@@ -53,6 +52,7 @@ LawOfDemeterCheck: { }
|
|
53
52
|
UseObserverCheck: { }
|
54
53
|
IsolateSeedDataCheck: { }
|
55
54
|
AlwaysAddDbIndexCheck: { }
|
55
|
+
UseFilterCheck: { }
|
56
56
|
</code></pre>
|
57
57
|
|
58
58
|
*************************************************
|
@@ -90,7 +90,7 @@ h2. Progress
|
|
90
90
|
## [-Always add DB index-]
|
91
91
|
|
92
92
|
* Lesson 5. Controller
|
93
|
-
## Use before_filter
|
93
|
+
## [-Use before_filter-]
|
94
94
|
## DRY Controller
|
95
95
|
|
96
96
|
* Lesson 6. View
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.12
|
@@ -15,3 +15,4 @@ require 'rails_best_practices/checks/law_of_demeter_check'
|
|
15
15
|
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
|
+
require 'rails_best_practices/checks/use_filter_check'
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rails_best_practices/checks/check'
|
2
|
+
|
3
|
+
module RailsBestPractices
|
4
|
+
module Checks
|
5
|
+
# Check a controller file to make sure to use filter to remove duplicate call in different action.
|
6
|
+
#
|
7
|
+
# Implementation: Check all methods' first call, if they are duplicate, then should use filter.
|
8
|
+
class UseFilterCheck < Check
|
9
|
+
|
10
|
+
def interesting_nodes
|
11
|
+
[:class]
|
12
|
+
end
|
13
|
+
|
14
|
+
def interesting_files
|
15
|
+
/_controller.rb$/
|
16
|
+
end
|
17
|
+
|
18
|
+
def evaluate_start(node)
|
19
|
+
@methods = {}
|
20
|
+
node.grep_nodes({:node_type => :defn}).each { |method_node| remember_method(method_node) }
|
21
|
+
@methods.each do |first_call, method_names|
|
22
|
+
add_error "use filter for #{first_call.to_ruby} in #{method_names.join(',')}" if method_names.size > 1
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def remember_method(method_node)
|
29
|
+
method_name = method_node.message_name
|
30
|
+
first_call = method_node.body[1]
|
31
|
+
@methods[first_call] ||= []
|
32
|
+
@methods[first_call] << method_name
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
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.
|
8
|
+
s.version = "0.2.12"
|
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"]
|
@@ -41,6 +41,7 @@ Gem::Specification.new do |s|
|
|
41
41
|
"lib/rails_best_practices/checks/not_use_default_route_check.rb",
|
42
42
|
"lib/rails_best_practices/checks/overuse_route_customizations_check.rb",
|
43
43
|
"lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb",
|
44
|
+
"lib/rails_best_practices/checks/use_filter_check.rb",
|
44
45
|
"lib/rails_best_practices/checks/use_model_association_check.rb",
|
45
46
|
"lib/rails_best_practices/checks/use_model_callback_check.rb",
|
46
47
|
"lib/rails_best_practices/checks/use_observer_check.rb",
|
@@ -67,6 +68,7 @@ Gem::Specification.new do |s|
|
|
67
68
|
"spec/rails_best_practices/checks/not_use_default_route_check_spec.rb",
|
68
69
|
"spec/rails_best_practices/checks/overuse_route_customizations_check_spec.rb",
|
69
70
|
"spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb",
|
71
|
+
"spec/rails_best_practices/checks/use_filter_check_spec.rb",
|
70
72
|
"spec/rails_best_practices/checks/use_model_association_check_spec.rb",
|
71
73
|
"spec/rails_best_practices/checks/use_model_callback_check_spec.rb",
|
72
74
|
"spec/rails_best_practices/checks/use_observer_check_spec.rb",
|
@@ -82,6 +84,7 @@ Gem::Specification.new do |s|
|
|
82
84
|
s.test_files = [
|
83
85
|
"spec/rails_best_practices/checks/use_scope_access_check_spec.rb",
|
84
86
|
"spec/rails_best_practices/checks/not_use_default_route_check_spec.rb",
|
87
|
+
"spec/rails_best_practices/checks/use_filter_check_spec.rb",
|
85
88
|
"spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb",
|
86
89
|
"spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb",
|
87
90
|
"spec/rails_best_practices/checks/use_model_callback_check_spec.rb",
|
data/rails_best_practices.yml
CHANGED
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe RailsBestPractices::Checks::UseFilterCheck do
|
4
|
+
before(:each) do
|
5
|
+
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::UseFilterCheck.new)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should use filter" do
|
9
|
+
content = <<-EOF
|
10
|
+
class PostsController < ApplicationController
|
11
|
+
|
12
|
+
def show
|
13
|
+
@post = current_user.posts.find(params[:id])
|
14
|
+
end
|
15
|
+
|
16
|
+
def edit
|
17
|
+
@post = current_user.posts.find(params[:id])
|
18
|
+
end
|
19
|
+
|
20
|
+
def update
|
21
|
+
@post = current_user.posts.find(params[:id])
|
22
|
+
@post.update_attributes(params[:post])
|
23
|
+
end
|
24
|
+
|
25
|
+
def destroy
|
26
|
+
@post = current_user.posts.find(params[:id])
|
27
|
+
@post.destroy
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
EOF
|
32
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
33
|
+
errors = @runner.errors
|
34
|
+
errors.should_not be_empty
|
35
|
+
errors[0].to_s.should == "app/controllers/posts_controller.rb:1 - use filter for @post = current_user.posts.find(params[:id]) in show,edit,update,destroy"
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should not use filter" do
|
39
|
+
content = <<-EOF
|
40
|
+
class PostsController < ApplicationController
|
41
|
+
before_filter :find_post, :only => [:show, :edit, :update, :destroy]
|
42
|
+
|
43
|
+
def update
|
44
|
+
@post.update_attributes(params[:post])
|
45
|
+
end
|
46
|
+
|
47
|
+
def destroy
|
48
|
+
@post.destroy
|
49
|
+
end
|
50
|
+
|
51
|
+
protected
|
52
|
+
|
53
|
+
def find_post
|
54
|
+
@post = current_user.posts.find(params[:id])
|
55
|
+
end
|
56
|
+
end
|
57
|
+
EOF
|
58
|
+
@runner.check('app/controllers/posts_controller.rb', content)
|
59
|
+
errors = @runner.errors
|
60
|
+
errors.should be_empty
|
61
|
+
end
|
62
|
+
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.
|
4
|
+
version: 0.2.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
@@ -64,6 +64,7 @@ files:
|
|
64
64
|
- lib/rails_best_practices/checks/not_use_default_route_check.rb
|
65
65
|
- lib/rails_best_practices/checks/overuse_route_customizations_check.rb
|
66
66
|
- lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
|
67
|
+
- lib/rails_best_practices/checks/use_filter_check.rb
|
67
68
|
- lib/rails_best_practices/checks/use_model_association_check.rb
|
68
69
|
- lib/rails_best_practices/checks/use_model_callback_check.rb
|
69
70
|
- lib/rails_best_practices/checks/use_observer_check.rb
|
@@ -90,6 +91,7 @@ files:
|
|
90
91
|
- spec/rails_best_practices/checks/not_use_default_route_check_spec.rb
|
91
92
|
- spec/rails_best_practices/checks/overuse_route_customizations_check_spec.rb
|
92
93
|
- spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb
|
94
|
+
- spec/rails_best_practices/checks/use_filter_check_spec.rb
|
93
95
|
- spec/rails_best_practices/checks/use_model_association_check_spec.rb
|
94
96
|
- spec/rails_best_practices/checks/use_model_callback_check_spec.rb
|
95
97
|
- spec/rails_best_practices/checks/use_observer_check_spec.rb
|
@@ -127,6 +129,7 @@ summary: check rails files according to ihower's presentation 'rails best practi
|
|
127
129
|
test_files:
|
128
130
|
- spec/rails_best_practices/checks/use_scope_access_check_spec.rb
|
129
131
|
- spec/rails_best_practices/checks/not_use_default_route_check_spec.rb
|
132
|
+
- spec/rails_best_practices/checks/use_filter_check_spec.rb
|
130
133
|
- spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb
|
131
134
|
- spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb
|
132
135
|
- spec/rails_best_practices/checks/use_model_callback_check_spec.rb
|