rails_best_practices 0.2.10 → 0.2.11

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/Rakefile CHANGED
@@ -27,7 +27,7 @@ Jeweler::Tasks.new do |gemspec|
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
+ gemspec.add_dependency 'ruby_parser', '>= 2.0.4'
31
+ gemspec.add_dependency 'ruby2ruby', '>= 1.2.4'
32
32
  end
33
33
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.10
1
+ 0.2.11
@@ -34,23 +34,28 @@ module RailsBestPractices
34
34
 
35
35
  def attribute_assignment(node)
36
36
  variable = node.subject
37
- return if variable.nil?
37
+ arguments_node = nil
38
+ node.arguments.recursive_children do |child|
39
+ if :[] == child.message
40
+ arguments_node = child
41
+ break
42
+ end
43
+ end
44
+ return if variable.nil? or arguments_node.nil?
38
45
  @variables[variable] ||= []
39
- @variables[variable] << {:message => node.message, :arguments => node.arguments}
46
+ @variables[variable] << {:message => node.message, :arguments => arguments_node}
40
47
  end
41
48
 
42
49
  def call_assignment(node)
43
50
  if node.message == :save
44
51
  variable = node.subject
45
- add_error "add model virtual attribute (for #{node.subject.to_ruby})" if params_dup?(@variables[variable].collect {|h| h[:arguments] })
52
+ add_error "add model virtual attribute (for #{node.subject.to_ruby})" if params_dup?(@variables[variable].collect {|h| h[:arguments]})
46
53
  end
47
54
  end
48
55
 
49
56
  def params_dup?(nodes)
50
57
  return false if nodes.nil?
51
- params_nodes = nodes.collect {|node| node.grep_nodes({:subject => s(:call, nil, :params, s(:arglist)), :message => :[]}).first}.compact
52
- params_arguments = params_nodes.collect{|node| node.arguments}
53
- !params_arguments.dups.empty?
58
+ !nodes.dups.empty?
54
59
  end
55
60
  end
56
61
  end
@@ -16,8 +16,8 @@ module RailsBestPractices
16
16
  NODE_TYPES.each do |node|
17
17
  start_node_method = "evaluate_start_#{node}"
18
18
  end_node_method = "evaluate_end_#{node}"
19
- define_method(start_node_method) { } unless self.respond_to?(start_node_method)
20
- define_method(end_node_method) { } unless self.respond_to?(end_node_method)
19
+ define_method(start_node_method) { |node| } unless self.respond_to?(start_node_method)
20
+ define_method(end_node_method) { |node| } unless self.respond_to?(end_node_method)
21
21
  end
22
22
 
23
23
  def position(offset = 0)
@@ -1,8 +1,7 @@
1
1
  require 'optparse'
2
2
 
3
3
  def expand_dirs_to_files *dirs
4
- # extensions = ['rb', 'erb', 'builder']
5
- extensions = ['rb', 'builder']
4
+ extensions = ['rb', 'erb', 'builder']
6
5
 
