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 command_call nodes in route file.
if the message of command_call node is resources,
and the second argument of command_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 method_add_block nodes in route file.
if the subject of method_add_block node is with message resources,
and in the block body of method_add_block node, there are more than @customize_count command nodes,
whose message is get, post, update or delete,
then these custom routes are overuse.

Constant Summary collapse

VERBS =
%w(get post update delete)

Constants inherited from Core::Check

Core::Check::ALL_FILES, Core::Check::CONTROLLER_FILES, Core::Check::DEPLOY_FILES, Core::Check::HELPER_FILES, Core::Check::MAILER_FILES, Core::Check::MIGRATION_FILES, Core::Check::MODEL_FILES, Core::Check::PARTIAL_VIEW_FILES, Core::Check::ROUTE_FILES, Core::Check::SCHEMA_FILE, Core::Check::VIEW_FILES

Instance Method Summary collapse

Methods inherited from Review

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

Methods inherited from Core::Check

add_callback, #add_error, #after_prepare, #after_review, callbacks, #errors, #increment_total_files_checked!, #interesting_files, interesting_files, interesting_nodes, #interesting_nodes, #method_missing, #node_end, #node_start, #parse_file?, #result, #total_files_checked

Constructor Details

#initialize(options = {}) ⇒ OveruseRouteCustomizationsReview

Returns a new instance of OveruseRouteCustomizationsReview.



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

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

#start_command_call(node) ⇒ Object

check command_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, then they are overuse route customizations.



52
53
54
55
56
# File 'lib/rails_best_practices/reviews/overuse_route_customizations_review.rb', line 52

def start_command_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_method_add_block(node) ⇒ Object

check method_add_block 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 method_add_block node is with message “resources”, and in the block body of method_add_block node, there are more than @customize_count call nodes, whose message is :get, :post, :update or :delete, then they are overuse route customizations.



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

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

#urlObject



36
37
38
# File 'lib/rails_best_practices/reviews/overuse_route_customizations_review.rb', line 36

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