Created
March 6, 2013 21:43
-
-
Save listrophy/5103363 to your computer and use it in GitHub Desktop.
GitHub-style URL matching, with only one query. Is this belt-and-suspenders if ActiveRecord caches the query?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProjectLookupConstraint | |
def matches? request | |
Project.where(request.params.slice(:account_name, :project_name)).first.tap do |project| | |
request.flash.now['project'] = project | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProjectsController < ApplicationController | |
expose(:project) { flash.now['project'].decorate } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'project_lookup_constraint' | |
MyApp::Application.routes.draw do | |
get ':account_name/:project_name' => 'projects#show', constraints: ProjectLookupConstraint.new | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment