routing-filter 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,2 +1,8 @@
1
1
  source :rubygems
2
- gemspec
2
+
3
+ group :test do
4
+ gem 'rails', '~> 3.0'
5
+ gem 'i18n'
6
+ gem 'test_declarative'
7
+ gem 'ruby-debug'
8
+ end
data/Gemfile.lock CHANGED
@@ -1,9 +1,3 @@
1
- PATH
2
- remote: .
3
- specs:
4
- routing-filter (0.2.0)
5
- actionpack
6
-
7
1
  GEM
8
2
  remote: http://rubygems.org/
9
3
  specs:
@@ -36,9 +30,11 @@ GEM
36
30
  activesupport (3.0.3)
37
31
  arel (2.0.6)
38
32
  builder (2.1.2)
33
+ columnize (0.3.2)
39
34
  erubis (2.6.6)
40
35
  abstract (>= 1.0.0)
41
36
  i18n (0.5.0)
37
+ linecache (0.43)
42
38
  mail (2.2.12)
43
39
  activesupport (>= 2.3.6)
44
40
  i18n (>= 0.4.0)
@@ -65,7 +61,12 @@ GEM
65
61
  rake (>= 0.8.7)
66
62
  thor (~> 0.14.4)
67
63
  rake (0.8.7)
68
- test_declarative (0.0.4)
64
+ ruby-debug (0.10.4)
65
+ columnize (>= 0.1)
66
+ ruby-debug-base (~> 0.10.4.0)
67
+ ruby-debug-base (0.10.4)
68
+ linecache (>= 0.3)
69
+ test_declarative (0.0.5)
69
70
  thor (0.14.6)
70
71
  treetop (1.4.9)
71
72
  polyglot (>= 0.3.1)
@@ -75,8 +76,7 @@ PLATFORMS
75
76
  ruby
76
77
 
77
78
  DEPENDENCIES
78
- actionpack
79
79
  i18n
80
- rails
81
- routing-filter!
80
+ rails (~> 3.0)
81
+ ruby-debug
82
82
  test_declarative
data/README.markdown CHANGED
@@ -59,6 +59,20 @@ Filters can also accept options:
59
59
  filter :extension, :exclude => %r(^admin/)
60
60
  end
61
61
 
62
+ ## Running the tests
63
+
64
+ There are two Gemfiles in the `ci` directory in order to run the tests against different dependencies. The Rails 3 Gemfile is symlinked to the root folder, so it will be used by default.
65
+
66
+ Running the tests with Rails 3.x:
67
+
68
+ $ bundle install
69
+ $ ruby -Itest -Ilib test/all.rb
70
+
71
+ Running the tests with Rails 2.3.x:
72
+
73
+ $ BUNDLE_GEMFILE=ci/Gemfile.rails-2.3.x bundle install
74
+ $ BUNDLE_GEMFILE=ci/Gemfile.rails-2.3.x ruby -Itest -Ilib test/all.rb
75
+
62
76
  ## Filter order
63
77
 
64
78
  You can picture the way routing-filter wraps filters around your application as a russian puppet pattern. Your application sits in the center and is wrapped by a number of filters. An incoming request's path will be past through these layers of filters from the outside in until it is passed to the regular application routes set. When you generate URLs on the other hand then the filters will be run from the inside out.
@@ -58,7 +58,7 @@ ActionController::Routing::RouteSet.class_eval do
58
58
  # TODO move this ... where?
59
59
  alias_method :extract_request_environment_without_host, :extract_request_environment unless method_defined? :extract_request_environment_without_host
60
60
  def extract_request_environment(request)
61
- returning extract_request_environment_without_host(request) do |env|
61
+ extract_request_environment_without_host(request).tap do |env|
62
62
  env.merge! :host => request.host,
63
63
  :port => request.port,
64
64
  :host_with_port => request.host_with_port,
@@ -2,7 +2,9 @@ require 'action_dispatch'
2
2
  require 'active_support/core_ext/module/aliasing'
3
3
  require 'active_support/core_ext/hash/reverse_merge'
4
4
 
5
- [ActionDispatch::Routing::Mapper, ActionDispatch::Routing::DeprecatedMapper].each do |mapper|
5
+ mappers = [ActionDispatch::Routing::Mapper]
6
+ mappers << ActionDispatch::Routing::DeprecatedMapper if defined?(ActionDispatch::Routing::DeprecatedMapper)
7
+ mappers.each do |mapper|
6
8
  mapper.class_eval do
7
9
  def filter(*args)
8
10
  @set.add_filters(*args)
@@ -11,9 +13,13 @@ require 'active_support/core_ext/hash/reverse_merge'
11
13
  end
12
14
 
13
15
  ActionDispatch::Routing::RouteSet.class_eval do
16
+ def filters
17
+ @set.filters if @set
18
+ end
19
+
14
20
  def add_filters(*names)
15
21
  options = names.extract_options!
16
- names.each { |name| @set.filters.unshift(RoutingFilter.build(name, options)) }
22
+ names.each { |name| filters.unshift(RoutingFilter.build(name, options)) }
17
23
  end
18
24
 
19
25
  # def recognize_path_with_filtering(path, env = {})
@@ -22,12 +28,12 @@ ActionDispatch::Routing::RouteSet.class_eval do
22
28
  # alias_method_chain :recognize_path, :filtering
23
29
 
24
30
  def generate_with_filtering(options, recall = {}, extras = false)
25
- @set.filters.run(:around_generate, options, &lambda{ generate_without_filtering(options, recall, extras) })
31
+ filters.run(:around_generate, options, &lambda{ generate_without_filtering(options, recall, extras) })
26
32
  end
27
33
  alias_method_chain :generate, :filtering
28
34
 
29
35
  def clear_with_filtering!
30
- @set.filters.clear if @set
36
+ filters.clear if filters
31
37
  clear_without_filtering!
32
38
  end
33
39
  alias_method_chain :clear!, :filtering
@@ -7,7 +7,7 @@ module RoutingFilter
7
7
  end
8
8
 
9
9
  def run(method, *args, &block)
10
- _next = self.next ? lambda { self.next.run(method, *args, &block) } : block
10
+ _next = self.next ? proc {|path, env| self.next.run(method, *args, &block) } : block
11
11
  RoutingFilter.active? ? send(method, *args, &_next) : _next.call(*args)
12
12
  end
13
13
 
@@ -52,11 +52,15 @@ module RoutingFilter
52
52
  end
53
53
  end
54
54
 
55
- def around_generate(params, &block)
55
+ def around_generate(*args, &block)
56
+ params = args.extract_options! # this is because we might get a call like forum_topics_path(forum, topic, :locale => :en)
57
+
56
58
  locale = params.delete(:locale) # extract the passed :locale option
57
59
  locale = I18n.locale if locale.nil? # default to I18n.locale when locale is nil (could also be false)
58
60
  locale = nil unless valid_locale?(locale) # reset to no locale when locale is not valid
59
61
 
62
+ args << params
63
+
60
64
  yield.tap do |result|
61
65
  prepend_segment!(result, locale) if prepend_locale?(locale)
62
66
  end
@@ -1,3 +1,3 @@
1
1
  module RoutingFilter
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.3'
3
3
  end
data/test/blocks.rb ADDED
@@ -0,0 +1,33 @@
1
+ # def foo
2
+ # f = Proc.new { return "return from foo from inside proc" }
3
+ # f.call # control leaves foo here
4
+ # return "return from foo"
5
+ # end
6
+ #
7
+ # def bar
8
+ # f = lambda { return "return from lambda" }
9
+ # f.call # control does not leave bar here
10
+ # return "return from bar"
11
+ # end
12
+ #
13
+ # puts foo # prints "return from foo from inside proc"
14
+ # puts bar # prints "return from bar"
15
+
16
+
17
+ class RouteSet
18
+ def call
19
+ recognize &Proc.new { return 'return from recognize block' }
20
+ p "KEKSE"
21
+ # recognize do
22
+ # return 'return from recognize block'
23
+ # end
24
+ end
25
+
26
+ def recognize
27
+ yield
28
+ p "KEKSE"
29
+ end
30
+ end
31
+
32
+
33
+ puts RouteSet.new.call
@@ -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
data/test/routes_test.rb CHANGED
@@ -18,7 +18,7 @@ class RoutesTest < Test::Unit::TestCase
18
18
  end
