Class: RailsBestPractices::Prepares::RoutePrepare

Inherits:
Core::Check
  • Object
show all
Defined in:
lib/rails_best_practices/prepares/route_prepare.rb

Overview

Remembber routes.

Constant Summary collapse

RESOURCES_ACTIONS =
%w(index show new create edit update destroy)
RESOURCE_ACTIONS =
%w(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 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, #url

Constructor Details

#initializeRoutePrepare

Returns a new instance of RoutePrepare.



14
15
16
17
18
# File 'lib/rails_best_practices/prepares/route_prepare.rb', line 14

def initialize
  @routes = Prepares.routes
  @namespaces = []
  @controller_names = []
end

Dynamic Method Handling

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

Instance Method Details

#current_controller_nameObject



228
229
230
# File 'lib/rails_best_practices/prepares/route_prepare.rb', line 228

def current_controller_name
  @controller_names.last || @controller_name.try(:last)
end

#current_namespacesObject



224
225
226
# File 'lib/rails_best_practices/prepares/route_prepare.rb', line 224

def current_namespaces
  @namespaces.dup
end

#end_do_block(node) ⇒ Object Also known as: end_brace_block

remove current controller name, and use upper lever resource name.



156
157
158
# File 'lib/rails_best_practices/prepares/route_prepare.rb', line 156

def end_do_block(node)
  @controller_names.pop
end

#end_method_add_block(node) ⇒ Object

end of namespace call.



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rails_best_practices/prepares/route_prepare.rb', line 137

def end_method_add_block(node)
  case node.message.to_s
  when "namespace"
    @namespaces.pop
  when "scope"
    if node.arguments.all.last.hash_value("module").present?
      @namespaces.pop
    end
  else
    # do nothing
  end
end

#start_command(node) ⇒ Object

remember route for rails3.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rails_best_practices/prepares/route_prepare.rb', line 21

def start_command(node)
  case node.message.to_s
  when "resources"
    add_resources_routes(node)
  when "resource"
    add_resource_routes(node)
  when "get", "post", "put", "delete"
    first_argument = node.arguments.all.first
    second_argument = node.arguments.all[1]
    if @controller_names.last
      if :bare_assoc_hash == first_argument.sexp_type
        action_names = [first_argument.hash_values.first.to_s]
      elsif :array == first_argument.sexp_type
        action_names = first_argument.array_values.map(&:to_s)
      else
        action_names = [first_argument.to_s]
      end
      action_names.each do |action_name|
        @routes.add_route(current_namespaces, current_controller_name, action_name)
      end
    else
      if :bare_assoc_hash == first_argument.sexp_type
        route_node = first_argument.hash_values.first
        # do not parse redirect block
        return if :method_add_arg == route_node.sexp_type
        controller_name, action_name = route_node.to_s.split('#')
      elsif :array == first_argument.sexp_type
        first_argument.array_values.map(&:to_s).each do |action_node|
          @routes.add_route(current_namespaces, controller_name, action_node.to_s)
        end
        return
      elsif :bare_assoc_hash == second_argument.try(:sexp_type)
        if second_argument.hash_value("to").present?
          controller_name, action_name = second_argument.hash_value("to").to_s.split('#')
        else
          controller_name = current_controller_name
          action_name = second_argument.hash_value("action")
        end
      else
        controller_name, action_name = first_argument.to_s.split('/')
      end
      @routes.add_route(current_namespaces, controller_name.try(:underscore), action_name)
    end
  when "match", "root"
    options = node.arguments.all.last
    case options.sexp_type
    when :bare_assoc_hash
      if options.hash_value("controller").present?
        return if :regexp_literal == options.hash_value("controller").sexp_type
        controller_name = options.hash_value("controller").to_s
        action_name = options.hash_value("action").present? ? options.hash_value("action").to_s : "*"
        @routes.add_route(current_namespaces, controller_name, action_name)
      else
        route_node = options.hash_values.find { |value_node| :string_literal == value_node.sexp_type && value_node.to_s.include?('#') }
        if route_node.present?
          controller_name, action_name = route_node.to_s.split('#')
          @routes.add_route(current_namespaces, controller_name.underscore, action_name)
        end
      end
    when :string_literal, :symbol_literal
      if current_controller_name
        @routes.add_route(current_namespaces, current_controller_name, options.to_s)
      end
    else
      # do nothing
    end
  else
    # nothing to do
  end
end

#start_command_call(node) ⇒ Object

remember route for rails2.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rails_best_practices/prepares/route_prepare.rb', line 93

def start_command_call(node)
  case node.message.to_s
  when "resources"
    add_resources_routes(node)
  when "resource"
    add_resource_routes(node)
  when "namespace"
    # nothing to do
  else
    options = node.arguments.all.last
    if options.hash_value("controller").present?
      @controller_name = [:option, options.hash_value("controller").to_s]
    end
    action_name = options.hash_value("action").present? ? options.hash_value("action").to_s : "*"
    @routes.add_route(current_namespaces, current_controller_name, action_name)
  end
end

#start_do_block(node) ⇒ Object Also known as: start_brace_block

remember current controller name, used for nested resources.



151
152
153
# File 'lib/rails_best_practices/prepares/route_prepare.rb', line 151

def start_do_block(node)
  @controller_names << @controller_name.try(:last)
end

#start_method_add_block(node) ⇒ Object

remember the namespace.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rails_best_practices/prepares/route_prepare.rb', line 112

def start_method_add_block(node)
  case node.message.to_s
  when "namespace"
    @namespaces << node.arguments.all.first.to_s
    @controller_name = nil
  when "scope"
    if node.arguments.all.last.hash_value("module").present?
      @namespaces << node.arguments.all.last.hash_value("module").to_s
    end
    if node.arguments.all.last.hash_value("controller").present?
      @controller_name = [:scope, node.arguments.all.last.hash_value("controller").to_s]
    else
      @controller_name = @controller_name.try(:first) == :scope ? @controller_name : nil
    end
  when "with_options"
    argument = node.arguments.all.last
    if :bare_assoc_hash == argument.sexp_type && argument.hash_value("controller").present?
      @controller_name = [:with_option, argument.hash_value("controller").to_s]
    end
  else
    # do nothing
  end
end