loading sites based on site path

Oleg committed Jun 23, 2011
commit dd94cf80e37457b12495c0ed8148078ad1621034
Showing 2 changed files with 46 additions and 2 deletions
app/controllers/cms_content_controller.rb +15 -2
@@ @@ -25,8 +25,21 @@ class CmsContentController < ApplicationController
protected
def load_cms_site
- @cms_site = Cms::Site.find_by_hostname(request.host.downcase)
- render :text => 'Site Not Found', :status => 404 if !@cms_site
+ @cms_site ||= Cms::Site.first if Cms::Site.count == 1
+ Cms::Site.find_all_by_hostname(request.host.downcase).each do |site|
+ if site.path.blank?
+ @cms_site = site
+ elsif "#{request.fullpath}/".match /^\/#{Regexp.escape(site.path.to_s)}\//
+ @cms_site = site
+ break
+ end
+ end unless @cms_site
+
+ if @cms_site
+ params[:cms_path].to_s.gsub!(/^#{@cms_site.path}/, '').gsub!(/^\//, '')
+ else
+ render :text => 'Site Not Found', :status => 404
+ end
end
def load_fixtures
test/integration/sites_test.rb +31 -0
@@ @@ -26,4 +26,35 @@ class SitesTest < ActionDispatch::IntegrationTest
assert_equal 'test.host', assigns(:cms_site).hostname
end
+ def test_get_public_page_with_sites_with_different_paths
+ Cms::Site.delete_all
+ site_a = Cms::Site.create!(:label => 'Site A', :hostname => 'test.host', :path => '')
+ site_b = Cms::Site.create!(:label => 'Site B', :hostname => 'test.host', :path => 'path-b')
+ site_c = Cms::Site.create!(:label => 'Site C', :hostname => 'test.host', :path => 'path-c/child')
+
+ %w(/ /path-a /path-a/child /path-c).each do |path|
+ get path
+ assert_response 404
+ assert assigns(:cms_site), path
+ assert_equal site_a, assigns(:cms_site)
+ assert_equal path.gsub(/^\//, ''), @controller.params[:cms_path].to_s
+ end
+
+ %w(/path-b /path-b/child).each do |path|
+ get path
+ assert_response 404
+ assert assigns(:cms_site), path
+ assert_equal site_b, assigns(:cms_site)
+ assert_equal path.gsub(/^\/path-b/, '').gsub(/^\//, ''), @controller.params[:cms_path].to_s
+ end
+
+ %w(/path-c/child /path-c/child/child).each do |path|
+ get path
+ assert_response 404
+ assert assigns(:cms_site), path
+ assert_equal site_c, assigns(:cms_site)
+ assert_equal path.gsub(/^\/path-c\/child/, '').gsub(/^\//, ''), @controller.params[:cms_path].to_s
+ end
+ end
+
end
\ No newline at end of file