padrino-routing 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -26,7 +26,7 @@ Let's take a look at how to define named route mappings:
26
26
  require 'padrino-routing'
27
27
 
28
28
  class RoutingDemo < Sinatra::Application
29
- register SinatraMore::RoutingPlugin
29
+ register Padrino::Routing
30
30
 
31
31
  # Define the named route mappings
32
32
  map(:account).to("/the/accounts/:name/and/:id")
@@ -52,6 +52,22 @@ You can also refer to the url in views using <tt>url_for</tt>
52
52
  Simply invoking <tt>url_for(name, *parameters)</tt> will return the full mapped url for use in links or anywhere else
53
53
  that the url might be required.
54
54
 
55
+ The routing plugin supports inline route definitions as well in which the url and the named alias
56
+ are defined together within the controller:
57
+
58
+ # app/controllers/example.rb
59
+ SimpleApp.controllers do
60
+ get :index, :map => '/index' do
61
+ ...
62
+ end
63
+
64
+ get :account, :map => '/the/accounts/:name/and/:id' do
65
+ # access params[:name] and params[:index]
66
+ end
67
+ end
68
+
69
+ Routes defined inline this way can be accessed and treated the same way as traditional named aliases.
70
+
55
71
  The routing system also supports url route configuration namespaces:
56
72
 
57
73
  # /app/routes/example.rb
@@ -83,9 +99,23 @@ You could also define the route aliases themselves using a namespace for conveni
83
99
 
84
100
  You can then reference the urls using the same <tt>url_for</tt> method:
85
101
 
86
- <%= link_to 'admin page', url_for(:admin, :show, :id => 25) %>
87
- <%= link_to 'admin page', url_for(:admin, :update, :id => 25) %>
88
- <%= link_to 'admin page', url_for(:admin, :show, :id => 25) %>
102
+ <%= link_to 'admin show page', url_for(:admin, :show, :id => 25) %>
103
+ <%= link_to 'admin index page', url_for(:admin, :destroy, :id => 25) %>
104
+
105
+ In addition to regular namespacing, as a shortcut you can have a controller serve as a namespace as well:
106
+
107
+ # /app/routes/admin.rb
108
+ SimpleApp.controllers :admin do
109
+ get :index, :map => "/admin/:id/index" do
110
+ "admin destroy for #{params[:id]}"
111
+ end
112
+ end
113
+
114
+ This will put all named routes within this controller block into the 'admin' namespace and then can be referenced:
115
+
116
+ <%= link_to 'admin index page', url_for(:admin, :index, :id => 25) %>
117
+
118
+ Note that controller namespaces are simply a shortcut for standard namespaces and do not differ in any other way.
89
119
 
90
120
  You can freely use both named route aliases and traditional Sinatra routes in the same application without conflict.
91
121
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
@@ -36,10 +36,10 @@ module Padrino
36
36
  # Supports namespaces by accessing the instance variable and appending this to the route alias name
37
37
  # If the path is not a symbol, nothing is changed and the original route method is invoked
38
38
  def route(verb, path, options={}, &block)
39
- if path.kind_of?(Symbol)
39
+ if path.kind_of?(Symbol)
40
40
  route_name = [@_namespace, path].flatten.compact
41
41
  if mapped_url = options.delete(:map) # constructing named route
42
- map(*route_name).to(mapped_url)
42
+ map(*route_name).to(mapped_url)
43
43
  path = mapped_url
44
44
  else # referencing prior named route
45
45
  route_name.unshift(self.app_name)
@@ -1,20 +1,28 @@
1
- # TODO add tests to make sure this works
1
+ if defined?(Padrino::Application) # Extends padrino application if being used
2
+ module Padrino
3
+ module ControllerNamespacing
4
+ # Makes the routes defined in the block and in the Modules given
5
+ # in `extensions` available to the application
6
+ def controllers_with_namespaces(*namespace, &block)
7
+ must_use_namespaces = namespace.size == 1 and namespace.first.is_a?(Symbol)
8
+ return controllers_without_namespaces(*namespace, &block) unless must_use_namespaces
9
+ self.reset_routes! if reload?
10
+ namespace(namespace.first) { instance_eval(&block) } if block_given?
11
+ end
2
12
 
