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) || {})
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
options[:name_prefix] ||= nil
options[:resource_prefix] ||= nil
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("/")
resource.match("(/index)(.:format)", :method => :get).to(:action => "index").
name(name).register_resource(name)
resource.match("(.:format)", :method => :post).to(:action => "create")
resource.match("/new(.:format)", :method => :get).to(:action => "new").
name("new", singular).register_resource(name, "new")
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
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]
builders = {}
builders[:collection] = lambda do |action, to, method|
resource.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
resource.match("/#{root_keys}(.:format)", match_opts.merge(:method => :get)).to(:action => "show").
name(singular).register_resource(klass_name, :identifiers => keys)
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
resource.match("/#{root_keys}(.:format)", match_opts.merge(:method => :put)).
to(:action => "update")
resource.match("/#{root_keys}(.:format)", match_opts.merge(:method => :delete)).
to(:action => "destroy")
end
end