better site loading method

Oleg committed Sep 01, 2011
commit 2e77f877e1eb65dc2db864f6bd10cde5ded110de
Showing 4 changed files with 46 additions and 12 deletions
app/controllers/cms_content_controller.rb +3 -11
@@ @@ -35,18 +35,10 @@ protected
end
def load_cms_site
- if params[:site_id]
- @cms_site ||= Cms::Site.find_by_id(params[:site_id])
+ @cms_site ||= if params[:site_id]
+ Cms::Site.find_by_id(params[:site_id])
else
- @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
+ Cms::Site.find_site(request.host.downcase, request.fullpath)
end
if @cms_site
app/models/cms/site.rb +16 -0
@@ @@ -25,6 +25,22 @@ class Cms::Site < ActiveRecord::Base
# -- Scopes ---------------------------------------------------------------
scope :mirrored, where(:is_mirrored => true)
+ # -- Class Methods --------------------------------------------------------
+ # returning the Cms::Site instance based on host and path
+ def self.find_site(host, path = nil)
+ return Cms::Site.first if Cms::Site.count == 1
+ cms_site = nil
+ Cms::Site.find_all_by_hostname(host).each do |site|
+ if site.path.blank?
+ cms_site = site
+ elsif "#{path}/".match /^\/#{Regexp.escape(site.path.to_s)}\//
+ cms_site = site
+ break
+ end
+ end
+ return cms_site
+ end
+
protected
def assign_label
comfortable_mexican_sofa/view_methods.rb b/lib/comfortable_mexican_sofa/view_methods.rb +1 -1
@@ @@ -23,7 +23,7 @@ module ComfortableMexicanSofa::ViewMethods
# Content of a snippet. Example:
# cms_snippet_content(:my_snippet)
def cms_snippet_content(snippet_slug, cms_site = nil)
- return '' unless cms_site ||= (@cms_site || Cms::Site.find_by_hostname(request.host.downcase))
+ return '' unless cms_site ||= (@cms_site || Cms::Site.find_site(request.host.downcase, request.fullpath))
return '' unless snippet = cms_site.snippets.find_by_slug(snippet_slug)
render :inline => ComfortableMexicanSofa::Tag.process_content(Cms::Page.new, snippet.content)
end
test/unit/models/site_test.rb +26 -0
@@ @@ -80,4 +80,30 @@ class CmsSiteTest < ActiveSupport::TestCase
assert_equal 1, Cms::Site.mirrored.count
end
+ def test_find_site
+ site_a = cms_sites(:default)
+ assert_equal 'test.host', site_a.hostname
+ assert_equal nil, site_a.path
+
+ assert_equal site_a, Cms::Site.find_site('test.host')
+ assert_equal site_a, Cms::Site.find_site('test.host', '/some/path')
+ assert_equal site_a, Cms::Site.find_site('test99.host', '/some/path')
+
+ site_b = Cms::Site.create!(:hostname => 'test2.host', :path => 'en')
+ site_c = Cms::Site.create!(:hostname => 'test2.host', :path => 'fr')
+
+ assert_equal site_a, Cms::Site.find_site('test.host')
+ assert_equal site_a, Cms::Site.find_site('test.host', '/some/path')
+ assert_equal nil, Cms::Site.find_site('test99.host', '/some/path')
+
+ assert_equal nil, Cms::Site.find_site('test2.host')
+ assert_equal nil, Cms::Site.find_site('test2.host', '/some/path')
+ assert_equal site_b, Cms::Site.find_site('test2.host', '/en')
+ assert_equal site_b, Cms::Site.find_site('test2.host', '/en/some/path')
+ assert_equal nil, Cms::Site.find_site('test2.host', '/english/some/path')
+
+ assert_equal site_c, Cms::Site.find_site('test2.host', '/fr')
+ assert_equal site_c, Cms::Site.find_site('test2.host', '/fr/some/path')
+ end
+
end
\ No newline at end of file