Skip to content

Commit

Permalink
adding support for hosting aintablog under a relative url.
Browse files Browse the repository at this point in the history
  • Loading branch information
flavorjones committed Oct 29, 2008
1 parent 646e073 commit 94c9447
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/controllers/application.rb
Expand Up @@ -39,7 +39,7 @@ def post_repo

def not_found
cookies[:error] = "Sorry but that post could not be found."
redirect_to '/' and return
redirect_to root_path and return
end

end
4 changes: 2 additions & 2 deletions app/controllers/posts_controller.rb
Expand Up @@ -21,7 +21,7 @@ def index
# GET /posts/1.xml
def show
@post = Post.find_by_permalink(params[:id], :include => :comments) || Post.find(params[:id])
redirect_to '/' and return unless @post.type.match(/Article|Snippet/)
redirect_to root_path and return unless @post.type.match(/Article|Snippet/)
@comment = flash[:comment] || @post.comments.build
respond_to do |format|
format.html # show.html.erb
Expand All @@ -31,6 +31,6 @@ def show

private
def redirect_to_admin
redirect_to "/admin" + request.path
redirect_to admin_root_path + request.path
end
end
5 changes: 3 additions & 2 deletions app/controllers/sessions_controller.rb
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# This controller handles the login/logout function of the site.
class SessionsController < ApplicationController

Expand All @@ -20,7 +21,7 @@ def create
self.current_user.remember_me
cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
end
redirect_back_or_default('/admin')
redirect_back_or_default(admin_root_path)
cookies[:notice] = "You’ve logged in successfully."
else
render :action => 'new'
Expand All @@ -32,6 +33,6 @@ def destroy
cookies.delete :auth_token
reset_session
cookies[:notice] = "You have been logged out."
redirect_back_or_default('/')
redirect_back_or_default(root_path)
end
end
4 changes: 2 additions & 2 deletions app/controllers/users_controller.rb
@@ -1,7 +1,7 @@
class UsersController < ApplicationController
# render new.rhtml
def new
redirect_to '/' if (User.count >= 1) && !logged_in?
redirect_to root_path if (User.count >= 1) && !logged_in?
@user = User.new
end

Expand All @@ -15,7 +15,7 @@ def create
@user.save
if @user.errors.empty?
self.current_user = @user
redirect_back_or_default('/')
redirect_back_or_default(root_path)
cookies[:notice] = "Thanks for signing up! Choose one of the links at the top to do something interesting."
else
render :action => 'new'
Expand Down
37 changes: 36 additions & 1 deletion test/functional/posts_controller_test.rb
Expand Up @@ -20,12 +20,28 @@ def test_should_redirect_to_admin_index_if_logged_in
get :index
assert_redirected_to admin_posts_path
end

def test_should_redirect_to_admin_index_if_logged_in_even_for_relative_urls
set_relative_url do
login_as :quentin
get :index
assert_redirected_to admin_posts_path
end
end

def test_should_redirect_to_admin_show_if_logged_in
login_as :quentin
get :show, :id => posts(:one).permalink
assert_redirected_to admin_post_path(posts(:one))
end

def test_should_redirect_to_admin_show_if_logged_in_even_for_relative_urls
set_relative_url do
login_as :quentin
get :show, :id => posts(:one).permalink
assert_redirected_to admin_post_path(posts(:one))
end
end

# Post types

Expand Down Expand Up @@ -90,9 +106,28 @@ def test_should_show_post

def test_should_redirect_to_index_from_show_unless_article
get :show, :id => posts(:two).id
assert_redirected_to '/'
assert_redirected_to root_path
end

def test_should_redirect_to_index_from_show_unless_article_even_for_relative_urls
set_relative_url do
get :show, :id => posts(:two).id
assert_redirected_to root_path
end
end

def test_should_redirect_to_root_when_post_not_found
get :show, :id => 999999
assert_redirected_to root_path
end

def test_should_redirect_to_root_when_post_not_found_even_for_relative_urls
set_relative_url do
get :show, :id => 999999
assert_redirected_to root_path
end
end

private

def posts_stub
Expand Down
21 changes: 20 additions & 1 deletion test/functional/sessions_controller_test.rb
Expand Up @@ -18,7 +18,15 @@ def setup
def test_should_login_and_redirect
post :create, :email => 'quentin@example.com', :password => 'test'
assert session[:user_id]
assert_redirected_to '/admin'
assert_redirected_to admin_root_path
end

def test_should_login_and_redirect_even_for_relative_urls
set_relative_url do
post :create, :email => 'quentin@example.com', :password => 'test'
assert session[:user_id]
assert_redirected_to admin_root_path
end
end

def test_should_fail_login_and_not_redirect
Expand All @@ -32,6 +40,17 @@ def test_should_logout
get :destroy
assert_nil session[:user_id]
assert_response :redirect
assert_redirected_to root_path
end

def test_should_logout_when_relative_url
set_relative_url do
login_as :quentin
get :destroy
assert_nil session[:user_id]
assert_response :redirect
assert_redirected_to root_path
end
end

def test_should_remember_me
Expand Down
20 changes: 19 additions & 1 deletion test/functional/users_controller_test.rb
Expand Up @@ -21,13 +21,31 @@ def test_should_get_new

def test_should_not_get_new_if_users_exist
get :new
assert_redirected_to '/'
assert_redirected_to root_path
end

def test_should_not_get_new_if_users_exist_even_for_relative_url
set_relative_url do
get :new
assert_redirected_to root_path
end
end

def test_should_allow_signup
assert_difference 'User.count' do
create_user
assert_response :redirect
assert_redirected_to root_path
end
end

def test_should_allow_signup_when_relative_url
set_relative_url do
assert_difference 'User.count' do
create_user
assert_response :redirect
assert_redirected_to root_path
end
end
end

Expand Down
26 changes: 26 additions & 0 deletions test/test_helper.rb
Expand Up @@ -43,4 +43,30 @@ class Test::Unit::TestCase

# Add more helper methods to be used by all tests here...
include AuthenticatedTestHelper

#
# for testing when aintablog is hosted under a relative url,
# e.g., "http://my.domain.com/aintablog/"
#
def set_relative_url &block
old = nil
new = "/relative"
if ActionController::Base.respond_to?('relative_url_root=')
old = ActionController::Base.relative_url_root
ActionController::Base.relative_url_root = new
else
old = ActionController::AbstractRequest.relative_url_root
ActionController::AbstractRequest.relative_url_root = new
end
begin
block.call
ensure
if ActionController::Base.respond_to?('relative_url_root=')
ActionController::Base.relative_url_root = old
else
ActionController::AbstractRequest.relative_url_root = old
end
end
end

end

0 comments on commit 94c9447

Please sign in to comment.