adjusting how site management is handled

Oleg committed Jan 14, 2011
commit f1e8288bfba282f79397718093e55ee89171b87f
Showing 6 changed files with 82 additions and 18 deletions
app/controllers/cms_admin/base_controller.rb +16 -4
@@ @@ -11,12 +11,24 @@ class CmsAdmin::BaseController < ActionController::Base
layout 'cms_admin'
protected
-
+
def load_admin_cms_site
@cms_site = CmsSite.find_by_hostname!(request.host.downcase)
+
rescue ActiveRecord::RecordNotFound
- flash[:error] = 'No Site defined for this hostname. Create it now.'
- redirect_to new_cms_admin_site_path
+
+ if ComfortableMexicanSofa.config.auto_manage_sites
+ if CmsSite.count == 0
+ @cms_site = CmsSite.create!(:label => 'Default Site', :hostname => request.host.downcase)
+ elsif CmsSite.count == 1
+ @cms_site = CmsSite.first
+ @cms_site.update_attribute(:hostname, request.host.downcase)
+ end
+ end
+
+ unless @cms_site
+ flash[:error] = 'No Site defined for this hostname. Create it now.'
+ return redirect_to(cms_admin_sites_path)
+ end
end
-
end
app/views/layouts/cms_admin.html.erb +3 -1
@@ @@ -13,7 +13,9 @@
<div class='body_wrapper'>
<div class='left_column'>
<div class='left_column_content'>
- <%= active_link_to 'Sites', cms_admin_sites_path %>
+ <% unless ComfortableMexicanSofa.config.auto_manage_sites %>
+ <%= active_link_to 'Sites', cms_admin_sites_path %>
+ <% end %>
<%= active_link_to 'Layouts', cms_admin_layouts_path %>
<%= active_link_to 'Pages', cms_admin_pages_path %>
<%= active_link_to 'Snippets', cms_admin_snippets_path %>
config/initializers/comfortable_mexican_sofa.rb +4 -1
@@ @@ -11,12 +11,15 @@ ComfortableMexicanSofa.configure do |config|
# config.admin_route_prefix = 'cms-admin'
# Path: /cms-admin redirects to /cms-admin/pages but you can change it
- # to something else like:
+ # You don't need to change it when changing admin_route_prefix
# config.admin_route_redirect = '/cms-admin/pages'
# Location of YAML files that can be used to render pages instead of pulling
# data from the database. Not active if not specified.
# config.seed_data_path = File.expand_path('db/cms_seeds', Rails.root)
+
+ # Let CMS handle site creation and management. Enabled by default.
+ # config.auto_manage_sites = true
end
# Default credentials for ComfortableMexicanSofa::HttpAuth
comfortable_mexican_sofa/configuration.rb b/lib/comfortable_mexican_sofa/configuration.rb +4 -0
@@ @@ -18,6 +18,9 @@ class ComfortableMexicanSofa::Configuration
# to something else
attr_accessor :admin_route_redirect
+ # Let CMS handle site creation and management. Enabled by default.
+ attr_accessor :auto_manage_sites
+
# Configuration defaults
def initialize
@cms_title = 'ComfortableMexicanSofa'
@@ @@ -25,6 +28,7 @@ class ComfortableMexicanSofa::Configuration
@seed_data_path = nil
@admin_route_prefix = 'cms-admin'
@admin_route_redirect = "/#{@admin_route_prefix}/pages"
+ @auto_manage_sites = true
end
end
\ No newline at end of file
test/integration/sites_test.rb +42 -5
@@ @@ -2,16 +2,53 @@ require File.expand_path('../test_helper', File.dirname(__FILE__))
class SitesTest < ActionDispatch::IntegrationTest
- def test_get_admin_pages_index
+ def test_get_admin
http_auth :get, cms_admin_pages_path
assert_response :success
end
- def test_get_admin_pages_index_with_no_site
+ def test_get_admin_with_no_site
CmsSite.delete_all
- http_auth :get, cms_admin_pages_path
- assert_response :redirect
- assert_redirected_to new_cms_admin_site_path
+ assert_difference 'CmsSite.count' do
+ http_auth :get, cms_admin_pages_path
+ assert_response :redirect
+ assert_redirected_to new_cms_admin_page_path
+ site = CmsSite.first
+ assert_equal 'test.host', site.hostname
+ assert_equal 'Default Site', site.label
+ end
+ end
+
+ def test_get_admin_with_wrong_site
+ site = cms_sites(:default)
+ site.update_attribute(:hostname, 'remote.host')
+ assert_no_difference 'CmsSite.count' do
+ http_auth :get, cms_admin_pages_path
+ assert_response :success
+ site.reload
+ assert_equal 'test.host', site.hostname
+ end
+ end
+
+ def test_get_admin_with_two_wrong_sites
+ CmsSite.delete_all
+ CmsSite.create!(:label => 'Site1', :hostname => 'site1.host')
+ CmsSite.create!(:label => 'Site2', :hostname => 'site2.host')
+ assert_no_difference 'CmsSite.count' do
+ http_auth :get, cms_admin_pages_path
+ assert_response :redirect
+ assert_redirected_to cms_admin_sites_path
+ end
+ end
+
+ def test_get_admin_with_no_site_and_no_auto_manage
+ ComfortableMexicanSofa.config.auto_manage_sites = false
+ CmsSite.delete_all
+ assert_no_difference 'CmsSite.count' do
+ http_auth :get, cms_admin_pages_path
+ assert_response :redirect
+ assert_redirected_to cms_admin_sites_path
+ end
end
def test_get_public_page_for_non_existent_site
test/test_helper.rb +13 -7
@@ @@ -3,15 +3,23 @@ require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
+
fixtures :all
include ActionDispatch::TestProcess
def setup
- # resetting default configuration
+ reset_config
+ end
+
+ # resetting default configuration
+ def reset_config
ComfortableMexicanSofa.configure do |config|
- config.cms_title = 'ComfortableMexicanSofa'
- config.authentication = 'ComfortableMexicanSofa::HttpAuth'
- config.seed_data_path = nil
+ config.cms_title = 'ComfortableMexicanSofa'
+ config.authentication = 'ComfortableMexicanSofa::HttpAuth'
+ config.seed_data_path = nil
+ config.admin_route_prefix = 'cms-admin'
+ config.admin_route_redirect = "/cms-admin/pages"
+ config.auto_manage_sites = true
end
ComfortableMexicanSofa::HttpAuth.username = 'username'
ComfortableMexicanSofa::HttpAuth.password = 'password'
@@ @@ -69,9 +77,7 @@ class ActionDispatch::IntegrationTest
def setup
host! 'test.host'
- ComfortableMexicanSofa.config.seed_data_path = nil
- ComfortableMexicanSofa::HttpAuth.username = 'username'
- ComfortableMexicanSofa::HttpAuth.password = 'password'
+ reset_config
end
# Attaching http_auth stuff with request. Example use: