Class: RailsBestPractices::Prepares::ControllerPrepare

Inherits:
Core::Check
  • Object
show all
Includes:
Core::Check::Accessable, Core::Check::Afterable, Core::Check::Classable, Core::Check::InheritedResourcesable
Defined in:
lib/rails_best_practices/prepares/controller_prepare.rb

Overview

Remember controllers and controller methods

Constant Summary collapse

DEFAULT_ACTIONS =
%w(index show new create edit update destroy)

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 included from Core::Check::Afterable

included

Methods included from Core::Check::Accessable

#current_access_control, included

Methods included from Core::Check::InheritedResourcesable

included

Methods included from Core::Check::Classable

#classable_modules, #current_class_name, #current_extend_class_name, included

Methods inherited from Core::Check

add_callback, #add_error, #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, #url

Constructor Details

#initializeControllerPrepare

Returns a new instance of ControllerPrepare.



18
19
20
21
22
23
# File 'lib/rails_best_practices/prepares/controller_prepare.rb', line 18

def initialize
  @controllers = Prepares.controllers
  @methods = Prepares.controller_methods
  @helpers = Prepares.helpers
  @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_prepareObject

ask Reviews::RemoveUnusedMoethodsInHelperReview to check the controllers who include helpers.



92
93
94
95
96
97
# File 'lib/rails_best_practices/prepares/controller_prepare.rb', line 92

def after_prepare
  decendants = @helpers.map(&:decendants).flatten
  if decendants.present?
    Reviews::RemoveUnusedMethodsInHelpersReview.interesting_files *decendants.map { |decendant| %r|#{decendant.underscore}| }
  end
end

#end_class(node) ⇒ Object

remember the action names at the end of class node if the controller is a InheritedResources.



35
36
37
38
39
40
41
# File 'lib/rails_best_practices/prepares/controller_prepare.rb', line 35

def end_class(node)
  if @inherited_resources && "ApplicationController" != current_class_name
    @actions.each do |action|
      @methods.add_method(current_class_name, action, {"file" => node.file, "line" => node.line})
    end
  end
end

#start_class(node) ⇒ Object

check class node to remember the class name. also check if the controller is inherit from InheritedResources::Base.



27
28
29
30
31
32
# File 'lib/rails_best_practices/prepares/controller_prepare.rb', line 27

def start_class(node)
  @controllers << @klass
  if @inherited_resources
    @actions = DEFAULT_ACTIONS
  end
end

#start_command(node) ⇒ Object

restrict actions for inherited_resources



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rails_best_practices/prepares/controller_prepare.rb', line 58

def start_command(node)
  if "include" == node.message.to_s
    @helpers.add_module_decendant(node.arguments.all.first.to_s, current_class_name)
  elsif @inherited_resources && "actions" ==  node.message.to_s
    if "all" == node.arguments.all.first.to_s
      @actions = DEFAULT_ACTIONS
      option_argument = node.arguments.all[1]
      if option_argument && :bare_assoc_hash == option_argument.sexp_type && option_argument.hash_value("except")
        @actions -= option_argument.hash_value("except").to_object
      end
    else
      @actions = node.arguments.all.map(&:to_s)
    end
  end
end

#start_def(node) ⇒ Object

check def node to remember all methods.

the remembered methods (@methods) are like

{
  "PostsController" => {
    "save" => {"file" => "app/controllers/posts_controller.rb", "line" => 10, "unused" => false},
    "find" => {"file" => "app/controllers/posts_controller.rb", "line" => 10, "unused" => false}
  },
  "CommentsController" => {
    "create" => {"file" => "app/controllers/comments_controller.rb", "line" => 10, "unused" => false},
  }
}


86
87
88
89
# File 'lib/rails_best_practices/prepares/controller_prepare.rb', line 86

def start_def(node)
  method_name = node.method_name.to_s
  @methods.add_method(current_class_name, method_name, {"file" => node.file, "line" => node.line}, current_access_control)
end

#start_var_ref(node) ⇒ Object

check if there is a DSL call inherit_resources.



44
45
46
47
48
# File 'lib/rails_best_practices/prepares/controller_prepare.rb', line 44

def start_var_ref(node)
  if @inherited_resources
    @actions = DEFAULT_ACTIONS
  end
end

#start_vcall(node) ⇒ Object

check if there is a DSL call inherit_resources.



51
52
53
54
55
# File 'lib/rails_best_practices/prepares/controller_prepare.rb', line 51

def start_vcall(node)
  if @inherited_resources
    @actions = DEFAULT_ACTIONS
  end
end