Class: RailsBestPractices::Reviews::RemoveUnusedMethodsInControllersReview
- Inherits:
-
Review
- Object
- Core::Check
- Review
- RailsBestPractices::Reviews::RemoveUnusedMethodsInControllersReview
- Includes:
- Afterable, Callable, Classable, Exceptable, InheritedResourcesable, Moduleable
- Defined in:
- lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb
Overview
Find out unused methods in controllers.
Implementation:
Review process:
remember all method calls in controllers,
if they are not defined in routes,
and they are not called in controllers,
then they are the unused methods in controllers.
Constant Summary collapse
- INHERITED_RESOURCES_METHODS =
%w(resource collection begin_of_association_chain build_resource)
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
-
#after_review ⇒ Object
get all unused methods at the end of review process.
-
#end_class(node) ⇒ Object
mark custom inherited_resources methods as used.
-
#initialize(options = {}) ⇒ RemoveUnusedMethodsInControllersReview
constructor
A new instance of RemoveUnusedMethodsInControllersReview.
-
#skip_command_callback_nodes ⇒ Object
skip render and around_filter nodes for start_command callbacks.
-
#start_command(node) ⇒ Object
(also: #start_method_add_arg)
mark corresponding action as used for cells’ render and render_call.
Methods inherited from Review
#model_associations, #model_attributes, #models, #remember_variable_use_count, #reset_variable_use_count, #url, #variable, #variable_use_count
Methods inherited from Core::Check
add_callback, #add_error, #after_prepare, 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, #url
Constructor Details
#initialize(options = {}) ⇒ RemoveUnusedMethodsInControllersReview
Returns a new instance of RemoveUnusedMethodsInControllersReview.
28 29 30 31 32 33 |
# File 'lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb', line 28 def initialize(={}) super @controller_methods = Prepares.controller_methods @routes = Prepares.routes @inherited_resources = false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class RailsBestPractices::Core::Check
Instance Method Details
#after_review ⇒ Object
get all unused methods at the end of review process.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb', line 79 def after_review @routes.each do |route| if "*" == route.action_name action_names = @controller_methods.get_methods(route.controller_name_with_namespaces).map(&:method_name) action_names.each { |action_name| call_method(action_name, route.controller_name_with_namespaces) } else call_method(route.action_name, route.controller_name_with_namespaces) end end @controller_methods.get_all_unused_methods.each do |method| if !excepted?(method) add_error "remove unused methods (#{method.class_name}##{method.method_name})", method.file, method.line end end end |
#end_class(node) ⇒ Object
mark custom inherited_resources methods as used.
36 37 38 39 40 41 42 |
# File 'lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb', line 36 def end_class(node) if @inherited_resources INHERITED_RESOURCES_METHODS.each do |method| call_method(method) end end end |
#skip_command_callback_nodes ⇒ Object
skip render and around_filter nodes for start_command callbacks.
45 46 47 |
# File 'lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb', line 45 def skip_command_callback_nodes %w(render_cell render around_filter) end |
#start_command(node) ⇒ Object Also known as: start_method_add_arg
mark corresponding action as used for cells’ render and render_call.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb', line 50 def start_command(node) case node..to_s when "render_cell" controller_name, action_name, _ = *node.arguments.all.map(&:to_s) call_method(action_name, "#{controller_name}_cell".classify) when "render" first_argument = node.arguments.all.first if first_argument.present? && first_argument.hash_value("state").present? action_name = first_argument.hash_value("state").to_s call_method(action_name, current_class_name) end when "around_filter" node.arguments.all.each { |argument| mark_used(argument) } when "helper_method" node.arguments.all.each { |argument| mark_publicize(argument.to_s) } when "delegate" last_argument = node.arguments.all.last if :bare_assoc_hash == last_argument.sexp_type && "controller" == last_argument.hash_value("to").to_s controller_name = current_module_name.sub("Helper", "Controller") node.arguments.all[0..-2].each { |method| mark_publicize(method.to_s, controller_name) } end else # nothing end end |