Module: Scrivito::RoutingExtensions

Defined in:
app/cms/scrivito/routing_extensions.rb

Instance Method Summary collapse

Instance Method Details

#scrivito_route(path, using:, format: nil, via: :get) ⇒ Object

Note:

To use scrivito_route Configuration.inject_preset_routes needs to be disabled.

scrivito_route defines routes for Scrivito pages. The default routes are:

scrivito_route '(/)(*slug-):id', using: 'slug_id', via: :all
scrivito_route '/', using: 'homepage', via: :all
scrivito_route '/*permalink', using: 'permalink', format: false, via: :all

The first parameter of scrivito_route should look familiar - it is just a regular Rails route pattern. You can make full use of the Rails route patterns when specifying Scrivito routes.

In addition to directing incoming traffic to the code that handles it, Scrivito routes have another distinct functionality: They determine which URL or path is generated for a given CMS object. The using: option needs to be provided to tell Scrivito which route should be used for which type of CMS objects. For example, using: "permalink" instructs Scrivito to use the route for CMS objects to which a _permalink has been assigned. The Scrivito routes expect the route pattern to contain route parameters for dynamically inserting the values for individual CMS objects. If, for example, the permalink of an object is about and the route pattern is my_page/*permalink, Scrivito generates the url http://www.example.com/my_page/about. Valid :using values and their required route parameters are:

slug_id

Route pattern for a normal CMS object. Expected route parameters are slug and id.

homepage

Route pattern for a homepage CMS object. No route parameters needed.

permalink

Route pattern for CMS objects that have a _permalink assigned to them. The required route parameter is permalink.

Examples:

Route that starts with the object ID

scrivito_route '(/):id(/*slug)', using: 'slug_id', via: [:get, :post]

Scoped permalink with disabled format

scrivito_route '/pl/*permalink', using: 'permalink', format: false

Parameters:

  • path (String)

    a Rails route pattern

  • using (String)

    The name of the route. Valid values are slug_id, homepage, and permalink.

  • format (Boolean) (defaults to: nil)

    Use format true or false to enforce or disable the format.

  • via (Symbol) (defaults to: :get)

    Use to specify for which HTTP verbs the route should match. :get is the default. Use :all to allow every verb.



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
# File 'app/cms/scrivito/routing_extensions.rb', line 50

def scrivito_route(path, using:, format: nil, via: :get)
  assert_scrivito_route_enabled
  # @set is a ActionDispatch::Routing::RouteSet
  # see: http://git.io/v4UYF and http://git.io/v4UOI
  route_set = @set

  route_name = using.to_sym

  route = Route.register(route_set, route_name)

  options = {
    to: 'scrivito/cms_dispatch#index',
    via: via,
    format: format,
    as: route.helper_name,
  }

  options[:constraints] = {id: /[a-f0-9]{16}/} if route_name == :slug_id

  begin
    match(path, options)
  rescue ArgumentError => error
    if error.message.include?(route.helper_name)
      raise ScrivitoError,
          %(You have already defined a Scrivito route with the name "#{route_name}".)
    else
      raise error
    end
  end
end