Old Emmanuel Oga's Weblog (new one is at www.emmanueloga.com)

Nested resources

Posted in Uncategorized by emmanueloga on septiembre 18, 2007

Estoy investigando como detectar la relacion padre/hijo de los controladores, cuando estos manejan recursos rest.

Si alguien sabe de lo que estoy hablando, quizas le interese este pequeño experimiento.

http://pastie.caboo.se/98345

Se puede cargar en init.rb de un plugin o en enviroment.rb :

require ‘nombre_del_pastie.rb’

AutoRest::RailsResources.clear
ActionController::Routing::Routes.reload

Ejemplo:

routes.rb:

ActionController::Routing::Routes.draw do |map|

map.connect ”, :controller => “zones”

search_opt= { :get => :search }

map.resources :zones, :collection => search_opt do |zone|
zone.resources :customers, :name_prefix => “zone_”
end

map.resources :customers, :collection => search_opt do |customer|
customer.resources :documents, :name_prefix => “customer_”
end

map.resources :documents, :collection => search_opt
end

Y en la consola… :

>> AutoRest::RailsResources.resources_parents.each do |controller, parent|
puts “Controller: #{controller}, parents #{parent.to_a.join(“,”)} ”
end};
Controller: customers, parents zones
Controller: documents, parents customers

>> AutoRest::RailsResources.all.collect { |res| res.controller }
=> [“zones”, “customers”, “customers”, “documents”, “documents”]

WARNING! monster output ahead🙂

>> AutoRest::RailsResources.all.collect { |res| puts res.to_yaml; puts “-“*80 }
— !ruby/object:ActionController::Resources::Resource
collection_methods:
:search:
– :get
controller: zones
member_methods:
:get:
– :edit
member_path: /zones/:id
name_prefix:
nesting_path_prefix: /zones/:zone_id
new_methods:
:get:
– :new
new_path: /zones/new
options: {}

path: /zones
path_prefix:
plural: :zones
singular: zone
——————————————————————————–
— !ruby/object:ActionController::Resources::Resource
collection_methods: {}

controller: customers
member_methods:
:get:
– :edit
member_path: /zones/:zone_id/customers/:id
name_prefix: zone_
nesting_path_prefix: /zones/:zone_id/customers/:customer_id
new_methods:
:get:
– :new
new_path: /zones/:zone_id/customers/new
options: {}

path: /zones/:zone_id/customers
path_prefix: /zones/:zone_id
plural: :customers
singular: customer
——————————————————————————–
— !ruby/object:ActionController::Resources::Resource
collection_methods:
:search:
– :get
controller: customers
member_methods:
:get:
– :edit
member_path: /customers/:id
name_prefix:
nesting_path_prefix: /customers/:customer_id
new_methods:
:get:
– :new
new_path: /customers/new
options: {}

path: /customers
path_prefix:
plural: :customers
singular: customer
——————————————————————————–
— !ruby/object:ActionController::Resources::Resource
collection_methods: {}

controller: documents
member_methods:
:get:
– :edit
member_path: /customers/:customer_id/documents/:id
name_prefix: customer_
nesting_path_prefix: /customers/:customer_id/documents/:document_id
new_methods:
:get:
– :new
new_path: /customers/:customer_id/documents/new
options: {}

path: /customers/:customer_id/documents
path_prefix: /customers/:customer_id
plural: :documents
singular: document
——————————————————————————–
— !ruby/object:ActionController::Resources::Resource
collection_methods:
:search:
– :get
controller: documents
member_methods:
:get:
– :edit
member_path: /documents/:id
name_prefix:
nesting_path_prefix: /documents/:document_id
new_methods:
:get:
– :new
new_path: /documents/new
options: {}

path: /documents
path_prefix:
plural: :documents
singular: document
——————————————————————————–

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

A %d blogueros les gusta esto: