/usr/lib/ruby/1.8/merb-core/dispatch/router/resources.rb is in libmerb-core-ruby1.8 1.0.12+dfsg-4fakesync1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | module Merb
class Router
module Resources
# Behavior#+resources+ is a route helper for defining a collection of
# RESTful resources. It yields to a block for child routes.
#
# ==== Parameters
# name<String, Symbol>:: The name of the resources
# options<Hash>::
# Ovverides and parameters to be associated with the route
#
# ==== Options (options)
# :namespace<~to_s>: The namespace for this route.
# :name_prefix<~to_s>:
# A prefix for the named routes. If a namespace is passed and there
# isn't a name prefix, the namespace will become the prefix.
# :controller<~to_s>: The controller for this route
# :collection<~to_s>: Special settings for the collections routes
# :member<Hash>:
# Special settings and resources related to a specific member of this
# resource.
# :identify<Symbol|Array>: The method(s) that should be called on the object
# before inserting it into an URL.
# :keys<Array>:
# A list of the keys to be used instead of :id with the resource in the order of the url.
# :singular<Symbol>
#
# ==== Block parameters
# next_level<Behavior>:: The child behavior.
#
# ==== Returns
# Array::
# Routes which will define the specified RESTful collection of resources
#
# ==== Examples
#
# r.resources :posts # will result in the typical RESTful CRUD
# # lists resources
# # GET /posts/?(\.:format)? :action => "index"
# # GET /posts/index(\.:format)? :action => "index"
#
# # shows new resource form
# # GET /posts/new :action => "new"
#
# # creates resource
# # POST /posts/?(\.:format)?, :action => "create"
#
# # shows resource
# # GET /posts/:id(\.:format)? :action => "show"
#
# # shows edit form
# # GET /posts/:id/edit :action => "edit"
#
# # updates resource
# # PUT /posts/:id(\.:format)? :action => "update"
#
# # shows deletion confirmation page
# # GET /posts/:id/delete :action => "delete"
#
# # destroys resources
# # DELETE /posts/:id(\.:format)? :action => "destroy"
#
# # Nesting resources
# r.resources :posts do |posts|
# posts.resources :comments
# end
#
# :api: public
def resources(name, *args, &block)
name = name.to_s
options = extract_options_from_args!(args) || {}
match_opts = options.except(*resource_options)
options = options.only(*resource_options)
singular = options[:singular] ? options[:singular].to_s : Extlib::Inflection.singularize(name)
klass_name = args.first ? args.first.to_s : singular.to_const_string
keys = options.delete(:keys) || options.delete(:key)
params = { :controller => options.delete(:controller) || name }
collection = options.delete(:collection) || {}
member = { :edit => :get, :delete => :get }.merge(options.delete(:member) || {})
# Use the identifier for the class as a default
begin
if klass = Object.full_const_get(klass_name)
keys ||= options[:identify]
keys ||= @identifiers[klass]
elsif options[:identify]
raise Error, "The constant #{klass_name} does not exist, please specify the constant for this resource"
end
rescue NameError => e
Merb.logger.debug!("Could not find resource model #{klass_name}")
end
keys = [ keys || :id ].flatten
# Try pulling :namespace out of options for backwards compatibility
options[:name_prefix] ||= nil # Don't use a name_prefix if not needed
options[:resource_prefix] ||= nil # Don't use a resource_prefix if not needed
options[:controller_prefix] ||= options.delete(:namespace)
context = options[:identify]
context = klass && options[:identify] ? identify(klass => options.delete(:identify)) : self
context.namespace(name, options).to(params) do |resource|
root_keys = keys.map { |k| ":#{k}" }.join("/")
# => index
resource.match("(/index)(.:format)", :method => :get).to(:action => "index").
name(name).register_resource(name)
# => create
resource.match("(.:format)", :method => :post).to(:action => "create")
# => new
resource.match("/new(.:format)", :method => :get).to(:action => "new").
name("new", singular).register_resource(name, "new")
# => user defined collection routes
collection.each_pair do |action, method|
action = action.to_s
resource.match("/#{action}(.:format)", :method => method).to(:action => "#{action}").
name(action, name).register_resource(name, action)
end
# => show
resource.match("/#{root_keys}(.:format)", match_opts.merge(:method => :get)).to(:action => "show").
name(singular).register_resource(klass_name, :identifiers => keys)
# => user defined member routes
member.each_pair do |action, method|
action = action.to_s
resource.match("/#{root_keys}/#{action}(.:format)", match_opts.merge(:method => method)).
to(:action => "#{action}").name(action, singular).register_resource(klass_name, action, :identifiers => keys)
end
# => update
resource.match("/#{root_keys}(.:format)", match_opts.merge(:method => :put)).
to(:action => "update")
# => destroy
resource.match("/#{root_keys}(.:format)", match_opts.merge(:method => :delete)).
to(:action => "destroy")
if block_given?
parent_keys = keys.map do |k|
k == :id ? "#{singular}_id".to_sym : k
end
nested_keys = parent_keys.map { |k| ":#{k}" }.join("/")
nested_match_opts = match_opts.except(:id)
nested_match_opts["#{singular}_id".to_sym] = match_opts[:id] if match_opts[:id]
# Procs for building the extra collection/member resource routes
placeholder = Router.resource_routes[ [@options[:resource_prefix], klass_name].flatten.compact ]
builders = {}
builders[:collection] = lambda do |action, to, method|
resource.before(placeholder).match("/#{action}(.:format)", match_opts.merge(:method => method)).
to(:action => to).name(action, name).register_resource(name, action)
end
builders[:member] = lambda do |action, to, method|
resource.match("/#{root_keys}/#{action}(.:format)", match_opts.merge(:method => method)).
to(:action => to).name(action, singular).register_resource(klass_name, action, :identifiers => keys)
end
resource.options(:name_prefix => singular, :resource_prefix => klass_name, :parent_keys => parent_keys).
match("/#{nested_keys}", nested_match_opts).resource_block(builders, &block)
end
end # namespace
end # resources
# Behavior#+resource+ is a route helper for defining a singular RESTful
# resource. It yields to a block for child routes.
#
# ==== Parameters
# name<String, Symbol>:: The name of the resource.
# options<Hash>::
# Overides and parameters to be associated with the route.
#
# ==== Options (options)
# :namespace<~to_s>: The namespace for this route.
# :name_prefix<~to_s>:
# A prefix for the named routes. If a namespace is passed and there
# isn't a name prefix, the namespace will become the prefix.
# :controller<~to_s>: The controller for this route
#
# ==== Block parameters
# next_level<Behavior>:: The child behavior.
#
# ==== Returns
# Array:: Routes which define a RESTful single resource.
#
# ==== Examples
#
# r.resource :account # will result in the typical RESTful CRUD
# # shows new resource form
# # GET /account/new :action => "new"
#
# # creates resource
# # POST /account/?(\.:format)?, :action => "create"
#
# # shows resource
# # GET /account/(\.:format)? :action => "show"
#
# # shows edit form
# # GET /account//edit :action => "edit"
#
# # updates resource
# # PUT /account/(\.:format)? :action => "update"
#
# # shows deletion confirmation page
# # GET /account//delete :action => "delete"
#
# # destroys resources
# # DELETE /account/(\.:format)? :action => "destroy"
#
# You can optionally pass :namespace and :controller to refine the routing
# or pass a block to nest resources.
#
# r.resource :account, :namespace => "admin" do |account|
# account.resources :preferences, :controller => "settings"
# end
#
# :api: public
def resource(name, *args, &block)
name = name.to_s
options = extract_options_from_args!(args) || {}
params = { :controller => options.delete(:controller) || name.pluralize }
member = { :new => :get, :edit => :get, :delete => :get }.merge(options.delete(:member) || {})
options[:name_prefix] ||= nil # Don't use a name_prefix if not needed
options[:resource_prefix] ||= nil # Don't use a resource_prefix if not needed
options[:controller_prefix] ||= options.delete(:namespace)
self.namespace(name, options).to(params) do |resource|
# => show
resource.match("(.:format)", :method => :get).to(:action => "show").
name(name).register_resource(name)
# => create
resource.match("(.:format)", :method => :post).to(:action => "create")
# => update
resource.match("(.:format)", :method => :put).to(:action => "update")
# => destroy
resource.match("(.:format)", :method => :delete).to(:action => "destroy")
member.each_pair do |action, method|
action = action.to_s
resource.match("/#{action}(.:format)", :method => method).to(:action => action).
name(action, name).register_resource(name, action)
end
if block_given?
builders = {}
builders[:member] = lambda do |action, to, method|
resource.match("/#{action}(.:format)", :method => method).to(:action => to).
name(action, name).register_resource(name, action)
end
resource.options(:name_prefix => name, :resource_prefix => name).
resource_block(builders, &block)
end
end
end
protected
# :api: private
def register_resource(*key)
options = extract_options_from_args!(key) || {}
key = [ @options[:resource_prefix], key ].flatten.compact
identifiers = [ @options[:parent_keys], options[:identifiers] ]
@route.resource = key
@route.resource_identifiers = identifiers.flatten.compact.map { |id| id.to_sym }
self
end
# :api: private
def resource_block(builders, &block)
behavior = ResourceBehavior.new(builders, @proxy, @conditions, @params, @defaults, @identifiers, @options, @blocks)
with_behavior_context(behavior, &block)
end
def resource_options
[:singular, :keys, :key, :controller, :member, :collection, :identify,
:name_prefix, :resource_prefix, :controller_prefix, :namespace, :path]
end
end # Resources
class Behavior
include Resources
end
# Adding the collection and member methods to behavior
class ResourceBehavior < Behavior #:nodoc:
# :api: private
def initialize(builders, *args)
super(*args)
@collection = builders[:collection]
@member = builders[:member]
end
# :api: private
def collection(action, options = {})
action = action.to_s
method = options[:method]
to = options[:to] || action
@collection[action, to, method]
end
# :api: private
def member(action, options = {})
action = action.to_s
method = options[:method]
to = options[:to] || action
@member[action, to, method]
end
end
end
end
|