From 2b966b414091e3bd225405ab442f223c66b26249 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Fri, 25 Jul 2008 21:47:05 +0000 Subject: [PATCH] ActionItem519: routing hosted domain paths into content_viewer controller --- config/environment.rb | 2 ++ config/routes.rb | 9 +++++++-- lib/needs_profile.rb | 2 +- lib/route_if.rb | 18 ++++++++++++++++++ test/functional/content_viewer_controller_test.rb | 12 ++++++++++++ test/integration/routing_test.rb | 10 ++++++++++ 6 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 lib/route_if.rb diff --git a/config/environment.rb b/config/environment.rb index ba8a4e4..82e0bea 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -104,6 +104,8 @@ require 'acts_as_having_image' require 'sqlite_extension' require 'will_paginate' +require 'route_if' + # load a local configuration if present, but not under test environment. if ENV['RAILS_ENV'] != 'test' localconfigfile = File.join(RAILS_ROOT, 'config', 'local.rb') diff --git a/config/routes.rb b/config/routes.rb index ce7b7c7..dfb391c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,9 +15,14 @@ ActionController::Routing::Routes.draw do |map| ## Public controllers ###################################################### - map.connect 'test/:controller/:action/:id' , :controller => /.*test.*/ + # You can have the root of your site routed by hooking up '' + hosted_domain_matcher = lambda do |env| + Domain.find_by_name(env[:host]) + end + map.connect '*page', :controller => 'content_viewer', :action => 'view_page', :conditions => { :if => hosted_domain_matcher } + + map.connect 'test/:controller/:action/:id' , :controller => /.*test.*/ - # You can have the root of your site routed by hooking up '' # -- just remember to delete public/index.html. map.connect '', :controller => "home" diff --git a/lib/needs_profile.rb b/lib/needs_profile.rb index 7db2348..31d6588 100644 --- a/lib/needs_profile.rb +++ b/lib/needs_profile.rb @@ -21,7 +21,7 @@ module NeedsProfile end def load_profile - @profile = Profile.find_by_identifier(params[:profile]) + @profile ||= Profile.find_by_identifier(params[:profile]) render_not_found unless @profile end diff --git a/lib/route_if.rb b/lib/route_if.rb new file mode 100644 index 0000000..4bd637a --- /dev/null +++ b/lib/route_if.rb @@ -0,0 +1,18 @@ +require 'action_controller/routing' + +class ActionController::Routing::RouteSet + alias :orig_extract_request_environment :extract_request_environment + def extract_request_environment(request) + orig_extract_request_environment(request).merge(:host => request.host) + end +end + +class ActionController::Routing::Route + alias :orig_recognition_conditions :recognition_conditions + def recognition_conditions + result = orig_recognition_conditions + result << "conditions[:if].call(env)" if conditions[:if] + result + end +end + diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index a8d12b9..51ad62f 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -286,4 +286,16 @@ class ContentViewerControllerTest < Test::Unit::TestCase assert_no_tag :tag => 'div', :attributes => { :id => 'profile-disabled' }, :content => Environment.default.message_for_disabled_enterprise end + should 'load the correct profile when using hosted domain' do + profile = create_user('mytestuser').person + profile.domains << Domain.create!(:name => 'micojones.net') + profile.save! + + ActionController::TestRequest.any_instance.expects(:host).returns('www.micojones.net').at_least_once + + get :view_page, :page => [] + + assert_equal profile, assigns(:profile) + end + end diff --git a/test/integration/routing_test.rb b/test/integration/routing_test.rb index 803da3d..d8435e5 100644 --- a/test/integration/routing_test.rb +++ b/test/integration/routing_test.rb @@ -156,4 +156,14 @@ class RoutingTest < ActionController::IntegrationTest assert_routing('/catalog/profile.withdot/1234', :controller => 'catalog', :action => 'show', :profile => 'profile.withdot', :id => '1234') end + def test_hosted_domain_routing + + user = create_user('testuser').person + domain = Domain.create!(:name => 'example.com', :owner => user) + + ActionController::TestRequest.any_instance.expects(:host).returns('www.example.com') + + assert_routing('/work/free-software', :controller => 'content_viewer', :action => 'view_page', :page => [ 'work', 'free-software'] ) + end + end -- libgit2 0.21.2