1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
require 'abstract_unit'
module TestGenerationPrefix
class WithMountedEngine < ActionDispatch::IntegrationTest
require 'rack/test'
include Rack::Test::Methods
class BlogEngine
def self.routes
@routes ||= begin
routes = ActionDispatch::Routing::RouteSet.new
routes.draw do
match "/posts/:id", :to => "inside_engine_generating#index", :as => :post
match "/bare_url_for", :to => "inside_engine_generating#bare_url_for", :as => :bare_url_for
end
routes
end
end
def self.call(env)
env['action_dispatch.routes'] = routes
routes.call(env)
end
end
class RailsApplication
def self.routes
@routes ||= begin
routes = ActionDispatch::Routing::RouteSet.new
routes.draw do
scope "/:omg", :omg => "awesome" do
mount BlogEngine => "/blog"
end
match "/generate", :to => "outside_engine_generating#index"
root :to => "outside_engine_generating#index"
end
routes
end
end
def self.call(env)
env['action_dispatch.routes'] = routes
# the next to values should be set only in application
env['ORIGINAL_SCRIPT_NAME'] = env['SCRIPT_NAME']
env['action_dispatch.parent_routes'] = routes
routes.call(env)
end
end
class ::InsideEngineGeneratingController < ActionController::Base
include BlogEngine.routes.url_helpers
def index
render :text => post_path(:id => params[:id])
end
def bare_url_for
path = url_for( :routes => RailsApplication.routes,
:controller => "outside_engine_generating",
:action => "index",
:only_path => true)
render :text => path
end
end
class ::OutsideEngineGeneratingController < ActionController::Base
include BlogEngine.routes.url_helpers
def index
render :text => post_path(:id => 1)
end
end
class Foo
include ActionDispatch::Routing::UrlFor
include BlogEngine.routes.url_helpers
def foo
post_path(42)
end
end
class Bar
include ActionDispatch::Routing::UrlFor
include RailsApplication.routes.url_helpers
def bar
root_path
end
end
RailsApplication.routes # force draw
include BlogEngine.routes.url_helpers
def app
RailsApplication
end
def setup
RailsApplication.routes.default_url_options = {}
end
test "generating URL with prefix" do
assert_equal "/awesome/blog/posts/1", post_path(:id => 1)
end
test "use SCRIPT_NAME inside the engine" do
get "/pure-awesomness/blog/posts/1"
assert_equal "/pure-awesomness/blog/posts/1", last_response.body
end
test "prepend prefix outside the engine" do
RailsApplication.routes.default_url_options = {:script_name => "/something"}
get "/generate", {}, 'SCRIPT_NAME' => "/something"
assert_equal "/something/awesome/blog/posts/1", last_response.body
end
test "generating urls with options for both prefix and named_route" do
assert_equal "/pure-awesomness/blog/posts/3", post_path(:id => 3, :omg => "pure-awesomness")
end
test "generating urls with url_for should prepend the prefix" do
path = BlogEngine.routes.url_for(:omg => 'omg', :controller => "inside_engine_generating", :action => "index", :id => 1, :only_path => true)
assert_equal "/omg/blog/posts/1", path
end
test "generating urls from a regular class" do
assert_equal "/awesome/blog/posts/42", Foo.new.foo
end
test "passing :routes to url_for to change current routes" do
RailsApplication.routes.default_url_options = {:script_name => "/something"}
get "/pure-awesomness/blog/bare_url_for", {}, 'SCRIPT_NAME' => "/something"
assert_equal "/something/generate", last_response.body
end
test "using default_url_options[:script_name] in regular classes" do
RailsApplication.routes.default_url_options = {:script_name => "/something"}
assert_equal "/something/", Bar.new.bar
end
end
end
|