routing-filter 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -0
- data/Gemfile.lock +82 -0
- data/MIT-LICENSE +20 -0
- data/README.markdown +190 -0
- data/lib/routing-filter.rb +1 -0
- data/lib/routing_filter.rb +27 -0
- data/lib/routing_filter/adapters/rails_2.rb +69 -0
- data/lib/routing_filter/adapters/rails_3.rb +71 -0
- data/lib/routing_filter/chain.rb +22 -0
- data/lib/routing_filter/filter.rb +37 -0
- data/lib/routing_filter/filters/extension.rb +76 -0
- data/lib/routing_filter/filters/locale.rb +79 -0
- data/lib/routing_filter/filters/pagination.rb +47 -0
- data/lib/routing_filter/filters/uuid.rb +40 -0
- data/lib/routing_filter/version.rb +3 -0
- data/test/all.rb +1 -0
- data/test/filters/all_filters/generation.rb +42 -0
- data/test/filters/all_filters/recognition.rb +92 -0
- data/test/filters/all_filters_test.rb +25 -0
- data/test/filters/extension_test.rb +54 -0
- data/test/filters/locale_test.rb +69 -0
- data/test/filters/pagination_test.rb +29 -0
- data/test/filters/uuid_test.rb +40 -0
- data/test/rails_3_test.rb +73 -0
- data/test/routes_test.rb +31 -0
- data/test/routing_filter_test.rb +47 -0
- data/test/test_helper.rb +42 -0
- metadata +31 -5
@@ -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,73 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
if ActionPack::VERSION::MAJOR == 3
|
4
|
+
require "rails"
|
5
|
+
require 'rack/test'
|
6
|
+
|
7
|
+
class Rails3Test < Test::Unit::TestCase
|
8
|
+
include ::Rack::Test::Methods
|
9
|
+
|
10
|
+
I18n.available_locales = [:en, :de]
|
11
|
+
|
12
|
+
APP = Class.new(Rails::Application).tap do |app|
|
13
|
+
app.config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"
|
14
|
+
app.config.session_store :cookie_store, :key => "_myapp_session"
|
15
|
+
app.config.active_support.deprecation = :log
|
16
|
+
app.routes.draw do
|
17
|
+
match "/" => "rails3_test/tests#index"
|
18
|
+
filter :uuid, :pagination ,:locale, :extension
|
19
|
+
end
|
20
|
+
app.initialize!
|
21
|
+
end
|
22
|
+
|
23
|
+
class TestsController < ActionController::Base
|
24
|
+
include Rails.application.routes.url_helpers
|
25
|
+
|
26
|
+
def index
|
27
|
+
url = url_for(params.merge(:only_path => true))
|
28
|
+
render :text => params.merge(:url => url).inspect
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def app
|
33
|
+
APP
|
34
|
+
end
|
35
|
+
|
36
|
+
def params
|
37
|
+
last_response.status == 200 ? eval(last_response.body).symbolize_keys : {}
|
38
|
+
end
|
39
|
+
|
40
|
+
test "get to /" do
|
41
|
+
get '/'
|
42
|
+
assert_nil params[:locale]
|
43
|
+
assert_nil params[:page]
|
44
|
+
assert_nil params[:uuid]
|
45
|
+
assert_equal '/en.html', params[:url]
|
46
|
+
end
|
47
|
+
|
48
|
+
test "get to /de" do
|
49
|
+
get '/de'
|
50
|
+
assert_equal 'de', params[:locale]
|
51
|
+
assert_nil params[:page]
|
52
|
+
assert_nil params[:uuid]
|
53
|
+
assert_equal '/de.html', params[:url]
|
54
|
+
end
|
55
|
+
|
56
|
+
test "get to /page/2" do
|
57
|
+
get '/page/2'
|
58
|
+
assert_nil params[:locale]
|
59
|
+
assert_equal 2, params[:page]
|
60
|
+
assert_nil params[:uuid]
|
61
|
+
assert_equal '/en/page/2.html', params[:url]
|
62
|
+
end
|
63
|
+
|
64
|
+
test "get to /:uuid" do
|
65
|
+
uuid = 'd00fbbd1-82b6-4c1a-a57d-098d529d6854'
|
66
|
+
get "/#{uuid}"
|
67
|
+
assert_nil params[:locale]
|
68
|
+
assert_nil params[:page]
|
69
|
+
assert_equal uuid, params[:uuid]
|
70
|
+
assert_equal "/en/#{uuid}.html", params[:url]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/test/routes_test.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class RoutesTest < Test::Unit::TestCase
|
4
|
+
class RoutingFilter::Test < Filter
|
5
|
+
def around_recognize(path, env, &block)
|
6
|
+
'recognized'
|
7
|
+
end
|
8
|
+
|
9
|
+
def around_generate(*args, &block)
|
10
|
+
'generated'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
attr_reader :routes
|
15
|
+
|
16
|
+
def setup
|
17
|
+
@routes = draw_routes { |set| set.filter :test }
|
18
|
+
end
|
19
|
+
|
20
|
+
test "routes.filter instantiates and registers a filter" do
|
21
|
+
assert routes.instance_variable_get(:@set).filters.first.is_a?(RoutingFilter::Test)
|
22
|
+
end
|
23
|
+
|
24
|
+
# test "filter.around_recognize is being called" do
|
25
|
+
# assert_equal 'recognized', routes.recognize_path('/')
|
26
|
+
# end
|
27
|
+
|
28
|
+
test "filter.around_generate is being called" do
|
29
|
+
assert_equal 'generated', routes.generate({})
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
include RoutingFilter
|
4
|
+
|
5
|
+
class RoutingFilterTest < Test::Unit::TestCase
|
6
|
+
class FooFilter < Filter
|
7
|
+
attr_reader :name
|
8
|
+
|
9
|
+
def initialize(name)
|
10
|
+
@name = name
|
11
|
+
end
|
12
|
+
|
13
|
+
def foo(log, &block)
|
14
|
+
log << name
|
15
|
+
yield
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
attr_reader :chain
|
20
|
+
|
21
|
+
def setup
|
22
|
+
@chain = Chain.new
|
23
|
+
@chain.unshift FooFilter.new('custom filter')
|
24
|
+
@chain.unshift FooFilter.new('common filter')
|
25
|
+
end
|
26
|
+
|
27
|
+
test "filter.previous is nil for the first filter in the chain" do
|
28
|
+
assert_nil chain.first.previous
|
29
|
+
end
|
30
|
+
|
31
|
+
test "filter.previous returns the previous filter in the chain" do
|
32
|
+
assert_equal chain.first, chain.last.previous
|
33
|
+
end
|
34
|
+
|
35
|
+
test "filter.next is nil for the last filter in the chain" do
|
36
|
+
assert_nil chain.last.next
|
37
|
+
end
|
38
|
+
|
39
|
+
test "filter.next returns the next filter in the chain" do
|
40
|
+
assert_equal chain.last, chain.first.next
|
41
|
+
end
|
42
|
+
|
43
|
+
test "chain.run calls the given method on registered filters in reverse order" do
|
44
|
+
log = []
|
45
|
+
assert_equal 'common filter, custom filter, finalizer', chain.run(:foo, log, &lambda { log << 'finalizer' }).join(', ')
|
46
|
+
end
|
47
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
ENV['RAILS_ENV'] = 'test'
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
# gem 'actionpack', '~> 2.3' if ENV['RAILS_2']
|
5
|
+
require 'test/unit'
|
6
|
+
require 'bundler/setup'
|
7
|
+
|
8
|
+
require 'i18n'
|
9
|
+
require 'action_pack'
|
10
|
+
require 'active_support'
|
11
|
+
require 'action_controller'
|
12
|
+
require 'active_support/core_ext/enumerable.rb'
|
13
|
+
require 'test_declarative'
|
14
|
+
require 'routing_filter'
|
15
|
+
|
16
|
+
include RoutingFilter
|
17
|
+
|
18
|
+
class SomeController < ActionController::Base
|
19
|
+
end
|
20
|
+
|
21
|
+
class Test::Unit::TestCase
|
22
|
+
def draw_routes(&block)
|
23
|
+
normalized_block = rails_2? ? lambda { |set| set.instance_eval(&block) } : block
|
24
|
+
klass = rails_2? ? ActionController::Routing::RouteSet : ActionDispatch::Routing::RouteSet
|
25
|
+
klass.new.tap { |set| set.draw(&normalized_block) }
|
26
|
+
end
|
27
|
+
|
28
|
+
def rails_2?
|
29
|
+
ActionPack::VERSION::MAJOR == 2
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
if ActionPack::VERSION::MAJOR == 2
|
34
|
+
ActionController::Routing::RouteSet::Mapper.class_eval do
|
35
|
+
def match(pattern, options)
|
36
|
+
pattern.gsub!('(.:format)', '.:format')
|
37
|
+
controller, action = options.delete(:to).split('#')
|
38
|
+
options.merge!(:controller => controller, :action => action)
|
39
|
+
connect(pattern, options)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: routing-filter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 2
|
10
|
+
version: 0.2.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Sven Fuchs
|
@@ -82,8 +82,34 @@ extensions: []
|
|
82
82
|
|
83
83
|
extra_rdoc_files: []
|
84
84
|
|
85
|
-
files:
|
86
|
-
|
85
|
+
files:
|
86
|
+
- lib/routing-filter.rb
|
87
|
+
- lib/routing_filter/adapters/rails_2.rb
|
88
|
+
- lib/routing_filter/adapters/rails_3.rb
|
89
|
+
- lib/routing_filter/chain.rb
|
90
|
+
- lib/routing_filter/filter.rb
|
91
|
+
- lib/routing_filter/filters/extension.rb
|
92
|
+
- lib/routing_filter/filters/locale.rb
|
93
|
+
- lib/routing_filter/filters/pagination.rb
|
94
|
+
- lib/routing_filter/filters/uuid.rb
|
95
|
+
- lib/routing_filter/version.rb
|
96
|
+
- lib/routing_filter.rb
|
97
|
+
- test/all.rb
|
98
|
+
- test/filters/all_filters/generation.rb
|
99
|
+
- test/filters/all_filters/recognition.rb
|
100
|
+
- test/filters/all_filters_test.rb
|
101
|
+
- test/filters/extension_test.rb
|
102
|
+
- test/filters/locale_test.rb
|
103
|
+
- test/filters/pagination_test.rb
|
104
|
+
- test/filters/uuid_test.rb
|
105
|
+
- test/rails_3_test.rb
|
106
|
+
- test/routes_test.rb
|
107
|
+
- test/routing_filter_test.rb
|
108
|
+
- test/test_helper.rb
|
109
|
+
- Gemfile
|
110
|
+
- Gemfile.lock
|
111
|
+
- MIT-LICENSE
|
112
|
+
- README.markdown
|
87
113
|
has_rdoc: true
|
88
114
|
homepage: http://github.com/svenfuchs/routing-filter
|
89
115
|
licenses: []
|