The :path,:as,:module,:shallow_path,
and :shallow_prefix options all default to the name of the namespace.
For options, see Base#match. For :shallow_path option,
see Resources#resources.
# accessible through /sekret/posts rather than /admin/postsnamespace:admin,path:"sekret"doresources:postsend# maps to +Sekret::PostsController+ rather than +Admin::PostsController+namespace:admin,module:"sekret"doresources:postsend# generates +sekret_posts_path+ rather than +admin_posts_path+namespace:admin,as:"sekret"doresources:postsend
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 940
def namespace(path, options = {}, &block)
path = path.to_s
defaults = {
module: path,
as: options.fetch(:as, path),
shallow_path: options.fetch(:path, path),
shallow_prefix: options.fetch(:as, path)
}
path_scope(options.delete(:path) { path }) do
scope(defaults.merge!(options), &block)
end
end
# === Parameter Restriction
# Allows you to constrain the nested routes based on a set of rules.
# For instance, in order to change the routes to allow for a dot character in the +id+ parameter:
#
# constraints(id: /\d+\.\d+/) do
# resources :posts
# end
#
# Now routes such as +/posts/1+ will no longer be valid, but +/posts/1.1+ will be.
# The +id+ parameter must match the constraint passed in for this example.
#
# You may use this to also restrict other parameters:
#
# resources :posts do
# constraints(post_id: /\d+\.\d+/) do
# resources :comments
# end
# end
#
# === Restricting based on IP
#
# Routes can also be constrained to an IP or a certain range of IP addresses:
#
# constraints(ip: /192\.168\.\d+\.\d+/) do
# resources :posts
# end
#
# Any user connecting from the 192.168.* range will be able to see this resource,
# where as any user connecting outside of this range will be told there is no such route.
#
# === Dynamic request matching
#
# Requests to routes can be constrained based on specific criteria:
#
# constraints(-> (req) { /iPhone/.match?(req.env["HTTP_USER_AGENT"]) }) do
# resources :iphones
# end
#
# You are able to move this logic out into a class if it is too complex for routes.
# This class must have a +matches?+ method defined on it which either returns +true+
# if the user should be given access to that route, or +false+ if the user should not.
#
# class Iphone
# def self.matches?(request)
# /iPhone/.match?(request.env["HTTP_USER_AGENT"])
# end
# end
#
# An expected place for this code would be +lib/constraints+.
#
# This class is then used like this:
#
# constraints(Iphone) do
# resources :iphones
# end
def constraints(constraints = {}, &block)
scope(constraints: constraints, &block)
end
# Allows you to set default parameters for a route, such as this:
# defaults id: 'home' do
# match 'scoped_pages/(:id)', to: 'pages#show'
# end
# Using this, the +:id+ parameter here will default to 'home'.
def defaults(defaults = {})
@scope = @scope.new(defaults: merge_defaults_scope(@scope[:defaults], defaults))
yield
ensure
@scope = @scope.parent
end
private
def merge_path_scope(parent, child)
Mapper.normalize_path("#{parent}/#{child}")
end
def merge_shallow_path_scope(parent, child)
Mapper.normalize_path("#{parent}/#{child}")
end
def merge_as_scope(parent, child)
parent ? "#{parent}_#{child}" : child
end
def merge_shallow_prefix_scope(parent, child)
parent ? "#{parent}_#{child}" : child
end
def merge_module_scope(parent, child)
parent ? "#{parent}/#{child}" : child
end
def merge_controller_scope(parent, child)
child
end
def merge_action_scope(parent, child)
child
end
def merge_via_scope(parent, child)
child
end
def merge_format_scope(parent, child)
child
end
def merge_path_names_scope(parent, child)
merge_options_scope(parent, child)
end
def merge_constraints_scope(parent, child)
merge_options_scope(parent, child)
end
def merge_defaults_scope(parent, child)
merge_options_scope(parent, child)
end
def merge_blocks_scope(parent, child)
merged = parent ? parent.dup : []
merged << child if child
merged
end
def merge_options_scope(parent, child)
(parent || {}).merge(child)
end
def merge_shallow_scope(parent, child)
child ? true : false
end
def merge_to_scope(parent, child)
child
end
end