redirect_routing 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +21 -0
- data/README +64 -0
- data/Rakefile +42 -0
- data/VERSION.yml +4 -0
- data/init.rb +3 -0
- data/lib/redirect_routing.rb +2 -0
- data/lib/redirect_routing/routes.rb +7 -0
- data/lib/redirect_routing_controller.rb +25 -0
- data/redirect_routing.gemspec +51 -0
- data/test/redirect_routing_test.rb +91 -0
- data/test/test_helper.rb +27 -0
- metadata +66 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2006 Lars Pind
|
2
|
+
Copyright (c) 2008 Manfred Stienstra, Fingertips <manfred@fngtps.com>
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
RedirectRouting
|
2
|
+
===============
|
3
|
+
|
4
|
+
Forked by Seamus Abshere (seamus@abshere.net)
|
5
|
+
|
6
|
+
Note: you may have to put
|
7
|
+
|
8
|
+
ActionController::Routing::RouteSet::Mapper.send :include, RedirectRouting::Routes
|
9
|
+
|
10
|
+
at the top of config/routes.rb.
|
11
|
+
|
12
|
+
This plugin lets you do simple redirects straight from your routes.rb file:
|
13
|
+
|
14
|
+
map.redirect '', :controller => 'events'
|
15
|
+
|
16
|
+
This will make the root of your site redirect to the events controller, typically at /events.
|
17
|
+
|
18
|
+
You can redirect any URL to any set of options, or event a string URL, like this:
|
19
|
+
|
20
|
+
map.redirect 'test', 'http://example.com'
|
21
|
+
|
22
|
+
GET /test, and you'll be redirected to my blog.
|
23
|
+
|
24
|
+
You can also set the status to be a 301 permanent redirect instead of a temporary 302:
|
25
|
+
|
26
|
+
map.redirect 'oldurl', 'newurl', :permament => true
|
27
|
+
map.redirect 'oldurl', :controller => 'new_controller', :action => 'new_action', :permament => true
|
28
|
+
|
29
|
+
Finally you can preserve wildcard paths
|
30
|
+
|
31
|
+
map.redirect 'festivals/*path', 'http://example.com/events/festivals', :keep_path => :path
|
32
|
+
map.redirect 'news/*articles', 'http://blog.example.com', :keep_path => :articles
|
33
|
+
|
34
|
+
Motivation
|
35
|
+
----------
|
36
|
+
|
37
|
+
Why this plugin?
|
38
|
+
|
39
|
+
Because if Rails Routing is supposed to be a Ruby replacement for mod_rewrite, then at least some redirect capability is called for.
|
40
|
+
|
41
|
+
But more concretely, because there's no good alternative unless you're using Apache. The alternative options that I've been able to figure out are:
|
42
|
+
|
43
|
+
* Configure your web server to do the redirect for you, which is trivial with mod_rewrite in Apache, but not so trivial with Mongrel, or
|
44
|
+
* Manually create a controller for the sole purpose of redirecting, or
|
45
|
+
* Tack the redirect onto another controller, which isn't very RESTful
|
46
|
+
|
47
|
+
If there's demand, I'll add a switch for a permanent redirect, but I don't have the need yet.
|
48
|
+
|
49
|
+
If you know of a simpler way to do this, please let me know.
|
50
|
+
|
51
|
+
Credits
|
52
|
+
-------
|
53
|
+
|
54
|
+
Written by Lars Pind
|
55
|
+
|
56
|
+
http://pinds.com
|
57
|
+
|
58
|
+
Changelog
|
59
|
+
---------
|
60
|
+
|
61
|
+
* Added :keep_path options [Seamus Abshere & Manfred Stienstra]
|
62
|
+
* Updated to Rails Edge [Manfred Stienstra]
|
63
|
+
* Support for 301 redirects [Gioele Barabucci]
|
64
|
+
* Silence warning about missing helper [Tim Connor]
|
data/Rakefile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the test plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the test plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'Test'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
23
|
+
|
24
|
+
begin
|
25
|
+
require 'jeweler'
|
26
|
+
Jeweler::Tasks.new do |spec|
|
27
|
+
spec.name = "redirect_routing"
|
28
|
+
|
29
|
+
spec.author = "Manfred Stienstra"
|
30
|
+
spec.email = "manfred@fngtps.com"
|
31
|
+
|
32
|
+
spec.description = <<-EOF
|
33
|
+
simple redirects straight from your routes.rb file
|
34
|
+
EOF
|
35
|
+
spec.summary = <<-EOF
|
36
|
+
simple redirects straight from your routes.rb file
|
37
|
+
EOF
|
38
|
+
spec.homepage = "http://github.com/seamusabshere/redirect_routing/tree/master"
|
39
|
+
end
|
40
|
+
rescue LoadError
|
41
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
42
|
+
end
|
data/VERSION.yml
ADDED
data/init.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
class RedirectRoutingController < ActionController::Base
|
2
|
+
def redirect
|
3
|
+
options = params[:args].extract_options!
|
4
|
+
status = options.delete(:permanent) == true ? :moved_permanently : :found
|
5
|
+
url_options = params[:args].first || options
|
6
|
+
|
7
|
+
if path_to_keep = options[:keep_path] and params[path_to_keep].present?
|
8
|
+
raise ArgumentError, "Redirect target should be a String when using the :keep_path option" unless url_options.is_a?(String)
|
9
|
+
parsed_url = URI.parse url_options
|
10
|
+
parsed_url.path = ([parsed_url.path] + params[path_to_keep]).join '/'
|
11
|
+
url_options = parsed_url.to_s
|
12
|
+
end
|
13
|
+
|
14
|
+
if params[:format].present?
|
15
|
+
case url_options
|
16
|
+
when String
|
17
|
+
url_options << ".#{params[:format]}"
|
18
|
+
when Hash
|
19
|
+
url_options[:format] = params[:format]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
redirect_to url_options, :status => status
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{redirect_routing}
|
8
|
+
s.version = "0.0.4"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Manfred Stienstra"]
|
12
|
+
s.date = %q{2009-10-08}
|
13
|
+
s.description = %q{ simple redirects straight from your routes.rb file
|
14
|
+
}
|
15
|
+
s.email = %q{manfred@fngtps.com}
|
16
|
+
s.extra_rdoc_files = [
|
17
|
+
"README"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
"MIT-LICENSE",
|
21
|
+
"README",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION.yml",
|
24
|
+
"init.rb",
|
25
|
+
"lib/redirect_routing.rb",
|
26
|
+
"lib/redirect_routing/routes.rb",
|
27
|
+
"lib/redirect_routing_controller.rb",
|
28
|
+
"redirect_routing.gemspec",
|
29
|
+
"test/redirect_routing_test.rb",
|
30
|
+
"test/test_helper.rb"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/seamusabshere/redirect_routing/tree/master}
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.3.5}
|
36
|
+
s.summary = %q{simple redirects straight from your routes.rb file}
|
37
|
+
s.test_files = [
|
38
|
+
"test/redirect_routing_test.rb",
|
39
|
+
"test/test_helper.rb"
|
40
|
+
]
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
47
|
+
else
|
48
|
+
end
|
49
|
+
else
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class EventsController < ActionController::Base
|
4
|
+
end
|
5
|
+
ActionController::Routing::Routes.add_route('/:controller/:action/:id')
|
6
|
+
|
7
|
+
class RedirectRoutingTest < TestCase
|
8
|
+
tests RedirectRoutingController
|
9
|
+
|
10
|
+
def setup
|
11
|
+
@controller = RedirectRoutingController.new
|
12
|
+
@request = ActionController::TestRequest.new
|
13
|
+
@response = ActionController::TestResponse.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_redirect_routes
|
17
|
+
with_routing do |set|
|
18
|
+
set.draw do |map|
|
19
|
+
map.redirect '', :controller => 'events'
|
20
|
+
map.redirect 'test', 'http://example.com'
|
21
|
+
map.redirect 'oldurl', 'newurl', :permanent => true
|
22
|
+
map.redirect 'festivals/*path', 'http://example.com/events/festivals', :keep_path => :path
|
23
|
+
end
|
24
|
+
|
25
|
+
assert_recognizes({ :controller => "redirect_routing", :action => "redirect", :args => [{ 'controller' => "events" }] }, "/")
|
26
|
+
assert_recognizes({ :controller => "redirect_routing", :action => "redirect", :args => ["http://example.com"] }, "/test")
|
27
|
+
assert_recognizes({ :controller => "redirect_routing", :action => "redirect", :args => ["newurl", {'permanent' => true}] }, "/oldurl")
|
28
|
+
assert_recognizes({ :controller => "redirect_routing", :action => "redirect", :args => ["http://example.com/events/festivals", {'keep_path' => :path}], :path => ['music', 'recent'] }, "/festivals/music/recent")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_redirect_controller_with_hash
|
33
|
+
get :redirect, :args => [{ :controller => "events" }]
|
34
|
+
assert_redirected_to :controller => "events"
|
35
|
+
assert_response 302
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_redirect_controller_with_string
|
39
|
+
get :redirect, :args => ["http://example.com"]
|
40
|
+
assert_redirected_to "http://example.com"
|
41
|
+
assert_response 302
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_permanent_redirect_controller_with_hash
|
45
|
+
get :redirect, :args => [{ :controller => "events", :permanent => true }]
|
46
|
+
assert_redirected_to :controller => "events"
|
47
|
+
assert_response 301
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_permanent_redirect_controller_with_string
|
51
|
+
get :redirect, :args => ["http://example.com", { :permanent => true }]
|
52
|
+
assert_redirected_to "http://example.com"
|
53
|
+
assert_response 301
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_redirect_controller_with_hash_and_append_path
|
57
|
+
assert_raises(ArgumentError) do
|
58
|
+
get :redirect, :args => [{ :controller => "events", :keep_path => :path }], :path => [ "festivals/recent" ]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_redirect_controller_with_string_and_append_path
|
63
|
+
get :redirect, :args => ["http://example.com", { :keep_path => :path }], :path => [ "festivals/recent" ]
|
64
|
+
assert_redirected_to "http://example.com/festivals/recent"
|
65
|
+
assert_response 302
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_redirect_controller_with_string_and_append_path_array
|
69
|
+
get :redirect, :args => ["http://example.com", { :keep_path => :path }], :path => [ "festivals", "recent" ]
|
70
|
+
assert_redirected_to "http://example.com/festivals/recent"
|
71
|
+
assert_response 302
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_permanent_redirect_controller_with_hash_and_append_path
|
75
|
+
assert_raises(ArgumentError) do
|
76
|
+
get :redirect, :args => [{ :controller => "events", :permanent => true, :keep_path => :path }], :path => [ "festivals/recent" ]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_permanent_redirect_controller_with_string_and_append_path
|
81
|
+
get :redirect, :args => ["http://example.com", { :permanent => true, :keep_path => :path }], :path => [ "festivals/recent" ]
|
82
|
+
assert_redirected_to "http://example.com/festivals/recent"
|
83
|
+
assert_response 301
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_redirect_normally_when_the_path_to_keep_is_blank
|
87
|
+
get :redirect, :args => ["http://example.com", { :keep_path => :unknown }], :path => [ "festivals/recent" ]
|
88
|
+
assert_redirected_to "http://example.com"
|
89
|
+
assert_response 302
|
90
|
+
end
|
91
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
ENV['RAILS_ENV'] = "test"
|
2
|
+
|
3
|
+
rails_vendor_path = File.expand_path('../../../vendor/rails')
|
4
|
+
if File.exist?(rails_vendor_path)
|
5
|
+
$:.concat([
|
6
|
+
File.join(rails_vendor_path, 'activesupport', 'lib'),
|
7
|
+
File.join(rails_vendor_path, 'actionpack', 'lib')
|
8
|
+
])
|
9
|
+
require 'active_support'
|
10
|
+
require 'action_controller'
|
11
|
+
require 'action_controller/test_process'
|
12
|
+
else
|
13
|
+
puts "\n[!] Tests only run when the plugin is installed in the plugins directory, running the tests against anything other than your particular Rails version doesn't really serve a purpose."
|
14
|
+
exit -1
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'test/unit'
|
18
|
+
|
19
|
+
begin
|
20
|
+
TestCase = ActionController::TestCase
|
21
|
+
rescue NameError
|
22
|
+
TestCase = Test::Unit::TestCase
|
23
|
+
end
|
24
|
+
|
25
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
26
|
+
require 'init'
|
27
|
+
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redirect_routing
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Manfred Stienstra
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-08 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: " simple redirects straight from your routes.rb file\n"
|
17
|
+
email: manfred@fngtps.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
files:
|
25
|
+
- MIT-LICENSE
|
26
|
+
- README
|
27
|
+
- Rakefile
|
28
|
+
- VERSION.yml
|
29
|
+
- init.rb
|
30
|
+
- lib/redirect_routing.rb
|
31
|
+
- lib/redirect_routing/routes.rb
|
32
|
+
- lib/redirect_routing_controller.rb
|
33
|
+
- redirect_routing.gemspec
|
34
|
+
- test/redirect_routing_test.rb
|
35
|
+
- test/test_helper.rb
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://github.com/seamusabshere/redirect_routing/tree/master
|
38
|
+
licenses: []
|
39
|
+
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options:
|
42
|
+
- --charset=UTF-8
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.3.5
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: simple redirects straight from your routes.rb file
|
64
|
+
test_files:
|
65
|
+
- test/redirect_routing_test.rb
|
66
|
+
- test/test_helper.rb
|