Skip to content

Instantly share code, notes, and snippets.

@lukesutton
Created January 26, 2009 23:58
Show Gist options
  • Select an option

  • Save lukesutton/53060 to your computer and use it in GitHub Desktop.

Select an option

Save lukesutton/53060 to your computer and use it in GitHub Desktop.
PUBLIC_DEFER_TO_PROC = lambda do |request, params|
locale = Config::Locale.find(params)
# Find the page with the closest matching path. If the matching path is
# shorter than the one specified, it will still match, since it may actually
# be a redirect.
page = Page.first_with_localization(
:locale_name => locale[:name]
:conditions => ["? LIKE (path || '%')", params[:full_path]],
:order => [:path.asc]
)
# If no page matched, 404. If we have a page, but it isn't a redirect and the
# path isn't an exact match, we also 404.
if !page || (page.path != params[:full_path] && page.description[:behaviour] != :rewrite)
raise Merb::ControllerExceptions::NotFound
end
# Check to see how we need to hand the request based on the behaviour
# specified.
case page.description[:behaviour]
when :default
# Just send it to the default controller specified in the routes.
{
:controller => params[:controller],
:action => params[:action],
:page => page,
:format => params[:format],
:locale => locale
}
when :rewrite
# Overwrite the REQUEST_PATH with the real controller path and pass it
# through the router again to get the params — controller, action etc —
# into which we can inject our own entries.
rewrite_path = Merb::Router.url(page.description.rewrite_route)
request.env["REQUEST_PATH"] = params[:full_path].gsub(page.current_localization.path, rewrite_path)
new_params = Merb::Router.match(request)[1]
new_params.merge(:page => page, :locale => locale)
when :redirect
# TODO: find out how to get redirects working inside a block. Seems to be
# a scoping issue, since redirect returns a MethodMissing
redirect(page.description.redirect_url(page, params))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment