0
- # Behavior#+resources+ is a route helper for defining a collection of
0
- # RESTful resources. It yields to a block for child routes.
0
- # name<String, Symbol>:: The name of the resources
0
- # Ovverides and parameters to be associated with the route
0
- # ==== Options (options)
0
- # :namespace<~to_s>: The namespace for this route.
0
- # :name_prefix<~to_s>:
0
- # A prefix for the named routes. If a namespace is passed and there
0
- # isn't a name prefix, the namespace will become the prefix.
0
- # :controller<~to_s>: The controller for this route
0
- # :collection<~to_s>: Special settings for the collections routes
0
- # Special settings and resources related to a specific member of this
0
- # A list of the keys to be used instead of :id with the resource in the order of the url.
0
- # ==== Block parameters
0
- # next_level<Behavior>:: The child behavior.
0
- # Routes which will define the specified RESTful collection of resources
0
- # r.resources :posts # will result in the typical RESTful CRUD
0
- # # GET /posts/?(\.:format)? :action => "index"
0
- # # GET /posts/index(\.:format)? :action => "index"
0
- # # shows new resource form
0
- # # GET /posts/new :action => "new"
0
- # # POST /posts/?(\.:format)?, :action => "create"
0
- # # GET /posts/:id(\.:format)? :action => "show"
0
- # # GET /posts/:id/edit :action => "edit"
0
- # # PUT /posts/:id(\.:format)? :action => "update"
0
- # # shows deletion confirmation page
0
- # # GET /posts/:id/delete :action => "delete"
0
- # # destroys resources
0
- # # DELETE /posts/:id(\.:format)? :action => "destroy"
0
- # r.resources :posts do |posts|
0
- # posts.resources :comments
0
- def resources(name, *args, &block)
0
- options = extract_options_from_args!(args) || {}
0
- singular = options[:singular] ? options[:singular].to_s : Extlib::Inflection.singularize(name)
0
- klass = args.first ? args.first.to_s : Extlib::Inflection.classify(singular)
0
- keys = [ options.delete(:keys) || options.delete(:key) || :id ].flatten
0
- params = { :controller => options.delete(:controller) || name }
0
- collection = options.delete(:collection) || {}
0
- member = { :edit => :get, :delete => :get }.merge(options.delete(:member) || {})
0
+ # Behavior#+resources+ is a route helper for defining a collection of
0
+ # RESTful resources. It yields to a block for child routes.
0
+ # name<String, Symbol>:: The name of the resources
0
+ # Ovverides and parameters to be associated with the route
0
+ # ==== Options (options)
0
+ # :namespace<~to_s>: The namespace for this route.
0
+ # :name_prefix<~to_s>:
0
+ # A prefix for the named routes. If a namespace is passed and there
0
+ # isn't a name prefix, the namespace will become the prefix.
0
+ # :controller<~to_s>: The controller for this route
0
+ # :collection<~to_s>: Special settings for the collections routes
0
+ # Special settings and resources related to a specific member of this
0
+ # A list of the keys to be used instead of :id with the resource in the order of the url.
0
+ # ==== Block parameters
0
+ # next_level<Behavior>:: The child behavior.
0
+ # Routes which will define the specified RESTful collection of resources
0
+ # r.resources :posts # will result in the typical RESTful CRUD
0
+ # # GET /posts/?(\.:format)? :action => "index"
0
+ # # GET /posts/index(\.:format)? :action => "index"
0
+ # # shows new resource form
0
+ # # GET /posts/new :action => "new"
0
+ # # POST /posts/?(\.:format)?, :action => "create"
0
+ # # GET /posts/:id(\.:format)? :action => "show"
0
+ # # GET /posts/:id/edit :action => "edit"
0
+ # # PUT /posts/:id(\.:format)? :action => "update"
0
+ # # shows deletion confirmation page
0
+ # # GET /posts/:id/delete :action => "delete"
0
+ # # destroys resources
0
+ # # DELETE /posts/:id(\.:format)? :action => "destroy"
0
+ # r.resources :posts do |posts|
0
+ # posts.resources :comments
0
+ def resources(name, *args, &block)
0
+ options = extract_options_from_args!(args) || {}
0
+ singular = options[:singular] ? options[:singular].to_s : Extlib::Inflection.singularize(name)
0
+ klass = args.first ? args.first.to_s : Extlib::Inflection.classify(singular)
0
+ keys = [ options.delete(:keys) || options.delete(:key) || :id ].flatten
0
+ params = { :controller => options.delete(:controller) || name }
0
+ collection = options.delete(:collection) || {}
0
+ member = { :edit => :get, :delete => :get }.merge(options.delete(:member) || {})
0
- # Try pulling :namespace out of options for backwards compatibility
0
- options[:name_prefix] ||= nil # Don't use a name_prefix if not needed
0
- options[:resource_prefix] ||= nil # Don't use a resource_prefix if not needed
0
- options[:controller_prefix] ||= options.delete(:namespace)
0
+ # Try pulling :namespace out of options for backwards compatibility
0
+ options[:name_prefix] ||= nil # Don't use a name_prefix if not needed
0
+ options[:resource_prefix] ||= nil # Don't use a resource_prefix if not needed
0
+ options[:controller_prefix] ||= options.delete(:namespace)
0
- self.namespace(name, options).to(params) do |resource|
0
- root_keys = keys.map { |k| ":#{k}" }.join("/")
0
- resource.match("(/index)(.:format)", :method => :get).to(:action => "index").
0
- name(name).register_resource(name)
0
- resource.match("(.:format)", :method => :post).to(:action => "create")
0
+ self.namespace(name, options).to(params) do |resource|
0
+ root_keys = keys.map { |k| ":#{k}" }.join("/")
0
+ resource.match("(/index)(.:format)", :method => :get).to(:action => "index").
0
+ name(name).register_resource(name)
0
- resource.match("/new(.:format)", :method => :get).to(:action => "new").
0
- name("new", singular).register_resource(name, "new")
0
+ resource.match("(.:format)", :method => :post).to(:action => "create")
0
+ resource.match("/new(.:format)", :method => :get).to(:action => "new").
0
+ name("new", singular).register_resource(name, "new")
0
- # => user defined collection routes
0
- collection.each_pair do |action, method|
0
- resource.match("/#{action}(.:format)", :method => method).to(:action => "#{action}").
0
- name(action, name).register_resource(name, action)
0
+ # => user defined collection routes
0
+ collection.each_pair do |action, method|
0
+ resource.match("/#{action}(.:format)", :method => method).to(:action => "#{action}").
0
+ name(action, name).register_resource(name, action)
0
- resource.match("/#{root_keys}(.:format)", :method => :get).to(:action => "show").
0
- name(singular).register_resource(klass)
0
+ resource.match("/#{root_keys}(.:format)", :method => :get).to(:action => "show").
0
+ name(singular).register_resource(klass)
0
- # => user defined member routes
0
- member.each_pair do |action, method|
0
- resource.match("/#{root_keys}/#{action}(.:format)", :method => method).
0
- to(:action => "#{action}").name(action, singular).register_resource(klass, action)
0
+ # => user defined member routes
0
+ member.each_pair do |action, method|
0
+ resource.match("/#{root_keys}/#{action}(.:format)", :method => method).
0
+ to(:action => "#{action}").name(action, singular).register_resource(klass, action)
0
- resource.match("/#{root_keys}(.:format)", :method => :put).
0
- to(:action => "update")
0
- resource.match("/#{root_keys}(.:format)", :method => :delete).
0
- to(:action => "destroy")
0
+ resource.match("/#{root_keys}(.:format)", :method => :put).
0
+ to(:action => "update")
0
+ resource.match("/#{root_keys}(.:format)", :method => :delete).
0
+ to(:action => "destroy")
0
- nested_keys = keys.map { |k| k.to_s == "id" ? ":#{singular}_id" : ":#{k}" }.join("/")
0
- resource.options(:name_prefix => singular, :resource_prefix => klass).match("/#{nested_keys}", &block)
0
+ nested_keys = keys.map do |k|
0
+ k.to_s == "id" ? ":#{singular}_id" : ":#{k}"
0
+ # Procs for building the extra collection/member resource routes
0
+ placeholder = Router.resource_routes[ [@options[:resource_prefix], klass].flatten.compact ]
0
+ builders[:collection] = lambda do |action, to, method|
0
+ resource.before(placeholder).match("/#{action}(.:format)", :method => method).
0
+ to(:action => to).name(action, name).register_resource(name, action)
0
+ builders[:member] = lambda do |action, to, method|
0
+ resource.match("/#{root_keys}/#{action}(.:format)", :method => method).
0
+ to(:action => to).name(action, singular).register_resource(klass, action)
0
+ resource.options(:name_prefix => singular, :resource_prefix => klass).
0
+ match("/#{nested_keys}").resource_block(builders, &block)
0
- # Behavior#+resource+ is a route helper for defining a singular RESTful
0
- # resource. It yields to a block for child routes.
0
- # name<String, Symbol>:: The name of the resource.
0
- # Overides and parameters to be associated with the route.
0
- # ==== Options (options)
0
- # :namespace<~to_s>: The namespace for this route.
0
- # :name_prefix<~to_s>:
0
- # A prefix for the named routes. If a namespace is passed and there
0
- # isn't a name prefix, the namespace will become the prefix.
0
- # :controller<~to_s>: The controller for this route
0
- # ==== Block parameters
0
- # next_level<Behavior>:: The child behavior.
0
- # Array:: Routes which define a RESTful single resource.
0
- # r.resource :account # will result in the typical RESTful CRUD
0
- # # shows new resource form
0
- # # GET /account/new :action => "new"
0
- # # POST /account/?(\.:format)?, :action => "create"
0
- # # GET /account/(\.:format)? :action => "show"
0
- # # GET /account//edit :action => "edit"
0
- # # PUT /account/(\.:format)? :action => "update"
0
- # # shows deletion confirmation page
0
- # # GET /account//delete :action => "delete"
0
- # # destroys resources
0
- # # DELETE /account/(\.:format)? :action => "destroy"
0
- # You can optionally pass :namespace and :controller to refine the routing
0
- # or pass a block to nest resources.
0
- # r.resource :account, :namespace => "admin" do |account|
0
- # account.resources :preferences, :controller => "settings"
0
- def resource(name, *args, &block)
0
- options = extract_options_from_args!(args) || {}
0
- params = { :controller => options.delete(:controller) || name.pluralize }
0
+ # Behavior#+resource+ is a route helper for defining a singular RESTful
0
+ # resource. It yields to a block for child routes.
0
+ # name<String, Symbol>:: The name of the resource.
0
+ # Overides and parameters to be associated with the route.
0
+ # ==== Options (options)
0
+ # :namespace<~to_s>: The namespace for this route.
0
+ # :name_prefix<~to_s>:
0
+ # A prefix for the named routes. If a namespace is passed and there
0
+ # isn't a name prefix, the namespace will become the prefix.
0
+ # :controller<~to_s>: The controller for this route
0
+ # ==== Block parameters
0
+ # next_level<Behavior>:: The child behavior.
0
+ # Array:: Routes which define a RESTful single resource.
0
+ # r.resource :account # will result in the typical RESTful CRUD
0
+ # # shows new resource form
0
+ # # GET /account/new :action => "new"
0
+ # # POST /account/?(\.:format)?, :action => "create"
0
+ # # GET /account/(\.:format)? :action => "show"
0
+ # # GET /account//edit :action => "edit"
0
+ # # PUT /account/(\.:format)? :action => "update"
0
+ # # shows deletion confirmation page
0
+ # # GET /account//delete :action => "delete"
0
+ # # destroys resources
0
+ # # DELETE /account/(\.:format)? :action => "destroy"
0
+ # You can optionally pass :namespace and :controller to refine the routing
0
+ # or pass a block to nest resources.
0
+ # r.resource :account, :namespace => "admin" do |account|
0
+ # account.resources :preferences, :controller => "settings"
0
+ def resource(name, *args, &block)
0
+ options = extract_options_from_args!(args) || {}
0
+ params = { :controller => options.delete(:controller) || name.pluralize }
0
+ member = { :new => :get, :edit => :get, :delete => :get }.merge(options.delete(:member) || {})
0
- options[:name_prefix] ||= nil # Don't use a name_prefix if not needed
0
- options[:resource_prefix] ||= nil # Don't use a resource_prefix if not needed
0
- options[:controller_prefix] ||= options.delete(:namespace)
0
+ options[:name_prefix] ||= nil # Don't use a name_prefix if not needed
0
+ options[:resource_prefix] ||= nil # Don't use a resource_prefix if not needed
0
+ options[:controller_prefix] ||= options.delete(:namespace)
0
- self.namespace(name, options).to(params) do |resource|
0
- resource.match("(.:format)", :method => :get ).to(:action => "show" ).name(name).register_resource(name)
0
- resource.match("(.:format)", :method => :post ).to(:action => "create" )
0
- resource.match("(.:format)", :method => :put ).to(:action => "update" )
0
- resource.match("(.:format)", :method => :delete).to(:action => "destroy")
0
- resource.match("/new(.:format)", :method => :get ).to(:action => "new" ).name(:new, name).register_resource(name, "new")
0
- resource.match("/edit(.:format)", :method => :get ).to(:action => "edit" ).name(:edit, name).register_resource(name, "edit")
0
- resource.match("/delete(.:format)", :method => :get ).to(:action => "delete" ).name(:delete, name).register_resource(name, "delete")
0
+ self.namespace(name, options).to(params) do |resource|
0
+ resource.match("(.:format)", :method => :get).to(:action => "show").
0
+ name(name).register_resource(name)
0
+ resource.match("(.:format)", :method => :post).to(:action => "create")
0
+ resource.match("(.:format)", :method => :put).to(:action => "update")
0
+ resource.match("(.:format)", :method => :delete).to(:action => "destroy")
0
+ member.each_pair do |action, method|
0
+ resource.match("/#{action}(.:format)", :method => method).to(:action => action).
0
+ name(action, name).register_resource(name, action)
0
- resource.options(:name_prefix => name, :resource_prefix => name, &block) if block_given?
0
+ builders[:member] = lambda do |action, to, method|
0
+ resource.match("/#{action}(.:format)", :method => method).to(:action => to).
0
+ name(action, name).register_resource(name, action)
0
+ resource.options(:name_prefix => name, :resource_prefix => name).
0
+ resource_block(builders, &block)
0
- def register_resource(*key)
0
- key = [@options[:resource_prefix], key].flatten.compact
0
+ def register_resource(*key)
0
+ key = [@options[:resource_prefix], key].flatten.compact
0
+ def resource_block(builders, &block)
0
+ behavior = ResourceBehavior.new(builders, @proxy, @conditions, @params, @defaults, @identifiers, @options, @blocks)
0
+ with_behavior_context(behavior, &block)
0
+ # Adding the collection and member methods to behavior
0
+ class ResourceBehavior < Behavior #:nodoc:
0
+ def initialize(builders, *args)
0
+ @collection = builders[:collection]
0
+ @member = builders[:member]
0
+ def collection(action, options = {})
0
+ method = options[:method]
0
+ to = options[:to] || action
0
+ @collection[action, to, method]
0
+ def member(action, options = {})
0
+ method = options[:method]
0
+ to = options[:to] || action
0
+ @member[action, to, method]
0
\ No newline at end of file