7
6
  dirs.flatten.map { |p|
8
7
  if File.directory? p
@@ -10,8 +9,12 @@ def expand_dirs_to_files *dirs
10
9
  else
11
10
  p
12
11
  end
13
- }.flatten.sort { |a, b|
14
- # for law_of_demeter_check
12
+ }.flatten
13
+ end
14
+
15
+ # for law_of_demeter_check
16
+ def model_first_sort files
17
+ files.sort { |a, b|
15
18
  if a =~ /models\/.*rb/
16
19
  -1
17
20
  elsif b =~ /models\/.*rb/
@@ -23,13 +26,13 @@ def expand_dirs_to_files *dirs
23
26
  end
24
27
 
25
28
  # for always_add_db_index_check
26
- def add_duplicate_migration_files files
29
+ def add_duplicate_migrations files
27
30
  migration_files = files.select { |file| file.index("db/migrate") }
28
31
  (files << migration_files).flatten
29
32
  end
30
33
 
31
- def ignore_vendor_directories files
32
- files.reject { |file| file.index("vendor/") }
34
+ def ignore_files files, pattern
35
+ files.reject { |file| file.index(pattern) }
33
36
  end
34
37
 
35
38
  options = {}
@@ -39,7 +42,13 @@ OptionParser.new do |opts|
39
42
  opts.on("-d", "--debug", "Debug mode") do
40
43
  options['debug'] = true
41
44
  end
42
-
45
+
46
+ ['vendor', 'spec', 'test', 'stories'].each do |pattern|
47
+ opts.on("--#{pattern}", "include #{pattern} files") do
48
+ options[pattern] = true
49
+ end
50
+ end
51
+
43
52
  opts.on_tail("-h", "--help", "Show this message") do
44
53
  puts opts
45
54
  exit
@@ -50,7 +59,15 @@ end
50
59
 
51
60
  runner = RailsBestPractices::Core::Runner.new
52
61
  runner.set_debug if options['debug']
53
- ignore_vendor_directories(add_duplicate_migration_files(expand_dirs_to_files(ARGV))).each { |file| runner.check_file(file) }
62
+
63
+ files = expand_dirs_to_files(ARGV)
64
+ files = model_first_sort(files)
65
+ files = add_duplicate_migrations(files)
66
+ ['vendor', 'spec', 'test', 'stories'].each do |pattern|
67
+ files = ignore_files(files, "#{pattern}/") unless options[pattern]
68
+ end
69
+ files.each { |file| runner.check_file(file) }
70
+
54
71
  runner.errors.each {|error| puts error}
55
72
  puts "\nFound #{runner.errors.size} errors."
56
73
 
@@ -18,7 +18,6 @@ module RailsBestPractices
18
18
  @checks = checks unless checks.empty?
19
19
  @checks ||= load_checks
20
20
  @checker ||= CheckingVisitor.new(@checks)
21
- @parser = RubyParser.new
22
21
  @debug = false
23
22
  end
24
23
 
@@ -53,7 +52,7 @@ module RailsBestPractices
53
52
  def parse(filename, content)
54
53
  puts filename if @debug
55
54
  begin
56
- @parser.parse(content, filename)
55
+ RubyParser.new.parse(content, filename)
57
56
  rescue Exception => e
58
57
  puts "#{filename} looks like it's not a valid Ruby file. Skipping..."
59
58
  nil
@@ -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.10"
8
+ s.version = "0.2.11"
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"]
@@ -80,23 +80,23 @@ Gem::Specification.new do |s|
80
80
  s.rubygems_version = %q{1.3.5}
81
81
  s.summary = %q{check rails files according to ihower's presentation 'rails best practices'}
82
82
  s.test_files = [
83
- "spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb",
84
- "spec/rails_best_practices/checks/always_add_db_index_check_spec.rb",
85
- "spec/rails_best_practices/checks/isolate_seed_data_check_spec.rb",
83
+ "spec/rails_best_practices/checks/use_scope_access_check_spec.rb",
84
+ "spec/rails_best_practices/checks/not_use_default_route_check_spec.rb",
85
+ "spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb",
86
86
  "spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb",
87
- "spec/rails_best_practices/checks/law_of_demeter_check_spec.rb",
87
+ "spec/rails_best_practices/checks/use_model_callback_check_spec.rb",
88
+ "spec/rails_best_practices/checks/isolate_seed_data_check_spec.rb",
88
89
  "spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb",
89
- "spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb",
90
+ "spec/rails_best_practices/checks/use_model_association_check_spec.rb",
90
91
  "spec/rails_best_practices/checks/move_model_logic_into_model_check_spec.rb",
91
- "spec/rails_best_practices/checks/needless_deep_nesting_check_spec.rb",
92
- "spec/rails_best_practices/checks/nested_model_forms_check_spec.rb",
93
- "spec/rails_best_practices/checks/not_use_default_route_check_spec.rb",
94
- "spec/rails_best_practices/checks/overuse_route_customizations_check_spec.rb",
92
+ "spec/rails_best_practices/checks/law_of_demeter_check_spec.rb",
95
93
  "spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb",
96
- "spec/rails_best_practices/checks/use_model_association_check_spec.rb",
97
- "spec/rails_best_practices/checks/use_model_callback_check_spec.rb",
94
+ "spec/rails_best_practices/checks/nested_model_forms_check_spec.rb",
98
95
  "spec/rails_best_practices/checks/use_observer_check_spec.rb",
99
- "spec/rails_best_practices/checks/use_scope_access_check_spec.rb",
96
+ "spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb",
97
+ "spec/rails_best_practices/checks/needless_deep_nesting_check_spec.rb",
98
+ "spec/rails_best_practices/checks/overuse_route_customizations_check_spec.rb",
99
+ "spec/rails_best_practices/checks/always_add_db_index_check_spec.rb",
100
100
  "spec/spec_helper.rb"
101
101
  ]
102
102
 
@@ -105,15 +105,15 @@ Gem::Specification.new do |s|
105
105
  s.specification_version = 3
106
106
 
107
107
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
108
- s.add_runtime_dependency(%q<ruby_parser>, [">= 0"])
109
- s.add_runtime_dependency(%q<ruby2ruby>, [">= 0"])
108
+ s.add_runtime_dependency(%q<ruby_parser>, [">= 2.0.4"])
109
+ s.add_runtime_dependency(%q<ruby2ruby>, [">= 1.2.4"])
110
110
  else
111
- s.add_dependency(%q<ruby_parser>, [">= 0"])
112
- s.add_dependency(%q<ruby2ruby>, [">= 0"])
111
+ s.add_dependency(%q<ruby_parser>, [">= 2.0.4"])
112
+ s.add_dependency(%q<ruby2ruby>, [">= 1.2.4"])
113
113
  end
114
114
  else
115
- s.add_dependency(%q<ruby_parser>, [">= 0"])
116
- s.add_dependency(%q<ruby2ruby>, [">= 0"])
115
+ s.add_dependency(%q<ruby_parser>, [">= 2.0.4"])
116
+ s.add_dependency(%q<ruby2ruby>, [">= 1.2.4"])
117
117
  end
118
118
  end
119
119
 
@@ -75,4 +75,39 @@ describe RailsBestPractices::Checks::AddModelVirtualAttributeCheck do
75
75
  errors = @runner.errors
76
76
  errors.should be_empty
77
77
  end
78
+
79
+ it "should add model virtual attribute with two dimension params" do
80
+ content = <<-EOF
81
+ class UsersController < ApplicationController
82
+
83
+ def create
84
+ @user = User.new(params[:user])
85
+ @user.first_name = params[:user][:full_name].split(' ', 2).first
86
+ @user.last_name = params[:user][:full_name].split(' ', 2).last
87
+ @user.save
88
+ end
89
+ end
90
+ EOF
91
+ @runner.check('app/controllers/users_controller.rb', content)
92
+ errors = @runner.errors
93
+ errors.should_not be_empty
94
+ errors[0].to_s.should == "app/controllers/users_controller.rb:3 - add model virtual attribute (for @user)"
95
+ end
96
+
97
+ it "should no add model virtual attribute with two dimension params" do
98
+ content = <<-EOF
99
+ class UsersController < ApplicationController
100
+
101
+ def create
102
+ @user = User.new(params[:user])
103
+ @user.first_name = params[:user][:first_name]
104
+ @user.last_name = params[:user][:last_name]
105
+ @user.save
106
+ end
107
+ end
108
+ EOF
109
+ @runner.check('app/controllers/users_controller.rb', content)
110
+ errors = @runner.errors
111
+ errors.should be_empty
112
+ end
78
113
  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.10
4
+ version: 0.2.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: "0"
23
+ version: 2.0.4
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: ruby2ruby
@@ -30,7 +30,7 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: "0"
33
+ version: 1.2.4
34
34
  version:
35
35
  description: check rails files according to ihower's presentation 'rails best practices'
36
36
  email: flyerhzm@gmail.com
@@ -125,21 +125,21 @@ signing_key:
125
125
  specification_version: 3
126
126
  summary: check rails files according to ihower's presentation 'rails best practices'
127
127
  test_files:
128
+ - spec/rails_best_practices/checks/use_scope_access_check_spec.rb
129
+ - spec/rails_best_practices/checks/not_use_default_route_check_spec.rb
128
130
  - spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb
129
- - spec/rails_best_practices/checks/always_add_db_index_check_spec.rb
130
- - spec/rails_best_practices/checks/isolate_seed_data_check_spec.rb
131
131
  - spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb
132
- - spec/rails_best_practices/checks/law_of_demeter_check_spec.rb
132
+ - spec/rails_best_practices/checks/use_model_callback_check_spec.rb
133
+ - spec/rails_best_practices/checks/isolate_seed_data_check_spec.rb
133
134
  - spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb
134
- - spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb
135
+ - spec/rails_best_practices/checks/use_model_association_check_spec.rb
135
136
  - spec/rails_best_practices/checks/move_model_logic_into_model_check_spec.rb
136
- - spec/rails_best_practices/checks/needless_deep_nesting_check_spec.rb
137
- - spec/rails_best_practices/checks/nested_model_forms_check_spec.rb
138
- - spec/rails_best_practices/checks/not_use_default_route_check_spec.rb
139
- - spec/rails_best_practices/checks/overuse_route_customizations_check_spec.rb
137
+ - spec/rails_best_practices/checks/law_of_demeter_check_spec.rb
140
138
  - spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb
141
- - spec/rails_best_practices/checks/use_model_association_check_spec.rb
142
- - spec/rails_best_practices/checks/use_model_callback_check_spec.rb
139
+ - spec/rails_best_practices/checks/nested_model_forms_check_spec.rb
143
140
  - spec/rails_best_practices/checks/use_observer_check_spec.rb
144
- - spec/rails_best_practices/checks/use_scope_access_check_spec.rb
141
+ - spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb
142
+ - spec/rails_best_practices/checks/needless_deep_nesting_check_spec.rb
143
+ - spec/rails_best_practices/checks/overuse_route_customizations_check_spec.rb
144
+ - spec/rails_best_practices/checks/always_add_db_index_check_spec.rb
145
145
  - spec/spec_helper.rb