seamusabshere-redirect_routing 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +64 -0
- data/Rakefile +42 -0
- data/VERSION.yml +4 -0
- data/lib/redirect_routing.rb +3 -0
- data/lib/redirect_routing/routes.rb +7 -0
- data/lib/redirect_routing_controller.rb +16 -0
- data/lib/redirect_routing_helper.rb +2 -0
- data/test/redirect_routing_test.rb +91 -0
- data/test/test_helper.rb +27 -0
- metadata +62 -0
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
@@ -0,0 +1,16 @@
|
|
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].blank?
|
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
|
+
redirect_to url_options, :status => status
|
15
|
+
end
|
16
|
+
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,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: seamusabshere-redirect_routing
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Manfred Stienstra
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-07 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: simple redirects straight from your routes.rb file
|
17
|
+
email: manfred@fngtps.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
files:
|
25
|
+
- Rakefile
|
26
|
+
- VERSION.yml
|
27
|
+
- lib/redirect_routing.rb
|
28
|
+
- lib/redirect_routing/routes.rb
|
29
|
+
- lib/redirect_routing_controller.rb
|
30
|
+
- lib/redirect_routing_helper.rb
|
31
|
+
- test/redirect_routing_test.rb
|
32
|
+
- test/test_helper.rb
|
33
|
+
- README
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://github.com/seamusabshere/redirect_routing/tree/master
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options:
|
38
|
+
- --charset=UTF-8
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.2.0
|
57
|
+
signing_key:
|
58
|
+
specification_version: 2
|
59
|
+
summary: simple redirects straight from your routes.rb file
|
60
|
+
test_files:
|
61
|
+
- test/redirect_routing_test.rb
|
62
|
+
- test/test_helper.rb
|