3
- module Padrino
4
- module ControllerNamespacing
5
- # Makes the routes defined in the block and in the Modules given
6
- # in `extensions` available to the application
7
- def controllers_with_namespaces(*namespace, &block)
8
- controllers_without_namespaces unless namespace.size == 1 && namespace.first.is_a?(Symbol)
9
- @routes = Padrino::Application.dupe_routes if reload?
10
- namespace(namespace.first) { instance_eval(&block) } if block_given?
13
+ # Makes the routing urls defined in this block and in the Modules given
14
+ # in `extensions` available to the application
15
+ def urls(*extensions, &block)
16
+ instance_eval(&block) if block_given?
17
+ include(*extensions) if extensions.any?
18
+ end
11
19
  end
12
- end
13
-
14
- class Application
15
- extend Padrino::ControllerNamespacing
16
- class << self
17
- alias_method_chain :controllers, :namespaces
20
+
21
+ class Application
22
+ extend Padrino::ControllerNamespacing
23
+ class << self
24
+ alias_method_chain :controllers, :namespaces
25
+ end
18
26
  end
19
- end if defined?(Padrino::Application)
20
- end
27
+ end
28
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{padrino-routing}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
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-11-18}
12
+ s.date = %q{2009-11-20}
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 = [
@@ -36,8 +36,8 @@ Gem::Specification.new do |s|
36
36
  "test/fixtures/adv_routing_app/config/apps.rb",
37
37
  "test/fixtures/adv_routing_app/config/boot.rb",
38
38
  "test/fixtures/adv_routing_app/config/urls.rb",
39
+ "test/fixtures/adv_routing_app/controllers/autogenerated.rb",
39
40
  "test/fixtures/adv_routing_app/controllers/namespacing.rb",
40
- "test/fixtures/adv_routing_app/controllers/url_autogeneration.rb",
41
41
  "test/fixtures/routing_app/app.rb",
42
42
  "test/fixtures/routing_app/views/index.haml",
43
43
  "test/helper.rb",
@@ -1,4 +1,4 @@
1
- AdvRoutingDemo::urls do
1
+ AdvRoutingDemo.urls do
2
2
  map(:test).to("/test/:id/action")
3
3
  map(:admin, :settings).to("/admin/settings/action")
4
4
  map :admin do |admin|
@@ -1,8 +1,7 @@
1
- AdvRoutingDemo::controllers do
1
+ AdvRoutingDemo.controllers do
2
2
  get :simple, :map => "/some/simple/action" do
3
3
  "<h1>simple action welcomes you</h1>"
4
4
  end
5
-
6
5
  get :hello, :map => "/some/hello/action/:id/param" do
7
6
  "<h1>hello params id is #{params[:id]}</h1>"
8
7
  end
@@ -1,4 +1,4 @@
1
- AdvRoutingDemo::controllers :admin do
1
+ AdvRoutingDemo.controllers :admin do
2
2
  get :dashboard do
3
3
  "<h1>This is the admin dashboard, id: #{params[:id]}</h1>"
4
4
  end
@@ -17,7 +17,7 @@ AdvRoutingDemo::controllers :admin do
17
17
  end
18
18
 
19
19
 
20
- AdvRoutingDemo::controllers do
20
+ AdvRoutingDemo.controllers do
21
21
  namespace :blog do
22
22
  get :index, :map => "/blog/index/action" do
23
23
  "<h1>Here is the blog</h1>"
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.1.2
4
+ version: 0.1.3
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-11-18 00:00:00 -08:00
15
+ date: 2009-11-20 00:00:00 -08:00
16
16
  default_executable:
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
@@ -112,8 +112,8 @@ files:
112
112
  - test/fixtures/adv_routing_app/config/apps.rb
113
113
  - test/fixtures/adv_routing_app/config/boot.rb
114
114
  - test/fixtures/adv_routing_app/config/urls.rb
115
+ - test/fixtures/adv_routing_app/controllers/autogenerated.rb
115
116
  - test/fixtures/adv_routing_app/controllers/namespacing.rb
116
- - test/fixtures/adv_routing_app/controllers/url_autogeneration.rb
117
117
  - test/fixtures/routing_app/app.rb
118
118
  - test/fixtures/routing_app/views/index.haml
119
119
  - test/helper.rb