Commit 2b966b414091e3bd225405ab442f223c66b26249

Authored by AntonioTerceiro
1 parent 2623f171

ActionItem519: routing hosted domain paths into content_viewer

controller

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2319 3f533792-8f58-4932-b0fe-aaf55b0a4547
config/environment.rb
@@ -104,6 +104,8 @@ require 'acts_as_having_image' @@ -104,6 +104,8 @@ require 'acts_as_having_image'
104 require 'sqlite_extension' 104 require 'sqlite_extension'
105 require 'will_paginate' 105 require 'will_paginate'
106 106
  107 +require 'route_if'
  108 +
107 # load a local configuration if present, but not under test environment. 109 # load a local configuration if present, but not under test environment.
108 if ENV['RAILS_ENV'] != 'test' 110 if ENV['RAILS_ENV'] != 'test'
109 localconfigfile = File.join(RAILS_ROOT, 'config', 'local.rb') 111 localconfigfile = File.join(RAILS_ROOT, 'config', 'local.rb')
config/routes.rb
@@ -15,9 +15,14 @@ ActionController::Routing::Routes.draw do |map| @@ -15,9 +15,14 @@ ActionController::Routing::Routes.draw do |map|
15 ## Public controllers 15 ## Public controllers
16 ###################################################### 16 ######################################################
17 17
18 - map.connect 'test/:controller/:action/:id' , :controller => /.*test.*/ 18 + # You can have the root of your site routed by hooking up ''
  19 + hosted_domain_matcher = lambda do |env|
  20 + Domain.find_by_name(env[:host])
  21 + end
  22 + map.connect '*page', :controller => 'content_viewer', :action => 'view_page', :conditions => { :if => hosted_domain_matcher }
  23 +
  24 + map.connect 'test/:controller/:action/:id' , :controller => /.*test.*/
19 25
20 - # You can have the root of your site routed by hooking up ''  
21 # -- just remember to delete public/index.html. 26 # -- just remember to delete public/index.html.
22 map.connect '', :controller => "home" 27 map.connect '', :controller => "home"
23 28
lib/needs_profile.rb
@@ -21,7 +21,7 @@ module NeedsProfile @@ -21,7 +21,7 @@ module NeedsProfile
21 end 21 end
22 22
23 def load_profile 23 def load_profile
24 - @profile = Profile.find_by_identifier(params[:profile]) 24 + @profile ||= Profile.find_by_identifier(params[:profile])
25 render_not_found unless @profile 25 render_not_found unless @profile
26 end 26 end
27 27
lib/route_if.rb 0 → 100644
@@ -0,0 +1,18 @@ @@ -0,0 +1,18 @@
  1 +require 'action_controller/routing'
  2 +
  3 +class ActionController::Routing::RouteSet
  4 + alias :orig_extract_request_environment :extract_request_environment
  5 + def extract_request_environment(request)
  6 + orig_extract_request_environment(request).merge(:host => request.host)
  7 + end
  8 +end
  9 +
  10 +class ActionController::Routing::Route
  11 + alias :orig_recognition_conditions :recognition_conditions
  12 + def recognition_conditions
  13 + result = orig_recognition_conditions
  14 + result << "conditions[:if].call(env)" if conditions[:if]
  15 + result
  16 + end
  17 +end
  18 +
test/functional/content_viewer_controller_test.rb
@@ -286,4 +286,16 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase @@ -286,4 +286,16 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
286 assert_no_tag :tag => 'div', :attributes => { :id => 'profile-disabled' }, :content => Environment.default.message_for_disabled_enterprise 286 assert_no_tag :tag => 'div', :attributes => { :id => 'profile-disabled' }, :content => Environment.default.message_for_disabled_enterprise
287 end 287 end
288 288
  289 + should 'load the correct profile when using hosted domain' do
  290 + profile = create_user('mytestuser').person
  291 + profile.domains << Domain.create!(:name => 'micojones.net')
  292 + profile.save!
  293 +
  294 + ActionController::TestRequest.any_instance.expects(:host).returns('www.micojones.net').at_least_once
  295 +
  296 + get :view_page, :page => []
  297 +
  298 + assert_equal profile, assigns(:profile)
  299 + end
  300 +
289 end 301 end
test/integration/routing_test.rb
@@ -156,4 +156,14 @@ class RoutingTest &lt; ActionController::IntegrationTest @@ -156,4 +156,14 @@ class RoutingTest &lt; ActionController::IntegrationTest
156 assert_routing('/catalog/profile.withdot/1234', :controller => 'catalog', :action => 'show', :profile => 'profile.withdot', :id => '1234') 156 assert_routing('/catalog/profile.withdot/1234', :controller => 'catalog', :action => 'show', :profile => 'profile.withdot', :id => '1234')
157 end 157 end
158 158
  159 + def test_hosted_domain_routing
  160 +
  161 + user = create_user('testuser').person
  162 + domain = Domain.create!(:name => 'example.com', :owner => user)
  163 +
  164 + ActionController::TestRequest.any_instance.expects(:host).returns('www.example.com')
  165 +
  166 + assert_routing('/work/free-software', :controller => 'content_viewer', :action => 'view_page', :page => [ 'work', 'free-software'] )
  167 + end
  168 +
159 end 169 end