Class: RailsBestPractices::Reviews::NeedlessDeepNestingReview

Inherits:
Review show all
Defined in:
lib/rails_best_practices/reviews/needless_deep_nesting_review.rb

Overview

Review config/routes.rb file to make sure not to use too deep nesting routes.

See the best practice details here rails-bestpractices.com/posts/11-needless-deep-nesting.

Implementation:

Review process:

chech all iter nodes in route file.

it is a recursively check in :iter node,

if it is a :iter node,
increment @counter at the beginning of resources,
decrement @counter at the end of resrouces,
recursively check nodes in iter's block body.

if it is a :block node,
then recursively check all child nodes in block node.

if it is a :call node,
and the message of the node is :resources or :resource,
and the @counter is greater than @nested_count defined,
then it is a needless deep nesting.

Constant Summary

Constants inherited from Core::Check

Core::Check::CONTROLLER_FILES, Core::Check::HELPER_FILES, Core::Check::MAILER_FILES, Core::Check::MIGRATION_FILES, Core::Check::MODEL_FILES, Core::Check::NODE_TYPES, Core::Check::PARTIAL_VIEW_FILES, Core::Check::ROUTE_FILE, Core::Check::SCHEMA_FILE, Core::Check::VIEW_FILES

Instance Attribute Summary

Attributes inherited from Core::Check

#errors

Instance Method Summary collapse

Methods inherited from Review

#equal?, #model_associations, #model_attributes, #models, #remember_variable_use_count, #reset_variable_use_count, #variable, #variable_use_count

Methods inherited from Core::Check

#add_error, #method_missing, #node_end, #node_start

Constructor Details

#initialize(options = {}) ⇒ NeedlessDeepNestingReview

Returns a new instance of NeedlessDeepNestingReview.



42
43
44
45
46
# File 'lib/rails_best_practices/reviews/needless_deep_nesting_review.rb', line 42

def initialize(options = {})
  super()
  @counter = 0
  @nested_count = options['nested_count'] || 2
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RailsBestPractices::Core::Check

Instance Method Details

#interesting_filesObject



38
39
40
# File 'lib/rails_best_practices/reviews/needless_deep_nesting_review.rb', line 38

def interesting_files
  ROUTE_FILE
end

#interesting_nodesObject



34
35
36
# File 'lib/rails_best_practices/reviews/needless_deep_nesting_review.rb', line 34

def interesting_nodes
  [:call, :iter]
end

#start_iter(node) ⇒ Object

check all iter node.

It is a recursively check,

if it is a :iter node, like

resources posts do
  ...
end

increment @counter at the beginning of resources, decrement @counter at the end of iter resources, recursively check the block body.

if it is a :block node, like

resources :posts do
  resources :comments
  resources :votes
end

just recursively check each child node in block node.

if it is a :call node with message :resources or :resource, like

resources :comments

test if the @counter is greater than or equal to @nested_count, if so, it is a needless deep nesting.



76
77
78
# File 'lib/rails_best_practices/reviews/needless_deep_nesting_review.rb', line 76

def start_iter(node)
  recursively_check(node)
end

#urlObject



30
31
32
# File 'lib/rails_best_practices/reviews/needless_deep_nesting_review.rb', line 30

def url
  "http://rails-bestpractices.com/posts/11-needless-deep-nesting"
end