Class: ActionController::Routing::ControllerComponent

Inherits:
DynamicComponent show all
Defined in:
lib/action_controller/routing.rb

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from DynamicComponent

#condition, #default

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DynamicComponent

#default_check, #dynamic?, #initialize, #optional?, #write_continue_generation, #write_dropout_generation, #write_generation, #write_recognition

Methods inherited from Component

#dynamic?, new, #optional?

Constructor Details

This class inherits a constructor from ActionController::Routing::DynamicComponent

Class Method Details

.assign_controller(g, controller) ⇒ Object



217
218
219
220
# File 'lib/action_controller/routing.rb', line 217

def assign_controller(g, controller)
  expr = "::#{controller.split('/').collect {|c| c.camelize}.join('::')}Controller"
  g.result :controller, expr, true
end

.traverse_to_controller(segments, start_at = 0) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/action_controller/routing.rb', line 222

def traverse_to_controller(segments, start_at = 0)
  mod = ::Object
  length = segments.length
  index = start_at
  mod_name = controller_name = segment = nil
  
  while index < length
    return nil unless /^[A-Za-z][A-Za-z\d_]*$/ =~ (segment = segments[index])
    index += 1
    
    mod_name = segment.camelize
    controller_name = "#{mod_name}Controller"
    
    begin
      # We use eval instead of const_get to avoid obtaining values from parent modules.
      controller = eval("mod::#{controller_name}", nil, __FILE__, __LINE__)
      expected_name = "#{mod.name}::#{controller_name}"
      
      # Detect the case when const_get returns an object from a parent namespace.
      if controller.is_a?(Class) && controller.ancestors.include?(ActionController::Base) && (mod == Object || controller.name == expected_name)
        return controller, (index - start_at)
      end
    rescue NameError => e
      raise unless /^uninitialized constant .*#{controller_name}$/ =~ e.message                            
    end
    
    begin
      next_mod = eval("mod::#{mod_name}", nil, __FILE__, __LINE__)
      # Check that we didn't get a module from a parent namespace
      mod = (mod == Object || next_mod.name == "#{mod.name}::#{mod_name}") ? next_mod : nil
    rescue NameError => e
      raise unless /^uninitialized constant .*#{mod_name}$/ =~ e.message
    end
    
    return nil unless mod
  end
end

Instance Method Details

#add_segments_to(g) ⇒ Object



188
189
190
# File 'lib/action_controller/routing.rb', line 188

def add_segments_to(g)
  g.add_segment(%(\#{#{g.hash_value(key, true, default)}})) {|gp| yield gp}
end

#assign_default(g) ⇒ Object



212
213
214
# File 'lib/action_controller/routing.rb', line 212

def assign_default(g)
  ControllerComponent.assign_controller(g, default)
end

#assign_result(g) {|g| ... } ⇒ Object

Yields:

  • (g)


207
208
209
210
# File 'lib/action_controller/routing.rb', line 207

def assign_result(g)
  g.result key, 'controller_value'
  yield g
end

#keyObject



186
# File 'lib/action_controller/routing.rb', line 186

def key() :controller end

#recognition_check(g) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/action_controller/routing.rb', line 192

def recognition_check(g)
  g << "controller_result = ::ActionController::Routing::ControllerComponent.traverse_to_controller(#{g.path_name}, #{g.index_name})" 
  g.if('controller_result') do |gp|
    gp << 'controller_value, segments_to_controller = controller_result'
    if condition
      gp << "controller_path = #{gp.path_name}[#{gp.index_name},segments_to_controller].join('/')"
      gp.if(Routing.test_condition("controller_path", condition)) do |gpp|
        gpp.move_forward('segments_to_controller') {|gppp| yield gppp, :constraint}
      end
    else
      gp.move_forward('segments_to_controller') {|gpp| yield gpp, :constraint}
    end
  end
end