rails_best_practices 1.9.1 → 1.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. data/.rvmrc +1 -1
  2. data/Gemfile +1 -1
  3. data/Gemfile.lock +10 -2
  4. data/README.md +33 -27
  5. data/lib/rails_best_practices/analyzer.rb +13 -13
  6. data/lib/rails_best_practices/core/check.rb +7 -6
  7. data/lib/rails_best_practices/core/checking_visitor.rb +1 -1
  8. data/lib/rails_best_practices/core/runner.rb +2 -3
  9. data/lib/rails_best_practices/core_ext/sexp.rb +18 -17
  10. data/lib/rails_best_practices/lexicals.rb +1 -0
  11. data/lib/rails_best_practices/lexicals/long_line_check.rb +31 -0
  12. data/lib/rails_best_practices/prepares/config_prepare.rb +1 -1
  13. data/lib/rails_best_practices/reviews.rb +2 -0
  14. data/lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb +2 -2
  15. data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +1 -1
  16. data/lib/rails_best_practices/reviews/hash_syntax_review.rb +63 -0
  17. data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +1 -1
  18. data/lib/rails_best_practices/reviews/move_code_into_model_review.rb +1 -1
  19. data/lib/rails_best_practices/reviews/move_finder_to_named_scope_review.rb +1 -1
  20. data/lib/rails_best_practices/reviews/move_model_logic_into_model_review.rb +1 -1
  21. data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +1 -1
  22. data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +1 -1
  23. data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +15 -6
  24. data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +4 -2
  25. data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +8 -4
  26. data/lib/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review.rb +2 -1
  27. data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +52 -8
  28. data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +14 -2
  29. data/lib/rails_best_practices/reviews/use_before_filter_review.rb +8 -3
  30. data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +2 -1
  31. data/lib/rails_best_practices/reviews/use_observer_review.rb +1 -1
  32. data/lib/rails_best_practices/reviews/use_parentheses_in_method_def_review.rb +33 -0
  33. data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +16 -6
  34. data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +9 -6
  35. data/lib/rails_best_practices/reviews/use_scope_access_review.rb +8 -3
  36. data/lib/rails_best_practices/version.rb +1 -1
  37. data/rails_best_practices.yml +28 -25
  38. data/rake_rubies.sh +1 -1
  39. data/spec/rails_best_practices/analyzer_spec.rb +1 -1
  40. data/spec/rails_best_practices/core/check_spec.rb +7 -7
  41. data/spec/rails_best_practices/core/checking_visitor_spec.rb +7 -7
  42. data/spec/rails_best_practices/core/error_spec.rb +12 -12
  43. data/spec/rails_best_practices/core_ext/sexp_spec.rb +103 -85
  44. data/spec/rails_best_practices/lexicals/long_line_check_spec.rb +47 -0
  45. data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +1 -1
  46. data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +1 -1
  47. data/spec/rails_best_practices/prepares/config_prepare_spec.rb +1 -1
  48. data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +1 -1
  49. data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +1 -1
  50. data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +1 -1
  51. data/spec/rails_best_practices/prepares/model_prepare_spec.rb +4 -4
  52. data/spec/rails_best_practices/prepares/route_prepare_spec.rb +54 -54
  53. data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +6 -6
  54. data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +4 -4
  55. data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +46 -46
  56. data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +3 -3
  57. data/spec/rails_best_practices/reviews/hash_syntax_review_spec.rb +67 -0
  58. data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +8 -8
  59. data/spec/rails_best_practices/reviews/keep_finders_on_their_own_model_review_spec.rb +9 -9
  60. data/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb +7 -7
  61. data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +1 -1
  62. data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +1 -1
  63. data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +1 -1
  64. data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +13 -13
  65. data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +2 -2
  66. data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +3 -3
  67. data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +3 -3
  68. data/spec/rails_best_practices/reviews/not_use_times_ago_in_words_review_spec.rb +1 -1
  69. data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +26 -26
  70. data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +11 -2
  71. data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +1 -1
  72. data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +8 -8
  73. data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +2 -2
  74. data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +25 -10
  75. data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +3 -3
  76. data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +1 -1
  77. data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +28 -28
  78. data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +4 -4
  79. data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +15 -7
  80. data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +3 -3
  81. data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +3 -3
  82. data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +3 -3
  83. data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +5 -5
  84. data/spec/rails_best_practices/reviews/use_parenthesis_in_method_def_spec.rb +41 -0
  85. data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +3 -3
  86. data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +8 -8
  87. data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +9 -9
  88. data/spec/spec_helper.rb +1 -1
  89. metadata +36 -29
  90. data/.rspec.example +0 -2
  91. data/.rvmrc.example +0 -2
data/.rvmrc CHANGED
@@ -1,2 +1,2 @@
1
1
  rvm_gemset_create_on_use_flag=1
2
- rvm gemset use ruby-1.9.3-p125@rails_best_practices
2
+ rvm gemset use ruby-1.9.3-p194@rails_best_practices
data/Gemfile CHANGED
@@ -5,6 +5,6 @@ if RUBY_PLATFORM =~ /darwin/i
5
5
  gem 'rb-fsevent'
6
6
  gem 'growl'
7
7
  end
8
-
8
+ gem 'pry'
9
9
  gem 'guard'
10
10
  gem 'guard-rspec'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_best_practices (1.9.1)
4
+ rails_best_practices (1.10.0)
5
5
  activesupport
6
6
  awesome_print
7
7
  colored
@@ -17,6 +17,7 @@ GEM
17
17
  i18n (~> 0.6)
18
18
  multi_json (~> 1.0)
19
19
  awesome_print (1.0.2)
20
+ coderay (1.0.6)
20
21
  colored (1.2)
21
22
  diff-lcs (1.1.3)
22
23
  erubis (2.7.0)
@@ -27,10 +28,15 @@ GEM
27
28
  guard (>= 0.8.4)
28
29
  haml (3.1.3)
29
30
  i18n (0.6.0)
31
+ method_source (0.7.1)
30
32
  multi_json (1.2.0)
31
33
  progressbar (0.11.0)
34
+ pry (0.9.9.6)
35
+ coderay (~> 1.0.5)
36
+ method_source (~> 0.7.1)
37
+ slop (>= 2.4.4, < 3)
32
38
  rake (0.9.2.2)
33
- rb-fsevent (0.4.3.1)
39
+ rb-fsevent (0.9.1)
34
40
  rspec (2.7.0)
35
41
  rspec-core (~> 2.7.0)
36
42
  rspec-expectations (~> 2.7.0)
@@ -43,6 +49,7 @@ GEM
43
49
  slim (1.0.4)
44
50
  temple (~> 0.3.4)
45
51
  tilt (~> 1.3.2)
52
+ slop (2.4.4)
46
53
  temple (0.3.4)
47
54
  thor (0.14.6)
48
55
  tilt (1.3.3)
@@ -56,6 +63,7 @@ DEPENDENCIES
56
63
  guard
57
64
  guard-rspec
58
65
  haml
66
+ pry
59
67
  rails_best_practices!
60
68
  rake
61
69
  rb-fsevent
data/README.md CHANGED
@@ -112,40 +112,43 @@ to generate `rails_best_practices.yml` file.
112
112
 
113
113
  Now you can customize this configuration file, the default configuration is as follows:
114
114
 
115
- MoveFinderToNamedScopeCheck: { }
116
- UseModelAssociationCheck: { }
117
- UseScopeAccessCheck: { }
118
115
  AddModelVirtualAttributeCheck: { }
119
- ReplaceComplexCreationWithFactoryMethodCheck: { attribute_assignment_count: 2 }
120
- MoveModelLogicIntoModelCheck: { use_count: 4 }
121
- OveruseRouteCustomizationsCheck: { customize_count: 3 }
122
- NeedlessDeepNestingCheck: { nested_count: 2 }
123
- NotUseDefaultRouteCheck: { }
116
+ AlwaysAddDbIndexCheck: { }
117
+ DryBundlerInCapistranoCheck: { }
118
+ HashSyntaxCheck: { only_symbol: false, only_string: false }
119
+ IsolateSeedDataCheck: { }
124
120
  KeepFindersOnTheirOwnModelCheck: { }
125
121
  LawOfDemeterCheck: { }
126
- UseObserverCheck: { }
127
- IsolateSeedDataCheck: { }
128
- AlwaysAddDbIndexCheck: { }
129
- UseBeforeFilterCheck: { customize_count: 2 }
122
+ LongLineCheck: { max_line_length: 80 }
130
123
  MoveCodeIntoControllerCheck: { }
131
- MoveCodeIntoModelCheck: { use_count: 2 }
132
124
  MoveCodeIntoHelperCheck: { array_count: 3 }
133
- ReplaceInstanceVariableWithLocalVariableCheck: { }
134
- DryBundlerInCapistranoCheck: { }
135
- UseSayWithTimeInMigrationsCheck: { }
136
- UseQueryAttributeCheck: { }
137
- RemoveTrailingWhitespaceCheck: { }
138
- UseMultipartAlternativeAsContentTypeOfEmailCheck: {}
139
- SimplifyRenderInViewsCheck: {}
140
- SimplifyRenderInControllersCheck: {}
125
+ MoveCodeIntoModelCheck: { use_count: 2 }
126
+ MoveFinderToNamedScopeCheck: { }
127
+ MoveModelLogicIntoModelCheck: { use_count: 4 }
128
+ NeedlessDeepNestingCheck: { nested_count: 2 }
129
+ NotUseDefaultRouteCheck: { }
130
+ NotUseTimeAgoInWordsCheck: {}
131
+ OveruseRouteCustomizationsCheck: { customize_count: 3 }
132
+ ProtectMassAssignmentCheck: {}
141
133
  RemoveEmptyHelpersCheck: {}
142
134
  RemoveTabCheck: {}
143
- RestrictAutoGeneratedRoutesCheck: { }
144
- RemoveUnusedMethodsInModelsCheck: { except_methods: [] }
135
+ RemoveTrailingWhitespaceCheck: { }
145
136
  RemoveUnusedMethodsInControllersCheck: { except_methods: [] }
146
137
  RemoveUnusedMethodsInHelpersCheck: { except_methods: [] }
147
- NotUseTimeAgoInWordsCheck: {}
148
- ProtectMassAssignmentCheck: {}
138
+ RemoveUnusedMethodsInModelsCheck: { except_methods: [] }
139
+ ReplaceComplexCreationWithFactoryMethodCheck: { attribute_assignment_count: 2 }
140
+ ReplaceInstanceVariableWithLocalVariableCheck: { }
141
+ RestrictAutoGeneratedRoutesCheck: { }
142
+ SimplifyRenderInControllersCheck: {}
143
+ SimplifyRenderInViewsCheck: {}
144
+ UseBeforeFilterCheck: { customize_count: 2 }
145
+ UseModelAssociationCheck: { }
146
+ UseMultipartAlternativeAsContentTypeOfEmailCheck: {}
147
+ UseObserverCheck: { }
148
+ UseParenthesesInMethodDefCheck: {}
149
+ UseQueryAttributeCheck: { }
150
+ UseSayWithTimeInMigrationsCheck: { }
151
+ UseScopeAccessCheck: { }
149
152
 
150
153
  You can remove or comment one review to disable it, and you can change the options.
151
154
 
@@ -184,7 +187,7 @@ Mailer
184
187
  Migration
185
188
 
186
189
  1. Isolating seed data
