rails_best_practices 0.1.0 → 0.1.1

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/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .*.swp
2
+ pkg/**
data/README.textile CHANGED
@@ -1,3 +1,30 @@
1
+ h1. rails_best_practices
2
+
3
+ rails_best_practices is a gem to check quality of rails app files according to ihower's presentation "Rails Best Practices".
4
+ rails_best_practices is a static file parser tool based on ruby_parser.
5
+
6
+ *************************************************
7
+
8
+ h2. Install
9
+
10
+ <pre><code>
11
+ sudo gem install rails_best_practices --source http://gemcutter.org
12
+ </code></pre>
13
+
14
+ *************************************************
15
+
16
+ h2. Usage
17
+
18
+ at the root of rails
19
+
20
+ <pre><code>
21
+ rails_best_practices .
22
+ </code></pre>
23
+
24
+ *************************************************
25
+
26
+ h2. Progress
27
+
1
28
  * Lesson 1. Move code from Controller to Model
2
29
  ## [-Move finder to named_scope-]
3
30
  ## [-Use model association-]
@@ -11,7 +38,7 @@
11
38
  ## Nested Model Forms (one-to-many)
12
39
 
13
40
  * Lesson 2. RESTful Conventions
14
- ## Overuse route customizations
41
+ ## [-Overuse route customizations-]
15
42
  ## Needless deep nesting
16
43
  ## Not use default route
17
44
 
@@ -38,4 +65,8 @@
38
65
  ## Move code into helper
39
66
  ## Replace instance variable with local variable
40
67
  ## Use Form Builder
41
- ## Organize Helper files
68
+ ## Organize Helper files
69
+
70
+ *************************************************
71
+
72
+ Copyright © 2009 Richard Huang (flyerhzm@gmail.com), released under the MIT license
data/Rakefile CHANGED
@@ -22,10 +22,12 @@ end
22
22
 
23
23
  Jeweler::Tasks.new do |gemspec|
24
24
  gemspec.name = "rails_best_practices"
25
- gemspec.summary = ""
26
- gemspec.description = ""
25
+ gemspec.summary = "check rails files according to ihower's presentation 'rails best practices'"
26
+ gemspec.description = "check rails files according to ihower's presentation 'rails best practices'"
27
27
  gemspec.email = "flyerhzm@gmail.com"
28
28
  gemspec.homepage = "http://github.com/flyerhzm/rails_best_practices"
29
29
  gemspec.authors = ["Richard Huang"]
30
+ gemspec.add_dependency 'ruby_parser'
31
+ gemspec.add_dependency 'ruby2ruby'
30
32
  end
31
33
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -6,4 +6,5 @@ require 'rails_best_practices/checks/use_model_callback_check'
6
6
  require 'rails_best_practices/checks/replace_complex_creation_with_factory_method_check'
7
7
  require 'rails_best_practices/checks/move_model_logic_into_model_check'
8
8
  require 'rails_best_practices/checks/many_to_many_collection_check'
9
- require 'rails_best_practices/checks/nested_model_forms_check'
9
+ require 'rails_best_practices/checks/nested_model_forms_check'
10
+ require 'rails_best_practices/checks/overuse_route_customizations_check'
@@ -0,0 +1,34 @@
1
+ require 'rails_best_practices/checks/check'
2
+
3
+ module RailsBestPractices
4
+ module Checks
5
+ class OveruseRouteCustomizationsCheck < Check
6
+
7
+ def interesting_nodes
8
+ [:call]
9
+ end
10
+
11
+ def interesting_files
12
+ /config\/routes.rb/
13
+ end
14
+
15
+ def initialize(options = {})
16
+ super()
17
+ @customize_count = options['customize_count'] || 3
18
+ end
19
+
20
+ def evaluate_start(node)
21
+ if s(:lvar, :map) == node.subject and :resources == node.message
22
+ add_error "overuse route customizations" if member_and_collection_count(node) > @customize_count
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def member_and_collection_count(node)
29
+ customize_hash = eval(Ruby2Ruby.new.process(node.grep_nodes(:node_type => :hash).first))
30
+ (customize_hash[:member].size || 0) + (customize_hash[:collection].size || 0)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,5 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'ruby_parser'
3
+ require 'ruby2ruby'
3
4
  require 'yaml'
4
5
 
5
6
  module RailsBestPractices
@@ -56,4 +57,4 @@ module RailsBestPractices
56
57
  end
57
58
  end
58
59
  end
59
- end
60
+ end
@@ -32,12 +32,13 @@ class Sexp
32
32
 
33
33
  def grep_nodes(options)
34
34
  return self if options.empty?
35
+ node_type = options[:node_type]
35
36
  subject = options[:subject]
36
37
  message = options[:message]
37
38
  arguments = options[:arguments]
38
39
  nodes = []
39
40
  self.recursive_children do |child|
40
- if (!subject or subject == child.subject) and (!message or message == child.message) and (!arguments or arguments == child.arguments)
41
+ if (!node_type or node_type == child.node_type) and (!subject or subject == child.subject) and (!message or message == child.message) and (!arguments or arguments == child.arguments)
41
42
  nodes << child
42
43
  end
43
44
  end
@@ -0,0 +1,98 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rails_best_practices}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Richard Huang"]
12
+ s.date = %q{2009-11-05}
13
+ s.default_executable = %q{rails_best_practices}
14
+ s.description = %q{check rails files according to ihower's presentation 'rails best practices'}
15
+ s.email = %q{flyerhzm@gmail.com}
16
+ s.executables = ["rails_best_practices"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.textile"
20
+ ]
21
+ s.files = [
22
+ ".gitignore",
23
+ "LICENSE",
24
+ "README.textile",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "bin/rails_best_practices",
28
+ "lib/rails_best_practices.rb",
29
+ "lib/rails_best_practices/checks.rb",
30
+ "lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb",
31
+ "lib/rails_best_practices/checks/check.rb",
32
+ "lib/rails_best_practices/checks/many_to_many_collection_check.rb",
33
+ "lib/rails_best_practices/checks/move_finder_to_named_scope_check.rb",
34
+ "lib/rails_best_practices/checks/move_model_logic_into_model_check.rb",
35
+ "lib/rails_best_practices/checks/nested_model_forms_check.rb",
36
+ "lib/rails_best_practices/checks/overuse_route_customizations_check.rb",
37
+ "lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb",
38
+ "lib/rails_best_practices/checks/use_model_association_check.rb",
39
+ "lib/rails_best_practices/checks/use_model_callback_check.rb",
40
+ "lib/rails_best_practices/checks/use_scope_access_check.rb",
41
+ "lib/rails_best_practices/command.rb",
42
+ "lib/rails_best_practices/core.rb",
43
+ "lib/rails_best_practices/core/checking_visitor.rb",
44
+ "lib/rails_best_practices/core/core_ext.rb",
45
+ "lib/rails_best_practices/core/error.rb",
46
+ "lib/rails_best_practices/core/runner.rb",
47
+ "lib/rails_best_practices/core/visitable_sexp.rb",
48
+ "rails_best_practices.gemspec",
49
+ "rails_best_practices.yml",
50
+ "spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb",
51
+ "spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb",
52
+ "spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb",
53
+ "spec/rails_best_practices/checks/move_model_logic_into_model_check_spec.rb",
54
+ "spec/rails_best_practices/checks/nested_model_forms_check_spec.rb",
55
+ "spec/rails_best_practices/checks/overuse_route_customizations_check_spec.rb",
56
+ "spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb",
57
+ "spec/rails_best_practices/checks/use_model_association_check_spec.rb",
58
+ "spec/rails_best_practices/checks/use_model_callback_check_spec.rb",
59
+ "spec/rails_best_practices/checks/use_scope_access_check_spec.rb",
60
+ "spec/spec.opts",
61
+ "spec/spec_helper.rb"
62
+ ]
63
+ s.homepage = %q{http://github.com/flyerhzm/rails_best_practices}
64
+ s.rdoc_options = ["--charset=UTF-8"]
65
+ s.require_paths = ["lib"]
66
+ s.rubygems_version = %q{1.3.5}
67
+ s.summary = %q{check rails files according to ihower's presentation 'rails best practices'}
68
+ s.test_files = [
69
+ "spec/rails_best_practices/checks/use_scope_access_check_spec.rb",
70
+ "spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb",
71
+ "spec/rails_best_practices/checks/use_model_callback_check_spec.rb",
72
+ "spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb",
73
+ "spec/rails_best_practices/checks/use_model_association_check_spec.rb",
74
+ "spec/rails_best_practices/checks/move_model_logic_into_model_check_spec.rb",
75
+ "spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb",
76
+ "spec/rails_best_practices/checks/nested_model_forms_check_spec.rb",
77
+ "spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb",
78
+ "spec/rails_best_practices/checks/overuse_route_customizations_check_spec.rb",
79
+ "spec/spec_helper.rb"
80
+ ]
81
+
82
+ if s.respond_to? :specification_version then
83
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
84
+ s.specification_version = 3
85
+
86
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
87
+ s.add_runtime_dependency(%q<ruby_parser>, [">= 0"])
88
+ s.add_runtime_dependency(%q<ruby2ruby>, [">= 0"])
89
+ else
90
+ s.add_dependency(%q<ruby_parser>, [">= 0"])
91
+ s.add_dependency(%q<ruby2ruby>, [">= 0"])
92
+ end
93
+ else
94
+ s.add_dependency(%q<ruby_parser>, [">= 0"])
95
+ s.add_dependency(%q<ruby2ruby>, [">= 0"])
96
+ end
97
+ end
98
+
@@ -4,4 +4,7 @@ UseScopeAccessCheck: { }
4
4
  AddModelVirtualAttributeCheck: { }
5
5
  # UseModelCallbackCheck: { }
6
6
  ReplaceComplexCreationWithFactoryMethodCheck: { attribute_assignment_count: 2 }
7
- MoveModelLogicIntoModelCheck: { called_count: 4 }
7
+ MoveModelLogicIntoModelCheck: { called_count: 4 }
8
+ # ManyToManyCollectionCheck: { }
9
+ # NestedModelFormsCheck: { }
10
+ OveruseRouteCustomizations: { customize_count: 3 }
@@ -10,12 +10,39 @@ describe RailsBestPractices::Checks::OveruseRouteCustomizationsCheck do
10
10
  ActionController::Routing::Routes.draw do |map|
11
11
  map.resources :posts, :member => { :comments => :get,
12
12
  :create_comment => :post,
13
- :udpate_comment => :post,
13
+ :update_comment => :post,
14
14
  :delete_comment => :post }
15
15
  end
16
16
  EOF
17
17
  @runner.check('config/routes.rb', content)
18
18
  errors = @runner.errors
19
19
  errors.should_not be_empty
20
+ errors[0].to_s.should == "config/routes.rb:2 - overuse route customizations"
20
21
  end
21
- end
22
+
23
+ it "should overuse route customizations with collection" do
24
+ content = <<-EOF
25
+ ActionController::Routing::Routes.draw do |map|
26
+ map.resources :posts, :member => { :create_comment => :post,
27
+ :update_comment => :post,
28
+ :delete_comment => :post },
29
+ :collection => { :comments => :get }
30
+ end
31
+ EOF
32
+ @runner.check('config/routes.rb', content)
33
+ errors = @runner.errors
34
+ errors.should_not be_empty
35
+ errors[0].to_s.should == "config/routes.rb:2 - overuse route customizations"
36
+ end
37
+
38
+ it "should not overuse route customizations when customize route is only one" do
39
+ content = <<-EOF
40
+ ActionController::Routing::Routes.draw do |map|
41
+ map.resources :posts, :member => { :vote => :post }
42
+ end
43
+ EOF
44
+ @runner.check('config/routes.rb', content)
45
+ errors = @runner.errors
46
+ errors.should be_empty
47
+ end
48
+ 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.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
@@ -11,9 +11,28 @@ cert_chain: []
11
11
 
12
12
  date: 2009-11-05 00:00:00 +08:00
13
13
  default_executable: rails_best_practices
14
- dependencies: []
15
-
16
- description: ""
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: ruby_parser
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: ruby2ruby
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: check rails files according to ihower's presentation 'rails best practices'
17
36
  email: flyerhzm@gmail.com
18
37
  executables:
19
38
  - rails_best_practices
@@ -23,6 +42,7 @@ extra_rdoc_files:
23
42
  - LICENSE
24
43
  - README.textile
25
44
  files:
45
+ - .gitignore
26
46
  - LICENSE
27
47
  - README.textile
28
48
  - Rakefile
@@ -36,6 +56,7 @@ files:
36
56
  - lib/rails_best_practices/checks/move_finder_to_named_scope_check.rb
37
57
  - lib/rails_best_practices/checks/move_model_logic_into_model_check.rb
38
58
  - lib/rails_best_practices/checks/nested_model_forms_check.rb
59
+ - lib/rails_best_practices/checks/overuse_route_customizations_check.rb
39
60
  - lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
40
61
  - lib/rails_best_practices/checks/use_model_association_check.rb
41
62
  - lib/rails_best_practices/checks/use_model_callback_check.rb
@@ -47,12 +68,14 @@ files:
47
68
  - lib/rails_best_practices/core/error.rb
48
69
  - lib/rails_best_practices/core/runner.rb
49
70
  - lib/rails_best_practices/core/visitable_sexp.rb
71
+ - rails_best_practices.gemspec
50
72
  - rails_best_practices.yml
51
73
  - spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb
52
74
  - spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb
53
75
  - spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb
54
76
  - spec/rails_best_practices/checks/move_model_logic_into_model_check_spec.rb
55
77
  - spec/rails_best_practices/checks/nested_model_forms_check_spec.rb
78
+ - spec/rails_best_practices/checks/overuse_route_customizations_check_spec.rb
56
79
  - spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb
57
80
  - spec/rails_best_practices/checks/use_model_association_check_spec.rb
58
81
  - spec/rails_best_practices/checks/use_model_callback_check_spec.rb
@@ -86,7 +109,7 @@ rubyforge_project:
86
109
  rubygems_version: 1.3.5
87
110
  signing_key:
88
111
  specification_version: 3
89
- summary: ""
112
+ summary: check rails files according to ihower's presentation 'rails best practices'
90
113
  test_files:
91
114
  - spec/rails_best_practices/checks/use_scope_access_check_spec.rb
92
115
  - spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb