padrino-routing 0.2.9 → 0.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -26,7 +26,7 @@ end
26
26
 
27
27
  require 'rake/testtask'
28
28
  Rake::TestTask.new(:test) do |test|
29
- test.libs << 'lib' << 'test'
29
+ test.libs << 'test'
30
30
  test.pattern = 'test/**/test_*.rb'
31
31
  test.verbose = true
32
32
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.9
1
+ 0.4.5
@@ -1,8 +1,8 @@
1
- # require 'padrino-core/support_lite'
2
1
  Dir[File.dirname(__FILE__) + '/padrino-routing/**/*.rb'].each {|file| require file }
3
2
 
4
3
  module Padrino
5
- class RouteNotFound < RuntimeError; end
4
+ class RouteNotFound < RuntimeError; end
5
+ class InvalidNameSpace < RuntimeError; end
6
6
 
7
7
  module Routing
8
8
  def self.registered(app)
@@ -26,10 +26,14 @@ module Padrino
26
26
  # Used to define namespaced route configurations in order to group similar routes
27
27
  # Class evals the routes but with the namespace assigned which will append to each route
28
28
  # namespace(:admin) { get(:show) { "..." } }
29
- def namespace(name, &block)
30
- original, @_namespace = @_namespace, name
31
- self.class_eval(&block)
32
- @_namespace = original
29
+ def namespace(*args, &block)
30
+ if namespace = args.find { |ns| !ns.kind_of?(Symbol) }
31
+ raise InvalidNameSpace, "The name space #{namespace.inspect} must be a symbol"
32
+ else
33
+ original, @_namespaces = @_namespaces, args
34
+ self.class_eval(&block)
35
+ @_namespaces = original
36
+ end
33
37
  end
34
38
 
35
39
  # Hijacking route method in Sinatra to replace a route alias (i.e :account) with the full url string mapping
@@ -37,16 +41,20 @@ module Padrino
37
41
  # If the path is not a symbol, nothing is changed and the original route method is invoked
38
42
  def route(verb, path, options={}, &block)
39
43
  if path.kind_of?(Symbol)
40
- route_name = [@_namespace, path].flatten.compact
44
+ route_name = [@_namespaces, path].flatten.compact
41
45
  if mapped_url = options.delete(:map) # constructing named route
42
46
  map(*route_name).to(mapped_url)
43
47
  path = mapped_url
44
48
  else # referencing prior named route
45
- route_name.unshift(self.app_name.to_sym)
46
- path = named_paths[route_name]
49
+ path = named_paths[route_name.dup.unshift(self.app_name.to_sym)]
50
+ end
51
+ # If here we don't have a path we autobuild them
52
+ unless path
53
+ mapped_url = "/" + route_name.join("/")
54
+ map(*route_name).to(mapped_url)
55
+ path = mapped_url
47
56
  end
48
57
  end
49
- raise RouteNotFound.new("Route alias #{route_name.inspect} is not mapped to a url") unless path
50
58
  super verb, path, options, &block
51
59
  end
52
60
  end
@@ -3,10 +3,9 @@ if defined?(Padrino::Application) # Extends padrino application if being used
3
3
  module ControllerNamespacing
4
4
  # Makes the routes defined in the block and in the Modules given
5
5
  # in `extensions` available to the application
6
- def controllers_with_namespaces(*namespace, &block)
7
- must_use_namespaces = namespace.size == 1 && namespace.first.is_a?(Symbol)
8
- return controllers_without_namespaces(*namespace, &block) unless must_use_namespaces
9
- namespace(namespace.first) { instance_eval(&block) } if block_given?
6
+ def controllers_with_namespaces(*args, &block)
7
+ return controllers_without_namespaces(*args, &block) unless args.all? { |a| a.kind_of?(Symbol) }
8
+ namespace(*args) { instance_eval(&block) } if block_given?
10
9
  end
11
10
 
12
11
  # Makes the routing urls defined in this block and in the Modules given
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{padrino-routing}
8
- s.version = "0.2.9"
8
+ s.version = "0.4.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Padrino Team", "Nathan Esquenazi", "Davide D'Agostino", "Arthur Chiu"]
12
- s.date = %q{2009-12-22}
12
+ s.date = %q{2010-01-06}
13
13
  s.description = %q{Enhances padrino with a named route mapping system allowing for advanced routes}
14
14
  s.email = %q{nesquena@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -1,7 +1,3 @@
1
- require 'sinatra/base'
2
- require 'haml'
3
- require 'padrino-routing'
4
-
5
1
  class RoutingDemo < Sinatra::Base
6
2
  register Padrino::Routing
7
3
 
@@ -31,6 +27,18 @@ class RoutingDemo < Sinatra::Base
31
27
  end
32
28
  end
33
29
 
30
+ namespace :autogenerated, :accounts do
31
+ get :index do
32
+ url_for(:autogenerated, :accounts, :index)
33
+ end
34
+ end
35
+
36
+ namespace :autogenerated, :accounts do
37
+ get :with_mapping, :map => "/autogenerated/accounts/with/mapping" do
38
+ url_for(:autogenerated, :accounts, :with_mapping)
39
+ end
40
+ end
41
+
34
42
  get :account do
35
43
  "<h1>the account url for #{params[:name]} and id #{params[:id]}</h1>"
36
44
  end
data/test/helper.rb CHANGED
@@ -4,10 +4,13 @@ require 'shoulda'
4
4
  require 'mocha'
5
5
  require 'rack/test'
6
6
  require 'webrat'
7
+ require 'padrino-core'
8
+ require 'padrino-helpers'
7
9
 
8
10
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
11
  $LOAD_PATH.unshift(File.dirname(__FILE__))
10
- require 'padrino-helpers'
12
+
13
+ require 'padrino-routing'
11
14
 
12
15
  class Test::Unit::TestCase
13
16
  include Rack::Test::Methods
@@ -12,32 +12,38 @@ class TestPadrinoRouting < Test::Unit::TestCase
12
12
  demo.class.map(:admin, :demo).to('/admin/demo/:name')
13
13
  assert_equal '/admin/demo/john', demo.url_for(:admin, :demo, :name => 'john')
14
14
  end
15
+
15
16
  should "support finding known urls ignoring blank extra param" do
16
17
  demo = app.new
17
18
  demo.class.map(:admin, :demo).to('/admin/demo')
18
19
  assert_equal '/admin/demo', demo.url_for(:admin, :demo, :foo => '')
19
20
  end
21
+
20
22
  should "support calling to_param on an object which supports the call" do
21
23
  obj = stub(:to_param => '25', :to_s => "<Fake>")
22
24
  demo = app.new
23
25
  demo.class.map(:admin, :demo).to('/admin/demo/:foo')
24
26
  assert_equal '/admin/demo/25', demo.url_for(:admin, :demo, :foo => obj)
25
27
  end
28
+
26
29
  should "support finding known urls with named param ignoring blank extra param" do
27
30
  demo = app.new
28
31
  demo.class.map(:admin, :demo).to('/admin/demo/:name')
29
32
  assert_equal '/admin/demo/john', demo.url_for(:admin, :demo, :name => 'john', :foo => ' ')
30
33
  end
34
+
31
35
  should "support finding known urls with one extra param" do
32
36
  demo = app.new
33
37
  demo.class.map(:admin, :demo).to('/admin/demo/:name')
34
38
  assert_equal '/admin/demo/john?foo=bar', demo.url_for(:admin, :demo, :name => 'john', :foo => 'bar')
35
39
  end
40
+
36
41
  should "support finding known urls with extra params" do
37
42
  demo = app.new
38
43
  demo.class.map(:demo).to('/demo/:name')
39
44
  assert_equal '/demo/john?bar=foo&foo=bar', demo.url_for(:demo, :name => 'john', :foo => 'bar', :bar => 'foo')
40
45
  end
46
+
41
47
  should "support finding known urls with multiple named params with extra params" do
42
48
  demo = app.new
43
49
  demo.class.map(:demo).to('/demo/:name/id/:id')
@@ -52,11 +58,13 @@ class TestPadrinoRouting < Test::Unit::TestCase
52
58
  assert_have_selector :p, :class => 'account_url', :content => '/the/accounts/foobar/path/10/end'
53
59
  assert_have_selector :p, :class => 'accounts_index', :content => '/the/accounts/index'
54
60
  end
61
+
55
62
  should "display admin route links" do
56
63
  assert_have_selector :p, :class => 'admin_url', :content => '/admin/25/show'
57
64
  assert_have_selector :p, :class => 'admin_url2', :content => '/admin/10/update/test'
58
65
  assert_have_selector :p, :class => 'admin_url3', :content => '/admin/12/destroy'
59
66
  end
67
+
60
68
  should "support app namespaces" do
61
69
  assert_have_selector :p, :class => 'app_accounts_index', :content => '/the/accounts/index'
62
70
  assert_have_selector :p, :class => 'app_admin_url', :content => '/admin/25/show'
@@ -71,6 +79,7 @@ class TestPadrinoRouting < Test::Unit::TestCase
71
79
  assert_equal "/demo", demo.class.named_paths[[:routing_demo, :demo]]
72
80
  assert_equal "/demo", demo.url_for(:demo)
73
81
  end
82
+
74
83
  should "support changing uri root with mount" do
75
84
  demo = app.new
76
85
  demo.class.stubs(:uri_root).returns("/blog")
@@ -85,27 +94,44 @@ class TestPadrinoRouting < Test::Unit::TestCase
85
94
  assert_nothing_raised { app.new.url_for(:accounts) }
86
95
  assert_nothing_raised { app.new.url_for(:routing_demo, :admin, :show, :id => 5) }
87
96
  end
97
+
88
98
  should "properly raise not found exception" do
89
99
  assert_raises(Padrino::RouteNotFound) { visit '/failed_route' }
90
100
  assert_raises(Padrino::RouteNotFound) { app.new.url_for(:admin, :fake) }
91
101
  end
92
- should "properly raise about an invalid alias for route definition" do
93
- assert_raises(Padrino::RouteNotFound) { app.get(:fake) }
102
+
103
+ should "properly autogenerate routes" do
104
+ assert_nothing_raised { app.get(:fake) do; end }
94
105
  end
106
+
95
107
  should "properly work when alias is used in proper route definition" do
96
108
  assert_nothing_raised { app.get(:accounts) do; end }
97
109
  end
98
110
  end
99
111
 
112
+ context 'for autogenerated routed' do
113
+ setup { visit '/autogenerated/accounts/index' }
114
+ should "return proper content" do
115
+ assert_equal '/autogenerated/accounts/index', response_body
116
+ end
117
+ end
118
+
119
+ context 'for autogenerated routed with mapping' do
120
+ setup { visit '/autogenerated/accounts/with/mapping' }
121
+ should "return proper content" do
122
+ assert_equal '/autogenerated/accounts/with/mapping', response_body
123
+ end
124
+ end
125
+
100
126
  context 'for no namespaced account route' do
101
- setup { visit '/the/accounts/demo/path/5/end'}
127
+ setup { visit '/the/accounts/demo/path/5/end' }
102
128
  should "return proper account text" do
103
129
  assert_have_selector :h1, :content => "the account url for demo and id 5"
104
130
  end
105
131
  end
106
132
 
107
133
  context 'for no namespaced accounts index route' do
108
- setup { visit '/the/accounts/index/'}
134
+ setup { visit '/the/accounts/index/' }
109
135
  should "return proper account text" do
110
136
  assert_have_selector :h1, :content => "the accounts index"
111
137
  end
@@ -131,4 +157,4 @@ class TestPadrinoRouting < Test::Unit::TestCase
131
157
  assert_have_selector :p, :content => "destroy admin with id 60"
132
158
  end
133
159
  end
134
- end
160
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: padrino-routing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.9
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Padrino Team
@@ -12,7 +12,7 @@ autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
14
 
15
- date: 2009-12-22 00:00:00 -08:00
15
+ date: 2010-01-06 00:00:00 +01:00
16
16
  default_executable:
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency