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-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'}, [] ]
|
2009-12-07 20:59:23 -05:00
|
|
|
elsif constraint.respond_to?(:call) && !constraint.call(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
|
|
|
|
end
|
|
|
|
|
2010-03-31 09:54:07 -04:00
|
|
|
class Mapping #:nodoc:
|
2010-06-19 07:53:09 -04:00
|
|
|
IGNORE_OPTIONS = [:to, :as, :controller, :action, :via, :on, :constraints, :defaults, :only, :except, :anchor, :shallow, :shallow_path, :shallow_prefix]
|
2010-03-26 08:16:25 -04:00
|
|
|
|
2009-12-24 18:23:39 -05:00
|
|
|
def initialize(set, scope, args)
|
|
|
|
@set, @scope = set, scope
|
|
|
|
@path, @options = extract_path_and_options(args)
|
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
|
|
|
|
def extract_path_and_options(args)
|
2009-12-20 23:37:36 -05:00
|
|
|
options = args.extract_options!
|
2009-12-07 20:59:23 -05:00
|
|
|
|
2010-03-22 16:03:43 -04:00
|
|
|
if using_to_shorthand?(args, options)
|
2009-12-24 18:23:39 -05:00
|
|
|
path, to = options.find { |name, value| name.is_a?(String) }
|
|
|
|
options.merge!(:to => to).delete(path) if path
|
|
|
|
else
|
|
|
|
path = args.first
|
|
|
|
end
|
2009-12-26 13:43:50 -05:00
|
|
|
|
2010-06-24 06:48:05 -04:00
|
|
|
if @scope[:module] && options[:to] && !options[:to].is_a?(Proc)
|
2010-04-11 03:39:25 -04:00
|
|
|
if options[:to].to_s.include?("#")
|
|
|
|
options[:to] = "#{@scope[:module]}/#{options[:to]}"
|
|
|
|
elsif @scope[:controller].nil?
|
|
|
|
options[:to] = "#{@scope[:module]}##{options[:to]}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-22 16:03:43 -04:00
|
|
|
path = normalize_path(path)
|
2010-04-20 08:04:46 -04:00
|
|
|
path_without_format = path.sub(/\(\.:format\)$/, '')
|
2010-03-22 16:03:43 -04:00
|
|
|
|
2010-04-20 08:04:46 -04:00
|
|
|
if using_match_shorthand?(path_without_format, options)
|
|
|
|
options[:to] ||= path_without_format[1..-1].sub(%r{/([^/]*)$}, '#\1')
|
|
|
|
options[:as] ||= path_without_format[1..-1].gsub("/", "_")
|
2010-03-22 16:03:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
[ path, options ]
|
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" => "account#index"
|
|
|
|
def using_to_shorthand?(args, options)
|
|
|
|
args.empty? && options.present?
|
|
|
|
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-03-22 16:03:43 -04:00
|
|
|
raise ArgumentError, "path is required" if @scope[:path].blank? && path.blank?
|
|
|
|
Mapper.normalize_path("#{@scope[:path]}/#{path}")
|
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-02 13:13:00 -04:00
|
|
|
to.respond_to?(:call) ? to : Routing::RouteSet::Dispatcher.new(:defaults => defaults, :module => @scope[:module]),
|
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.merge!(default_controller_and_action)
|
|
|
|
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
|
|
|
|
|
|
|
|
def default_controller_and_action
|
|
|
|
if to.respond_to?(:call)
|
2009-12-24 18:23:39 -05:00
|
|
|
{ }
|
|
|
|
else
|
|
|
|
defaults = case to
|
|
|
|
when String
|
|
|
|
controller, action = to.split('#')
|
|
|
|
{ :controller => controller, :action => action }
|
|
|
|
when Symbol
|
2010-04-12 10:49:41 -04:00
|
|
|
{ :action => to.to_s }
|
2009-12-24 18:23:39 -05:00
|
|
|
else
|
2010-04-12 10:49:41 -04:00
|
|
|
{}
|
2009-12-24 18:23:39 -05:00
|
|
|
end
|
2009-12-26 13:43:50 -05:00
|
|
|
|
2010-04-12 14:23:35 -04:00
|
|
|
defaults[:controller] ||= default_controller
|
2010-06-30 06:34:15 -04:00
|
|
|
defaults[:action] ||= default_action
|
2010-04-12 14:23:35 -04:00
|
|
|
|
|
|
|
defaults.delete(:controller) if defaults[:controller].blank?
|
|
|
|
defaults.delete(:action) if defaults[:action].blank?
|
2010-04-12 10:49:41 -04:00
|
|
|
|
2009-12-24 18:23:39 -05:00
|
|
|
if defaults[:controller].blank? && segment_keys.exclude?("controller")
|
|
|
|
raise ArgumentError, "missing :controller"
|
|
|
|
end
|
2009-12-26 13:43:50 -05:00
|
|
|
|
2009-12-24 18:23:39 -05:00
|
|
|
if defaults[:action].blank? && segment_keys.exclude?("action")
|
|
|
|
raise ArgumentError, "missing :action"
|
|
|
|
end
|
2009-12-26 13:43:50 -05:00
|
|
|
|
2009-12-24 18:23:39 -05:00
|
|
|
defaults
|
|
|
|
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]
|
|
|
|
@options[:controller].to_s
|
|
|
|
elsif @scope[:controller]
|
|
|
|
@scope[:controller].to_s
|
|
|
|
end
|
2009-12-07 20:59:23 -05:00
|
|
|
end
|
2010-06-30 06:34:15 -04:00
|
|
|
|
|
|
|
def default_action
|
|
|
|
if @options[:action]
|
|
|
|
@options[:action].to_s
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2009-12-24 18:23:39 -05:00
|
|
|
def match(*args)
|
2010-03-08 15:24:49 -05:00
|
|
|
mapping = Mapping.new(@set, @scope, args).to_route
|
|
|
|
@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
|
|
|
|
|
|
|
|
match(path, options.merge(:to => app, :anchor => false))
|
|
|
|
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=
|
2009-12-07 20:59:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
module HttpHelpers
|
|
|
|
def get(*args, &block)
|
|
|
|
map_method(:get, *args, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def post(*args, &block)
|
|
|
|
map_method(:post, *args, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def put(*args, &block)
|
|
|
|
map_method(:put, *args, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(*args, &block)
|
|
|
|
map_method(:delete, *args, &block)
|
|
|
|
end
|
|
|
|
|
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
|
2010-01-05 13:00:38 -05:00
|
|
|
body = 'Moved Permanently'
|
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-01-05 17:40:56 -05:00
|
|
|
uri.port ||= req.port unless req.port == 80
|
2010-01-05 13:00:38 -05:00
|
|
|
|
|
|
|
headers = {
|
|
|
|
'Location' => uri.to_s,
|
|
|
|
'Content-Type' => 'text/html',
|
|
|
|
'Content-Length' => body.length.to_s
|
|
|
|
}
|
|
|
|
[ 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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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-01 18:29:20 -04:00
|
|
|
ActiveSupport::Deprecation.warn ":name_prefix was deprecated in the new routes syntax. Use :as instead.", caller
|
2010-06-22 17:23:18 -04:00
|
|
|
end
|
|
|
|
|
2009-12-07 20:59:23 -05:00
|
|
|
case args.first
|
|
|
|
when String
|
|
|
|
options[:path] = args.first
|
|
|
|
when Symbol
|
|
|
|
options[:controller] = args.first
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def controller(controller)
|
|
|
|
scope(controller.to_sym) { yield }
|
|
|
|
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
|
|
|
|
|
2009-12-07 20:59:23 -05:00
|
|
|
def match(*args)
|
|
|
|
options = args.extract_options!
|
|
|
|
|
|
|
|
options = (@scope[:options] || {}).merge(options)
|
|
|
|
|
2010-06-22 17:23:18 -04:00
|
|
|
if @scope[:as] && !options[:as].blank?
|
|
|
|
options[:as] = "#{@scope[:as]}_#{options[:as]}"
|
|
|
|
elsif @scope[:as] && options[:as] == ""
|
|
|
|
options[:as] = @scope[:as].to_s
|
2009-12-07 20:59:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
args.push(options)
|
|
|
|
super(*args)
|
|
|
|
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-04-10 02:09:15 -04:00
|
|
|
@scope[:module] ? "#{@scope[:module]}/#{child}" : 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)
|
|
|
|
(parent || {}).merge(child)
|
|
|
|
end
|
2010-06-10 13:31:28 -04:00
|
|
|
|
|
|
|
def merge_shallow_scope(parent, child)
|
|
|
|
child ? true : false
|
|
|
|
end
|
2009-12-07 20:59:23 -05:00
|
|
|
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.
|
|
|
|
VALID_ON_OPTIONS = [:new, :collection, :member]
|
|
|
|
CANONICAL_ACTIONS = [:index, :create, :new, :show, :update, :destroy]
|
2010-06-28 07:04:13 -04:00
|
|
|
MERGE_FROM_SCOPE_OPTIONS = [:shallow, :constraints]
|
|
|
|
|
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
|
|
|
|
@path = options.delete(:path) || @name
|
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
|
|
|
|
|
|
|
|
def actions
|
|
|
|
if only = options[:only]
|
2010-02-16 16:31:40 -05:00
|
|
|
Array(only).map(&:to_sym)
|
2010-01-13 18:23:14 -05:00
|
|
|
elsif except = options[:except]
|
2010-02-16 16:31:40 -05:00
|
|
|
default_actions - Array(except).map(&:to_sym)
|
2010-01-13 18:23:14 -05:00
|
|
|
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
|
|
|
|
name.to_s.pluralize
|
|
|
|
end
|
|
|
|
|
|
|
|
def singular
|
|
|
|
name.to_s.singularize
|
2009-11-29 17:59:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def member_name
|
2009-12-07 20:24:33 -05:00
|
|
|
singular
|
2009-11-29 17:59:44 -05:00
|
|
|
end
|
|
|
|
|
2010-06-10 13:31:28 -04:00
|
|
|
alias_method :nested_name, :member_name
|
|
|
|
|
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 shallow?
|
|
|
|
options[:shallow] ? true : false
|
2009-12-07 20:47:47 -05:00
|
|
|
end
|
2010-03-28 06:10:38 -04:00
|
|
|
|
|
|
|
def constraints
|
|
|
|
options[:constraints] || {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def id_constraint?
|
|
|
|
options[:id] && options[:id].is_a?(Regexp) || constraints[:id] && constraints[:id].is_a?(Regexp)
|
|
|
|
end
|
|
|
|
|
|
|
|
def id_constraint
|
|
|
|
options[:id] || constraints[:id]
|
|
|
|
end
|
|
|
|
|
|
|
|
def collection_options
|
2010-06-10 13:31:28 -04:00
|
|
|
(options || {}).dup.tap do |opts|
|
|
|
|
opts.delete(:id)
|
|
|
|
opts[:constraints] = options[:constraints].dup if options[:constraints]
|
|
|
|
opts[:constraints].delete(:id) if options[:constraints].is_a?(Hash)
|
2010-03-28 06:10:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-10 13:31:28 -04:00
|
|
|
def nested_path
|
|
|
|
"#{path}/:#{singular}_id"
|
2010-04-05 01:48:02 -04:00
|
|
|
end
|
|
|
|
|
2010-03-28 06:10:38 -04:00
|
|
|
def nested_options
|
2010-06-10 13:31:28 -04:00
|
|
|
{}.tap do |opts|
|
2010-06-22 17:23:18 -04:00
|
|
|
opts[:as] = member_name
|
2010-06-10 13:31:28 -04:00
|
|
|
opts["#{singular}_id".to_sym] = id_constraint if id_constraint?
|
|
|
|
opts[:options] = { :shallow => shallow? } unless options[:shallow].nil?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def resource_scope
|
|
|
|
[{ :controller => controller }]
|
|
|
|
end
|
|
|
|
|
|
|
|
def collection_scope
|
|
|
|
[path, collection_options]
|
|
|
|
end
|
|
|
|
|
|
|
|
def member_scope
|
|
|
|
["#{path}/:id", options]
|
|
|
|
end
|
|
|
|
|
2010-06-30 05:33:15 -04:00
|
|
|
def new_scope(new_path)
|
|
|
|
["#{path}/#{new_path}"]
|
2010-06-10 13:31:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def nested_scope
|
|
|
|
[nested_path, nested_options]
|
2010-03-28 06:10:38 -04:00
|
|
|
end
|
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
|
|
|
|
@path = options.delete(:path) || @name
|
|
|
|
@controller = (options.delete(:controller) || @name.to_s.pluralize).to_s
|
|
|
|
@as = options.delete(:as)
|
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
2010-06-10 13:31:28 -04:00
|
|
|
def member_name
|
|
|
|
name
|
2010-01-19 09:20:52 -05:00
|
|
|
end
|
2010-06-22 17:23:18 -04:00
|
|
|
alias :collection_name :member_name
|
2010-01-19 09:20:52 -05:00
|
|
|
|
2010-06-10 13:31:28 -04:00
|
|
|
def nested_path
|
|
|
|
path
|
2010-04-05 01:48:02 -04:00
|
|
|
end
|
|
|
|
|
2010-06-10 13:31:28 -04:00
|
|
|
def nested_options
|
|
|
|
{}.tap do |opts|
|
2010-06-22 17:23:18 -04:00
|
|
|
opts[:as] = member_name
|
2010-06-10 13:31:28 -04:00
|
|
|
opts[:options] = { :shallow => shallow? } unless @options[:shallow].nil?
|
|
|
|
end
|
2009-11-29 17:59:44 -05:00
|
|
|
end
|
2010-04-05 01:48:02 -04:00
|
|
|
|
2010-06-10 13:31:28 -04:00
|
|
|
def shallow?
|
|
|
|
false
|
2010-04-05 01:48:02 -04:00
|
|
|
end
|
|
|
|
|
2010-06-10 13:31:28 -04:00
|
|
|
def member_scope
|
|
|
|
[path, options]
|
2010-04-05 01:48:02 -04:00
|
|
|
end
|
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
|
|
|
|
|
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
|
|
|
|
end if parent_resource.actions.include?(:create)
|
|
|
|
|
|
|
|
new_scope do
|
|
|
|
get :new
|
|
|
|
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
|
|
|
|
get :show if parent_resource.actions.include?(:show)
|
|
|
|
put :update if parent_resource.actions.include?(:update)
|
|
|
|
delete :destroy if parent_resource.actions.include?(:destroy)
|
|
|
|
get :edit if parent_resource.actions.include?(:edit)
|
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
|
|
|
|
|
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
|
|
|
|
get :index if parent_resource.actions.include?(:index)
|
|
|
|
post :create if parent_resource.actions.include?(:create)
|
|
|
|
end
|
2009-11-29 16:23:27 -05:00
|
|
|
|
2010-06-30 05:33:15 -04:00
|
|
|
new_scope do
|
|
|
|
get :new
|
|
|
|
end if parent_resource.actions.include?(:new)
|
|
|
|
|
2010-06-10 13:31:28 -04:00
|
|
|
member_scope do
|
|
|
|
get :show if parent_resource.actions.include?(:show)
|
|
|
|
put :update if parent_resource.actions.include?(:update)
|
|
|
|
delete :destroy if parent_resource.actions.include?(:destroy)
|
|
|
|
get :edit if parent_resource.actions.include?(:edit)
|
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
|
|
|
|
|
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
|
|
|
|
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-06-10 13:31:28 -04:00
|
|
|
if parent_resource.shallow?
|
|
|
|
with_exclusive_scope do
|
2010-06-19 07:53:09 -04:00
|
|
|
if @scope[:shallow_path].blank?
|
2010-06-10 13:31:28 -04:00
|
|
|
scope(*parent_resource.nested_scope) { yield }
|
|
|
|
else
|
2010-06-22 17:23:18 -04:00
|
|
|
scope(@scope[:shallow_path], :as => @scope[:shallow_prefix]) do
|
2010-06-10 13:31:28 -04:00
|
|
|
scope(*parent_resource.nested_scope) { yield }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
scope(*parent_resource.nested_scope) { 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
|
|
|
|
|
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-06-10 13:31:28 -04:00
|
|
|
if @scope[:scope_level] == :resource
|
|
|
|
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-22 18:29:07 -04:00
|
|
|
path = options.delete(:path)
|
2010-06-30 06:34:15 -04:00
|
|
|
action = args.first
|
2010-06-10 13:31:28 -04:00
|
|
|
|
2010-06-30 06:34:15 -04:00
|
|
|
if action.is_a?(Symbol)
|
|
|
|
path = path_for_action(action, path)
|
|
|
|
options[:to] ||= action
|
|
|
|
options[:as] = name_for_action(action, options[:as])
|
2010-06-10 13:31:28 -04:00
|
|
|
|
|
|
|
with_exclusive_scope do
|
2010-06-19 02:23:57 -04:00
|
|
|
return super(path, options)
|
|
|
|
end
|
|
|
|
elsif resource_method_scope?
|
|
|
|
path = path_for_custom_action
|
|
|
|
options[:as] = name_for_action(options[:as]) if options[:as]
|
|
|
|
args.push(options)
|
|
|
|
|
|
|
|
with_exclusive_scope do
|
|
|
|
scope(path) do
|
|
|
|
return super
|
|
|
|
end
|
2010-06-10 13:31:28 -04:00
|
|
|
end
|
2010-04-05 01:48:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if resource_scope?
|
|
|
|
raise ArgumentError, "can't define route directly in 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
|
|
|
args.push(options)
|
2009-10-20 13:31:23 -04:00
|
|
|
super
|
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-06-19 02:23:57 -04:00
|
|
|
with_scope_level(:nested) do
|
2010-06-22 17:23:18 -04:00
|
|
|
scope(parent_resource.path, :as => parent_resource.collection_name) 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
|
|
|
|
|
|
|
|
if path_names = options.delete(:path_names)
|
2010-04-10 15:13:21 -04:00
|
|
|
scope(:path_names => path_names) do
|
2010-01-17 10:18:14 -05:00
|
|
|
send(method, resources.pop, options, &block)
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2010-06-28 07:04:13 -04:00
|
|
|
scope_options = @scope.slice(*MERGE_FROM_SCOPE_OPTIONS).delete_if{ |k,v| v.blank? }
|
|
|
|
options.reverse_merge!(scope_options) unless scope_options.empty?
|
|
|
|
options.reverse_merge!(@scope[:options]) unless @scope[:options].blank?
|
|
|
|
|
2010-04-05 01:48:02 -04:00
|
|
|
if resource_scope?
|
2010-01-17 10:18:14 -05:00
|
|
|
nested do
|
|
|
|
send(method, resources.pop, options, &block)
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
|
|
|
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
|
|
|
|
scope(*parent_resource.resource_scope) do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-30 05:33:15 -04:00
|
|
|
def new_scope
|
|
|
|
with_scope_level(:new) do
|
|
|
|
scope(*parent_resource.new_scope(action_path(:new))) do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-10 13:31:28 -04:00
|
|
|
def collection_scope
|
|
|
|
with_scope_level(:collection) do
|
|
|
|
scope(*parent_resource.collection_scope) do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def member_scope
|
|
|
|
with_scope_level(:member) do
|
|
|
|
scope(*parent_resource.member_scope) do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-30 06:34:15 -04:00
|
|
|
def canonical_action?(action, flag)
|
|
|
|
flag && CANONICAL_ACTIONS.include?(action)
|
|
|
|
end
|
|
|
|
|
|
|
|
def shallow_scoping?
|
|
|
|
parent_resource && parent_resource.shallow? && @scope[:scope_level] == :member
|
|
|
|
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-06-30 06:34:15 -04:00
|
|
|
if canonical_action?(action, path.blank?)
|
2010-06-30 05:33:15 -04:00
|
|
|
"#{prefix}(.:format)"
|
2010-06-10 13:31:28 -04:00
|
|
|
else
|
2010-06-30 05:33:15 -04:00
|
|
|
"#{prefix}/#{action_path(action, path)}(.:format)"
|
2010-06-10 13:31:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-19 02:23:57 -04:00
|
|
|
def path_for_custom_action
|
2010-06-30 06:34:15 -04:00
|
|
|
if shallow_scoping?
|
|
|
|
"#{@scope[:shallow_path]}/#{parent_resource.path}/:id"
|
2010-06-19 02:23:57 -04:00
|
|
|
else
|
2010-06-30 06:34:15 -04:00
|
|
|
@scope[: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?
|
|
|
|
"#{as}_"
|
|
|
|
elsif as
|
|
|
|
""
|
|
|
|
elsif !canonical_action?(action, @scope[:scope_level])
|
|
|
|
"#{action}_"
|
|
|
|
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)
|
|
|
|
name_prefix = @scope[:as]
|
|
|
|
|
|
|
|
if parent_resource
|
|
|
|
collection_name = parent_resource.collection_name
|
|
|
|
member_name = parent_resource.member_name
|
|
|
|
name_prefix = "#{name_prefix}_" if name_prefix.present?
|
|
|
|
end
|
2010-06-10 13:31:28 -04:00
|
|
|
|
2010-06-30 05:33:15 -04:00
|
|
|
case @scope[:scope_level]
|
|
|
|
when :collection
|
2010-06-30 06:34:15 -04:00
|
|
|
"#{prefix}#{name_prefix}#{collection_name}"
|
2010-06-10 13:31:28 -04:00
|
|
|
when :new
|
2010-06-30 06:34:15 -04:00
|
|
|
"#{prefix}new_#{name_prefix}#{member_name}"
|
2010-06-10 13:31:28 -04:00
|
|
|
else
|
2010-06-30 06:34:15 -04:00
|
|
|
if shallow_scoping?
|
2010-06-30 05:33:15 -04:00
|
|
|
shallow_prefix = "#{@scope[:shallow_prefix]}_" if @scope[:shallow_prefix].present?
|
2010-06-30 06:34:15 -04:00
|
|
|
"#{prefix}#{shallow_prefix}#{member_name}"
|
2010-06-10 13:31:28 -04:00
|
|
|
else
|
2010-06-30 06:34:15 -04:00
|
|
|
"#{prefix}#{name_prefix}#{member_name}"
|
2010-06-10 13:31:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-10-20 13:31:23 -04:00
|
|
|
end
|
2009-10-20 11:14:46 -04:00
|
|
|
|
2009-12-07 20:59:23 -05:00
|
|
|
include Base
|
|
|
|
include HttpHelpers
|
|
|
|
include Scoping
|
|
|
|
include Resources
|
2009-10-20 11:14:46 -04:00
|
|
|
end
|
|
|
|
end
|
2009-12-26 13:43:50 -05:00
|
|
|
end
|