187
- 2. Always add db`index
190
+ 2. Always add db index
188
191
  3. Use say with time in migrations
189
192
 
190
193
  Controller
@@ -215,6 +218,9 @@ Other
215
218
 
216
219
  1. Remove trailing whitespace
217
220
  2. Remove tab
221
+ 3. Hash Syntax
222
+ 4. Use parentheses in method def
223
+ 5. Long line
218
224
 
219
225
  Write Your Own Check List
220
226
  -------------------------
@@ -236,7 +242,7 @@ Follow us on twitter: <http://twitter.com/railsbp>
236
242
  Send us email: <team@railsbp.com>
237
243
 
238
244
 
239
- Copyright © 2009 - 2011 Richard Huang (flyerhzm@gmail.com), released under the MIT license
245
+ Copyright © 2009 - 2012 Richard Huang (flyerhzm@gmail.com), released under the MIT license
240
246
 
241
247
 
242
248
  [1]:https://github.com/railsbp/rails_best_practices/wiki/How-to-write-your-own-check-list
@@ -115,7 +115,7 @@ module RailsBestPractices
115
115
  #
116
116
  # @param [Array] dirs what directories to expand
117
117
  # @return [Array] all files expanded
118
- def expand_dirs_to_files *dirs
118
+ def expand_dirs_to_files(*dirs)
119
119
  extensions = ['rb', 'erb', 'rake', 'rhtml', 'haml', 'slim', 'builder', 'rxml']
120
120
 
121
121
  dirs.flatten.map { |entry|
@@ -135,7 +135,7 @@ module RailsBestPractices
135
135
  #
136
136
  # @param [Array] files
137
137
  # @return [Array] sorted files
138
- def file_sort files
138
+ def file_sort(files)
139
139
  models = files.find_all { |file| file =~ Core::Check::MODEL_FILES }
140
140
  mailers = files.find_all { |file| file =~ Core::Check::MAILER_FILES }
141
141
  helpers = files.find_all { |file| file =~ Core::Check::HELPER_FILES }
@@ -152,7 +152,7 @@ module RailsBestPractices
152
152
  # @param [Array] files
153
153
  # @param [Regexp] pattern files match the pattern will be ignored
154
154
  # @return [Array] files that not match the pattern
155
- def file_ignore files, pattern
155
+ def file_ignore(files, pattern)
156
156
  files.reject { |file| file.index(pattern) }
157
157
  end
158
158
 
@@ -160,7 +160,7 @@ module RailsBestPractices
160
160
  #
161
161
  # @param [Array] files
162
162
  # @param [Regexp] patterns, files match any pattern will be accepted
163
- def file_accept files, patterns
163
+ def file_accept(files, patterns)
164
164
  files.reject { |file| !patterns.any? { |pattern| file =~ pattern } }
165
165
  end
166
166
 
@@ -217,15 +217,15 @@ module RailsBestPractices
217
217
  File.open(@options["output-file"], "w+") do |file|
218
218
  eruby = Erubis::Eruby.new(template)
219
219
  file.puts eruby.evaluate(
220
- :errors => errors,
221
- :error_types => error_types,
222
- :textmate => @options["with-textmate"],
223
- :mvim => @options["with-mvim"],
224
- :github => @options["with-github"],
225
- :github_name => @options["github-name"],
226
- :last_commit_id => last_commit_id,
227
- :git => @options["with-git"],
228
- :hg => @options["with-hg"]
220
+ errors: errors,
221
+ error_types: error_types,
222
+ textmate: @options["with-textmate"],
223
+ mvim: @options["with-mvim"],
224
+ github: @options["with-github"],
225
+ github_name: @options["github-name"],
226
+ last_commit_id: last_commit_id,
227
+ git: @options["with-git"],
228
+ hg: @options["with-hg"]
229
229
  )
230
230
  end
231
231
  end
@@ -81,11 +81,11 @@ module RailsBestPractices
81
81
  # @param [Integer] line_number, is the line number of the source code which is reviewing
82
82
  def add_error(message, filename = @node.file, line_number = @node.line)
83
83
  errors << RailsBestPractices::Core::Error.new(
84
- :filename => filename,
85
- :line_number => line_number,
86
- :message => message,
87
- :type => self.class.to_s,
88
- :url => url
84
+ filename: filename,
85
+ line_number: line_number,
86
+ message: message,
87
+ type: self.class.to_s,
88
+ url: url
89
89
  )
90
90
  end
91
91
 
@@ -312,7 +312,7 @@ module RailsBestPractices
312
312
  # remember hash values for hash key "methods".
313
313
  #
314
314
  # def to_xml(options = {})
315
- # super options.merge(:exclude => :visible, :methods => [:is_discussion_conversation])
315
+ # super options.merge(exclude: :visible, methods: [:is_discussion_conversation])
316
316
  # end
317
317
  add_callback "start_bare_assoc_hash" do |node|
318
318
  if node.hash_keys.include? "methods"
@@ -336,6 +336,7 @@ module RailsBestPractices
336
336
 
337
337
  private
338
338
  def mark_used(method_node)
339
+ return if method_node == :call
339
340
  if :bare_assoc_hash == method_node.sexp_type
340
341
  method_node.hash_values.each { |value_node| mark_used(value_node) }
341
342
  elsif :array == method_node.sexp_type
@@ -17,7 +17,7 @@ module RailsBestPractices
17
17
  # remember all the checks for prepare and review processes according to interesting_nodes.
18
18
  #
19
19
  # @param [Hash] options
20
- # {:lexicals => [], :prepares => [], :reviews => []}
20
+ # {lexicals: [], prepares: [], reviews: []}
21
21
  def initialize(options={})
22
22
  @lexicals = options[:lexicals]
23
23
  [:prepare, :review].each do |process|
@@ -48,10 +48,9 @@ module RailsBestPractices
48
48
  @lexicals = lexicals.empty? ? load_lexicals : lexicals
49
49
  @prepares = prepares.empty? ? load_prepares : prepares
50
50
  @reviews = reviews.empty? ? load_reviews : reviews
51
-
52
51
  load_plugin_reviews if reviews.empty?
53
52
 
54
- @checker ||= CheckingVisitor.new(:prepares => @prepares, :reviews => @reviews, :lexicals => @lexicals)
53
+ @checker ||= CheckingVisitor.new(prepares: @prepares, reviews: @reviews, lexicals: @lexicals)
55
54
  @debug = false
56
55
  @whiny = false
57
56
  end
@@ -169,7 +168,7 @@ module RailsBestPractices
169
168
  elsif filename =~ /.*\.haml$/
170
169
  begin
171
170
  require 'haml'
172
- content = Haml::Engine.new(content, {:ugly => true}).precompiled
171
+ content = Haml::Engine.new(content, {ugly: true}).precompiled
173
172
  # remove \xxx characters
174
173
  content.gsub!(/\\\d{3}/, '')
175
174
  rescue LoadError
@@ -21,11 +21,16 @@ class Sexp
21
21
  # s(:@ident, "test", s(2, 12)
22
22
  # => 2
23
23
  def line
24
- if [:def, :defs, :command, :command_call, :call, :fcall, :method_add_arg, :method_add_block,
25
- :var_ref, :vcall, :const_ref, :const_path_ref, :class, :module, :if, :unless, :elsif, :binary,
26
- :alias, :symbol_literal, :symbol, :aref].include? sexp_type
24
+ case sexp_type
25
+ when :def, :defs, :command, :command_call, :call, :fcall, :method_add_arg, :method_add_block,
26
+ :var_ref, :vcall, :const_ref, :const_path_ref, :class, :module, :if, :unless, :elsif, :ifop, :binary,
27
+ :alias, :symbol_literal, :symbol, :aref, :hash, :assoc_new, :string_literal
27
28
  self[1].line
28
- elsif :array == sexp_type
29
+ when :assoclist_from_args, :bare_assoc_hash
30
+ self[1][0].line
31
+ when :string_add
32
+ self[2].line
33
+ when :array
29
34
  array_values.first.line
30
35
  else
31
36
  self.last.first if self.last.is_a? Array
@@ -53,10 +58,10 @@ class Sexp
53
58
  #
54
59
  # options is the grep conditions, like
55
60
  #
56
- # :sexp_type => :call,
57
- # :subject => "Post",
58
- # :message => ["find", "new"]
59
- # :to_s => "devise"
61
+ # sexp_type: :call,
62
+ # subject: "Post",
63
+ # message: ["find", "new"]
64
+ # to_s: "devise"
60
65
  #
61
66
  # the condition key is one of :sexp_type, :subject, :message, :to_s,
62
67
  # the condition value can be Symbol, Array or Sexp.
@@ -81,9 +86,9 @@ class Sexp
81
86
  #
82
87
  # options is the grep conditions, like
83
88
  #
84
- # :sexp_type => :call,
85
- # :subject => s(:const, Post),
86
- # :message => [:find, :new]
89
+ # sexp_type: :call,
90
+ # subject: s(:const, Post),
91
+ # message: [:find, :new]
87
92
  #
88
93
  # the condition key is one of :sexp_type, :subject, :message, and to_s,
89
94
  # the condition value can be Symbol, Array or Sexp.
@@ -326,7 +331,7 @@ class Sexp
326
331
  #
327
332
  # @return [Sexp] conditional statement of if node
328
333
  def conditional_statement
329
- if [:if, :unless, :elsif].include? sexp_type
334
+ if [:if, :unless, :elsif, :ifop].include? sexp_type
330
335
  self[1]
331
336
  end
332
337
  end
@@ -745,11 +750,7 @@ class Sexp
745
750
  def to_object
746
751
  case sexp_type
747
752
  when :array
748
- if nil == self[1]
749
- []
750
- else
751
- arguments.all.map(&:to_s)
752
- end
753
+ array_values.map(&:to_s)
753
754
  else
754
755
  to_s
755
756
  end
@@ -1,3 +1,4 @@
1
1
  # encoding: utf-8
2
2
  require 'rails_best_practices/lexicals/remove_trailing_whitespace_check'
3
3
  require 'rails_best_practices/lexicals/remove_tab_check'
4
+ require 'rails_best_practices/lexicals/long_line_check'
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+ require 'rails_best_practices/reviews/review'
3
+ module RailsBestPractices
4
+ module Lexicals
5
+ # Keep lines fewer than 80 characters.
6
+ class LongLineCheck < Core::Check
7
+ def initialize(options = {})
8
+ super()
9
+ @max_line_length = options['max_line_length'] || 80
10
+ end
11
+
12
+ # check if a line is over 80 characters
13
+ #
14
+ # @param [String] filename name of the file
15
+ # @param [String] content content of the file
16
+ def check(filename, content)
17
+ # Only check ruby files
18
+ if /\.rb$/ =~ filename
19
+ line_no = 0
20
+ content.each_line do |line|
21
+ line_no += 1
22
+ actual_line_length = line.sub(/\s+$/, '').length
23
+ if actual_line_length > @max_line_length
24
+ add_error("line is longer than #{@max_line_length} characters (#{actual_line_length} characters)", filename, line_no)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -12,7 +12,7 @@ module RailsBestPractices
12
12
  end
13
13
 
14
14
  def start_assign(node)
15
- if node.left_value.grep_node(:sexp_type => [:vcall, :var_ref], :to_s => "config").present?
15
+ if node.left_value.grep_node(sexp_type: [:vcall, :var_ref], to_s: "config").present?
16
16
  @configs[node.left_value.to_s] = node.right_value.to_s
17
17
  end
18
18
  end
@@ -31,3 +31,5 @@ require 'rails_best_practices/reviews/remove_unused_methods_in_controllers_revie
31
31
  require 'rails_best_practices/reviews/remove_unused_methods_in_helpers_review'
32
32
  require 'rails_best_practices/reviews/not_use_time_ago_in_words_review'
33
33
  require 'rails_best_practices/reviews/protect_mass_assignment_review'
34
+ require 'rails_best_practices/reviews/use_parentheses_in_method_def_review'
35
+ require 'rails_best_practices/reviews/hash_syntax_review'
@@ -53,9 +53,9 @@ module RailsBestPractices
53
53
  left_value = node.left_value
54
54
  right_value = node.right_value
55
55
  return unless :field == left_value.sexp_type && :call == right_value.sexp_type
56
- aref_node = right_value.grep_node(:sexp_type => :aref)
56
+ aref_node = right_value.grep_node(sexp_type: :aref)
57
57
  if aref_node
58
- assignments(left_value.subject.to_s) << {:message => left_value.message.to_s, :arguments => aref_node.to_s}
58
+ assignments(left_value.subject.to_s) << {message: left_value.message.to_s, arguments: aref_node.to_s}
59
59
  end
60
60
  end
61
61
 
@@ -17,7 +17,7 @@ module RailsBestPractices
17
17
  # if the subject of command node is "add_index", then remember the index columns
18
18
  # after all of these, at the end of review process
19
19
  #
20
- # ActiveRecord::Schema.define(:version => 20101201111111) do
20
+ # ActiveRecord::Schema.define(version: 20101201111111) do
21
21
  # ......
22
22
  # end
23
23
  #
@@ -0,0 +1,63 @@
1
+ # encoding: utf-8
2
+ require 'rails_best_practices/reviews/review'
3
+
4
+ module RailsBestPractices
5
+ module Reviews
6
+ # Check ruby 1.8 style hash and suggest to change hash syntax to 1.9.
7
+ #
8
+ # Review process:
9
+ # check hash nodes in all files,
10
+ # if the sexp type of hash key nodes is not :@lable,
11
+ # then the hash is ruby 1.8 style.
12
+ class HashSyntaxReview < Review
13
+ interesting_nodes :hash, :bare_assoc_hash
14
+ interesting_files ALL_FILES
15
+
16
+ def initialize(options = {})
17
+ super()
18
+ @only_symbol = options[:only_symbol]
19
+ @only_string = options[:only_string]
20
+ end
21
+
22
+ # check hash node to see if it is ruby 1.8 style.
23
+ def start_hash(node)
24
+ return if s(:hash, nil) == node
25
+ pair_nodes = node[1][1]
26
+
27
+ if hash_is_18?(pair_nodes)
28
+ add_error "change Hash Syntax to 1.9"
29
+ end
30
+ end
31
+
32
+ # check bare_assoc_hash node to see if it is ruby 1.8 style.
33
+ def start_bare_assoc_hash(node)
34
+ pair_nodes = node[1]
35
+
36
+ if hash_is_18?(pair_nodes)
37
+ add_error "change Hash Syntax to 1.9"
38
+ end
39
+ end
40
+
41
+ protected
42
+ # check if hash key/value pairs are ruby 1.8 style.
43
+ #
44
+ # hash key of ruby 1.9 style is :@label,
45
+ # so if it is not, then it is ruby 1.8 style.
46
+ def hash_is_18?(pair_nodes)
47
+ return false if pair_nodes.blank?
48
+ pair_nodes.size.times do |i|
49
+ if @only_symbol
50
+ return true if :symbol_literal == pair_nodes[i][1].sexp_type
51
+ elsif @only_string
52
+ return true if :string_literal == pair_nodes[i][1].sexp_type
53
+ elsif :@label != pair_nodes[i][1].sexp_type
54
+ return true
55
+ end
56
+ end
57
+
58
+ false
59
+ end
60
+ end
61
+ end
62
+ end
63
+