Ticket #96 (closed defect: fixed)

Opened 2 years ago

Last modified 14 months ago

R helper only looks at the number of arguments

Reported by: mdaines Owned by: why
Priority: major Milestone: Camping 1.6
Component: camping.rb Version: 1.5
Keywords: Cc:

Description (last modified by archengule) (diff)

I'm not sure if this is going to be found much in the wild, but it seems useful to have the R helper also see if the url it choses matches what's generated. If you had a controller like this:

class Number < R '/name/([a-z]+)', '/number/(\d+)'
  def get(n)
    @number = n
    render :number
  end
end

And you ask for, say, R(:Number, 2), you'll always get /name/2 instead of /number/2.

Attachments

r_matches_generated_as_well.diff (0.7 kB) - added by mdaines 2 years ago.

Change History

Changed 2 years ago by mdaines

follow-up: ↓ 2   Changed 2 years ago by why

  • owner changed from somebody to why
  • status changed from new to assigned
  • version set to 1.5
  • milestone set to Camping 1.6

Let's see if we can wheedle it down a bit tinier.

in reply to: ↑ 1   Changed 15 months ago by mdaines

  • description modified (diff)

Replying to why:

Let's see if we can wheedle it down a bit tinier.

I will take a look! A little while after I posted this I just completely forgot about it.

As a point of interest I was also working on something slightly related -- Rails-style "REST" resource controllers. In one or two Camping apps I have started I have done stuff like this:

module Controllers

  class ReportsCollection < R '/reports'
    # ...
  end

  class NewReportsMember < R '/reports/new'
    # ...
  end

  class ReportsMember < R '/reports/(\d+)'
    # ...
  end
  
end

Instead I would like this:

module Controllers

  class Reports < ZZZ
    # ...
  end
  
end

Maybe that is too much magic though.

  Changed 14 months ago by archengule

  • description modified (diff)

How's this? (And by the way, attachments don't seem to be working)

Index: camping-unabridged.rb
===================================================================
--- camping-unabridged.rb       (revision 221)
+++ camping-unabridged.rb       (working copy)
@@ -194,9 +194,10 @@
     #
     def R(c,*g)
       p,h=/\(.+?\)/,g.grep(Hash)
-      (g-=h).inject(c.urls.find{|x|x.scan(p).size==g.size}.dup){|s,a|
-        s.sub p,C.escape((a[a.class.primary_key]rescue a))
-      }+(h.any?? "?"+h[0].map{|x|x.map{|z|C.escape z}*"="}*"&": "")
+      g-=h; c.urls.map{|x| (break w if (w = g.inject(x){|s,a|
+           s.sub p,C.escape((a[a.class.primary_key]rescue a))}) =~ /^#{x}\/?$/) \
+           if x.scan(p).size==g.size}
+         }+(h.any?? "?"+h[0].map{|x|x.map{|z|C.escape z}*"="}*"&": "")
     end
 
     # Shows AR validation errors for the object passed.

  Changed 14 months ago by zimbatm

  • status changed from assigned to closed
  • resolution set to fixed

Thanks, I used you patch as an inspiration for change set [236].

Note: See TracTickets for help on using tickets.