Permalink
Browse files

Merge remote-tracking branch 'upstream/master'

* upstream/master:
  Revert "[padrino-core] to_s route paths before joining"
  Added Mac DS_Store file
  README to be changed once project is generated
  [padrino-core] to_s route paths before joining
  add regexp route generation
  • Loading branch information...
WaYdotNET committed Oct 31, 2011
2 parents a8adddf + 5dfd167 commit dc6a3be1eec63d62e1bcfa3724649553bf1a6bc0
View
@@ -5,3 +5,4 @@ Gemfile.lock
*.rbc
.rbx/
doc/
+.DS_Store
@@ -519,24 +519,24 @@ def conform_uri(uri_string)
# Rewrite default routes.
#
# @example
- # get :index # => "/"
- # get :index, "/" # => "/"
- # get :index, :map => "/" # => "/"
- # get :show, "/show-me" # => "/show-me"
- # get :show, :map => "/show-me" # => "/show-me"
- # get "/foo/bar" # => "/show"
- # get :index, :parent => :user # => "/user/:user_id/index"
- # get :show, :with => :id, :parent => :user # => "/user/:user_id/show/:id"
- # get :show, :with => :id # => "/show/:id"
- # get [:show, :id] # => "/show/:id"
- # get :show, :with => [:id, :name] # => "/show/:id/:name"
- # get [:show, :id, :name] # => "/show/:id/:name"
- # get :list, :provides => :js # => "/list.{:format,js)"
- # get :list, :provides => :any # => "/list(.:format)"
- # get :list, :provides => [:js, :json] # => "/list.{!format,js|json}"
- # get :list, :provides => [:html, :js, :json] # => "/list(.{!format,js|json})"
- # get :list, :priority => :low # Defers route to be last
- #
+ # get :index # => "/"
+ # get :index, "/" # => "/"
+ # get :index, :map => "/" # => "/"
+ # get :show, "/show-me" # => "/show-me"
+ # get :show, :map => "/show-me" # => "/show-me"
+ # get "/foo/bar" # => "/show"
+ # get :index, :parent => :user # => "/user/:user_id/index"
+ # get :show, :with => :id, :parent => :user # => "/user/:user_id/show/:id"
+ # get :show, :with => :id # => "/show/:id"
+ # get [:show, :id] # => "/show/:id"
+ # get :show, :with => [:id, :name] # => "/show/:id/:name"
+ # get [:show, :id, :name] # => "/show/:id/:name"
+ # get :list, :provides => :js # => "/list.{:format,js)"
+ # get :list, :provides => :any # => "/list(.:format)"
+ # get :list, :provides => [:js, :json] # => "/list.{!format,js|json}"
+ # get :list, :provides => [:html, :js, :json] # => "/list(.{!format,js|json})"
+ # get :list, :priority => :low # Defers route to be last
+ # get /pattern/, :name => :foo, :generate_with => '/foo' # Generates :foo as /foo
def route(verb, path, *args, &block)
options = case args.size
when 2
@@ -557,7 +557,7 @@ def route(verb, path, *args, &block)
route_options = options.dup
route_options[:provides] = @_provides if @_provides
path, *route_options[:with] = path if path.is_a?(Array)
- path, name, options = *parse_route(path, route_options, verb)
+ path, name, options, route_options = *parse_route(path, route_options, verb)
options.reverse_merge!(@_conditions) if @_conditions
# Sinatra defaults
@@ -571,7 +571,7 @@ def route(verb, path, *args, &block)
invoke_hook(:route_added, verb, path, block)
# HTTPRouter route construction
- route = router.add(path)
+ route = router.add(path, route_options)
route.name(name) if name
priority_name = options.delete(:priority) || :normal
priority = ROUTE_PRIORITY[priority_name] or raise("Priority #{priority_name} not recognized, try #{ROUTE_PRIORITY.keys.join(', ')}")
@@ -620,6 +620,9 @@ def parse_route(path, options, verb)
# We need save our originals path/options so we can perform correctly cache.
original = [path, options.dup]
+ # options for the route directly
+ route_options = {}
+
# We need check if path is a symbol, if that it's a named route
map = options.delete(:map)
@@ -628,7 +631,11 @@ def parse_route(path, options, verb)
path = map ? map.dup : (path == :index ? '/' : path.to_s) # The route path
end
- if path.kind_of?(String) # path i.e "/index" or "/show"
+ # Build our controller
+ controller = Array(@_controller).map { |c| c.to_s }
+
+ case path
+ when String # path i.e "/index" or "/show"
# Now we need to parse our 'with' params
if with_params = options.delete(:with)
path = process_path_for_with_params(path, with_params)
@@ -643,9 +650,6 @@ def parse_route(path, options, verb)
options[:matching][:format] = /[^\.]+/
end
- # Build our controller
- controller = Array(@_controller).map { |c| c.to_s }
-
absolute_map = map && map[0] == ?/
unless controller.empty?
@@ -656,10 +660,6 @@ def parse_route(path, options, verb)
path = File.join(controller_path, path)
end
# Here we build the correct name route
- if name
- controller_name = controller.join("_")
- name = "#{controller_name}_#{name}".to_sym unless controller_name.blank?
- end
end
# Now we need to parse our 'parent' params and parent scope
@@ -678,12 +678,21 @@ def parse_route(path, options, verb)
path.sub!(%r{/(\))?$}, '\\1') if path != "/" # Remove latest trailing delimiter
path.gsub!(/\/(\(\.|$)/, '\\1') # Remove trailing slashes
path.squeeze!('/')
+ when Regexp
+ route_options[:path_for_generation] = options.delete(:generate_with) if options.key?(:generate_with)
+ end
+
+ name = options.delete(:route_name) if name.nil? && options.key?(:route_name)
+ name = options.delete(:name) if name.nil? && options.key?(:name)
+ if name
+ controller_name = controller.join("_")
+ name = "#{controller_name}_#{name}".to_sym unless controller_name.blank?
end
# Merge in option defaults
options.reverse_merge!(:default_values => @_defaults)
- [path, name, options]
+ [path, name, options, route_options]
end
##
@@ -78,6 +78,13 @@ class FooError < RuntimeError; end
assert_equal "My lucky number: 99 99", body
end
+ should 'accept regexp routes with generate with :generate_with' do
+ mock_app do
+ get(%r{/fob|/baz}, :name => :foo, :generate_with => '/fob') { "regexp" }
+ end
+ assert_equal "/fob", @app.url(:foo)
+ end
+
should "parse routes with question marks" do
mock_app do
get("/foo/?"){ "okey" }
@@ -0,0 +1,2 @@
+All projects should have a readme so here is one so you don't forget to
+create it.

0 comments on commit dc6a3be

Please sign in to comment.