Class: RailsBestPractices::Reviews::RemoveUnusedMethodsInModelsReview

Inherits:
Review
  • Object
show all
Includes:
Afterable, Callable, Classable, Exceptable
Defined in:
lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb

Overview

Find out unused methods in models.

Implemenation:

Review process:

remember all method calls,
at end, check if all defined methods are called,
if not, non called methods are unused.

Constant Summary

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, #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 = {}) ⇒ RemoveUnusedMethodsInModelsReview

Returns a new instance of RemoveUnusedMethodsInModelsReview.



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

def initialize(options={})
  super
  @model_methods = Prepares.model_methods
end

Dynamic Method Handling

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

Instance Method Details

#after_reviewObject

get all unused methods at the end of review process.



82
83
84
85
86
87
88
# File 'lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb', line 82

def after_review
  @model_methods.get_all_unused_methods.each do |method|
    if !excepted?(method) && method.method_name !~ /=$/
      add_error "remove unused methods (#{method.class_name}##{method.method_name})", method.file, method.line
    end
  end
end

#skip_command_callback_nodesObject

skip scope and validate nodes for start_command callbacks.



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

def skip_command_callback_nodes
  %w(named_scope scope validate validate_on_create validate_on_update)
end

#start_command(node) ⇒ Object

mark validate methods as used. mark key method and value method for collection_select and grouped_collection_select.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb', line 35

def start_command(node)
  arguments = node.arguments.all
  case node.message.to_s
  when "validate", "validate_on_create", "validate_on_update"
    arguments.each { |argument| mark_used(argument) }
  when "collection_select"
    mark_used(arguments[3])
    mark_used(arguments[4])
  when "grouped_collection_select"
    mark_used(arguments[3])
    mark_used(arguments[4])
    mark_used(arguments[5])
    mark_used(arguments[6])
  end
end

#start_command_call(node) ⇒ Object

mark key method and value method for collection_select and grouped_collection_select.



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb', line 52

def start_command_call(node)
  arguments = node.arguments.all
  case node.message.to_s
  when "collection_select"
    mark_used(arguments[2])
    mark_used(arguments[3])
  when "grouped_collection_select"
    mark_used(arguments[2])
    mark_used(arguments[3])
    mark_used(arguments[4])
    mark_used(arguments[5])
  end
end

#start_method_add_arg(node) ⇒ Object

mark key method and value method for options_from_collection_for_select and option_groups_from_collection_for_select.



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb', line 67

def start_method_add_arg(node)
  arguments = node.arguments.all
  case node.message.to_s
  when "options_from_collection_for_select"
    mark_used(arguments[1])
    mark_used(arguments[2])
  when "option_groups_from_collection_for_select"
    mark_used(arguments[1])
    mark_used(arguments[2])
    mark_used(arguments[3])
    mark_used(arguments[4])
  end
end