Module: Roda::RodaPlugins::BridgetownRoutes::RequestMethods

Defined in:
bridgetown-routes/lib/roda/plugins/bridgetown_routes.rb

Instance Method Summary collapse

Instance Method Details

#file_routesObject

This runs through all of the routes in the manifest, setting up Roda blocks for execution



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'bridgetown-routes/lib/roda/plugins/bridgetown_routes.rb', line 116

def file_routes
  base_path = Bridgetown::Current.preloaded_configuration.base_path.delete_prefix("/")

  scope.routes_manifest.routes.each do |route|
    file, localized_file_slugs, segment_keys = route

    localized_file_slugs.each do |slug|
      on("") { scope.run_file_route(file, slug:) } if slug == "index" && !base_path.empty?

      # This sets up an initial Roda route block at the slug, and handles segments as params
      #
      # _routes/nested/[slug].erb -> "nested/:slug"
      # "nested/123" -> r.params[:slug] == 123
      on slug do |*segment_values|
        segment_values.each_with_index do |value, index|
          params[segment_keys[index]] ||= value
        end

        # This is provided as an instance method by our Roda plugin:
        scope.run_file_route(file, slug:)
      end
    end
  end

  nil # be sure not to return the above array loop
end