Class: RailsBestPractices::Reviews::RestrictAutoGeneratedRoutesReview

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

Overview

Review a route file to make sure all auto-generated routes have corresponding actions in controller.

See the best practice details here rails-bestpractices.com/posts/86-restrict-auto-generated-routes

Implementation:

Review process:

check all resources and resource method calls,
compare the generated routes and corresponding actions in controller,
if there is a route generated, but there is not action in that controller,
then you should restrict your routes.

Constant Summary collapse

RESOURCE_METHODS =
["show", "new", "create", "edit", "update", "destroy"]
RESOURCES_METHODS =
RESOURCE_METHODS + ["index"]

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

#initializeRestrictAutoGeneratedRoutesReview

Returns a new instance of RestrictAutoGeneratedRoutesReview.



28
29
30
31
# File 'lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb', line 28

def initialize
  super
  @namespaces = []
end

Dynamic Method Handling

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

Instance Method Details

#end_method_add_block(node) ⇒ Object

end of namespace call.



50
51
52
53
54
# File 'lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb', line 50

def end_method_add_block(node)
  if "namespace" == node.message.to_s
    @namespaces.pop
  end
end

#start_command(node) ⇒ Object Also known as: start_command_call

check if the generated routes have the corresponding actions in controller for rails3 routes.



34
35
36
37
38
39
40
# File 'lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb', line 34

def start_command(node)
  if "resources" == node.message.to_s
    check_resources(node)
  elsif "resource" == node.message.to_s
    check_resource(node)
  end
end

#start_method_add_block(node) ⇒ Object

remember the namespace.



43
44
45
46
47
# File 'lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb', line 43

def start_method_add_block(node)
  if "namespace" == node.message.to_s
    @namespaces << node.arguments.all.first.to_s
  end
end

#urlObject



24
25
26
# File 'lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb', line 24

def url
  "http://rails-bestpractices.com/posts/86-restrict-auto-generated-routes"
end