module Scrivito # @api public module RoutingExtensions # @api public # #+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+. # # @example Route that starts with the object ID # scrivito_route '(/):id(/*slug)', using: 'slug_id', via: [:get, :post] # # @example Scoped permalink with disabled format # scrivito_route '/pl/*permalink', using: 'permalink', format: false # # @param path [String] a Rails route pattern # @param using [String] The name of the route. Valid values are +slug_id+, +homepage+, and # +permalink+. # @param format [Boolean] Use +format+ +true+ or +false+ to enforce or disable the format. # @param via [Symbol] Use to specify for which HTTP verbs the route should match. +:get+ is # the default. Use +:all+ to allow every verb. # # @note To use +scrivito_route+ {Scrivito::Configuration.inject_preset_routes} needs to be # disabled. 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-z0-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 private def assert_scrivito_route_enabled unless Scrivito::Configuration.scrivito_route_enabled? raise ScrivitoError, 'The preset routes are still enabled. Please disable them by ' \ 'setting the configuration "inject_preset_routes" to false before using scrivito_route' end end end end