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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Router

#initialize, #inject_parameters_into_page_instance

Constructor Details

This class inherits a constructor from Trellis::Router

Class Method Details

.to_uri(options = {}) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/trellis/trellis.rb', line 295

def self.to_uri(options={})
  url_root = options[:url_root]
  page = options[:page]
  event = options[:event]
  source = options[:source]
  value = options[:value]
  destination = page.kind_of?(Trellis::Page) ? (page.path || page.class.class_to_sym) : page
  url_root = page.kind_of?(Trellis::Page) && page.class.url_root ? "/#{page.class.url_root}" : '/' unless url_root
  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)


291
292
293
# File 'lib/trellis/trellis.rb', line 291

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

#route(request) ⇒ Object



283
284
285
286
287
288
289
# File 'lib/trellis/trellis.rb', line 283

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