summaryrefslogtreecommitdiff
path: root/vendor
diff options
context:
space:
mode:
authorhukl <contact@smyck.org>2009-02-19 22:42:09 +0100
committerhukl <contact@smyck.org>2009-02-19 22:42:09 +0100
commit5d4f1e0639c0c9cd3491fef8438a116508ad6e1d (patch)
treea412372cef3bcbd7f008e65309ef0496c79bc48c /vendor
parentc22eb34b560c685480a51e95653a04fa0b409453 (diff)
added this route filter from sven fuchs to make
this simplified route magic possible. hope i don't foot myself in the shot with it.
Diffstat (limited to 'vendor')
-rw-r--r--vendor/plugins/routing-filter/.gitignore1
-rw-r--r--vendor/plugins/routing-filter/MIT-LICENSE20
-rw-r--r--vendor/plugins/routing-filter/README.markdown123
-rw-r--r--vendor/plugins/routing-filter/init.rb1
-rw-r--r--vendor/plugins/routing-filter/lib/routing_filter.rb69
-rw-r--r--vendor/plugins/routing-filter/lib/routing_filter/base.rb15
-rw-r--r--vendor/plugins/routing-filter/lib/routing_filter/locale.rb34
-rw-r--r--vendor/plugins/routing-filter/lib/routing_filter/pagination.rb19
-rw-r--r--vendor/plugins/routing-filter/spec/generation_spec.rb283
-rw-r--r--vendor/plugins/routing-filter/spec/recognition_spec.rb76
-rw-r--r--vendor/plugins/routing-filter/spec/routing_filter_spec.rb70
-rw-r--r--vendor/plugins/routing-filter/spec/spec.opts4
-rw-r--r--vendor/plugins/routing-filter/spec/spec_helper.rb49
13 files changed, 764 insertions, 0 deletions
diff --git a/vendor/plugins/routing-filter/.gitignore b/vendor/plugins/routing-filter/.gitignore
new file mode 100644
index 0000000..5657f6e
--- /dev/null
+++ b/vendor/plugins/routing-filter/.gitignore
@@ -0,0 +1 @@
vendor \ No newline at end of file
diff --git a/vendor/plugins/routing-filter/MIT-LICENSE b/vendor/plugins/routing-filter/MIT-LICENSE
new file mode 100644
index 0000000..ac93a58
--- /dev/null
+++ b/vendor/plugins/routing-filter/MIT-LICENSE
@@ -0,0 +1,20 @@
1Copyright (c) 2008 Sven Fuchs
2
3Permission is hereby granted, free of charge, to any person obtaining
4a copy of this software and associated documentation files (the
5"Software"), to deal in the Software without restriction, including
6without limitation the rights to use, copy, modify, merge, publish,
7distribute, sublicense, and/or sell copies of the Software, and to
8permit persons to whom the Software is furnished to do so, subject to
9the following conditions:
10
11The above copyright notice and this permission notice shall be
12included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file
diff --git a/vendor/plugins/routing-filter/README.markdown b/vendor/plugins/routing-filter/README.markdown
new file mode 100644
index 0000000..77ee55d
--- /dev/null
+++ b/vendor/plugins/routing-filter/README.markdown
@@ -0,0 +1,123 @@
1## Routing Filter
2
3This plugin is a wild hack that wraps around the complex beast that the Rails
4routing system is to allow for unseen flexibility and power in Rails URL
5recognition and generation.
6
7As powerful and awesome the Rails' routes are, when you need to design your
8URLs in a manner that only slightly leaves the paved cowpaths of Rails
9conventions, you're usually unable to use all the goodness of helpers and
10convenience that Rails ships with.
11
12## Usage
13
14This plugin comes with a locale routing filter that demonstrates the
15implementation of a custom filter.
16
17The following would be a sceleton of an empty filter:
18
19 module RoutingFilter
20 class Awesomeness < Base
21 def around_recognize(route, path, env)
22 # Alter the path here before it gets recognized.
23 # Make sure to yield (calls the next around filter if present and
24 # eventually `recognize_path` on the routeset):
25 returning yield do |params|
26 # You can additionally modify the params here before they get passed
27 # to the controller.
28 end
29 end
30
31 def around_generate(controller, *args, &block)
32 # Alter arguments here before they are passed to `url_for`.
33 # Make sure to yield (calls the next around filter if present and
34 # eventually `url_for` on the controller):
35 returning yield do |result|
36 # You can change the generated url_or_path here. Make sure to use
37 # one of the "in-place" modifying String methods though (like sub!
38 # and friends).
39 end
40 end
41 end
42 end
43
44You then have to specify the filter explicitely in your routes.rb:
45
46 ActionController::Routing::Routes.draw do |map|
47 map.filter 'awesomeness'
48 end
49
50(I am not sure if it makes sense to provide more technical information than
51this because the usage of this plugin definitely requires some advanced
52knowledge about Rails internals and especially its routing system. So, I
53figure, anyone who could use this should also be able to read the code and
54figure out what it's doing much better then from any lengthy documentation.
55
56If I'm mistaken on this please drop me an email with your suggestions.)
57
58
59## Rationale: Two example usecases
60
61An early usecase from which this originated was the need to define a locale
62at the beginning of an URL in a way so that
63
64* the locale can be omitted when it is the default locale
65* all the url\_helpers that are generated by named routes continue to work in
66a concise manner (i.e. without specifying all parameters again and again)
67* ideally also plays nicely with default route helpers in tests/specs
68
69You can read about this struggle and two possible, yet unsatisfying solutions
70[here](http://www.artweb-design.de/2007/5/13/concise-localized-rails-url-helpers-solved-twice).
71The conclusion so far is that Rails itself does not provide the tools to solve
72this problem in a clean and dry way.
73
74Another usecase that eventually spawned the manifestation of this plugin was
75the need to map an arbitrary count of path segments to a certain model
76instance. In an application that I've been working on recently I needed to
77map URL paths to a nested tree of models like so:
78
79 root
80 + docs
81 + api
82 + wiki
83
84E.g. the docs section should map to the path `/docs`, the api section to
85the path `/docs/api` and so on. Furthermore, after these paths there need to be
86more things to be specified. E.g. the wiki needs to define a whole Rails
87resource with URLs like `/docs/wiki/pages/1/edit`.
88
89The only way to solve this problem with Rails' routing toolkit is to map
90a big, bold `/*everything` catch-all ("globbing") route and process the whole
91path in a custom dispatcher.
92
93This, of course, is a really unsatisfying solution because one has to
94reimplement everything that Rails routes are here to help with: regarding both
95URL recognition (like parameter mappings, resources, ...) and generation
96(url\_helpers).
97
98## Solution
99
100This plugin offers a solution that takes exactly the opposite route.
101
102Instead of trying to change things *between* the URL recognition and
103generation stages to achieve the desired result it *wraps around* the whole
104routing system and allows to pre- and post-filter both what goes into it
105(URL recognition) and what comes out of it (URL generation).
106
107This way we can leave *everything* else completely untouched.
108
109* We can tinker with the URLs that we receive from the server and feed URLs to
110Rails that perfectly match the best breed of Rails' conventions.
111* Inside of the application we can use all the nice helper goodness and
112conveniences that rely on these conventions being followed.
113* Finally we can accept URLs that have been generated by the url\_helpers and,
114again, mutate them in the way that matches our requirements.
115
116So, even though the plugin itself is a blatant monkey-patch to one of the
117most complex area of Rails internals, this solution seems to be effectively
118less intrusive and pricey than others are.
119
120## Etc
121
122Authors: [Sven Fuchs](http://www.artweb-design.de) <svenfuchs at artweb-design dot de>
123License: MIT \ No newline at end of file
diff --git a/vendor/plugins/routing-filter/init.rb b/vendor/plugins/routing-filter/init.rb
new file mode 100644
index 0000000..1189921
--- /dev/null
+++ b/vendor/plugins/routing-filter/init.rb
@@ -0,0 +1 @@
require 'routing_filter' \ No newline at end of file
diff --git a/vendor/plugins/routing-filter/lib/routing_filter.rb b/vendor/plugins/routing-filter/lib/routing_filter.rb
new file mode 100644
index 0000000..d67be14
--- /dev/null
+++ b/vendor/plugins/routing-filter/lib/routing_filter.rb
@@ -0,0 +1,69 @@
1module RoutingFilter
2 mattr_accessor :active
3 @@active = true
4
5 class Chain < Array
6 def << (filter)
7 filter.successor = last
8 super
9 end
10
11 def run(method, *args, &final)
12 RoutingFilter.active ? last.run(method, *args, &final) : final.call
13 end
14 end
15end
16
17# allows to install a filter to the route set by calling: map.filter 'locale'
18ActionController::Routing::RouteSet::Mapper.class_eval do
19 def filter(name, options = {})
20 require "routing_filter/#{name}"
21 klass = RoutingFilter.const_get name.to_s.camelize
22 @set.filters << klass.new(options)
23 end
24end
25
26# same here for the optimized url generation in named routes
27ActionController::Routing::RouteSet::NamedRouteCollection.class_eval do
28 # gosh. monkey engineering optimization code
29 def generate_optimisation_block_with_filtering(*args)
30 code = generate_optimisation_block_without_filtering *args
31 if match = code.match(%r(^return (.*) if (.*)))
32 # returned string must not contain newlines, or we'll spill out of inline code comments in ActionController::Routing::RouteSet::NamedRouteCollection#define_url_helper (as of http://github.com/rails/rails/commit/a2270ef2594b97891994848138614657363f2806)
33 "returning(#{match[1]}) { |result| ActionController::Routing::Routes.filters.run :around_generate, *args, &lambda{ result } } if #{match[2]}"
34 end
35 end
36 alias_method_chain :generate_optimisation_block, :filtering
37end
38
39ActionController::Routing::RouteSet.class_eval do
40 def filters
41 @filters ||= RoutingFilter::Chain.new
42 end
43
44 def recognize_path_with_filtering(path, env)
45 path = path.dup # string is frozen due to memoize
46 filters.run :around_recognize, path, env, &lambda{ recognize_path_without_filtering(path, env) }
47 end
48 alias_method_chain :recognize_path, :filtering
49
50 def generate_with_filtering(*args)
51 filters.run :around_generate, args.first, &lambda{ generate_without_filtering(*args) }
52 end
53 alias_method_chain :generate, :filtering
54
55 # add some useful information to the request environment
56 # right, this is from jamis buck's excellent article about routes internals
57 # http://weblog.jamisbuck.org/2006/10/26/monkey-patching-rails-extending-routes-2
58 # TODO move this ... where?
59 alias_method :extract_request_environment_without_host, :extract_request_environment unless method_defined? :extract_request_environment_without_host
60 def extract_request_environment(request)
61 returning extract_request_environment_without_host(request) do |env|
62 env.merge! :host => request.host,
63 :port => request.port,
64 :host_with_port => request.host_with_port,
65 :domain => request.domain,
66 :subdomain => request.subdomains.first
67 end
68 end
69end \ No newline at end of file
diff --git a/vendor/plugins/routing-filter/lib/routing_filter/base.rb b/vendor/plugins/routing-filter/lib/routing_filter/base.rb
new file mode 100644
index 0000000..bc60ba5
--- /dev/null
+++ b/vendor/plugins/routing-filter/lib/routing_filter/base.rb
@@ -0,0 +1,15 @@
1module RoutingFilter
2 class Base
3 attr_accessor :successor, :options
4
5 def initialize(options)
6 @options = options
7 options.each{|name, value| instance_variable_set :"@#{name}", value }
8 end
9
10 def run(method, *args, &block)
11 successor = @successor ? lambda { @successor.run(method, *args, &block) } : block
12 send method, *args, &successor
13 end
14 end
15end \ No newline at end of file
diff --git a/vendor/plugins/routing-filter/lib/routing_filter/locale.rb b/vendor/plugins/routing-filter/lib/routing_filter/locale.rb
new file mode 100644
index 0000000..b7fcc31
--- /dev/null
+++ b/vendor/plugins/routing-filter/lib/routing_filter/locale.rb
@@ -0,0 +1,34 @@
1require 'i18n'
2require 'routing_filter/base'
3
4module RoutingFilter
5 class Locale < Base
6 @@default_locale = :en
7 cattr_reader :default_locale
8
9 class << self
10 def default_locale=(locale)
11 @@default_locale = locale.to_sym
12 end
13 end
14
15 # remove the locale from the beginning of the path, pass the path
16 # to the given block and set it to the resulting params hash
17 def around_recognize(path, env, &block)
18 locale = nil
19 path.sub! %r(^/([a-zA-Z]{2})(?=/|$)) do locale = $1; '' end
20 returning yield do |params|
21 params[:locale] = locale if locale
22 end
23 end
24
25 def around_generate(*args, &block)
26 locale = args.extract_options!.delete(:locale) || I18n.locale
27 returning yield do |result|
28 if locale.to_sym != @@default_locale
29 result.sub!(%r(^(http.?://[^/]*)?(.*))){ "#{$1}/#{locale}#{$2}" }
30 end
31 end
32 end
33 end
34end \ No newline at end of file
diff --git a/vendor/plugins/routing-filter/lib/routing_filter/pagination.rb b/vendor/plugins/routing-filter/lib/routing_filter/pagination.rb
new file mode 100644
index 0000000..a5bd7aa
--- /dev/null
+++ b/vendor/plugins/routing-filter/lib/routing_filter/pagination.rb
@@ -0,0 +1,19 @@
1require 'routing_filter/base'
2
3module RoutingFilter
4 class Pagination < Base
5 def around_recognize(path, env, &block)
6 path.gsub! %r(/pages/([\d]+)/?$), ''
7 returning yield(path, env) do |params|
8 params[:page] = $1.to_i if $1
9 end
10 end
11
12 def around_generate(*args, &block)
13 page = args.extract_options!.delete(:page)
14 returning yield do |result|
15 result.replace "#{result}/pages/#{page}" if page && page != 1
16 end
17 end
18 end
19end \ No newline at end of file
diff --git a/vendor/plugins/routing-filter/spec/generation_spec.rb b/vendor/plugins/routing-filter/spec/generation_spec.rb
new file mode 100644
index 0000000..224cd4e
--- /dev/null
+++ b/vendor/plugins/routing-filter/spec/generation_spec.rb
@@ -0,0 +1,283 @@
1require File.dirname(__FILE__) + '/spec_helper.rb'
2
3describe 'RoutingFilter', 'url generation' do
4 include RoutingFilterHelpers
5
6 before :each do
7 RoutingFilter::Locale.default_locale = :en
8 I18n.default_locale = :en
9 I18n.locale = :en
10
11 @controller = instantiate_controller :locale => 'de', :id => 1
12 @set = draw_routes do |map|
13 map.section 'sections/:id', :controller => 'sections', :action => "show"
14 map.section_article 'sections/:section_id/articles/:id', :controller => 'articles', :action => "show"
15
16 map.filter 'locale'
17 map.filter 'pagination'
18 end
19
20 @site = Site.new
21 @section = Section.new
22 @article = Article.new
23
24 @params = {:controller => 'sections', :action => "show", :id => "1"}
25 @article_params = {:controller => 'articles', :action => 'show', :section_id => "1", :id => "1"}
26 @locale_filter = @set.filters.first
27
28 Section.stub!(:types).and_return ['Section']
29 Section.stub!(:find).and_return @section
30 end
31
32 def should_recognize_path(path, params)
33 @set.recognize_path(path, {}).should == params
34 end
35
36 def section_path(*args)
37 @controller.send :section_path, *args
38 end
39
40 def section_article_path(*args)
41 @controller.send :section_article_path, *args
42 end
43
44 def url_for(*args)
45 @controller.send :url_for, *args
46 end
47
48 describe "named route url_helpers" do
49 describe "a not nested resource" do
50 it 'does not change the section_path when the current locale is the default locale and no page option given' do
51 section_path(:id => 1).should == '/sections/1'
52 end
53
54 it 'does not change the section_path when given page option equals 1' do
55 section_path(:id => 1, :page => 1).should == '/sections/1'
56 end
57
58 it 'appends the pages segments to section_path when given page option does not equal 1' do
59 section_path(:id => 1, :page => 2).should == '/sections/1/pages/2'
60 end
61
62 it 'prepends the current locale to section_path when it is not the default locale' do
63 I18n.locale = :de
64 section_path(:id => 1).should == '/de/sections/1'
65 end
66
67 it 'prepends a given locale param to section_path when it is not the default locale' do
68 I18n.locale = :de
69 section_path(:id => 1, :locale => :fi).should == '/fi/sections/1'
70 end
71
72 it 'works on section_path with both a locale and page option' do
73 section_path(:id => 1, :locale => :fi, :page => 2).should == '/fi/sections/1/pages/2'
74 end
75 end
76
77 describe "a nested resource" do
78 it 'does not change the section_article_path when the current locale is the default locale and no page option given' do
79 section_article_path(:section_id => 1, :id => 1).should == '/sections/1/articles/1'
80 end
81
82 it 'does not change the section_article_path when given page option equals 1' do
83 section_article_path(:section_id => 1, :id => 1, :page => 1).should == '/sections/1/articles/1'
84 end
85
86 it 'appends the pages segments to section_article_path when given page option does not equal 1' do
87 section_article_path(:section_id => 1, :id => 1, :page => 2).should == '/sections/1/articles/1/pages/2'
88 end
89
90 it 'prepends the current locale to section_article_path when it is not the default locale' do
91 I18n.locale = :de
92 section_article_path(:section_id => 1, :id => 1).should == '/de/sections/1/articles/1'
93 end
94
95 it 'prepends a given locale param to section_article_path when it is not the default locale' do
96 I18n.locale = :de
97 section_article_path(:section_id => 1, :id => 1, :locale => :fi).should == '/fi/sections/1/articles/1'
98 end
99
100 it 'works on section_article_path with both a locale and page option' do
101 section_article_path(:section_id => 1, :id => 1, :locale => :fi, :page => 2).should == '/fi/sections/1/articles/1/pages/2'
102 end
103 end
104 end
105
106 describe 'when used with named route url_helper with "optimized" generation blocks' do
107 describe "a not nested resource" do
108 # uses optimization
109 it 'does not change the section_path when the current locale is the default locale and no page option given' do
110 section_path(1).should == '/sections/1'
111 end
112
113 # uses optimization
114 it 'prepends the current locale to section_path when it is not the default locale' do
115 I18n.locale = :de
116 section_path(1).should == '/de/sections/1'
117 end
118
119 it 'prepends a given locale param to section_path when it is not the default locale' do
120 I18n.locale = :de
121 section_path(1, :locale => :fi).should == '/fi/sections/1'
122 end
123
124 it 'does not change the section_path when given page option equals 1' do
125 section_path(1, :page => 1).should == '/sections/1'
126 end
127
128 it 'appends the pages segments to section_path when given page option does not equal 1' do
129 section_path(1, :page => 2).should == '/sections/1/pages/2'
130 end
131
132 it 'works for section_path with both a locale and page option' do
133 section_path(1, :locale => :fi, :page => 2).should == '/fi/sections/1/pages/2'
134 end
135 end
136
137 describe "a nested resource" do
138 # uses optimization
139 it 'does not change the section_article_path when the current locale is the default locale and no page option given' do
140 section_article_path(1, 1).should == '/sections/1/articles/1'
141 end
142
143 # uses optimization
144 it 'prepends the current locale to section_article_path when it is not the default locale' do
145 I18n.locale = :de
146 section_article_path(1, 1).should == '/de/sections/1/articles/1'
147 end
148
149 it 'prepends a given locale param when it is not the default locale' do
150 I18n.locale = :de
151 section_article_path(1, 1, :locale => :fi).should == '/fi/sections/1/articles/1'
152 end
153
154 it 'does not change the section_article_path when given page option equals 1' do
155 section_article_path(1, 1, :page => 1).should == '/sections/1/articles/1'
156 end
157
158 it 'appends the pages segments to section_article_path when given page option does not equal 1' do
159 section_article_path(1, 1, :page => 2).should == '/sections/1/articles/1/pages/2'
160 end
161
162 it 'works for section_article_path with both a locale and page option' do
163 section_article_path(1, 1, :locale => :fi, :page => 2).should == '/fi/sections/1/articles/1/pages/2'
164 end
165 end
166 end
167
168 describe 'when used with a polymorphic_path' do
169 describe "a not nested resource" do
170 # uses optimization
171 it 'does not change the section_path when the current locale is the default locale and no page option given' do
172 section_path(@section).should == '/sections/1'
173 end
174
175 # uses optimization
176 it 'prepends the current locale to section_path when it is not the default locale' do
177 I18n.locale = :de
178 section_path(@section).should == '/de/sections/1'
179 end
180
181 it 'prepends a given locale param to section_path when it is not the default locale' do
182 I18n.locale = :de
183 section_path(@section, :locale => :fi).should == '/fi/sections/1'
184 end
185
186 it 'does not change the section_path when given page option equals 1' do
187 section_path(@section, :page => 1).should == '/sections/1'
188 end
189
190 it 'appends the pages segments to section_path when given page option does not equal 1' do
191 section_path(@section, :page => 2).should == '/sections/1/pages/2'
192 end
193
194 it 'works for section_path with both a locale and page option' do
195 section_path(@section, :locale => :fi, :page => 2).should == '/fi/sections/1/pages/2'
196 end
197 end
198
199 describe "a nested resource" do
200 # uses optimization
201 it 'does not change the section_article_path when the current locale is the default locale and no page option given' do
202 section_article_path(@section, @article).should == '/sections/1/articles/1'
203 end
204
205 # uses optimization
206 it 'prepends the current locale to section_article_path when it is not the default locale' do
207 I18n.locale = :de
208 section_article_path(@section, @article).should == '/de/sections/1/articles/1'
209 end
210
211 it 'prepends a given locale param to section_article_path when it is not the default locale' do
212 I18n.locale = :de
213 section_article_path(@section, @article, :locale => :fi).should == '/fi/sections/1/articles/1'
214 end
215
216 it 'does not change the section_article_path when given page option equals 1' do
217 section_article_path(@section, @article, :page => 1).should == '/sections/1/articles/1'
218 end
219
220 it 'appends the pages segments to section_article_path when given page option does not equal 1' do
221 section_article_path(@section, @article, :page => 2).should == '/sections/1/articles/1/pages/2'
222 end
223
224 it 'works for section_article_path with both a locale and page option' do
225 section_article_path(@section, @article, :locale => :fi, :page => 2).should == '/fi/sections/1/articles/1/pages/2'
226 end
227 end
228 end
229
230 describe 'when used with url_for and an ActivRecord instance' do
231 describe "a not nested resource" do
232 it 'prepends the current locale to section_path when it is not the default locale' do
233 I18n.locale = :de
234 url_for(@section).should == 'http://test.host/de/sections/1'
235 end
236
237 it 'does not change the section_path when no page option given' do
238 url_for(@section).should == 'http://test.host/sections/1'
239 end
240
241 it 'does not change the section_path when given page option equals 1' do
242 params = @params.update :id => @section, :page => 1
243 url_for(params).should == 'http://test.host/sections/1'
244 end
245
246 it 'appends the pages segments to section_path when given page option does not equal 1' do
247 params = @params.update :id => @section, :page => 2
248 url_for(params).should == 'http://test.host/sections/1/pages/2'
249 end
250
251 it 'works for section_path with both a locale and page option' do
252 params = @params.update :id => @section, :locale => :fi, :page => 2
253 url_for(params).should == 'http://test.host/fi/sections/1/pages/2'
254 end
255 end
256
257 describe "a nested resource" do
258 it 'prepends the current locale to section_article_path when it is not the default locale' do
259 I18n.locale = :de
260 url_for([@section, @article]).should == 'http://test.host/de/sections/1/articles/1'
261 end
262
263 it 'does not change the section_article_path when no page option given' do
264 url_for([@section, @article]).should == 'http://test.host/sections/1/articles/1'
265 end
266
267 it 'does not change the section_article_path when given page option equals 1' do
268 params = @article_params.update :section_id => @section, :id => @article, :page => 1
269 url_for(params).should == 'http://test.host/sections/1/articles/1'
270 end
271
272 it 'appends the pages segments to section_article_path when given page option does not equal 1' do
273 params = @article_params.update :section_id => @section, :id => @article, :page => 2
274 url_for(params).should == 'http://test.host/sections/1/articles/1/pages/2'
275 end
276
277 it 'works for section_article_path with both a locale and page option' do
278 params = @article_params.update :section_id => @section, :id => @article, :locale => :fi, :page => 2
279 url_for(params).should == 'http://test.host/fi/sections/1/articles/1/pages/2'
280 end
281 end
282 end
283end \ No newline at end of file
diff --git a/vendor/plugins/routing-filter/spec/recognition_spec.rb b/vendor/plugins/routing-filter/spec/recognition_spec.rb
new file mode 100644
index 0000000..11f0892
--- /dev/null
+++ b/vendor/plugins/routing-filter/spec/recognition_spec.rb
@@ -0,0 +1,76 @@
1require File.dirname(__FILE__) + '/spec_helper.rb'
2
3describe 'RoutingFilter', 'url recognition' do
4 include RoutingFilterHelpers
5
6 before :each do
7 RoutingFilter::Locale.default_locale = :en
8 I18n.default_locale = :en
9 I18n.locale = :en
10
11 @controller = instantiate_controller :locale => 'de', :id => 1
12 @set = draw_routes do |map|
13 map.filter 'locale'
14 map.filter 'pagination'
15
16 map.section 'sections/:id', :controller => 'sections', :action => "show"
17 map.article 'sections/:section_id/articles/:id', :controller => 'articles', :action => "show"
18 end
19
20 @section_params = {:controller => 'sections', :action => "show", :id => "1"}
21 @article_params = {:controller => 'articles', :action => "show", :section_id => "1", :id => "1"}
22 @locale_filter = @set.filters.first
23 end
24
25 def should_recognize_path(path, params)
26 @set.recognize_path(path, {}).should == params
27 end
28
29 def section_path(*args)
30 @controller.send :section_path, *args
31 end
32
33 def url_for(*args)
34 @controller.send :url_for, *args
35 end
36
37 it 'recognizes the path /de/sections/1 and sets the :locale param' do
38 should_recognize_path '/de/sections/1', @section_params.update(:locale => 'de')
39 end
40
41 it 'recognizes the path /sections/1/pages/1 and sets the :page param' do
42 should_recognize_path '/sections/1/pages/1', @section_params.update(:page => 1)
43 end
44
45 it 'recognizes the path /de/sections/1/pages/1 and sets the :locale param' do
46 should_recognize_path '/de/sections/1/pages/1', @section_params.update(:locale => 'de', :page => 1)
47 end
48
49 it 'recognizes the path /sections/1/articles/1 and sets the :locale param' do
50 should_recognize_path '/sections/1/articles/1', @article_params
51 end
52
53 it 'recognizes the path /de/sections/1/articles/1 and sets the :locale param' do
54 should_recognize_path '/de/sections/1/articles/1', @article_params.update(:locale => 'de')
55 end
56
57 it 'recognizes the path /de/sections/1/articles/1/pages/1 and sets the :locale param' do
58 should_recognize_path '/de/sections/1/articles/1/pages/1', @article_params.update(:locale => 'de', :page => 1)
59 end
60
61 it 'recognizes the path /sections/1 and does not set a :locale param' do
62 should_recognize_path '/sections/1', @section_params
63 end
64
65 it 'recognizes the path /sections/1 and does not set a :page param' do
66 should_recognize_path '/sections/1', @section_params
67 end
68
69 it 'recognizes the path /sections/1/articles/1 and does not set a :locale param' do
70 should_recognize_path '/sections/1/articles/1', @article_params
71 end
72
73 it 'recognizes the path /sections/1/articles/1 and does not set a :page param' do
74 should_recognize_path '/sections/1/articles/1', @article_params
75 end
76end \ No newline at end of file
diff --git a/vendor/plugins/routing-filter/spec/routing_filter_spec.rb b/vendor/plugins/routing-filter/spec/routing_filter_spec.rb
new file mode 100644
index 0000000..f919c29
--- /dev/null
+++ b/vendor/plugins/routing-filter/spec/routing_filter_spec.rb
@@ -0,0 +1,70 @@
1require File.dirname(__FILE__) + '/spec_helper.rb'
2
3describe 'RoutingFilter' do
4 include RoutingFilterHelpers
5
6 before :each do
7 @controller = instantiate_controller :locale => 'de', :section_id => 1
8 @set = draw_routes do |map|
9 map.section 'sections/:section_id', :controller => 'sections', :action => "show"
10 map.filter 'locale'
11 map.filter 'pagination'
12 end
13 @locale_filter = @set.filters.first
14 @pagination_filter = @set.filters.last
15 end
16
17 def recognize_path(path = '/de/sections/1', options = {})
18 @set.recognize_path path, options
19 end
20
21 def url_for(options)
22 @controller.send :url_for, options
23 end
24
25 def section_path(*args)
26 @controller.send :section_path, *args
27 end
28
29 it 'installs filters to the route set' do
30 @locale_filter.should be_instance_of(RoutingFilter::Locale)
31 @pagination_filter.should be_instance_of(RoutingFilter::Pagination)
32 end
33
34 it 'calls the first filter for route recognition' do
35 @locale_filter.should_receive(:around_recognize).and_return :foo => :bar
36 recognize_path.should == {:foo => :bar}
37 end
38
39 it 'calls the second filter for route recognition' do
40 @pagination_filter.should_receive(:around_recognize).and_return :foo => :bar
41 recognize_path.should == {:foo => :bar}
42 end
43
44 it 'calls the first filter for url generation' do
45 @locale_filter.should_receive(:around_generate).and_return '/sections/1'
46 url_for :controller => 'sections', :action => 'show', :section_id => 1
47 end
48
49 it 'calls the second filter for url generation' do
50 @pagination_filter.should_receive(:around_generate).and_return '/sections/1'
51 url_for :controller => 'sections', :action => 'show', :section_id => 1
52 end
53
54 it 'calls the first filter for named route url_helper' do
55 @locale_filter.should_receive(:around_generate).and_return '/sections/1'
56 section_path :section_id => 1
57 end
58
59 it 'calls the filter for named route url_helper with "optimized" generation blocks' do
60 # at_least(1) since the inline code comments in ActionController::Routing::RouteSet::NamedRouteCollection#define_url_helper also call us (as of http://github.com/rails/rails/commit/a2270ef2594b97891994848138614657363f2806)
61 @locale_filter.should_receive(:around_generate).at_least(1).and_return '/sections/1'
62 section_path 1
63 end
64
65 it 'calls the filter for named route polymorphic_path' do
66 # at_least(1) since the inline code comments in ActionController::Routing::RouteSet::NamedRouteCollection#define_url_helper also call us (as of http://github.com/rails/rails/commit/a2270ef2594b97891994848138614657363f2806)
67 @locale_filter.should_receive(:around_generate).at_least(1).and_return '/sections/1'
68 section_path Section.new
69 end
70end \ No newline at end of file
diff --git a/vendor/plugins/routing-filter/spec/spec.opts b/vendor/plugins/routing-filter/spec/spec.opts
new file mode 100644
index 0000000..391705b
--- /dev/null
+++ b/vendor/plugins/routing-filter/spec/spec.opts
@@ -0,0 +1,4 @@
1--colour
2--format progress
3--loadby mtime
4--reverse
diff --git a/vendor/plugins/routing-filter/spec/spec_helper.rb b/vendor/plugins/routing-filter/spec/spec_helper.rb
new file mode 100644
index 0000000..a92a3d9
--- /dev/null
+++ b/vendor/plugins/routing-filter/spec/spec_helper.rb
@@ -0,0 +1,49 @@
1$: << File.dirname(__FILE__)
2$: << File.dirname(__FILE__) + '/../lib/'
3$: << File.dirname(__FILE__) + '/../vendor/rails/actionpack/lib'
4$: << File.dirname(__FILE__) + '/../vendor/rails/activesupport/lib'
5
6require 'action_controller'
7require 'action_controller/test_process'
8require 'active_support/vendor'
9
10require 'routing_filter'
11require 'routing_filter/locale'
12require 'routing_filter/pagination'
13
14class Site
15end
16
17class Section
18 def id; 1 end
19 alias :to_param :id
20
21 def type; 'Section' end
22
23 def path; 'section' end
24end
25
26class Article
27 def to_param; 1 end
28end
29
30module RoutingFilterHelpers
31 def draw_routes(&block)
32 set = returning ActionController::Routing::RouteSet.new do |set|
33 class << set; def clear!; end; end
34 set.draw &block
35 silence_warnings{ ActionController::Routing.const_set 'Routes', set }
36 end
37 set
38 end
39
40 def instantiate_controller(params)
41 returning ActionController::Base.new do |controller|
42 request = ActionController::TestRequest.new
43 url = ActionController::UrlRewriter.new(request, params)
44 controller.stub!(:request).and_return request
45 controller.instance_variable_set :@url, url
46 controller
47 end
48 end
49end \ No newline at end of file