Created
March 30, 2010 05:42
-
-
Save jo/348801 to your computer and use it in GitHub Desktop.
Route domains root to resources show
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 Domain < ActiveRecord::Base | |
validates :name, :presence => true, :uniqueness => true | |
validates :routable, :presence => true | |
belongs_to :routable, :polymorphic => true | |
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 Post < ActiveRecord::Base | |
extend RouteByDomain | |
has_one :domain, :as => :routable | |
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
module RouteByDomain | |
def matches?(request) | |
return unless request.get? | |
domain = Domain.find_by_routable_type_and_name(self.to_s, request.host) | |
return unless domain | |
request.params[:id] = domain.routable_id | |
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
Rails3Routing::Application.routes.draw do |map| | |
constraints(Post) do | |
root :to => "events#show" | |
end | |
root :to => "welcome#index" | |
resources :posts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment