vigetlabs-routing-filter 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,40 @@
1
+ # The Uuid filter extracts an UUID segment from the beginning of the recognized
2
+ # path and exposes the page parameter as params[:page]. When a path is generated
3
+ # the filter adds the segments to the path accordingly if the page parameter is
4
+ # passed to the url helper.
5
+ #
6
+ # incoming url: /d00fbbd1-82b6-4c1a-a57d-098d529d6854/product/1
7
+ # filtered url: /product/1
8
+ # params: params[:uuid] = 'd00fbbd1-82b6-4c1a-a57d-098d529d6854'
9
+ #
10
+ # You can install the filter like this:
11
+ #
12
+ # # in config/routes.rb
13
+ # Rails.application.routes.draw do
14
+ # filter :uuid
15
+ # end
16
+ #
17
+ # To make your named_route helpers or url_for add the uuid segment you can use:
18
+ #
19
+ # product_path(:uuid => uuid)
20
+ # url_for(product, :uuid => uuid)
21
+
22
+ module RoutingFilter
23
+ class Uuid < Filter
24
+ UUID_SEGMENT = %r(^/?([a-z\d]{8}\-[a-z\d]{4}\-[a-z\d]{4}\-[a-z\d]{4}\-[a-z\d]{12})(/)?)
25
+
26
+ def around_recognize(path, env, &block)
27
+ uuid = extract_segment!(UUID_SEGMENT, path)
28
+ yield.tap do |params|
29
+ params[:uuid] = uuid if uuid
30
+ end
31
+ end
32
+
33
+ def around_generate(params, &block)
34
+ uuid = params.delete(:uuid)
35
+ yield.tap do |result|
36
+ prepend_segment!(result, uuid) if uuid
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module RoutingFilter
2
+ VERSION = '0.2.4'
3
+ end
@@ -0,0 +1 @@
1
+ Dir[File.expand_path('../**/*_test.rb', __FILE__)].each { |file| require file }
@@ -0,0 +1,42 @@
1
+ module Generation
2
+ test 'generates the path /some.html (extension)' do
3
+ params = self.params
4
+ assert_equal '/some.html', routes.generate(params)
5
+ end
6
+
7
+ # extension with any
8
+
9
+ test 'generates the path /de/some (extension, locale)' do
10
+ params = self.params.merge(:locale => 'de')
11
+ assert_equal '/de/some.html', routes.generate(params)
12
+ end
13
+
14
+ test 'generates the path /some/page/2 (extension, pagination)' do
15
+ params = self.params.merge(:page => 2)
16
+ assert_equal '/some/page/2.html', routes.generate(params)
17
+ end
18
+
19
+ test 'generates the path /:uuid/some (extension, uuid)' do
20
+ params = self.params.merge(:uuid => uuid)
21
+ assert_equal "/#{uuid}/some.html", routes.generate(params)
22
+ end
23
+
24
+ # extension, locale with any
25
+
26
+ test 'generates the path /de/some/page/2 (extension, locale, pagination)' do
27
+ params = self.params.merge(:locale => 'de', :page => 2)
28
+ assert_equal '/de/some/page/2.html', routes.generate(params)
29
+ end
30
+
31
+ test 'generates the path /de/:uuid/some (extension, locale, uuid)' do
32
+ params = self.params.merge(:locale => 'de', :uuid => uuid)
33
+ assert_equal "/de/#{uuid}/some.html", routes.generate(params)
34
+ end
35
+
36
+ # all
37
+
38
+ test 'generates the path /de/some/page/2 (extension, pagination, uuid)' do
39
+ params = self.params.merge(:locale => 'de', :page => 2, :uuid => uuid)
40
+ assert_equal "/de/#{uuid}/some/page/2.html", routes.generate(params)
41
+ end
42
+ end
@@ -0,0 +1,92 @@
1
+ module Recognition
2
+ # 1 filter
3
+
4
+ test 'recognizes the path /some.html (extension)' do
5
+ params = self.params
6
+ assert_equal params, routes.recognize_path('/some.html')
7
+ end
8
+
9
+ test 'recognizes the path /de/some (locale)' do
10
+ params = self.params.merge(:locale => 'de')
11
+ assert_equal params, routes.recognize_path('/de/some')
12
+ end
13
+
14
+ test 'recognizes the path /some/page/2 (pagination)' do
15
+ params = self.params.merge(:page => 2)
16
+ assert_equal params, routes.recognize_path('/some/page/2')
17
+ end
18
+
19
+ test 'recognizes the path /:uuid/some (uuid)' do
20
+ params = self.params.merge(:uuid => uuid)
21
+ assert_equal params, routes.recognize_path("/#{uuid}/some")
22
+ end
23
+
24
+ # extension with any
25
+
26
+ test 'recognizes the path /de/some.html (extension, locale)' do
27
+ params = self.params.merge(:locale => 'de')
28
+ assert_equal params, routes.recognize_path('/de/some.html')
29
+ end
30
+
31
+ test 'recognizes the path /some/page/2.html (extension, pagination)' do
32
+ params = self.params.merge(:page => 2)
33
+ assert_equal params, routes.recognize_path('/some/page/2.html')
34
+ end
35
+
36
+ test 'recognizes the path /:uuid/some.html (extension, uuid)' do
37
+ params = self.params.merge(:uuid => uuid)
38
+ assert_equal params, routes.recognize_path("/#{uuid}/some.html")
39
+ end
40
+
41
+ # locale with any
42
+
43
+ test 'recognizes the path /de/some/page/2 (locale, pagination)' do
44
+ params = self.params.merge(:locale => 'de', :page => 2)
45
+ assert_equal params, routes.recognize_path('/de/some/page/2')
46
+ end
47
+
48
+ test 'recognizes the path /de/:uuid/some (locale, uuid)' do
49
+ params = self.params.merge(:locale => 'de', :uuid => uuid)
50
+ assert_equal params, routes.recognize_path("/de/#{uuid}/some")
51
+ end
52
+
53
+ # pagination with any
54
+
55
+ test 'recognizes the path /:uuid/some/page/2 (pagination, uuid)' do
56
+ params = self.params.merge(:page => 2, :uuid => uuid)
57
+ assert_equal params, routes.recognize_path("/#{uuid}/some/page/2")
58
+ end
59
+
60
+ # extension, locale with any
61
+
62
+ test 'recognizes the path /de/some/page/2.html (extension, locale, pagination)' do
63
+ params = self.params.merge(:locale => 'de', :page => 2)
64
+ assert_equal params, routes.recognize_path("/de/some/page/2.html")
65
+ end
66
+
67
+ test 'recognizes the path /de/:uuid/some.html (extension, locale, uuid)' do
68
+ params = self.params.merge(:locale => 'de', :uuid => uuid)
69
+ assert_equal params, routes.recognize_path("/de/#{uuid}/some.html")
70
+ end
71
+
72
+ # extension, pagination with any
73
+
74
+ test 'recognizes the path /some/page/2.html (extension, pagination, uuid)' do
75
+ params = self.params.merge(:page => 2, :uuid => uuid)
76
+ assert_equal params, routes.recognize_path("/#{uuid}/some/page/2.html")
77
+ end
78
+
79
+ # locale, pagination with any
80
+
81
+ test 'recognizes the path /de/some/page/2 (locale, pagination, uuid)' do
82
+ params = self.params.merge(:locale => 'de', :page => 2, :uuid => uuid)
83
+ assert_equal params, routes.recognize_path("/de/#{uuid}/some/page/2")
84
+ end
85
+
86
+ # all
87
+
88
+ test 'recognizes the path /de/:uuid/some/page/2.html (extension, locale, pagination, uuid)' do
89
+ params = self.params.merge(:locale => 'de', :page => 2, :uuid => uuid)
90
+ assert_equal params, routes.recognize_path("/de/#{uuid}/some/page/2.html")
91
+ end
92
+ end
@@ -0,0 +1,25 @@
1
+ require 'test_helper'
2
+ require 'filters/all_filters/generation'
3
+ require 'filters/all_filters/recognition'
4
+
5
+ class AllFiltersTest < Test::Unit::TestCase
6
+ attr_reader :routes, :params, :uuid
7
+
8
+ def setup
9
+ I18n.locale = nil
10
+ I18n.default_locale = :en
11
+ I18n.available_locales = %w(de en)
12
+
13
+ RoutingFilter::Locale.include_default_locale = false
14
+
15
+ @params = { :controller => 'some', :action => 'index' }
16
+ @uuid = 'd00fbbd1-82b6-4c1a-a57d-098d529d6854'
17
+
18
+ @routes = draw_routes do
19
+ filter :uuid, :pagination ,:locale, :extension
20
+ match 'some', :to => 'some#index'
21
+ end
22
+ end
23
+
24
+ include Recognition, Generation
25
+ end
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+
3
+ class ForceExtensionTest < Test::Unit::TestCase
4
+ attr_reader :routes, :params
5
+
6
+ def setup
7
+ @routes = draw_routes do
8
+ filter :extension, :exclude => %r(^/(admin|$))
9
+ match '/', :to => 'some#index'
10
+ match 'some/:id(.:format)', :to => 'some#show'
11
+ match '/admin/some/new', :to => 'some#new'
12
+ end
13
+ @params = { :controller => 'some', :action => 'show', :id => '1' }
14
+ end
15
+
16
+ test 'recognizes the path some/1.html and strips the extension' do
17
+ assert_nil routes.recognize_path('/some/1.html')[:format]
18
+ end
19
+
20
+ test 'recognizes the path some/1.xml but does not strip the extension' do
21
+ assert 'xml', routes.recognize_path('/some/1.xml')[:format]
22
+ end
23
+
24
+ test 'appends the extension .html to the generated path' do
25
+ assert_equal '/some/1.html', routes.generate(params)
26
+ end
27
+
28
+ test 'does not touch existing extensions in generated paths' do
29
+ assert_equal '/some/1.xml', routes.generate(params.merge(:format => 'xml'))
30
+ end
31
+
32
+ test 'does not touch url query params in generated paths' do
33
+ assert_equal '/some/1.html?foo=bar', routes.generate(params.merge(:foo => 'bar'))
34
+ end
35
+
36
+ test 'excludes / by default' do
37
+ assert_equal '/', routes.generate(:controller => 'some', :action => 'index')
38
+ end
39
+
40
+ test 'excludes / by default (with url query params)' do
41
+ assert_equal '/?foo=bar', routes.generate(:controller => 'some', :action => 'index', :foo => 'bar')
42
+ end
43
+
44
+ test 'excludes with custom regexp' do
45
+ assert_equal '/admin/some/new', routes.generate(:controller => 'some', :action => 'new')
46
+ end
47
+
48
+ # TODO - why would anyone want to have this?
49
+ #
50
+ # test 'does not exclude / when :exclude => false was passed' do
51
+ # routes.filters.first.instance_variable_set(:@exclude, false)
52
+ # assert_equal '/.html', routes.generate(:controller => 'some', :action => 'index')
53
+ # end
54
+ end
@@ -0,0 +1,69 @@
1
+ require 'test_helper'
2
+
3
+ class LocaleTest < Test::Unit::TestCase
4
+ attr_reader :routes, :show_params, :index_params
5
+
6
+ def setup
7
+ I18n.locale = nil
8
+ I18n.default_locale = :en
9
+ I18n.available_locales = %w(de en)
10
+
11
+ RoutingFilter::Locale.include_default_locale = true
12
+
13
+ @index_params = { :controller => 'some', :action => 'index' }
14
+ @show_params = { :controller => 'some', :action => 'show', :id => '1' }
15
+
16
+ @routes = draw_routes do
17
+ filter :locale
18
+ match 'products/:id', :to => 'some#show'
19
+ match '/', :to => 'some#index'
20
+ end
21
+ end
22
+
23
+ test 'recognizes the path /en' do
24
+ assert_equal index_params.merge(:locale => 'en'), routes.recognize_path('/en')
25
+ end
26
+
27
+ test 'recognizes the path /en/' do
28
+ assert_equal index_params.merge(:locale => 'en'), routes.recognize_path('/en/')
29
+ end
30
+
31
+ test 'recognizes the path /en/products/1' do
32
+ assert_equal show_params.merge(:locale => 'en'), routes.recognize_path('/en/products/1')
33
+ end
34
+
35
+ test 'recognizes the path /de/products/1' do
36
+ assert_equal show_params.merge(:locale => 'de'), routes.recognize_path('/de/products/1')
37
+ end
38
+
39
+
40
+ test 'prepends the segments /:locale to the generated path / if the current locale is not the default locale' do
41
+ I18n.locale = 'de'
42
+ assert_equal '/de', routes.generate(index_params)
43
+ end
44
+
45
+ test 'prepends the segments /:locale to the generated path /products/1 if the current locale is not the default locale' do
46
+ I18n.locale = 'de'
47
+ assert_equal '/de/products/1', routes.generate(show_params)
48
+ end
49
+
50
+ test 'prepends the segments /:locale to the generated path if it was passed as a param' do
51
+ assert_equal '/de/products/1', routes.generate(show_params.merge(:locale => 'de'))
52
+ end
53
+
54
+ test 'prepends the segments /:locale if the given locale is the default_locale and include_default_locale is true' do
55
+ assert RoutingFilter::Locale.include_default_locale?
56
+ assert_equal '/en/products/1', routes.generate(show_params.merge(:locale => 'en'))
57
+ end
58
+
59
+ test 'does not prepend the segments /:locale if the current locale is the default_locale and include_default_locale is false' do
60
+ I18n.locale = 'en'
61
+ RoutingFilter::Locale.include_default_locale = false
62
+ assert_equal '/products/1', routes.generate(show_params)
63
+ end
64
+
65
+ test 'does not prepend the segments /:locale if the given locale is the default_locale and include_default_locale is false' do
66
+ RoutingFilter::Locale.include_default_locale = false
67
+ assert_equal '/products/1', routes.generate(show_params.merge(:locale => I18n.default_locale))
68
+ end
69
+ end
@@ -0,0 +1,29 @@
1
+ require 'test_helper'
2
+
3
+ class PaginationTest < Test::Unit::TestCase
4
+ attr_reader :routes, :params
5
+
6
+ def setup
7
+ @routes = draw_routes do
8
+ filter :pagination
9
+ match 'some', :to => 'some#index'
10
+ end
11
+ @params = { :controller => 'some', :action => 'index', :page => 2 }
12
+ end
13
+
14
+ test 'recognizes the path some/page/2' do
15
+ assert_equal params, routes.recognize_path('/some/page/2')
16
+ end
17
+
18
+ test 'appends the segments /page/:page to the generated path if the passed :page param does not equal 1' do
19
+ assert_equal '/some/page/2', routes.generate(params)
20
+ end
21
+
22
+ test 'does not append anything to the generated path if the passed :page param equals 1' do
23
+ assert_equal '/some', routes.generate(params.merge(:page => 1))
24
+ end
25
+
26
+ test 'appends the segments /page/:page to the generated path but respects url query params' do
27
+ assert_equal '/some/page/2?foo=bar', routes.generate(params.merge(:foo => 'bar'))
28
+ end
29
+ end
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ class UuidTest < Test::Unit::TestCase
4
+ attr_reader :routes, :uuid, :params
5
+
6
+ def setup
7
+ @routes = draw_routes do
8
+ filter :uuid
9
+ match 'some/:id', :to => 'some#show'
10
+ end
11
+ @uuid = 'd00fbbd1-82b6-4c1a-a57d-098d529d6854'
12
+ @params = { :controller => 'some', :action => 'show', :id => '1', :uuid => uuid }
13
+ end
14
+
15
+ test 'recognizes the path :uuid/product/1' do
16
+ assert_equal params, routes.recognize_path("/#{uuid}/some/1")
17
+ end
18
+
19
+ test 'prepends the :uuid segment to the generated path if passed as a param' do
20
+ assert_equal "/#{uuid}/some/1", routes.generate(params)
21
+ end
22
+
23
+ test 'matches uuid segments' do
24
+ pattern = Uuid::UUID_SEGMENT
25
+ uuids = %w(
26
+ d00fbbd1-82b6-4c1a-a57d-098d529d6854 cdb33760-94da-11df-981c-0800200c9a66
27
+ 0c65a6ec-6491-4316-a137-0021cf4e6471 cbbd44c3-c195-48e5-be04-3cc8a6578f51
28
+ )
29
+ uuids.each { |uuid| assert pattern.match("/#{uuid}/"), "does not match /#{uuid}/ but should" }
30
+ end
31
+
32
+ test 'does not match non-uuid segments' do
33
+ pattern = Uuid::UUID_SEGMENT
34
+ uuids = %w(
35
+ !aaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa aaaa-aaaaaaaa-aaaa-aaaa-aaaaaaaaaaaa
36
+ aaaaaaaa_aaaa_aaaa_aaaa_aaaaaaaaaaaa aaaaaaaa-aaaa-aaaa-aaaaaaaaaaaa
37
+ )
38
+ uuids.each { |uuid| assert !pattern.match("/#{uuid}/"), "matches /#{uuid}/ but shouldn't" }
39
+ end
40
+ end
@@ -0,0 +1,92 @@
1
+ require 'test_helper'
2
+ require "test_adapters/rails_#{ActionPack::VERSION::MAJOR}"
3
+
4
+ class RailsTest < Test::Unit::TestCase
5
+ include TestRailsAdapter
6
+
7
+ I18n.available_locales = [:en, :de]
8
+
9
+ class TestsController < ActionController::Base
10
+ include Rails.application.routes.url_helpers if defined?(Rails)
11
+
12
+ def index
13
+ url = url_for(params.merge(:only_path => true))
14
+ render :text => params.merge(:url => url).inspect
15
+ end
16
+
17
+ def show
18
+ url = foo_path(params)
19
+ render :text => params.merge(:url => url).inspect
20
+ end
21
+ end
22
+
23
+ def params
24
+ response.status.to_s.include?('200') ? eval(response.body).symbolize_keys : {}
25
+ end
26
+
27
+ test "get to /" do
28
+ get '/'
29
+ assert_nil params[:locale]
30
+ assert_nil params[:page]
31
+ assert_nil params[:uuid]
32
+ assert_equal '/en.html', params[:url]
33
+ end
34
+
35
+ test "get to /foo/1" do
36
+ get '/foo/1'
37
+ assert_nil params[:locale]
38
+ assert_nil params[:page]
39
+ assert_nil params[:uuid]
40
+ assert_equal '/en/foo/1.html', params[:url]
41
+ end
42
+
43
+ test "get to /de" do
44
+ get '/de'
45
+ assert_equal 'de', params[:locale]
46
+ assert_nil params[:page]
47
+ assert_nil params[:uuid]
48
+ assert_equal '/de.html', params[:url]
49
+ end
50
+
51
+ test "get to /de/foo/1" do
52
+ get '/de/foo/1'
53
+ assert_equal 'de', params[:locale]
54
+ assert_nil params[:page]
55
+ assert_nil params[:uuid]
56
+ assert_equal '/de/foo/1.html', params[:url]
57
+ end
58
+
59
+ test "get to /page/2" do
60
+ get '/page/2'
61
+ assert_nil params[:locale]
62
+ assert_equal 2, params[:page]
63
+ assert_nil params[:uuid]
64
+ assert_equal '/en/page/2.html', params[:url]
65
+ end
66
+
67
+ test "get to /foo/1/page/2" do
68
+ get '/foo/1/page/2'
69
+ assert_nil params[:locale]
70
+ assert_equal 2, params[:page]
71
+ assert_nil params[:uuid]
72
+ assert_equal '/en/foo/1/page/2.html', params[:url]
73
+ end
74
+
75
+ test "get to /:uuid" do
76
+ uuid = 'd00fbbd1-82b6-4c1a-a57d-098d529d6854'
77
+ get "/#{uuid}"
78
+ assert_nil params[:locale]
79
+ assert_nil params[:page]
80
+ assert_equal uuid, params[:uuid]
81
+ assert_equal "/en/#{uuid}.html", params[:url]
82
+ end
83
+
84
+ test "get to /foo/1/:uuid" do
85
+ uuid = 'd00fbbd1-82b6-4c1a-a57d-098d529d6854'
86
+ get "/#{uuid}/foo/1"
87
+ assert_nil params[:locale]
88
+ assert_nil params[:page]
89
+ assert_equal uuid, params[:uuid]
90
+ assert_equal "/en/#{uuid}/foo/1.html", params[:url]
91
+ end
92
+ end