Class: RailsBestPractices::Reviews::OveruseRouteCustomizationsReview

Inherits:
Review
  • Object
show all
Defined in:
lib/rails_best_practices/reviews/overuse_route_customizations_review.rb

Overview

Review config/routes.rb file to make sure there are no overuse route customizations.

See the best practice details here rails-bestpractices.com/posts/10-overuse-route-customizations.

Implementation:

Review process:

the check methods are different for rails2 and rails3 syntax.

for rails2

check all call nodes in route file.
if the message of call node is resources,
and the second argument of call node is a hash,
and the count of the pair (key/value) in hash is greater than @customize_count,
then these custom routes are overuse.

for rails3

check all iter nodes in route file.
if the subject of iter node is with message resources,
and in the block body of iter node, there are more than @customize_count call nodes,
whose message is :get, :post, :update or :delete,
then these custom routes are overuse.

Constant Summary collapse

VERBS =
[:get, :post, :update, :delete]

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 = {}) ⇒ OveruseRouteCustomizationsReview

Returns a new instance of OveruseRouteCustomizationsReview.



46
47
48
49
# File 'lib/rails_best_practices/reviews/overuse_route_customizations_review.rb', line 46

def initialize(options = {})
  super()
  @customize_count = options['customize_count'] || 3
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



42
43
44
# File 'lib/rails_best_practices/reviews/overuse_route_customizations_review.rb', line 42

def interesting_files
  ROUTE_FILE
end

#interesting_nodesObject



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

def interesting_nodes
  [:call, :iter]
end

#start_call(node) ⇒ Object

check call node to see if the count of member and collection custom routes is more than @customize_count defined. this is for rails2 syntax.

if the message of call node is :resources, and the second argument of call node is a hash, and the count of the pair (key/value) in hash is greater than @customize_count, like

map.resources :posts, :member => { :create_comment => :post,
                                   :update_comment => :update,
                                   :delete_comment => :delete },
                      :collection => { :comments => :get }

then they are overuse route customizations.



64
65
66
67
68
# File 'lib/rails_best_practices/reviews/overuse_route_customizations_review.rb', line 64

def start_call(node)
  if member_and_collection_count_for_rails2(node) > @customize_count
    add_error "overuse route customizations (customize_count > #{@customize_count})", node.file, node.subject.line
  end
end

#start_iter(node) ⇒ Object

check iter node to see if the count of member and collection custom routes is more than @customize_count defined. this is for rails3 syntax.

if the subject of iter node is with message :resources, and in the block body of iter node, there are more than @customize_count call nodes, whose message is :get, :post, :update or :delete, like

resources :posts do
  member do
    post :create_comment
    update :update_comment
    delete :delete_comment
  end

  collection do
    get :comments
  end
end

then they are overuse route customizations.



90
91
92
93
94
# File 'lib/rails_best_practices/reviews/overuse_route_customizations_review.rb', line 90

def start_iter(node)
  if member_and_collection_count_for_rails3(node) > @customize_count
    add_error "overuse route customizations (customize_count > #{@customize_count})", node.file, node.subject.line
  end
end

#urlObject



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

def url
  "http://rails-bestpractices.com/posts/10-overuse-route-customizations"
end