From 8d5bf9161ca7238dddd55c18722efabc33d590e8 Mon Sep 17 00:00:00 2001 From: Shay Arnett Date: Wed, 9 Jul 2008 12:43:30 -0400 Subject: [PATCH] resource urls with additional params --- lib/merb-core/controller/abstract_controller.rb | 5 ++++- lib/merb-core/dispatch/router/route.rb | 5 ++++- spec/public/controller/url_spec.rb | 10 ++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/merb-core/controller/abstract_controller.rb b/lib/merb-core/controller/abstract_controller.rb index 94ce80a..703daac 100644 --- a/lib/merb-core/controller/abstract_controller.rb +++ b/lib/merb-core/controller/abstract_controller.rb @@ -441,7 +441,10 @@ class Merb::AbstractController # ==== Alternatives # If a hash is used as the first argument, a default route will be # generated based on it and rparams. - def url(name, rparams={}) + def url(name, rparams={},qparams={}) + unless rparams.is_a?(Hash) || qparams.empty? + rparams = qparams.merge(:id => rparams) + end uri = Merb::Router.generate(name, rparams, { :controller => controller_name, :action => action_name, diff --git a/lib/merb-core/dispatch/router/route.rb b/lib/merb-core/dispatch/router/route.rb index 076ef5b..ba35875 100644 --- a/lib/merb-core/dispatch/router/route.rb +++ b/lib/merb-core/dispatch/router/route.rb @@ -210,8 +210,11 @@ module Merb value = if segment.is_a? Symbol if params.is_a? Hash - params[segment] || fallback[segment] + if segment.to_s =~ /_id/ && params[:id].respond_to?(segment) + params[segment] = params[:id].send(segment) + end query_params.delete segment + params[segment] || fallback[segment] else if segment == :id && params.respond_to?(:to_param) params.to_param diff --git a/spec/public/controller/url_spec.rb b/spec/public/controller/url_spec.rb index 03eada5..7adacaa 100644 --- a/spec/public/controller/url_spec.rb +++ b/spec/public/controller/url_spec.rb @@ -166,5 +166,15 @@ describe Merb::Controller, " url" do @pink = Pink.new @controller.url(:monkey_blue_pink, @pink).should == "/monkeys/45/blues/13/pinks/22" end + + it "should match resource with additional params" do + @monkey = Monkey.new + @controller.url(:monkey, @monkey, {:foo => "bar"}).should == "/monkeys/45?foo=bar" + end + + it "should match a nested resource with additional params" do + @blue = Blue.new + @controller.url(:monkey_blue,@blue,{:foo => "bar"}).should == "/monkeys/45/blues/13?foo=bar" + end end \ No newline at end of file -- 1.5.6.1