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-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-08-24 15:42:14 -04:00
|
|
|
@options[:as] ||= Mapper.normalize_name(path_without_format)
|
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))
|
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)
|
2009-12-24 18:23:39 -05: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-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-07-12 13:42:57 -04:00
|
|
|
{ :controller => controller, :action => action }.tap do |hash|
|
|
|
|
hash.delete(:controller) if hash[:controller].blank?
|
|
|
|
hash.delete(:action) if hash[:action].blank?
|
|
|
|
end
|
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
|
|
|
|
if @options[:constraints].present? && !@options[:constraints].is_a?(Hash)
|
|
|
|
block = @options[:constraints]
|
|
|
|
else
|
|
|
|
block = nil
|
|
|
|
end
|
2009-12-26 13:43:50 -05:00
|
|
|
|
|
|
|
((@scope[:blocks] || []) + [ block ]).compact
|
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]
|
|
|
|
via = Array(via).map { |m| m.to_s.upcase }
|
|
|
|
{ :request_method => Regexp.union(*via) }
|
|
|
|
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'
|
|
|
|
#
|
|
|
|
# You should put the root route at the end of <tt>config/routes.rb</tt>.
|
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
|
|
|
|
2010-08-28 18:04:14 -04:00
|
|
|
# When you set up a regular route, you supply a series of symbols that
|
|
|
|
# Rails maps to parts of an incoming HTTP request.
|
|
|
|
#
|
|
|
|
# match ':controller/:action/:id/:user_id'
|
|
|
|
#
|
|
|
|
# Two of these symbols are special: :controller maps to the name of a
|
|
|
|
# controller in your application, and :action maps to the name of an
|
|
|
|
# action within that controller. Anything other than :controller or
|
|
|
|
# :action will be available to the action as part of params.
|
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-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
|
|
|
|
|
|
|
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)
|
|
|
|
return unless app.respond_to?(:routes)
|
|
|
|
|
|
|
|
_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.
|
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.
|
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.
|
2009-12-07 20:59:23 -05:00
|
|
|
def put(*args, &block)
|
|
|
|
map_method(:put, *args, &block)
|
|
|
|
end
|
|
|
|
|
2010-08-28 18:04:14 -04:00
|
|
|
# Define a route that only recognizes HTTP DELETE.
|
2009-12-07 20:59:23 -05:00
|
|
|
def delete(*args, &block)
|
|
|
|
map_method(:delete, *args, &block)
|
|
|
|
end
|
|
|
|
|
2010-08-28 18:04:14 -04:00
|
|
|
# Redirect any path to another path:
|
|
|
|
#
|
|
|
|
# match "/stories" => redirect("/posts")
|
2009-12-20 17:07:19 -05:00
|
|
|
def redirect(*args, &block)
|
|
|
|
options = args.last.is_a?(Hash) ? args.pop : {}
|
|
|
|
|
2009-12-24 18:23:39 -05:00
|
|
|
path = args.shift || block
|
|
|
|
path_proc = path.is_a?(Proc) ? path : proc { |params| path % params }
|
|
|
|
status = options[:status] || 301
|
2009-12-20 17:07:19 -05:00
|
|
|
|
|
|
|
lambda do |env|
|
2010-01-05 13:00:38 -05:00
|
|
|
req = Request.new(env)
|
2010-04-04 16:00:22 -04:00
|
|
|
|
|
|
|
params = [req.symbolized_path_parameters]
|
|
|
|
params << req if path_proc.arity > 1
|
|
|
|
|
|
|
|
uri = URI.parse(path_proc.call(*params))
|
2010-01-05 13:00:38 -05:00
|
|
|
uri.scheme ||= req.scheme
|
|
|
|
uri.host ||= req.host
|
2010-08-19 10:29:54 -04:00
|
|
|
uri.port ||= req.port unless req.standard_port?
|
2010-01-05 13:00:38 -05:00
|
|
|
|
2010-08-09 07:31:42 -04:00
|
|
|
body = %(<html><body>You are being <a href="#{ERB::Util.h(uri.to_s)}">redirected</a>.</body></html>)
|
|
|
|
|
2010-01-05 13:00:38 -05:00
|
|
|
headers = {
|
|
|
|
'Location' => uri.to_s,
|
|
|
|
'Content-Type' => 'text/html',
|
|
|
|
'Content-Length' => body.length.to_s
|
|
|
|
}
|
2010-08-09 07:31:42 -04:00
|
|
|
|
2010-01-05 13:00:38 -05:00
|
|
|
[ status, headers, [body] ]
|
2009-12-20 17:07:19 -05:00
|
|
|
end
|
2009-12-07 20:59:23 -05:00
|
|
|
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
|
|
|
|
#
|
|
|
|
# This will create a number of routes for each of the posts and comments
|
|
|
|
# controller. For Admin::PostsController, Rails will create:
|
|
|
|
#
|
|
|
|
# GET /admin/photos
|
|
|
|
# GET /admin/photos/new
|
|
|
|
# POST /admin/photos
|
|
|
|
# GET /admin/photos/1
|
|
|
|
# GET /admin/photos/1/edit
|
|
|
|
# PUT /admin/photos/1
|
|
|
|
# DELETE /admin/photos/1
|
|
|
|
#
|
|
|
|
# If you want to route /photos (without the prefix /admin) to
|
|
|
|
# Admin::PostsController, you could use
|
|
|
|
#
|
|
|
|
# scope :module => "admin" do
|
|
|
|
# resources :posts, :comments
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# or, for a single case
|
|
|
|
#
|
|
|
|
# resources :posts, :module => "admin"
|
|
|
|
#
|
|
|
|
# If you want to route /admin/photos to PostsController
|
|
|
|
# (without the Admin:: module prefix), you could use
|
|
|
|
#
|
|
|
|
# scope "/admin" do
|
|
|
|
# resources :posts, :comments
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# or, for a single case
|
|
|
|
#
|
|
|
|
# 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:
|
|
|
|
#
|
|
|
|
# GET /admin/photos
|
|
|
|
# GET /admin/photos/new
|
|
|
|
# POST /admin/photos
|
|
|
|
# GET /admin/photos/1
|
|
|
|
# GET /admin/photos/1/edit
|
|
|
|
# PUT /admin/photos/1
|
|
|
|
# DELETE /admin/photos/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
|
|
|
|
|
2010-08-28 18:04:14 -04:00
|
|
|
# Used to route <tt>/photos</tt> (without the prefix <tt>/admin</tt>)
|
|
|
|
# to Admin::PostsController:
|
|
|
|
#
|
|
|
|
# scope :module => "admin" 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-06-22 17:23:18 -04:00
|
|
|
if name_prefix = options.delete(:name_prefix)
|
|
|
|
options[:as] ||= name_prefix
|
2010-07-03 06:12:50 -04:00
|
|
|
ActiveSupport::Deprecation.warn ":name_prefix was deprecated in the new router syntax. Use :as instead.", caller
|
2010-06-22 17:23:18 -04:00
|
|
|
end
|
|
|
|
|
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-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-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
|
|
|
|
|
|
|
|
def constraints(constraints = {})
|
|
|
|
scope(:constraints => constraints) { yield }
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
def scope_options
|
|
|
|
@scope_options ||= private_methods.grep(/^merge_(.+)_scope$/) { $1.to_sym }
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_path_scope(parent, child)
|
2010-06-08 16:20:46 -04:00
|
|
|
Mapper.normalize_path("#{parent}/#{child}")
|
2010-01-13 21:21:04 -05:00
|
|
|
end
|
|
|
|
|
2010-06-19 07:53:09 -04:00
|
|
|
def merge_shallow_path_scope(parent, child)
|
|
|
|
Mapper.normalize_path("#{parent}/#{child}")
|
|
|
|
end
|
|
|
|
|
2010-06-22 17:23:18 -04:00
|
|
|
def merge_as_scope(parent, child)
|
2010-06-08 16:20:46 -04:00
|
|
|
parent ? "#{parent}_#{child}" : child
|
2010-01-13 21:21:04 -05:00
|
|
|
end
|
|
|
|
|
2010-06-19 07:53:09 -04:00
|
|
|
def merge_shallow_prefix_scope(parent, child)
|
|
|
|
parent ? "#{parent}_#{child}" : child
|
|
|
|
end
|
|
|
|
|
2010-04-10 02:09:15 -04:00
|
|
|
def merge_module_scope(parent, child)
|
2010-01-13 21:21:04 -05:00
|
|
|
parent ? "#{parent}/#{child}" : child
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_controller_scope(parent, child)
|
2010-07-12 09:11:15 -04:00
|
|
|
child
|
2010-01-13 21:21:04 -05:00
|
|
|
end
|
|
|
|
|
2010-04-10 15:13:21 -04:00
|
|
|
def merge_path_names_scope(parent, child)
|
2010-01-13 21:21:04 -05:00
|
|
|
merge_options_scope(parent, child)
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_constraints_scope(parent, child)
|
|
|
|
merge_options_scope(parent, child)
|
|
|
|
end
|
|
|
|
|
2010-03-26 08:16:25 -04:00
|
|
|
def merge_defaults_scope(parent, child)
|
|
|
|
merge_options_scope(parent, child)
|
|
|
|
end
|
|
|
|
|
2010-01-13 21:21:04 -05:00
|
|
|
def merge_blocks_scope(parent, child)
|
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
|
|
|
|
|
|
|
|
def merge_options_scope(parent, child)
|
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
|
|
|
|
|
|
|
def merge_shallow_scope(parent, child)
|
|
|
|
child ? true : false
|
|
|
|
end
|
2010-07-06 14:06:02 -04:00
|
|
|
|
|
|
|
def override_keys(child)
|
|
|
|
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
|
|
|
|
if only = @options[:only]
|
|
|
|
Array(only).map(&:to_sym)
|
|
|
|
elsif except = @options[:except]
|
|
|
|
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
|
|
|
|
2010-03-28 21:15:32 -04:00
|
|
|
# Checks for uncountable plurals, and appends "_index" if they're.
|
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)
|
|
|
|
@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
|
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-06-10 13:31:28 -04:00
|
|
|
collection_scope 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
|
|
|
|
|
|
|
new_scope do
|
|
|
|
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-06-10 13:31:28 -04:00
|
|
|
member_scope 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
|
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
|
|
|
|
yield if block_given?
|
2009-10-20 13:31:23 -04:00
|
|
|
|
2010-06-10 13:31:28 -04:00
|
|
|
collection_scope 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-06-30 05:33:15 -04:00
|
|
|
new_scope do
|
|
|
|
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-06-10 13:31:28 -04:00
|
|
|
member_scope 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
|
|
|
|
unless @scope[:scope_level] == :resources
|
|
|
|
raise ArgumentError, "can't use collection outside resources scope"
|
2009-10-20 11:46:27 -04:00
|
|
|
end
|
|
|
|
|
2010-06-10 13:31:28 -04:00
|
|
|
collection_scope do
|
|
|
|
yield
|
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-06-10 13:31:28 -04:00
|
|
|
member_scope do
|
|
|
|
yield
|
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-06-30 05:33:15 -04:00
|
|
|
new_scope do
|
|
|
|
yield
|
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-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
|
|
|
|
scope(:shallow => true) do
|
|
|
|
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?("/")
|
2010-08-24 09:09:06 -04:00
|
|
|
options[:as] = name_for_action(action, options[:as])
|
2010-08-24 15:42:14 -04:00
|
|
|
else
|
|
|
|
options[:as] = name_for_action(options[:as])
|
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-02-16 16:31:40 -05:00
|
|
|
def apply_common_behavior_for(method, resources, options, &block)
|
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-07-06 14:06:02 -04:00
|
|
|
def action_options?(options)
|
|
|
|
options[:only] || options[:except]
|
|
|
|
end
|
|
|
|
|
|
|
|
def scope_action_options?
|
|
|
|
@scope[:options].is_a?(Hash) && (@scope[:options][:only] || @scope[:options][:except])
|
|
|
|
end
|
|
|
|
|
|
|
|
def scope_action_options
|
|
|
|
@scope[:options].slice(:only, :except)
|
|
|
|
end
|
|
|
|
|
2010-04-05 01:48:02 -04:00
|
|
|
def resource_scope?
|
|
|
|
[:resource, :resources].include?(@scope[:scope_level])
|
|
|
|
end
|
|
|
|
|
2010-06-19 02:23:57 -04:00
|
|
|
def resource_method_scope?
|
|
|
|
[: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
|
|
|
|
|
|
|
def resource_scope(resource)
|
|
|
|
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-06-30 05:33:15 -04:00
|
|
|
def new_scope
|
|
|
|
with_scope_level(:new) do
|
2010-07-04 01:52:52 -04:00
|
|
|
scope(parent_resource.new_scope(action_path(:new))) do
|
2010-06-30 05:33:15 -04:00
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-10 13:31:28 -04:00
|
|
|
def collection_scope
|
|
|
|
with_scope_level(:collection) do
|
2010-07-04 01:52:52 -04:00
|
|
|
scope(parent_resource.collection_scope) do
|
2010-06-10 13:31:28 -04:00
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def member_scope
|
|
|
|
with_scope_level(:member) do
|
2010-07-04 01:52:52 -04:00
|
|
|
scope(parent_resource.member_scope) do
|
2010-06-10 13:31:28 -04:00
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-07-04 01:52:52 -04:00
|
|
|
def nested_options
|
|
|
|
{}.tap do |options|
|
|
|
|
options[:as] = parent_resource.member_name
|
|
|
|
options[:constraints] = { "#{parent_resource.singular}_id".to_sym => id_constraint } if id_constraint?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def id_constraint?
|
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
|
|
|
|
|
|
|
|
def id_constraint
|
2010-08-31 12:02:49 -04:00
|
|
|
@scope[:constraints][:id]
|
2010-07-04 01:52:52 -04:00
|
|
|
end
|
|
|
|
|
2010-06-30 06:34:15 -04:00
|
|
|
def canonical_action?(action, flag)
|
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
|
|
|
|
|
|
|
|
def shallow_scoping?
|
2010-07-04 01:52:52 -04:00
|
|
|
shallow? && @scope[:scope_level] == :member
|
2010-06-30 06:34:15 -04:00
|
|
|
end
|
|
|
|
|
2010-06-22 18:29:07 -04:00
|
|
|
def path_for_action(action, path)
|
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-06-22 18:29:07 -04:00
|
|
|
def action_path(name, path = nil)
|
|
|
|
path || @scope[:path_names][name.to_sym] || name.to_s
|
2010-06-10 13:31:28 -04:00
|
|
|
end
|
|
|
|
|
2010-06-30 06:34:15 -04:00
|
|
|
def prefix_name_for_action(action, as)
|
|
|
|
if as.present?
|
2010-08-24 10:22:51 -04:00
|
|
|
as.to_s
|
2010-06-30 06:34:15 -04:00
|
|
|
elsif as
|
2010-08-24 10:22:51 -04:00
|
|
|
nil
|
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-06-30 06:34:15 -04:00
|
|
|
def name_for_action(action, as=nil)
|
|
|
|
prefix = prefix_name_for_action(action, as)
|
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
|
|
|
|
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-08-24 15:42:14 -04:00
|
|
|
name.select(&:present?).join("_").presence
|
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-08-24 13:54:23 -04:00
|
|
|
module Shorthand
|
|
|
|
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
|
|
|
|
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
|