Class: Trellis::DefaultRouter

Inherits:
Router show all
Defined in:
lib/trellis/trellis.rb

Overview

– DefaultRouter – The default routing scheme is in the form /page[.event][/value]

Constant Summary collapse

ROUTE_REGEX =
%r{^/([^/]+)(?:/(?:events/(?:([^/\.]+)(?:\.([^/\.]+)?)?)(?:/(?:([^\.]+)?))?)?)?}

Constants inherited from Router

Router::EVENT_REGEX

Instance Attribute Summary

Attributes inherited from Router

#application, #keys, #page, #path, #pattern, #score

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Router

#initialize, #inject_parameters_into_page_instance, #to_s

Constructor Details

This class inherits a constructor from Trellis::Router

Class Method Details

.to_uri(options = {}) ⇒ Object



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/trellis/trellis.rb', line 458

def self.to_uri(options={})
  # get options
  url_root = options[:url_root]
  page = options[:page]
  event = options[:event]
  source = options[:source]
  value = options[:value]
  
  destination = page
  url_root = "/"
  
  if page.kind_of?(Trellis::Page)
    destination = page.path || page.class.class_to_sym
    root = page.class.url_root 
    url_root = (root && !root.empty?) ? "/#{root}" : '/'
  end

  source = source ? ".#{source}" : ''
  value = value ? "/#{value}" : ''
  event_info = event ? "/events/#{event}#{source}#{value}" : ''

  "#{url_root}#{destination}#{event_info}"
end

Instance Method Details

#matches?(request) ⇒ Boolean

Returns:

  • (Boolean)


454
455
456
# File 'lib/trellis/trellis.rb', line 454

def matches?(request)
  request.path_info.match(ROUTE_REGEX) != nil
end

#route(request) ⇒ Object



446
447
448
449
450
451
452
# File 'lib/trellis/trellis.rb', line 446

def route(request)
  value, source, event, destination = request.path_info.match(ROUTE_REGEX).to_a.reverse
  destination = @application.class.homepage unless destination
  page = Page.get_page(destination.to_sym)

  Route.new(page, event, source, value)
end