2010-08-09 07:31:42 -04:00
|
|
|
require 'erb'
|
2010-03-08 17:22:25 -05:00
|
|
|
require 'active_support/core_ext/hash/except'
|
2010-03-28 08:15:02 -04:00
|
|
|
require 'active_support/core_ext/object/blank'
|
2010-11-02 06:47:32 -04:00
|
|
|
require 'active_support/inflector'
|
2010-11-30 10:36:01 -05:00
|
|
|
require 'action_dispatch/routing/redirection'
|
2010-03-08 17:22:25 -05:00
|
|
|
|
2009-10-20 11:14:46 -04:00
|
|
|
module ActionDispatch
|
|
|
|
module Routing
|
2009-10-20 13:31:23 -04:00
|
|
|
class Mapper
|
2010-03-31 09:54:07 -04:00
|
|
|
class Constraints #:nodoc:
|
2010-04-03 23:23:23 -04:00
|
|
|
def self.new(app, constraints, request = Rack::Request)
|
2009-12-07 20:59:23 -05:00
|
|
|
if constraints.any?
|
2010-04-03 23:23:23 -04:00
|
|
|
super(app, constraints, request)
|
2009-12-07 20:59:23 -05:00
|
|
|
else
|
|
|
|
app
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-07-05 18:39:13 -04:00
|
|
|
attr_reader :app
|
|
|
|
|
2010-04-03 23:23:23 -04:00
|
|
|
def initialize(app, constraints, request)
|
|
|
|
@app, @constraints, @request = app, constraints, request
|
2009-12-07 20:59:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2010-04-03 23:23:23 -04:00
|
|
|
req = @request.new(env)
|
2009-12-07 20:59:23 -05:00
|
|
|
|
|
|
|
@constraints.each { |constraint|
|
|
|
|
if constraint.respond_to?(:matches?) && !constraint.matches?(req)
|
2009-12-26 13:43:50 -05:00
|
|
|
return [ 404, {'X-Cascade' => 'pass'}, [] ]
|
2010-08-22 13:48:26 -04:00
|
|
|
elsif constraint.respond_to?(:call) && !constraint.call(*constraint_args(constraint, req))
|
2009-12-26 13:43:50 -05:00
|
|
|
return [ 404, {'X-Cascade' => 'pass'}, [] ]
|
2009-12-07 20:59:23 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
@app.call(env)
|
|
|
|
end
|
2010-08-22 13:48:26 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
def constraint_args(constraint, request)
|
|
|
|
constraint.arity == 1 ? [request] : [request.symbolized_path_parameters, request]
|
|
|
|
end
|
2009-12-07 20:59:23 -05:00
|
|
|
end
|
|
|
|
|
2010-03-31 09:54:07 -04:00
|
|
|
class Mapping #:nodoc:
|
2010-07-12 09:11:15 -04:00
|
|
|
IGNORE_OPTIONS = [:to, :as, :via, :on, :constraints, :defaults, :only, :except, :anchor, :shallow, :shallow_path, :shallow_prefix]
|
2010-03-26 08:16:25 -04:00
|
|
|
|
2010-08-24 13:54:23 -04:00
|
|
|
def initialize(set, scope, path, options)
|
2010-08-24 15:42:14 -04:00
|
|
|
@set, @scope = set, scope
|
|
|
|
@options = (@scope[:options] || {}).merge(options)
|
2010-08-24 13:54:23 -04:00
|
|
|
@path = normalize_path(path)
|
2010-07-12 09:11:15 -04:00
|
|
|
normalize_options!
|
2009-12-07 20:59:23 -05:00
|
|
|
end
|
2009-12-26 13:43:50 -05:00
|
|
|
|
2009-12-24 18:23:39 -05:00
|
|
|
def to_route
|
2010-03-08 15:24:49 -05:00
|
|
|
[ app, conditions, requirements, defaults, @options[:as], @options[:anchor] ]
|
2009-12-07 20:59:23 -05:00
|
|
|
end
|
2009-12-26 13:43:50 -05:00
|
|
|
|
2009-12-24 18:23:39 -05:00
|
|
|
private
|
2010-07-12 09:11:15 -04:00
|
|
|
|
|
|
|
def normalize_options!
|
|
|
|
path_without_format = @path.sub(/\(\.:format\)$/, '')
|
2010-03-22 16:03:43 -04:00
|
|
|
|
2010-07-12 09:11:15 -04:00
|
|
|
if using_match_shorthand?(path_without_format, @options)
|
|
|
|
to_shorthand = @options[:to].blank?
|
|
|
|
@options[:to] ||= path_without_format[1..-1].sub(%r{/([^/]*)$}, '#\1')
|
2010-03-22 16:03:43 -04:00
|
|
|
end
|
|
|
|
|
2010-07-12 09:11:15 -04:00
|
|
|
@options.merge!(default_controller_and_action(to_shorthand))
|
2010-11-17 00:46:15 -05:00
|
|
|
|
|
|
|
requirements.each do |name, requirement|
|
|
|
|
# segment_keys.include?(k.to_s) || k == :controller
|
|
|
|
next unless Regexp === requirement && !constraints[name]
|
|
|
|
|
|
|
|
if requirement.source =~ %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z}
|
|
|
|
raise ArgumentError, "Regexp anchor characters are not allowed in routing requirements: #{requirement.inspect}"
|
|
|
|
end
|
|
|
|
if requirement.multiline?
|
|
|
|
raise ArgumentError, "Regexp multiline option not allowed in routing requirements: #{requirement.inspect}"
|
|
|
|
end
|
|
|
|
end
|
2009-12-07 20:59:23 -05:00
|
|
|
end
|
2009-12-27 18:38:00 -05:00
|
|
|
|
2009-12-27 17:13:03 -05:00
|
|
|
# match "account/overview"
|
2010-03-22 16:03:43 -04:00
|
|
|
def using_match_shorthand?(path, options)
|
2010-04-20 08:04:46 -04:00
|
|
|
path && options.except(:via, :anchor, :to, :as).empty? && path =~ %r{^/[\w\/]+$}
|
2009-12-27 17:13:03 -05:00
|
|
|
end
|
2009-12-07 20:59:23 -05:00
|
|
|
|
2009-12-24 18:23:39 -05:00
|
|
|
def normalize_path(path)
|
2010-08-24 15:42:14 -04:00
|
|
|
raise ArgumentError, "path is required" if path.blank?
|
|
|
|
path = Mapper.normalize_path(path)
|
2010-08-24 13:54:23 -04:00
|
|
|
|
|
|
|
if path.match(':controller')
|
|
|
|
raise ArgumentError, ":controller segment is not allowed within a namespace block" if @scope[:module]
|
|
|
|
|
|
|
|
# Add a default constraint for :controller path segments that matches namespaced
|
|
|
|
# controllers with default routes like :controller/:action/:id(.:format), e.g:
|
|
|
|
# GET /admin/products/show/1
|
|
|
|
# => { :controller => 'admin/products', :action => 'show', :id => '1' }
|
|
|
|
@options.reverse_merge!(:controller => /.+?/)
|
|
|
|
end
|
|
|
|
|
2010-08-24 15:56:51 -04:00
|
|
|
if @options[:format] == false
|
|
|
|
@options.delete(:format)
|
|
|
|
path
|
|
|
|
elsif path.include?(":format")
|
2010-08-24 15:42:14 -04:00
|
|
|
path
|
|
|
|
else
|
|
|
|
"#{path}(.:format)"
|
|
|
|
end
|
2009-12-24 18:23:39 -05:00
|
|
|
end
|
2009-12-07 20:59:23 -05:00
|
|
|
|
2009-12-24 18:23:39 -05:00
|
|
|
def app
|
|
|
|
Constraints.new(
|
2010-07-06 17:20:06 -04:00
|
|
|
to.respond_to?(:call) ? to : Routing::RouteSet::Dispatcher.new(:defaults => defaults),
|
2010-04-03 23:23:23 -04:00
|
|
|
blocks,
|
|
|
|
@set.request_class
|
2009-12-24 18:23:39 -05:00
|
|
|
)
|
2009-12-07 20:59:23 -05:00
|
|
|
end
|
|
|
|
|
2009-12-24 18:23:39 -05:00
|
|
|
def conditions
|
|
|
|
{ :path_info => @path }.merge(constraints).merge(request_method_condition)
|
|
|
|
end
|
2009-12-26 13:43:50 -05:00
|
|
|
|
2009-12-24 18:23:39 -05:00
|
|
|
def requirements
|
2010-06-19 15:52:55 -04:00
|
|
|
@requirements ||= (@options[:constraints].is_a?(Hash) ? @options[:constraints] : {}).tap do |requirements|
|
2009-12-24 18:23:39 -05:00
|
|
|
requirements.reverse_merge!(@scope[:constraints]) if @scope[:constraints]
|
|
|
|
@options.each { |k, v| requirements[k] = v if v.is_a?(Regexp) }
|
|
|
|
end
|
|
|
|
end
|
2009-12-07 20:59:23 -05:00
|
|
|
|
2009-12-24 18:23:39 -05:00
|
|
|
def defaults
|
2010-03-26 08:16:25 -04:00
|
|
|
@defaults ||= (@options[:defaults] || {}).tap do |defaults|
|
|
|
|
defaults.reverse_merge!(@scope[:defaults]) if @scope[:defaults]
|
|
|
|
@options.each { |k, v| defaults[k] = v unless v.is_a?(Regexp) || IGNORE_OPTIONS.include?(k.to_sym) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-07-12 09:11:15 -04:00
|
|
|
def default_controller_and_action(to_shorthand=nil)
|
2010-03-26 08:16:25 -04:00
|
|
|
if to.respond_to?(:call)
|
2009-12-24 18:23:39 -05:00
|
|
|
{ }
|
|
|
|
else
|
2010-07-12 09:11:15 -04:00
|
|
|
if to.is_a?(String)
|
2010-09-18 12:49:46 -04:00
|
|
|
controller, action = to.split('#')
|
2010-07-12 09:11:15 -04:00
|
|
|
elsif to.is_a?(Symbol)
|
|
|
|
action = to.to_s
|
2009-12-24 18:23:39 -05:00
|
|
|
end
|
2009-12-26 13:43:50 -05:00
|
|
|
|
2010-07-12 09:11:15 -04:00
|
|
|
controller ||= default_controller
|
|
|
|
action ||= default_action
|
2010-04-12 14:23:35 -04:00
|
|
|
|
2010-07-12 09:11:15 -04:00
|
|
|
unless controller.is_a?(Regexp) || to_shorthand
|
|
|
|
controller = [@scope[:module], controller].compact.join("/").presence
|
|
|
|
end
|
2010-07-06 17:20:06 -04:00
|
|
|
|
2010-09-18 12:49:46 -04:00
|
|
|
if controller.is_a?(String) && controller =~ %r{\A/}
|
|
|
|
raise ArgumentError, "controller name should not start with a slash"
|
|
|
|
end
|
|
|
|
|
2010-07-12 09:11:15 -04:00
|
|
|
controller = controller.to_s unless controller.is_a?(Regexp)
|
|
|
|
action = action.to_s unless action.is_a?(Regexp)
|
2010-04-12 10:49:41 -04:00
|
|
|
|
2010-07-12 09:11:15 -04:00
|
|
|
if controller.blank? && segment_keys.exclude?("controller")
|
2009-12-24 18:23:39 -05:00
|
|
|
raise ArgumentError, "missing :controller"
|
|
|
|
end
|
2009-12-26 13:43:50 -05:00
|
|
|
|
2010-07-12 09:11:15 -04:00
|
|
|
if action.blank? && segment_keys.exclude?("action")
|
2009-12-24 18:23:39 -05:00
|
|
|
raise ArgumentError, "missing :action"
|
|
|
|
end
|
2009-12-26 13:43:50 -05:00
|
|
|
|
2010-11-15 17:22:06 -05:00
|
|
|
hash = {}
|
2010-11-15 18:02:18 -05:00
|
|
|
hash[:controller] = controller unless controller.blank?
|
|
|
|
hash[:action] = action unless action.blank?
|
2010-11-15 17:22:06 -05:00
|
|
|
hash
|
2009-12-24 18:23:39 -05:00
|
|
|
end
|
|
|
|
end
|
2009-12-07 20:59:23 -05:00
|
|
|
|
2009-12-24 18:23:39 -05:00
|
|
|
def blocks
|
2010-10-04 19:08:01 -04:00
|
|
|
block = @scope[:blocks] || []
|
|
|
|
|
2009-12-24 18:23:39 -05:00
|
|
|
if @options[:constraints].present? && !@options[:constraints].is_a?(Hash)
|
2010-10-04 19:08:01 -04:00
|
|
|
block << @options[:constraints]
|
2009-12-24 18:23:39 -05:00
|
|
|
end
|
2009-12-26 13:43:50 -05:00
|
|
|
|
2010-10-04 19:08:01 -04:00
|
|
|
block
|
2009-12-24 18:23:39 -05:00
|
|
|
end
|
2009-12-26 13:43:50 -05:00
|
|
|
|
2009-12-24 18:23:39 -05:00
|
|
|
def constraints
|
|
|
|
@constraints ||= requirements.reject { |k, v| segment_keys.include?(k.to_s) || k == :controller }
|
|
|
|
end
|
2009-12-07 20:59:23 -05:00
|
|
|
|
2009-12-24 18:23:39 -05:00
|
|
|
def request_method_condition
|
|
|
|
if via = @options[:via]
|
2010-11-02 06:47:32 -04:00
|
|
|
via = Array(via).map { |m| m.to_s.dasherize.upcase }
|
|
|
|
{ :request_method => %r[^#{via.join('|')}$] }
|
2009-12-24 18:23:39 -05:00
|
|
|
else
|
|
|
|
{ }
|
2009-12-07 20:59:23 -05:00
|
|
|
end
|
2009-12-24 18:23:39 -05:00
|
|
|
end
|
2009-12-26 13:43:50 -05:00
|
|
|
|
2009-12-24 18:23:39 -05:00
|
|
|
def segment_keys
|
|
|
|
@segment_keys ||= Rack::Mount::RegexpWithNamedGroups.new(
|
2010-03-22 16:03:43 -04:00
|
|
|
Rack::Mount::Strexp.compile(@path, requirements, SEPARATORS)
|
|
|
|
).names
|
2009-12-24 18:23:39 -05:00
|
|
|
end
|
2009-12-07 20:59:23 -05:00
|
|
|
|
2009-12-24 18:23:39 -05:00
|
|
|
def to
|
|
|
|
@options[:to]
|
|
|
|
end
|
2009-12-26 13:43:50 -05:00
|
|
|
|
2009-12-24 18:23:39 -05:00
|
|
|
def default_controller
|
2010-04-12 14:23:35 -04:00
|
|
|
if @options[:controller]
|
2010-07-06 17:20:06 -04:00
|
|
|
@options[:controller]
|
2010-04-12 14:23:35 -04:00
|
|
|
elsif @scope[:controller]
|
2010-07-06 17:20:06 -04:00
|
|
|
@scope[:controller]
|
2010-04-12 14:23:35 -04:00
|
|
|
end
|
2009-12-07 20:59:23 -05:00
|
|
|
end
|
2010-06-30 06:34:15 -04:00
|
|
|
|
|
|
|
def default_action
|
|
|
|
if @options[:action]
|
2010-07-06 17:20:06 -04:00
|
|
|
@options[:action]
|
2010-07-12 09:11:15 -04:00
|
|
|
elsif @scope[:action]
|
|
|
|
@scope[:action]
|
2010-06-30 06:34:15 -04:00
|
|
|
end
|
|
|
|
end
|
2009-12-24 18:23:39 -05:00
|
|
|
end
|
2009-12-07 20:59:23 -05:00
|
|
|
|
2010-01-13 19:31:17 -05:00
|
|
|
# Invokes Rack::Mount::Utils.normalize path and ensure that
|
2010-01-25 18:55:26 -05:00
|
|
|
# (:locale) becomes (/:locale) instead of /(:locale). Except
|
|
|
|
# for root cases, where the latter is the correct one.
|
2010-01-13 19:31:17 -05:00
|
|
|
def self.normalize_path(path)
|
|
|
|
path = Rack::Mount::Utils.normalize_path(path)
|
2010-06-26 05:46:24 -04:00
|
|
|
path.gsub!(%r{/(\(+)/?}, '\1/') unless path =~ %r{^/\(+[^/]+\)$}
|
2010-01-13 19:31:17 -05:00
|
|
|
path
|
|
|
|
end
|
|
|
|
|
2010-08-24 15:42:14 -04:00
|
|
|
def self.normalize_name(name)
|
|
|
|
normalize_path(name)[1..-1].gsub("/", "_")
|
|
|
|
end
|
|
|
|
|
2009-12-24 18:23:39 -05:00
|
|
|
module Base
|
2010-03-31 09:54:07 -04:00
|
|
|
def initialize(set) #:nodoc:
|
2009-12-24 18:23:39 -05:00
|
|
|
@set = set
|
|
|
|
end
|
2009-12-07 20:59:23 -05:00
|
|
|
|
2010-08-28 18:04:14 -04:00
|
|
|
# You can specify what Rails should route "/" to with the root method:
|
|
|
|
#
|
|
|
|
# root :to => 'pages#main'
|
|
|
|
#
|
2011-02-05 13:12:09 -05:00
|
|
|
# For options, see +match+, as +root+ uses it internally.
|
2010-12-09 18:34:40 -05:00
|
|
|
#
|
2010-12-09 05:21:08 -05:00
|
|
|
# You should put the root route at the top of <tt>config/routes.rb</tt>,
|
|
|
|
# because this means it will be matched first. As this is the most popular route
|
|
|
|
# of most Rails applications, this is beneficial.
|
2009-12-24 18:23:39 -05:00
|
|
|
def root(options = {})
|
|
|
|
match '/', options.reverse_merge(:as => :root)
|
|
|
|
end
|
2009-12-07 20:59:23 -05:00
|
|
|
|
2011-02-05 13:12:09 -05:00
|
|
|
# Matches a url pattern to one or more routes. Any symbols in a pattern
|
|
|
|
# are interpreted as url query parameters and thus available as +params+
|
|
|
|
# in an action:
|
2010-08-28 18:04:14 -04:00
|
|
|
#
|
2011-02-05 13:12:09 -05:00
|
|
|
# # sets :controller, :action and :id in params
|
2011-01-29 17:24:37 -05:00
|
|
|
# match ':controller/:action/:id'
|
2010-08-28 18:04:14 -04:00
|
|
|
#
|
2011-02-05 13:12:09 -05:00
|
|
|
# Two of these symbols are special, +:controller+ maps to the controller
|
|
|
|
# and +:action+ to the controller's action. A pattern can also map
|
|
|
|
# wildcard segments (globs) to params:
|
|
|
|
#
|
|
|
|
# match 'songs/*category/:title' => 'songs#show'
|
|
|
|
#
|
|
|
|
# # 'songs/rock/classic/stairway-to-heaven' sets
|
|
|
|
# # params[:category] = 'rock/classic'
|
|
|
|
# # params[:title] = 'stairway-to-heaven'
|
|
|
|
#
|
|
|
|
# When a pattern points to an internal route, the route's +:action+ and
|
|
|
|
# +:controller+ should be set in options or hash shorthand. Examples:
|
2011-01-29 17:24:37 -05:00
|
|
|
#
|
|
|
|
# match 'photos/:id' => 'photos#show'
|
|
|
|
# match 'photos/:id', :to => 'photos#show'
|
|
|
|
# match 'photos/:id', :controller => 'photos', :action => 'show'
|
2011-01-29 10:16:45 -05:00
|
|
|
#
|
2011-02-05 13:12:09 -05:00
|
|
|
# A pattern can also point to a +Rack+ endpoint i.e. anything that
|
|
|
|
# responds to +call+:
|
|
|
|
#
|
|
|
|
# match 'photos/:id' => lambda {|hash| [200, {}, "Coming soon" }
|
|
|
|
# match 'photos/:id' => PhotoRackApp
|
|
|
|
# # Yes, controller actions are just rack endpoints
|
|
|
|
# match 'photos/:id' => PhotosController.action(:show)
|
|
|
|
#
|
2011-02-03 23:51:06 -05:00
|
|
|
# === Options
|
2011-01-29 10:16:45 -05:00
|
|
|
#
|
2011-02-05 13:12:09 -05:00
|
|
|
# Any options not seen here are passed on as params with the url.
|
2011-01-29 10:16:45 -05:00
|
|
|
#
|
|
|
|
# [:controller]
|
|
|
|
# The route's controller.
|
|
|
|
#
|
|
|
|
# [:action]
|
|
|
|
# The route's action.
|
|
|
|
#
|
|
|
|
# [:path]
|
|
|
|
# The path prefix for the routes.
|
|
|
|
#
|
|
|
|
# [:module]
|
|
|
|
# The namespace for :controller.
|
|
|
|
#
|
|
|
|
# match 'path' => 'c#a', :module => 'sekret', :controller => 'posts'
|
|
|
|
# #=> Sekret::PostsController
|
|
|
|
#
|
|
|
|
# See <tt>Scoping#namespace</tt> for its scope equivalent.
|
|
|
|
#
|
|
|
|
# [:as]
|
|
|
|
# The name used to generate routing helpers.
|
|
|
|
#
|
|
|
|
# [:via]
|
|
|
|
# Allowed HTTP verb(s) for route.
|
|
|
|
#
|
|
|
|
# match 'path' => 'c#a', :via => :get
|
|
|
|
# match 'path' => 'c#a', :via => [:get, :post]
|
|
|
|
#
|
|
|
|
# [:to]
|
2011-02-05 13:12:09 -05:00
|
|
|
# Points to a +Rack+ endpoint. Can be an object that responds to
|
|
|
|
# +call+ or a string representing a controller's action.
|
2011-01-29 10:16:45 -05:00
|
|
|
#
|
2011-02-05 13:12:09 -05:00
|
|
|
# match 'path', :to => 'controller#action'
|
|
|
|
# match 'path', :to => lambda { [200, {}, "Success!"] }
|
|
|
|
# match 'path', :to => RackApp
|
2011-01-29 10:16:45 -05:00
|
|
|
#
|
|
|
|
# [:on]
|
|
|
|
# Shorthand for wrapping routes in a specific RESTful context. Valid
|
|
|
|
# values are :member, :collection, and :new. Only use within
|
|
|
|
# <tt>resource(s)</tt> block. For example:
|
|
|
|
#
|
|
|
|
# resource :bar do
|
|
|
|
# match 'foo' => 'c#a', :on => :member, :via => [:get, :post]
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Is equivalent to:
|
|
|
|
#
|
|
|
|
# resource :bar do
|
|
|
|
# member do
|
|
|
|
# match 'foo' => 'c#a', :via => [:get, :post]
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# [:constraints]
|
|
|
|
# Constrains parameters with a hash of regular expressions or an
|
|
|
|
# object that responds to #matches?
|
|
|
|
#
|
|
|
|
# match 'path/:id', :constraints => { :id => /[A-Z]\d{5}/ }
|
|
|
|
#
|
|
|
|
# class Blacklist
|
|
|
|
# def matches?(request) request.remote_ip == '1.2.3.4' end
|
|
|
|
# end
|
|
|
|
# match 'path' => 'c#a', :constraints => Blacklist.new
|
|
|
|
#
|
|
|
|
# See <tt>Scoping#constraints</tt> for more examples with its scope
|
|
|
|
# equivalent.
|
|
|
|
#
|
|
|
|
# [:defaults]
|
|
|
|
# Sets defaults for parameters
|
|
|
|
#
|
|
|
|
# # Sets params[:format] to 'jpg' by default
|
|
|
|
# match 'path' => 'c#a', :defaults => { :format => 'jpg' }
|
|
|
|
#
|
|
|
|
# See <tt>Scoping#defaults</tt> for its scope equivalent.
|
2010-08-24 13:54:23 -04:00
|
|
|
def match(path, options=nil)
|
|
|
|
mapping = Mapping.new(@set, @scope, path, options || {}).to_route
|
2010-03-08 15:24:49 -05:00
|
|
|
@set.add_route(*mapping)
|
2009-12-24 18:23:39 -05:00
|
|
|
self
|
|
|
|
end
|
2010-03-09 22:20:13 -05:00
|
|
|
|
2010-11-12 22:15:17 -05:00
|
|
|
# Mount a Rack-based application to be used within the application.
|
|
|
|
#
|
2010-12-09 05:03:14 -05:00
|
|
|
# mount SomeRackApp, :at => "some_route"
|
2010-11-12 22:15:17 -05:00
|
|
|
#
|
|
|
|
# Alternatively:
|
|
|
|
#
|
2010-12-09 05:03:14 -05:00
|
|
|
# mount(SomeRackApp => "some_route")
|
2010-11-12 22:15:17 -05:00
|
|
|
#
|
2011-02-05 13:12:09 -05:00
|
|
|
# For options, see +match+, as +mount+ uses it internally.
|
|
|
|
#
|
2010-11-12 22:15:17 -05:00
|
|
|
# All mounted applications come with routing helpers to access them.
|
|
|
|
# These are named after the class specified, so for the above example
|
|
|
|
# the helper is either +some_rack_app_path+ or +some_rack_app_url+.
|
|
|
|
# To customize this helper's name, use the +:as+ option:
|
|
|
|
#
|
2010-12-09 05:03:14 -05:00
|
|
|
# mount(SomeRackApp => "some_route", :as => "exciting")
|
2010-11-12 22:15:17 -05:00
|
|
|
#
|
|
|
|
# This will generate the +exciting_path+ and +exciting_url+ helpers
|
|
|
|
# which can be used to navigate to this mounted app.
|
2010-04-02 13:13:47 -04:00
|
|
|
def mount(app, options = nil)
|
|
|
|
if options
|
|
|
|
path = options.delete(:at)
|
|
|
|
else
|
|
|
|
options = app
|
|
|
|
app, path = options.find { |k, v| k.respond_to?(:call) }
|
|
|
|
options.delete(app) if app
|
|
|
|
end
|
|
|
|
|
|
|
|
raise "A rack application must be specified" unless path
|
|
|
|
|
2010-07-01 03:04:30 -04:00
|
|
|
options[:as] ||= app_name(app)
|
|
|
|
|
2010-08-24 15:56:51 -04:00
|
|
|
match(path, options.merge(:to => app, :anchor => false, :format => false))
|
2010-07-01 03:04:30 -04:00
|
|
|
|
|
|
|
define_generate_prefix(app, options[:as])
|
2010-04-02 13:13:47 -04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2010-03-09 22:20:13 -05:00
|
|
|
def default_url_options=(options)
|
|
|
|
@set.default_url_options = options
|
|
|
|
end
|
|
|
|
alias_method :default_url_options, :default_url_options=
|
2010-07-01 03:04:30 -04:00
|
|
|
|
2010-09-01 13:17:47 -04:00
|
|
|
def with_default_scope(scope, &block)
|
|
|
|
scope(scope) do
|
|
|
|
instance_exec(&block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-07-01 03:04:30 -04:00
|
|
|
private
|
|
|
|
def app_name(app)
|
|
|
|
return unless app.respond_to?(:routes)
|
2010-07-26 11:05:04 -04:00
|
|
|
|
|
|
|
if app.respond_to?(:railtie_name)
|
|
|
|
app.railtie_name
|
|
|
|
else
|
|
|
|
class_name = app.class.is_a?(Class) ? app.name : app.class.name
|
|
|
|
ActiveSupport::Inflector.underscore(class_name).gsub("/", "_")
|
|
|
|
end
|
2010-07-01 03:04:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def define_generate_prefix(app, name)
|
2010-12-09 07:15:25 -05:00
|
|
|
return unless app.respond_to?(:routes) && app.routes.respond_to?(:define_mounted_helper)
|
2010-07-01 03:04:30 -04:00
|
|
|
|
|
|
|
_route = @set.named_routes.routes[name.to_sym]
|
2010-07-18 13:49:51 -04:00
|
|
|
_routes = @set
|
|
|
|
app.routes.define_mounted_helper(name)
|
2010-07-01 03:04:30 -04:00
|
|
|
app.routes.class_eval do
|
|
|
|
define_method :_generate_prefix do |options|
|
2010-07-14 19:18:02 -04:00
|
|
|
prefix_options = options.slice(*_route.segment_keys)
|
2010-07-01 03:04:30 -04:00
|
|
|
# we must actually delete prefix segment keys to avoid passing them to next url_for
|
|
|
|
_route.segment_keys.each { |k| options.delete(k) }
|
2010-07-18 13:49:51 -04:00
|
|
|
_routes.url_helpers.send("#{name}_path", prefix_options)
|
2010-07-01 03:04:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-12-07 20:59:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
module HttpHelpers
|
2010-08-28 18:04:14 -04:00
|
|
|
# Define a route that only recognizes HTTP GET.
|
2011-01-28 16:55:21 -05:00
|
|
|
# For supported arguments, see <tt>Base#match</tt>.
|
2010-11-06 20:26:18 -04:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# get 'bacon', :to => 'food#bacon'
|
2009-12-07 20:59:23 -05:00
|
|
|
def get(*args, &block)
|
|
|
|
map_method(:get, *args, &block)
|
|
|
|
end
|
|
|
|
|
2010-08-28 18:04:14 -04:00
|
|
|
# Define a route that only recognizes HTTP POST.
|
2011-01-28 16:55:21 -05:00
|
|
|
# For supported arguments, see <tt>Base#match</tt>.
|
2010-11-06 20:26:18 -04:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# post 'bacon', :to => 'food#bacon'
|
2009-12-07 20:59:23 -05:00
|
|
|
def post(*args, &block)
|
|
|
|
map_method(:post, *args, &block)
|
|
|
|
end
|
|
|
|
|
2010-08-28 18:04:14 -04:00
|
|
|
# Define a route that only recognizes HTTP PUT.
|
2011-01-28 16:55:21 -05:00
|
|
|
# For supported arguments, see <tt>Base#match</tt>.
|
2010-11-06 20:26:18 -04:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# put 'bacon', :to => 'food#bacon'
|
2009-12-07 20:59:23 -05:00
|
|
|
def put(*args, &block)
|
|
|
|
map_method(:put, *args, &block)
|
|
|
|
end
|
|
|
|
|
2010-11-06 20:26:18 -04:00
|
|
|
# Define a route that only recognizes HTTP PUT.
|
2011-01-28 16:55:21 -05:00
|
|
|
# For supported arguments, see <tt>Base#match</tt>.
|
2010-11-06 20:26:18 -04:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# delete 'broccoli', :to => 'food#broccoli'
|
2009-12-07 20:59:23 -05:00
|
|
|
def delete(*args, &block)
|
|
|
|
map_method(:delete, *args, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def map_method(method, *args, &block)
|
|
|
|
options = args.extract_options!
|
|
|
|
options[:via] = method
|
|
|
|
args.push(options)
|
|
|
|
match(*args, &block)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-28 18:04:14 -04:00
|
|
|
# You may wish to organize groups of controllers under a namespace.
|
|
|
|
# Most commonly, you might group a number of administrative controllers
|
|
|
|
# under an +admin+ namespace. You would place these controllers under
|
|
|
|
# the app/controllers/admin directory, and you can group them together
|
|
|
|
# in your router:
|
|
|
|
#
|
|
|
|
# namespace "admin" do
|
|
|
|
# resources :posts, :comments
|
|
|
|
# end
|
2010-09-22 15:11:15 -04:00
|
|
|
#
|
2010-08-28 18:04:14 -04:00
|
|
|
# This will create a number of routes for each of the posts and comments
|
|
|
|
# controller. For Admin::PostsController, Rails will create:
|
2010-09-22 15:11:15 -04:00
|
|
|
#
|
2010-11-23 17:21:25 -05:00
|
|
|
# GET /admin/posts
|
|
|
|
# GET /admin/posts/new
|
|
|
|
# POST /admin/posts
|
|
|
|
# GET /admin/posts/1
|
|
|
|
# GET /admin/posts/1/edit
|
|
|
|
# PUT /admin/posts/1
|
|
|
|
# DELETE /admin/posts/1
|
2010-09-22 15:11:15 -04:00
|
|
|
#
|
2010-11-06 20:33:06 -04:00
|
|
|
# If you want to route /posts (without the prefix /admin) to
|
2010-08-28 18:04:14 -04:00
|
|
|
# Admin::PostsController, you could use
|
2010-09-22 15:11:15 -04:00
|
|
|
#
|
2010-08-28 18:04:14 -04:00
|
|
|
# scope :module => "admin" do
|
2010-11-06 20:33:06 -04:00
|
|
|
# resources :posts
|
2010-08-28 18:04:14 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# or, for a single case
|
2010-09-22 15:11:15 -04:00
|
|
|
#
|
2010-08-28 18:04:14 -04:00
|
|
|
# resources :posts, :module => "admin"
|
2010-09-22 15:11:15 -04:00
|
|
|
#
|
2010-11-06 20:33:06 -04:00
|
|
|
# If you want to route /admin/posts to PostsController
|
2010-08-28 18:04:14 -04:00
|
|
|
# (without the Admin:: module prefix), you could use
|
2010-09-22 15:11:15 -04:00
|
|
|
#
|
2010-08-28 18:04:14 -04:00
|
|
|
# scope "/admin" do
|
2010-11-06 20:33:06 -04:00
|
|
|
# resources :posts
|
2010-08-28 18:04:14 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# or, for a single case
|
2010-09-22 15:11:15 -04:00
|
|
|
#
|
2010-08-28 18:04:14 -04:00
|
|
|
# resources :posts, :path => "/admin"
|
|
|
|
#
|
|
|
|
# In each of these cases, the named routes remain the same as if you did
|
|
|
|
# not use scope. In the last case, the following paths map to
|
|
|
|
# PostsController:
|
2010-09-22 15:11:15 -04:00
|
|
|
#
|
2010-11-23 17:21:25 -05:00
|
|
|
# GET /admin/posts
|
|
|
|
# GET /admin/posts/new
|
|
|
|
# POST /admin/posts
|
|
|
|
# GET /admin/posts/1
|
|
|
|
# GET /admin/posts/1/edit
|
|
|
|
# PUT /admin/posts/1
|
|
|
|
# DELETE /admin/posts/1
|
2009-12-07 20:59:23 -05:00
|
|
|
module Scoping
|
2010-03-31 09:54:07 -04:00
|
|
|
def initialize(*args) #:nodoc:
|
2009-12-07 20:59:23 -05:00
|
|
|
@scope = {}
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2011-01-29 10:16:45 -05:00
|
|
|
# Scopes a set of routes to the given default options.
|
2010-12-11 02:25:28 -05:00
|
|
|
#
|
|
|
|
# Take the following route definition as an example:
|
|
|
|
#
|
|
|
|
# scope :path => ":account_id", :as => "account" do
|
|
|
|
# resources :projects
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# This generates helpers such as +account_projects_path+, just like +resources+ does.
|
|
|
|
# The difference here being that the routes generated are like /rails/projects/2,
|
|
|
|
# rather than /accounts/rails/projects/2.
|
|
|
|
#
|
2011-02-03 23:51:06 -05:00
|
|
|
# === Options
|
2010-11-06 22:11:39 -04:00
|
|
|
#
|
2011-01-29 10:16:45 -05:00
|
|
|
# Takes same options as <tt>Base#match</tt> and <tt>Resources#resources</tt>.
|
2010-11-06 22:11:39 -04:00
|
|
|
#
|
2011-01-29 10:16:45 -05:00
|
|
|
# === Examples
|
2010-11-06 22:21:56 -04:00
|
|
|
#
|
2011-01-29 10:16:45 -05:00
|
|
|
# # route /posts (without the prefix /admin) to Admin::PostsController
|
|
|
|
# scope :module => "admin" do
|
|
|
|
# resources :posts
|
|
|
|
# end
|
2010-11-06 22:21:56 -04:00
|
|
|
#
|
2011-01-29 10:16:45 -05:00
|
|
|
# # prefix the posts resource's requests with '/admin'
|
|
|
|
# scope :path => "/admin" do
|
|
|
|
# resources :posts
|
|
|
|
# end
|
2010-11-06 22:21:56 -04:00
|
|
|
#
|
2011-01-29 10:16:45 -05:00
|
|
|
# # prefix the routing helper name: sekret_posts_path instead of posts_path
|
|
|
|
# scope :as => "sekret" do
|
|
|
|
# resources :posts
|
|
|
|
# end
|
2009-12-07 20:59:23 -05:00
|
|
|
def scope(*args)
|
|
|
|
options = args.extract_options!
|
2010-03-28 06:10:38 -04:00
|
|
|
options = options.dup
|
2009-12-07 20:59:23 -05:00
|
|
|
|
2010-08-24 15:42:14 -04:00
|
|
|
options[:path] = args.first if args.first.is_a?(String)
|
2010-01-13 21:21:04 -05:00
|
|
|
recover = {}
|
2010-01-10 12:42:45 -05:00
|
|
|
|
2010-01-13 21:21:04 -05:00
|
|
|
options[:constraints] ||= {}
|
|
|
|
unless options[:constraints].is_a?(Hash)
|
|
|
|
block, options[:constraints] = options[:constraints], {}
|
2010-01-08 20:51:31 -05:00
|
|
|
end
|
2009-12-07 20:59:23 -05:00
|
|
|
|
2010-01-13 21:21:04 -05:00
|
|
|
scope_options.each do |option|
|
|
|
|
if value = options.delete(option)
|
|
|
|
recover[option] = @scope[option]
|
|
|
|
@scope[option] = send("merge_#{option}_scope", @scope[option], value)
|
|
|
|
end
|
2009-12-07 20:59:23 -05:00
|
|
|
end
|
|
|
|
|
2010-01-13 21:21:04 -05:00
|
|
|
recover[:block] = @scope[:blocks]
|
|
|
|
@scope[:blocks] = merge_blocks_scope(@scope[:blocks], block)
|
2010-01-10 12:42:45 -05:00
|
|
|
|
2010-01-13 21:21:04 -05:00
|
|
|
recover[:options] = @scope[:options]
|
|
|
|
@scope[:options] = merge_options_scope(@scope[:options], options)
|
2009-12-07 20:59:23 -05:00
|
|
|
|
|
|
|
yield
|
|
|
|
self
|
|
|
|
ensure
|
2010-01-13 21:21:04 -05:00
|
|
|
scope_options.each do |option|
|
|
|
|
@scope[option] = recover[option] if recover.has_key?(option)
|
|
|
|
end
|
|
|
|
|
|
|
|
@scope[:options] = recover[:options]
|
|
|
|
@scope[:blocks] = recover[:block]
|
2009-12-07 20:59:23 -05:00
|
|
|
end
|
|
|
|
|
2010-11-06 21:49:57 -04:00
|
|
|
# Scopes routes to a specific controller
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# controller "food" do
|
|
|
|
# match "bacon", :action => "bacon"
|
|
|
|
# end
|
2010-08-24 15:42:14 -04:00
|
|
|
def controller(controller, options={})
|
|
|
|
options[:controller] = controller
|
|
|
|
scope(options) { yield }
|
2009-12-07 20:59:23 -05:00
|
|
|
end
|
|
|
|
|
2010-11-06 22:04:25 -04:00
|
|
|
# Scopes routes to a specific namespace. For example:
|
|
|
|
#
|
|
|
|
# namespace :admin do
|
|
|
|
# resources :posts
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# This generates the following routes:
|
|
|
|
#
|
2010-12-12 22:03:41 -05:00
|
|
|
# admin_posts GET /admin/posts(.:format) {:action=>"index", :controller=>"admin/posts"}
|
|
|
|
# admin_posts POST /admin/posts(.:format) {:action=>"create", :controller=>"admin/posts"}
|
|
|
|
# new_admin_post GET /admin/posts/new(.:format) {:action=>"new", :controller=>"admin/posts"}
|
|
|
|
# edit_admin_post GET /admin/posts/:id/edit(.:format) {:action=>"edit", :controller=>"admin/posts"}
|
|
|
|
# admin_post GET /admin/posts/:id(.:format) {:action=>"show", :controller=>"admin/posts"}
|
|
|
|
# admin_post PUT /admin/posts/:id(.:format) {:action=>"update", :controller=>"admin/posts"}
|
|
|
|
# admin_post DELETE /admin/posts/:id(.:format) {:action=>"destroy", :controller=>"admin/posts"}
|
2010-11-06 22:05:14 -04:00
|
|
|
#
|
2011-02-03 23:51:06 -05:00
|
|
|
# === Options
|
2010-11-06 22:05:55 -04:00
|
|
|
#
|
2011-01-29 10:16:45 -05:00
|
|
|
# The +:path+, +:as+, +:module+, +:shallow_path+ and +:shallow_prefix+
|
|
|
|
# options all default to the name of the namespace.
|
2010-11-06 22:05:55 -04:00
|
|
|
#
|
2011-01-29 10:16:45 -05:00
|
|
|
# For options, see <tt>Base#match</tt>. For +:shallow_path+ option, see
|
|
|
|
# <tt>Resources#resources</tt>.
|
2010-11-06 22:05:55 -04:00
|
|
|
#
|
2011-01-29 10:16:45 -05:00
|
|
|
# === Examples
|
2010-11-06 22:05:55 -04:00
|
|
|
#
|
2011-01-29 10:16:45 -05:00
|
|
|
# # accessible through /sekret/posts rather than /admin/posts
|
|
|
|
# namespace :admin, :path => "sekret" do
|
|
|
|
# resources :posts
|
|
|
|
# end
|
2010-11-06 22:07:29 -04:00
|
|
|
#
|
2011-01-29 10:16:45 -05:00
|
|
|
# # maps to Sekret::PostsController rather than Admin::PostsController
|
|
|
|
# namespace :admin, :module => "sekret" do
|
|
|
|
# resources :posts
|
|
|
|
# end
|
2010-11-12 22:16:06 -05:00
|
|
|
#
|
2011-01-29 10:16:45 -05:00
|
|
|
# # generates sekret_posts_path rather than admin_posts_path
|
|
|
|
# namespace :admin, :as => "sekret" do
|
|
|
|
# resources :posts
|
|
|
|
# end
|
2010-06-22 16:26:35 -04:00
|
|
|
def namespace(path, options = {})
|
2010-04-10 02:09:15 -04:00
|
|
|
path = path.to_s
|
2010-06-22 16:26:35 -04:00
|
|
|
options = { :path => path, :as => path, :module => path,
|
|
|
|
:shallow_path => path, :shallow_prefix => path }.merge!(options)
|
|
|
|
scope(options) { yield }
|
2009-12-07 20:59:23 -05:00
|
|
|
end
|
2010-11-30 10:36:01 -05:00
|
|
|
|
2010-11-13 18:32:54 -05:00
|
|
|
# === 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.
|
2010-11-30 10:36:01 -05:00
|
|
|
#
|
2010-11-13 18:32:54 -05:00
|
|
|
# You may use this to also resrict other parameters:
|
|
|
|
#
|
|
|
|
# resources :posts do
|
|
|
|
# constraints(:post_id => /\d+\.\d+) do
|
|
|
|
# resources :comments
|
|
|
|
# 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 critera:
|
|
|
|
#
|
|
|
|
# constraints(lambda { |req| req.env["HTTP_USER_AGENT"] =~ /iPhone/ }) 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)
|
|
|
|
# request.env["HTTP_USER_AGENT"] =~ /iPhone/
|
|
|
|
# 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
|
2009-12-07 20:59:23 -05:00
|
|
|
def constraints(constraints = {})
|
|
|
|
scope(:constraints => constraints) { yield }
|
|
|
|
end
|
|
|
|
|
2010-11-06 20:42:33 -04:00
|
|
|
# Allows you to set default parameters for a route, such as this:
|
2011-01-29 10:16:45 -05:00
|
|
|
# defaults :id => 'home' do
|
|
|
|
# match 'scoped_pages/(:id)', :to => 'pages#show'
|
|
|
|
# end
|
2010-11-06 20:42:33 -04:00
|
|
|
# Using this, the +:id+ parameter here will default to 'home'.
|
2010-03-26 08:16:25 -04:00
|
|
|
def defaults(defaults = {})
|
|
|
|
scope(:defaults => defaults) { yield }
|
|
|
|
end
|
|
|
|
|
2010-01-13 21:21:04 -05:00
|
|
|
private
|
2010-11-25 04:44:42 -05:00
|
|
|
def scope_options #:nodoc:
|
2010-01-13 21:21:04 -05:00
|
|
|
@scope_options ||= private_methods.grep(/^merge_(.+)_scope$/) { $1.to_sym }
|
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def merge_path_scope(parent, child) #:nodoc:
|
2010-06-08 16:20:46 -04:00
|
|
|
Mapper.normalize_path("#{parent}/#{child}")
|
2010-01-13 21:21:04 -05:00
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def merge_shallow_path_scope(parent, child) #:nodoc:
|
2010-06-19 07:53:09 -04:00
|
|
|
Mapper.normalize_path("#{parent}/#{child}")
|
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def merge_as_scope(parent, child) #:nodoc:
|
2010-06-08 16:20:46 -04:00
|
|
|
parent ? "#{parent}_#{child}" : child
|
2010-01-13 21:21:04 -05:00
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def merge_shallow_prefix_scope(parent, child) #:nodoc:
|
2010-06-19 07:53:09 -04:00
|
|
|
parent ? "#{parent}_#{child}" : child
|
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def merge_module_scope(parent, child) #:nodoc:
|
2010-01-13 21:21:04 -05:00
|
|
|
parent ? "#{parent}/#{child}" : child
|
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def merge_controller_scope(parent, child) #:nodoc:
|
2010-07-12 09:11:15 -04:00
|
|
|
child
|
2010-01-13 21:21:04 -05:00
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def merge_path_names_scope(parent, child) #:nodoc:
|
2010-01-13 21:21:04 -05:00
|
|
|
merge_options_scope(parent, child)
|
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def merge_constraints_scope(parent, child) #:nodoc:
|
2010-01-13 21:21:04 -05:00
|
|
|
merge_options_scope(parent, child)
|
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def merge_defaults_scope(parent, child) #:nodoc:
|
2010-03-26 08:16:25 -04:00
|
|
|
merge_options_scope(parent, child)
|
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def merge_blocks_scope(parent, child) #:nodoc:
|
2010-06-22 17:52:12 -04:00
|
|
|
merged = parent ? parent.dup : []
|
|
|
|
merged << child if child
|
|
|
|
merged
|
2010-01-13 21:21:04 -05:00
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def merge_options_scope(parent, child) #:nodoc:
|
2010-07-06 14:06:02 -04:00
|
|
|
(parent || {}).except(*override_keys(child)).merge(child)
|
2010-01-13 21:21:04 -05:00
|
|
|
end
|
2010-06-10 13:31:28 -04:00
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def merge_shallow_scope(parent, child) #:nodoc:
|
2010-06-10 13:31:28 -04:00
|
|
|
child ? true : false
|
|
|
|
end
|
2010-07-06 14:06:02 -04:00
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def override_keys(child) #:nodoc:
|
2010-07-06 14:06:02 -04:00
|
|
|
child.key?(:only) || child.key?(:except) ? [:only, :except] : []
|
|
|
|
end
|
2009-12-07 20:59:23 -05:00
|
|
|
end
|
|
|
|
|
2010-08-28 18:04:14 -04:00
|
|
|
# Resource routing allows you to quickly declare all of the common routes
|
|
|
|
# for a given resourceful controller. Instead of declaring separate routes
|
|
|
|
# for your +index+, +show+, +new+, +edit+, +create+, +update+ and +destroy+
|
|
|
|
# actions, a resourceful route declares them in a single line of code:
|
|
|
|
#
|
|
|
|
# resources :photos
|
|
|
|
#
|
|
|
|
# Sometimes, you have a resource that clients always look up without
|
|
|
|
# referencing an ID. A common example, /profile always shows the profile of
|
|
|
|
# the currently logged in user. In this case, you can use a singular resource
|
|
|
|
# to map /profile (rather than /profile/:id) to the show action.
|
|
|
|
#
|
|
|
|
# resource :profile
|
|
|
|
#
|
|
|
|
# It's common to have resources that are logically children of other
|
|
|
|
# resources:
|
|
|
|
#
|
|
|
|
# resources :magazines do
|
|
|
|
# resources :ads
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# You may wish to organize groups of controllers under a namespace. Most
|
|
|
|
# commonly, you might group a number of administrative controllers under
|
|
|
|
# an +admin+ namespace. You would place these controllers under the
|
|
|
|
# app/controllers/admin directory, and you can group them together in your
|
|
|
|
# router:
|
|
|
|
#
|
|
|
|
# namespace "admin" do
|
|
|
|
# resources :posts, :comments
|
|
|
|
# end
|
|
|
|
#
|
2009-10-20 13:31:23 -04:00
|
|
|
module Resources
|
2010-06-30 05:33:15 -04:00
|
|
|
# CANONICAL_ACTIONS holds all actions that does not need a prefix or
|
|
|
|
# a path appended since they fit properly in their scope level.
|
2010-08-24 15:42:14 -04:00
|
|
|
VALID_ON_OPTIONS = [:new, :collection, :member]
|
|
|
|
RESOURCE_OPTIONS = [:as, :controller, :path, :only, :except]
|
|
|
|
CANONICAL_ACTIONS = %w(index create new show update destroy)
|
2010-06-28 07:04:13 -04:00
|
|
|
|
2009-11-29 17:59:44 -05:00
|
|
|
class Resource #:nodoc:
|
2010-06-30 05:33:15 -04:00
|
|
|
DEFAULT_ACTIONS = [:index, :create, :new, :show, :update, :destroy, :edit]
|
2010-01-13 18:23:14 -05:00
|
|
|
|
2010-04-02 13:13:47 -04:00
|
|
|
attr_reader :controller, :path, :options
|
2009-11-29 17:59:44 -05:00
|
|
|
|
|
|
|
def initialize(entities, options = {})
|
2010-04-02 13:13:47 -04:00
|
|
|
@name = entities.to_s
|
2010-08-20 03:33:48 -04:00
|
|
|
@path = (options.delete(:path) || @name).to_s
|
2010-06-26 03:15:33 -04:00
|
|
|
@controller = (options.delete(:controller) || @name).to_s
|
2010-06-22 17:23:18 -04:00
|
|
|
@as = options.delete(:as)
|
2010-04-02 13:13:47 -04:00
|
|
|
@options = options
|
2009-11-29 17:59:44 -05:00
|
|
|
end
|
|
|
|
|
2010-01-13 18:23:14 -05:00
|
|
|
def default_actions
|
2010-06-30 05:33:15 -04:00
|
|
|
self.class::DEFAULT_ACTIONS
|
2010-01-13 18:23:14 -05:00
|
|
|
end
|
|
|
|
|
2010-07-06 14:06:02 -04:00
|
|
|
def actions
|
2010-09-05 09:59:25 -04:00
|
|
|
if only = @options[:only]
|
2010-07-06 14:06:02 -04:00
|
|
|
Array(only).map(&:to_sym)
|
2010-09-05 09:59:25 -04:00
|
|
|
elsif except = @options[:except]
|
2010-07-06 14:06:02 -04:00
|
|
|
default_actions - Array(except).map(&:to_sym)
|
|
|
|
else
|
|
|
|
default_actions
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-29 17:59:44 -05:00
|
|
|
def name
|
2010-06-22 17:23:18 -04:00
|
|
|
@as || @name
|
2009-11-29 17:59:44 -05:00
|
|
|
end
|
|
|
|
|
2010-04-02 13:13:47 -04:00
|
|
|
def plural
|
2010-08-18 08:35:05 -04:00
|
|
|
@plural ||= name.to_s
|
2010-04-02 13:13:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def singular
|
2010-08-18 08:35:05 -04:00
|
|
|
@singular ||= name.to_s.singularize
|
2009-11-29 17:59:44 -05:00
|
|
|
end
|
|
|
|
|
2010-08-18 08:35:05 -04:00
|
|
|
alias :member_name :singular
|
2009-11-29 17:59:44 -05:00
|
|
|
|
2011-01-27 12:25:22 -05:00
|
|
|
# Checks for uncountable plurals, and appends "_index" if the plural
|
|
|
|
# and singular form are the same.
|
2009-11-29 17:59:44 -05:00
|
|
|
def collection_name
|
2010-06-10 13:31:28 -04:00
|
|
|
singular == plural ? "#{plural}_index" : plural
|
2010-01-19 09:20:52 -05:00
|
|
|
end
|
|
|
|
|
2010-06-10 13:31:28 -04:00
|
|
|
def resource_scope
|
2010-07-04 01:52:52 -04:00
|
|
|
{ :controller => controller }
|
2010-06-10 13:31:28 -04:00
|
|
|
end
|
|
|
|
|
2010-08-18 08:35:05 -04:00
|
|
|
alias :collection_scope :path
|
2010-06-10 13:31:28 -04:00
|
|
|
|
|
|
|
def member_scope
|
2010-07-04 01:52:52 -04:00
|
|
|
"#{path}/:id"
|
2010-06-10 13:31:28 -04:00
|
|
|
end
|
|
|
|
|
2010-06-30 05:33:15 -04:00
|
|
|
def new_scope(new_path)
|
2010-07-04 01:52:52 -04:00
|
|
|
"#{path}/#{new_path}"
|
2010-06-10 13:31:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def nested_scope
|
2010-07-04 01:52:52 -04:00
|
|
|
"#{path}/:#{singular}_id"
|
2010-03-28 06:10:38 -04:00
|
|
|
end
|
2010-07-04 01:52:52 -04:00
|
|
|
|
2009-11-29 17:59:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class SingletonResource < Resource #:nodoc:
|
2010-06-30 05:33:15 -04:00
|
|
|
DEFAULT_ACTIONS = [:show, :create, :update, :destroy, :new, :edit]
|
2010-01-13 18:23:14 -05:00
|
|
|
|
2010-06-26 03:15:33 -04:00
|
|
|
def initialize(entities, options)
|
2010-09-24 12:04:51 -04:00
|
|
|
@as = nil
|
2010-06-26 03:15:33 -04:00
|
|
|
@name = entities.to_s
|
2010-08-20 03:33:48 -04:00
|
|
|
@path = (options.delete(:path) || @name).to_s
|
2010-07-04 01:52:52 -04:00
|
|
|
@controller = (options.delete(:controller) || plural).to_s
|
2010-06-26 03:15:33 -04:00
|
|
|
@as = options.delete(:as)
|
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
2010-08-18 08:35:05 -04:00
|
|
|
def plural
|
|
|
|
@plural ||= name.to_s.pluralize
|
2010-01-19 09:20:52 -05:00
|
|
|
end
|
|
|
|
|
2010-08-18 08:35:05 -04:00
|
|
|
def singular
|
|
|
|
@singular ||= name.to_s
|
2010-04-05 01:48:02 -04:00
|
|
|
end
|
2010-08-18 08:35:05 -04:00
|
|
|
|
|
|
|
alias :member_name :singular
|
|
|
|
alias :collection_name :singular
|
|
|
|
|
|
|
|
alias :member_scope :path
|
|
|
|
alias :nested_scope :path
|
2009-11-29 17:59:44 -05:00
|
|
|
end
|
|
|
|
|
2010-03-31 09:54:07 -04:00
|
|
|
def initialize(*args) #:nodoc:
|
2010-01-13 21:21:04 -05:00
|
|
|
super
|
2010-04-10 15:13:21 -04:00
|
|
|
@scope[:path_names] = @set.resources_path_names
|
2010-01-13 21:21:04 -05:00
|
|
|
end
|
|
|
|
|
2010-06-22 18:43:25 -04:00
|
|
|
def resources_path_names(options)
|
|
|
|
@scope[:path_names].merge!(options)
|
|
|
|
end
|
|
|
|
|
2010-08-28 18:04:14 -04:00
|
|
|
# Sometimes, you have a resource that clients always look up without
|
|
|
|
# referencing an ID. A common example, /profile always shows the
|
|
|
|
# profile of the currently logged in user. In this case, you can use
|
|
|
|
# a singular resource to map /profile (rather than /profile/:id) to
|
|
|
|
# the show action:
|
|
|
|
#
|
|
|
|
# resource :geocoder
|
|
|
|
#
|
|
|
|
# creates six different routes in your application, all mapping to
|
|
|
|
# the GeoCoders controller (note that the controller is named after
|
|
|
|
# the plural):
|
|
|
|
#
|
|
|
|
# GET /geocoder/new
|
|
|
|
# POST /geocoder
|
|
|
|
# GET /geocoder
|
|
|
|
# GET /geocoder/edit
|
|
|
|
# PUT /geocoder
|
|
|
|
# DELETE /geocoder
|
2011-01-29 10:16:45 -05:00
|
|
|
#
|
2011-02-03 23:51:06 -05:00
|
|
|
# === Options
|
2011-01-29 10:16:45 -05:00
|
|
|
# Takes same options as +resources+.
|
2009-10-20 13:31:23 -04:00
|
|
|
def resource(*resources, &block)
|
2009-11-29 18:01:14 -05:00
|
|
|
options = resources.extract_options!
|
2009-10-20 13:31:23 -04:00
|
|
|
|
2010-02-16 16:31:40 -05:00
|
|
|
if apply_common_behavior_for(:resource, resources, options, &block)
|
2010-01-13 21:21:04 -05:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2010-06-10 13:31:28 -04:00
|
|
|
resource_scope(SingletonResource.new(resources.pop, options)) do
|
|
|
|
yield if block_given?
|
2010-02-12 05:39:19 -05:00
|
|
|
|
2010-10-08 17:00:27 -04:00
|
|
|
collection do
|
2010-06-30 05:33:15 -04:00
|
|
|
post :create
|
2010-07-06 14:06:02 -04:00
|
|
|
end if parent_resource.actions.include?(:create)
|
2010-06-30 05:33:15 -04:00
|
|
|
|
2010-10-08 17:00:27 -04:00
|
|
|
new do
|
2010-06-30 05:33:15 -04:00
|
|
|
get :new
|
2010-07-06 14:06:02 -04:00
|
|
|
end if parent_resource.actions.include?(:new)
|
2009-12-08 17:13:00 -05:00
|
|
|
|
2010-10-08 17:00:27 -04:00
|
|
|
member do
|
2010-08-19 09:42:14 -04:00
|
|
|
get :edit if parent_resource.actions.include?(:edit)
|
2010-07-06 14:06:02 -04:00
|
|
|
get :show if parent_resource.actions.include?(:show)
|
|
|
|
put :update if parent_resource.actions.include?(:update)
|
|
|
|
delete :destroy if parent_resource.actions.include?(:destroy)
|
2009-10-20 11:46:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-20 13:31:23 -04:00
|
|
|
self
|
2009-10-20 11:46:27 -04:00
|
|
|
end
|
|
|
|
|
2010-08-28 18:04:14 -04:00
|
|
|
# In Rails, a resourceful route provides a mapping between HTTP verbs
|
|
|
|
# and URLs and controller actions. By convention, each action also maps
|
|
|
|
# to particular CRUD operations in a database. A single entry in the
|
|
|
|
# routing file, such as
|
|
|
|
#
|
|
|
|
# resources :photos
|
|
|
|
#
|
|
|
|
# creates seven different routes in your application, all mapping to
|
|
|
|
# the Photos controller:
|
|
|
|
#
|
|
|
|
# GET /photos/new
|
|
|
|
# POST /photos
|
|
|
|
# GET /photos/:id
|
|
|
|
# GET /photos/:id/edit
|
|
|
|
# PUT /photos/:id
|
|
|
|
# DELETE /photos/:id
|
2010-11-23 17:27:12 -05:00
|
|
|
#
|
2010-11-23 17:31:29 -05:00
|
|
|
# Resources can also be nested infinitely by using this block syntax:
|
|
|
|
#
|
|
|
|
# resources :photos do
|
|
|
|
# resources :comments
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# This generates the following comments routes:
|
|
|
|
#
|
|
|
|
# GET /photos/:id/comments/new
|
|
|
|
# POST /photos/:id/comments
|
|
|
|
# GET /photos/:id/comments/:id
|
|
|
|
# GET /photos/:id/comments/:id/edit
|
|
|
|
# PUT /photos/:id/comments/:id
|
|
|
|
# DELETE /photos/:id/comments/:id
|
|
|
|
#
|
2011-02-03 23:51:06 -05:00
|
|
|
# === Options
|
2011-01-29 10:16:45 -05:00
|
|
|
# Takes same options as <tt>Base#match</tt> as well as:
|
|
|
|
#
|
2010-11-12 22:16:32 -05:00
|
|
|
# [:path_names]
|
|
|
|
# Allows you to change the paths of the seven default actions.
|
|
|
|
# Paths not specified are not changed.
|
|
|
|
#
|
|
|
|
# resources :posts, :path_names => { :new => "brand_new" }
|
|
|
|
#
|
|
|
|
# The above example will now change /posts/new to /posts/brand_new
|
2010-11-23 17:27:12 -05:00
|
|
|
#
|
2011-01-29 10:16:45 -05:00
|
|
|
# [:only]
|
|
|
|
# Only generate routes for the given actions.
|
2010-11-23 17:27:12 -05:00
|
|
|
#
|
2011-01-29 10:16:45 -05:00
|
|
|
# resources :cows, :only => :show
|
|
|
|
# resources :cows, :only => [:show, :index]
|
2010-11-23 17:27:12 -05:00
|
|
|
#
|
2011-01-29 10:16:45 -05:00
|
|
|
# [:except]
|
|
|
|
# Generate all routes except for the given actions.
|
2010-11-23 17:28:21 -05:00
|
|
|
#
|
2011-01-29 10:16:45 -05:00
|
|
|
# resources :cows, :except => :show
|
|
|
|
# resources :cows, :except => [:show, :index]
|
|
|
|
#
|
|
|
|
# [:shallow]
|
|
|
|
# Generates shallow routes for nested resource(s). When placed on a parent resource,
|
|
|
|
# generates shallow routes for all nested resources.
|
|
|
|
#
|
|
|
|
# resources :posts, :shallow => true do
|
|
|
|
# resources :comments
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Is the same as:
|
|
|
|
#
|
|
|
|
# resources :posts do
|
|
|
|
# resources :comments
|
|
|
|
# end
|
|
|
|
# resources :comments
|
|
|
|
#
|
|
|
|
# [:shallow_path]
|
|
|
|
# Prefixes nested shallow routes with the specified path.
|
|
|
|
#
|
|
|
|
# scope :shallow_path => "sekret" do
|
|
|
|
# resources :posts do
|
|
|
|
# resources :comments, :shallow => true
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# The +comments+ resource here will have the following routes generated for it:
|
|
|
|
#
|
|
|
|
# post_comments GET /sekret/posts/:post_id/comments(.:format)
|
|
|
|
# post_comments POST /sekret/posts/:post_id/comments(.:format)
|
|
|
|
# new_post_comment GET /sekret/posts/:post_id/comments/new(.:format)
|
|
|
|
# edit_comment GET /sekret/comments/:id/edit(.:format)
|
|
|
|
# comment GET /sekret/comments/:id(.:format)
|
|
|
|
# comment PUT /sekret/comments/:id(.:format)
|
|
|
|
# comment DELETE /sekret/comments/:id(.:format)
|
|
|
|
#
|
|
|
|
# === Examples
|
2010-11-23 17:28:21 -05:00
|
|
|
#
|
2011-01-29 10:16:45 -05:00
|
|
|
# # routes call Admin::PostsController
|
|
|
|
# resources :posts, :module => "admin"
|
2010-11-23 17:28:21 -05:00
|
|
|
#
|
2011-01-29 10:16:45 -05:00
|
|
|
# # resource actions are at /admin/posts.
|
|
|
|
# resources :posts, :path => "admin"
|
2010-11-23 17:28:21 -05:00
|
|
|
#
|
2009-10-20 13:31:23 -04:00
|
|
|
def resources(*resources, &block)
|
2009-11-29 18:01:14 -05:00
|
|
|
options = resources.extract_options!
|
2009-10-20 11:46:27 -04:00
|
|
|
|
2010-02-16 16:31:40 -05:00
|
|
|
if apply_common_behavior_for(:resources, resources, options, &block)
|
2010-01-13 21:21:04 -05:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2010-06-10 13:31:28 -04:00
|
|
|
resource_scope(Resource.new(resources.pop, options)) do
|
2010-09-05 09:59:25 -04:00
|
|
|
yield if block_given?
|
2009-10-20 13:31:23 -04:00
|
|
|
|
2010-10-08 17:00:27 -04:00
|
|
|
collection do
|
2010-07-06 14:06:02 -04:00
|
|
|
get :index if parent_resource.actions.include?(:index)
|
|
|
|
post :create if parent_resource.actions.include?(:create)
|
2010-06-10 13:31:28 -04:00
|
|
|
end
|
2009-11-29 16:23:27 -05:00
|
|
|
|
2010-10-08 17:00:27 -04:00
|
|
|
new do
|
2010-06-30 05:33:15 -04:00
|
|
|
get :new
|
2010-07-06 14:06:02 -04:00
|
|
|
end if parent_resource.actions.include?(:new)
|
2010-06-30 05:33:15 -04:00
|
|
|
|
2010-10-08 17:00:27 -04:00
|
|
|
member do
|
2010-08-19 09:42:14 -04:00
|
|
|
get :edit if parent_resource.actions.include?(:edit)
|
2010-07-06 14:06:02 -04:00
|
|
|
get :show if parent_resource.actions.include?(:show)
|
|
|
|
put :update if parent_resource.actions.include?(:update)
|
|
|
|
delete :destroy if parent_resource.actions.include?(:destroy)
|
2009-10-20 11:46:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-20 13:31:23 -04:00
|
|
|
self
|
2009-10-20 11:46:27 -04:00
|
|
|
end
|
|
|
|
|
2010-08-28 18:04:14 -04:00
|
|
|
# To add a route to the collection:
|
|
|
|
#
|
|
|
|
# resources :photos do
|
|
|
|
# collection do
|
|
|
|
# get 'search'
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# This will enable Rails to recognize paths such as <tt>/photos/search</tt>
|
|
|
|
# with GET, and route to the search action of PhotosController. It will also
|
|
|
|
# create the <tt>search_photos_url</tt> and <tt>search_photos_path</tt>
|
|
|
|
# route helpers.
|
2009-10-20 13:31:23 -04:00
|
|
|
def collection
|
2010-10-08 17:00:27 -04:00
|
|
|
unless resource_scope?
|
|
|
|
raise ArgumentError, "can't use collection outside resource(s) scope"
|
2009-10-20 11:46:27 -04:00
|
|
|
end
|
|
|
|
|
2010-10-08 17:00:27 -04:00
|
|
|
with_scope_level(:collection) do
|
|
|
|
scope(parent_resource.collection_scope) do
|
|
|
|
yield
|
|
|
|
end
|
2009-10-20 13:31:23 -04:00
|
|
|
end
|
2009-10-20 11:46:27 -04:00
|
|
|
end
|
2009-10-20 11:14:46 -04:00
|
|
|
|
2010-08-28 18:04:14 -04:00
|
|
|
# To add a member route, add a member block into the resource block:
|
|
|
|
#
|
|
|
|
# resources :photos do
|
|
|
|
# member do
|
|
|
|
# get 'preview'
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# This will recognize <tt>/photos/1/preview</tt> with GET, and route to the
|
|
|
|
# preview action of PhotosController. It will also create the
|
|
|
|
# <tt>preview_photo_url</tt> and <tt>preview_photo_path</tt> helpers.
|
2009-10-20 13:31:23 -04:00
|
|
|
def member
|
2010-04-05 01:48:02 -04:00
|
|
|
unless resource_scope?
|
|
|
|
raise ArgumentError, "can't use member outside resource(s) scope"
|
2009-10-20 13:31:23 -04:00
|
|
|
end
|
2009-10-20 11:14:46 -04:00
|
|
|
|
2010-10-08 17:00:27 -04:00
|
|
|
with_scope_level(:member) do
|
|
|
|
scope(parent_resource.member_scope) do
|
|
|
|
yield
|
|
|
|
end
|
2010-04-05 01:48:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
unless resource_scope?
|
|
|
|
raise ArgumentError, "can't use new outside resource(s) scope"
|
|
|
|
end
|
2010-06-10 13:31:28 -04:00
|
|
|
|
2010-10-08 17:00:27 -04:00
|
|
|
with_scope_level(:new) do
|
|
|
|
scope(parent_resource.new_scope(action_path(:new))) do
|
|
|
|
yield
|
|
|
|
end
|
2009-10-20 13:31:23 -04:00
|
|
|
end
|
2009-10-20 11:14:46 -04:00
|
|
|
end
|
|
|
|
|
2009-12-08 17:06:46 -05:00
|
|
|
def nested
|
2010-04-05 01:48:02 -04:00
|
|
|
unless resource_scope?
|
|
|
|
raise ArgumentError, "can't use nested outside resource(s) scope"
|
2009-12-08 17:06:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
with_scope_level(:nested) do
|
2010-07-04 01:52:52 -04:00
|
|
|
if shallow?
|
2010-06-10 13:31:28 -04:00
|
|
|
with_exclusive_scope do
|
2010-06-19 07:53:09 -04:00
|
|
|
if @scope[:shallow_path].blank?
|
2010-07-04 01:52:52 -04:00
|
|
|
scope(parent_resource.nested_scope, nested_options) { yield }
|
2010-06-10 13:31:28 -04:00
|
|
|
else
|
2010-06-22 17:23:18 -04:00
|
|
|
scope(@scope[:shallow_path], :as => @scope[:shallow_prefix]) do
|
2010-07-04 01:52:52 -04:00
|
|
|
scope(parent_resource.nested_scope, nested_options) { yield }
|
2010-06-10 13:31:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2010-07-04 01:52:52 -04:00
|
|
|
scope(parent_resource.nested_scope, nested_options) { yield }
|
2009-12-08 17:06:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-10 00:55:46 -05:00
|
|
|
# See ActionDispatch::Routing::Mapper::Scoping#namespace
|
2010-06-22 16:26:35 -04:00
|
|
|
def namespace(path, options = {})
|
2010-06-08 17:26:51 -04:00
|
|
|
if resource_scope?
|
2010-04-14 04:34:42 -04:00
|
|
|
nested { super }
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-10 13:31:28 -04:00
|
|
|
def shallow
|
2011-02-04 11:51:34 -05:00
|
|
|
scope(:shallow => true, :shallow_path => @scope[:path]) do
|
2010-06-10 13:31:28 -04:00
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-07-04 01:52:52 -04:00
|
|
|
def shallow?
|
|
|
|
parent_resource.instance_of?(Resource) && @scope[:shallow]
|
|
|
|
end
|
|
|
|
|
2009-10-20 13:31:23 -04:00
|
|
|
def match(*args)
|
2010-06-30 06:34:15 -04:00
|
|
|
options = args.extract_options!.dup
|
2010-03-16 20:28:44 -04:00
|
|
|
options[:anchor] = true unless options.key?(:anchor)
|
|
|
|
|
2009-12-07 21:57:01 -05:00
|
|
|
if args.length > 1
|
2010-06-10 13:31:28 -04:00
|
|
|
args.each { |path| match(path, options.dup) }
|
2009-12-07 21:57:01 -05:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2010-06-30 05:33:15 -04:00
|
|
|
on = options.delete(:on)
|
|
|
|
if VALID_ON_OPTIONS.include?(on)
|
2010-06-10 13:31:28 -04:00
|
|
|
args.push(options)
|
2010-06-30 05:33:15 -04:00
|
|
|
return send(on){ match(*args) }
|
|
|
|
elsif on
|
|
|
|
raise ArgumentError, "Unknown scope #{on.inspect} given to :on"
|
2009-12-07 21:57:01 -05:00
|
|
|
end
|
|
|
|
|
2010-08-24 17:16:34 -04:00
|
|
|
if @scope[:scope_level] == :resources
|
|
|
|
args.push(options)
|
|
|
|
return nested { match(*args) }
|
|
|
|
elsif @scope[:scope_level] == :resource
|
2010-06-10 13:31:28 -04:00
|
|
|
args.push(options)
|
2009-10-20 13:31:23 -04:00
|
|
|
return member { match(*args) }
|
|
|
|
end
|
2009-10-20 11:14:46 -04:00
|
|
|
|
2010-06-30 06:34:15 -04:00
|
|
|
action = args.first
|
2010-08-24 15:42:14 -04:00
|
|
|
path = path_for_action(action, options.delete(:path))
|
2010-06-10 13:31:28 -04:00
|
|
|
|
2010-08-24 15:42:14 -04:00
|
|
|
if action.to_s =~ /^[\w\/]+$/
|
|
|
|
options[:action] ||= action unless action.to_s.include?("/")
|
|
|
|
else
|
2010-09-29 08:24:32 -04:00
|
|
|
action = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if options.key?(:as) && !options[:as]
|
|
|
|
options.delete(:as)
|
|
|
|
else
|
|
|
|
options[:as] = name_for_action(options[:as], action)
|
2009-10-20 13:31:23 -04:00
|
|
|
end
|
2009-10-20 11:14:46 -04:00
|
|
|
|
2010-08-24 15:42:14 -04:00
|
|
|
super(path, options)
|
2009-10-20 11:14:46 -04:00
|
|
|
end
|
|
|
|
|
2010-05-17 11:36:34 -04:00
|
|
|
def root(options={})
|
2010-06-10 13:31:28 -04:00
|
|
|
if @scope[:scope_level] == :resources
|
2010-08-24 15:42:14 -04:00
|
|
|
with_scope_level(:root) do
|
|
|
|
scope(parent_resource.path) do
|
2010-06-10 13:31:28 -04:00
|
|
|
super(options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
super(options)
|
|
|
|
end
|
2010-05-17 11:36:34 -04:00
|
|
|
end
|
|
|
|
|
2009-12-07 20:47:47 -05:00
|
|
|
protected
|
2010-06-30 05:33:15 -04:00
|
|
|
|
2010-03-31 09:54:07 -04:00
|
|
|
def parent_resource #:nodoc:
|
2009-12-07 20:47:47 -05:00
|
|
|
@scope[:scope_level_resource]
|
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def apply_common_behavior_for(method, resources, options, &block) #:nodoc:
|
2010-01-17 10:18:14 -05:00
|
|
|
if resources.length > 1
|
|
|
|
resources.each { |r| send(method, r, options, &block) }
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2010-08-31 12:02:49 -04:00
|
|
|
if resource_scope?
|
|
|
|
nested { send(method, resources.pop, options, &block) }
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2010-08-20 16:58:23 -04:00
|
|
|
options.keys.each do |k|
|
2010-08-18 06:50:15 -04:00
|
|
|
(options[:constraints] ||= {})[k] = options.delete(k) if options[k].is_a?(Regexp)
|
|
|
|
end
|
|
|
|
|
2010-07-04 01:52:52 -04:00
|
|
|
scope_options = options.slice!(*RESOURCE_OPTIONS)
|
|
|
|
unless scope_options.empty?
|
|
|
|
scope(scope_options) do
|
2010-01-17 10:18:14 -05:00
|
|
|
send(method, resources.pop, options, &block)
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2010-07-06 14:06:02 -04:00
|
|
|
unless action_options?(options)
|
|
|
|
options.merge!(scope_action_options) if scope_action_options?
|
|
|
|
end
|
|
|
|
|
2010-01-17 10:18:14 -05:00
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def action_options?(options) #:nodoc:
|
2010-07-06 14:06:02 -04:00
|
|
|
options[:only] || options[:except]
|
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def scope_action_options? #:nodoc:
|
2010-07-06 14:06:02 -04:00
|
|
|
@scope[:options].is_a?(Hash) && (@scope[:options][:only] || @scope[:options][:except])
|
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def scope_action_options #:nodoc:
|
2010-07-06 14:06:02 -04:00
|
|
|
@scope[:options].slice(:only, :except)
|
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def resource_scope? #:nodoc:
|
2010-04-05 01:48:02 -04:00
|
|
|
[:resource, :resources].include?(@scope[:scope_level])
|
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def resource_method_scope? #:nodoc:
|
2010-06-19 02:23:57 -04:00
|
|
|
[:collection, :member, :new].include?(@scope[:scope_level])
|
|
|
|
end
|
|
|
|
|
2010-06-10 13:31:28 -04:00
|
|
|
def with_exclusive_scope
|
2009-12-11 13:46:50 -05:00
|
|
|
begin
|
2010-06-22 17:23:18 -04:00
|
|
|
old_name_prefix, old_path = @scope[:as], @scope[:path]
|
|
|
|
@scope[:as], @scope[:path] = nil, nil
|
2009-12-11 13:46:50 -05:00
|
|
|
|
2010-06-19 02:23:57 -04:00
|
|
|
with_scope_level(:exclusive) do
|
|
|
|
yield
|
|
|
|
end
|
2009-12-11 13:46:50 -05:00
|
|
|
ensure
|
2010-06-22 17:23:18 -04:00
|
|
|
@scope[:as], @scope[:path] = old_name_prefix, old_path
|
2009-12-11 13:46:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-07 20:47:47 -05:00
|
|
|
def with_scope_level(kind, resource = parent_resource)
|
2009-10-20 13:31:23 -04:00
|
|
|
old, @scope[:scope_level] = @scope[:scope_level], kind
|
2009-12-07 20:47:47 -05:00
|
|
|
old_resource, @scope[:scope_level_resource] = @scope[:scope_level_resource], resource
|
2009-10-20 13:31:23 -04:00
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
@scope[:scope_level] = old
|
2009-12-07 20:47:47 -05:00
|
|
|
@scope[:scope_level_resource] = old_resource
|
2009-10-20 13:31:23 -04:00
|
|
|
end
|
2010-06-10 13:31:28 -04:00
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def resource_scope(resource) #:nodoc:
|
2010-06-10 13:31:28 -04:00
|
|
|
with_scope_level(resource.is_a?(SingletonResource) ? :resource : :resources, resource) do
|
2010-07-04 01:52:52 -04:00
|
|
|
scope(parent_resource.resource_scope) do
|
2010-06-10 13:31:28 -04:00
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def nested_options #:nodoc:
|
2010-07-04 01:52:52 -04:00
|
|
|
{}.tap do |options|
|
|
|
|
options[:as] = parent_resource.member_name
|
|
|
|
options[:constraints] = { "#{parent_resource.singular}_id".to_sym => id_constraint } if id_constraint?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def id_constraint? #:nodoc:
|
2010-08-31 12:02:49 -04:00
|
|
|
@scope[:constraints] && @scope[:constraints][:id].is_a?(Regexp)
|
2010-07-04 01:52:52 -04:00
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def id_constraint #:nodoc:
|
2010-08-31 12:02:49 -04:00
|
|
|
@scope[:constraints][:id]
|
2010-07-04 01:52:52 -04:00
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def canonical_action?(action, flag) #:nodoc:
|
2010-08-24 15:42:14 -04:00
|
|
|
flag && resource_method_scope? && CANONICAL_ACTIONS.include?(action.to_s)
|
2010-06-30 06:34:15 -04:00
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def shallow_scoping? #:nodoc:
|
2010-07-04 01:52:52 -04:00
|
|
|
shallow? && @scope[:scope_level] == :member
|
2010-06-30 06:34:15 -04:00
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def path_for_action(action, path) #:nodoc:
|
2010-06-30 06:34:15 -04:00
|
|
|
prefix = shallow_scoping? ?
|
2010-06-30 05:33:15 -04:00
|
|
|
"#{@scope[:shallow_path]}/#{parent_resource.path}/:id" : @scope[:path]
|
|
|
|
|
2010-08-24 15:42:14 -04:00
|
|
|
path = if canonical_action?(action, path.blank?)
|
|
|
|
prefix.to_s
|
2010-06-19 02:23:57 -04:00
|
|
|
else
|
2010-08-24 15:42:14 -04:00
|
|
|
"#{prefix}/#{action_path(action, path)}"
|
2010-06-19 02:23:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def action_path(name, path = nil) #:nodoc:
|
2010-06-22 18:29:07 -04:00
|
|
|
path || @scope[:path_names][name.to_sym] || name.to_s
|
2010-06-10 13:31:28 -04:00
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def prefix_name_for_action(as, action) #:nodoc:
|
2010-09-29 08:24:32 -04:00
|
|
|
if as
|
2010-08-24 10:22:51 -04:00
|
|
|
as.to_s
|
2010-06-30 06:34:15 -04:00
|
|
|
elsif !canonical_action?(action, @scope[:scope_level])
|
2010-08-24 10:22:51 -04:00
|
|
|
action.to_s
|
2010-06-30 06:34:15 -04:00
|
|
|
end
|
2010-06-10 13:31:28 -04:00
|
|
|
end
|
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
def name_for_action(as, action) #:nodoc:
|
2010-09-29 08:24:32 -04:00
|
|
|
prefix = prefix_name_for_action(as, action)
|
2010-08-24 15:42:14 -04:00
|
|
|
prefix = Mapper.normalize_name(prefix) if prefix
|
2010-06-30 06:34:15 -04:00
|
|
|
name_prefix = @scope[:as]
|
|
|
|
|
|
|
|
if parent_resource
|
2010-11-25 05:49:51 -05:00
|
|
|
return nil if as.nil? && action.nil?
|
|
|
|
|
2010-06-30 06:34:15 -04:00
|
|
|
collection_name = parent_resource.collection_name
|
|
|
|
member_name = parent_resource.member_name
|
2010-07-06 14:06:02 -04:00
|
|
|
end
|
2010-06-10 13:31:28 -04:00
|
|
|
|
2010-08-24 10:22:51 -04:00
|
|
|
name = case @scope[:scope_level]
|
2010-08-24 17:16:34 -04:00
|
|
|
when :nested
|
|
|
|
[member_name, prefix]
|
2010-06-30 05:33:15 -04:00
|
|
|
when :collection
|
2010-08-24 15:42:14 -04:00
|
|
|
[prefix, name_prefix, collection_name]
|
2010-06-10 13:31:28 -04:00
|
|
|
when :new
|
2010-08-24 15:42:14 -04:00
|
|
|
[prefix, :new, name_prefix, member_name]
|
|
|
|
when :member
|
|
|
|
[prefix, shallow_scoping? ? @scope[:shallow_prefix] : name_prefix, member_name]
|
|
|
|
when :root
|
|
|
|
[name_prefix, collection_name, prefix]
|
2010-06-10 13:31:28 -04:00
|
|
|
else
|
2010-08-24 15:42:14 -04:00
|
|
|
[name_prefix, member_name, prefix]
|
2010-06-10 13:31:28 -04:00
|
|
|
end
|
2010-08-24 10:22:51 -04:00
|
|
|
|
2010-09-29 08:24:32 -04:00
|
|
|
candidate = name.select(&:present?).join("_").presence
|
2010-09-30 01:25:06 -04:00
|
|
|
candidate unless as.nil? && @set.routes.find { |r| r.name == candidate }
|
2010-06-10 13:31:28 -04:00
|
|
|
end
|
2009-10-20 13:31:23 -04:00
|
|
|
end
|
2009-10-20 11:14:46 -04:00
|
|
|
|
2010-11-25 04:44:42 -05:00
|
|
|
module Shorthand #:nodoc:
|
2010-08-24 13:54:23 -04:00
|
|
|
def match(*args)
|
|
|
|
if args.size == 1 && args.last.is_a?(Hash)
|
|
|
|
options = args.pop
|
|
|
|
path, to = options.find { |name, value| name.is_a?(String) }
|
|
|
|
options.merge!(:to => to).delete(path)
|
|
|
|
super(path, options)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-07 20:59:23 -05:00
|
|
|
include Base
|
|
|
|
include HttpHelpers
|
2010-11-30 10:36:01 -05:00
|
|
|
include Redirection
|
2009-12-07 20:59:23 -05:00
|
|
|
include Scoping
|
|
|
|
include Resources
|
2010-08-24 13:54:23 -04:00
|
|
|
include Shorthand
|
2009-10-20 11:14:46 -04:00
|
|
|
end
|
|
|
|
end
|
2009-12-26 13:43:50 -05:00
|
|
|
end
|