19
19
 
20
20
  test "routes.filter instantiates and registers a filter" do
21
- assert routes.instance_variable_get(:@set).filters.first.is_a?(RoutingFilter::Test)
21
+ assert routes.filters.first.is_a?(RoutingFilter::Test)
22
22
  end
23
23
 
24
24
  # test "filter.around_recognize is being called" do
@@ -0,0 +1,17 @@
1
+ require 'test_helper'
2
+
3
+ module TestRailsAdapter
4
+ routes = ActionController::Routing::Routes = ActionController::Routing::RouteSet.new
5
+ routes.draw do |map|
6
+ map.connect '/', :controller => 'rails_test/tests', :action => 'index'
7
+ map.foo '/foo/:id', :controller => 'rails_test/tests', :action => 'show'
8
+ map.filter :uuid, :pagination ,:locale, :extension
9
+ end
10
+
11
+ attr_reader :session
12
+ delegate :get, :response, :to => :session
13
+
14
+ def setup
15
+ @session = ActionController::Integration::Session.new(lambda { |env| ActionController::Routing::Routes.call(env) })
16
+ end
17
+ end
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+
3
+ require "rails"
4
+ require 'rack/test'
5
+
6
+ module TestRailsAdapter
7
+ include ::Rack::Test::Methods
8
+
9
+ APP = Class.new(Rails::Application).tap do |app|
10
+ app.config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"
11
+ app.config.session_store :cookie_store, :key => "_myapp_session"
12
+ app.config.active_support.deprecation = :log
13
+ app.routes.draw do
14
+ match "/" => "rails_test/tests#index"
15
+ match "/foo/:id" => "rails_test/tests#show", :as => 'foo'
16
+ filter :uuid, :pagination ,:locale, :extension
17
+ end
18
+ app.initialize!
19
+ end
20
+
21
+ def app
22
+ APP
23
+ end
24
+
25
+ def response
26
+ last_response
27
+ end
28
+ end
data/test/test_helper.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  ENV['RAILS_ENV'] = 'test'
2
2
 
3
3
  require 'rubygems'
4
- # gem 'actionpack', '~> 2.3' if ENV['RAILS_2']
5
4
  require 'test/unit'
6
5
  require 'bundler/setup'
7
6
 
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: 19
5
- prerelease: false
4
+ hash: 17
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 2
10
- version: 0.2.2
9
+ - 3
10
+ version: 0.2.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sven Fuchs
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-26 00:00:00 +01:00
18
+ date: 2011-01-25 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -95,6 +95,7 @@ files:
95
95
  - lib/routing_filter/version.rb
96
96
  - lib/routing_filter.rb
97
97
  - test/all.rb
98
+ - test/blocks.rb
98
99
  - test/filters/all_filters/generation.rb
99
100
  - test/filters/all_filters/recognition.rb
100
101
  - test/filters/all_filters_test.rb
@@ -102,9 +103,11 @@ files:
102
103
  - test/filters/locale_test.rb
103
104
  - test/filters/pagination_test.rb
104
105
  - test/filters/uuid_test.rb
105
- - test/rails_3_test.rb
106
+ - test/rails_test.rb
106
107
  - test/routes_test.rb
107
108
  - test/routing_filter_test.rb
109
+ - test/test_adapters/rails_2.rb
110
+ - test/test_adapters/rails_3.rb
108
111
  - test/test_helper.rb
109
112
  - Gemfile
110
113
  - Gemfile.lock
@@ -140,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
143
  requirements: []
141
144
 
142
145
  rubyforge_project: "[none]"
143
- rubygems_version: 1.3.7
146
+ rubygems_version: 1.4.2
144
147
  signing_key:
145
148
  specification_version: 3
146
149
  summary: Routing filters wraps around the complex beast that the Rails routing system is, allowing for unseen flexibility and power in Rails URL recognition and generation
data/test/rails_3_test.rb DELETED
@@ -1,73 +0,0 @@
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