Skip to content

Instantly share code, notes, and snippets.

@lukesutton
Created November 17, 2008 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukesutton/25732 to your computer and use it in GitHub Desktop.
Save lukesutton/25732 to your computer and use it in GitHub Desktop.
# An example of how to find a component page. That matches a particular path.
# Used after the initial call using the full path fails.
Gluttonberg::PageLocalization.first(
:dialect_id => 1,
:locale_id => 1,
:behaviour => :component,
:conditions => ["? LIKE (path || '%')", "about_us/action/id"],
:order => [:path.asc]
)
match(path << "(/:full_path)", :full_path => /\S+/).defer_to do |request, params|
locale = Gluttonberg::Locale.first(:slug => params[:locale])
dialect = Gluttonberg::Dialect.first(:code => params[:dialect])
localization = Gluttonberg::PageLocalization.first(
:path.like => params[:full_path] + "%",
:locale_id => locale.id,
:dialect_id => dialect.id
)
if localization
page = localization.page
page.current_localization = localization
else
raise Merb::Exceptions::NotFound
end
# Additional stuff that'll be handy on the other end
additional_params = {:locale => locale, :dialect => dialect, :original_path => params[:full_path], :page => page}
# Check to see if this is a page to dispatch to directly, or if we
# need to reroute it.
if page.behaviour == :component
# Rewrite and reroute the request
new_path = params[:full_path].gsub(%r{\w+/}, page.component)
request.env["REQUEST_PATH"] = new_path
# Then mix in our own bits and pass it back to the dispatcher
results = Merb::Router.match(request)[1]
results.merge!(additional_params)
elsif localization.path == params[:full_path]
{:controller => "gluttonberg/content/public", :action => "show"}.merge!(additional_params)
else
params
end
end
* Raise a 404 if the dialect, locale, localization or page is missing
* Check the modes correctly — including falling back to a default
* Account for requests to the root
* Move a bunch of this logic out into it's own module
* Investigate a way to find the localization without repeated finds
* Finesse the path rewriting a LOT more, since this won't work properly
* Consider having forcing public controllers to be namespaced to prevent collisions with the page slugs
* Remember to note that this call has to go at the bottom of the router, or shit will blow up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment