Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
63 lines (41 sloc) 1.88 KB


Rewrite of resources_controller, main aim is to make backend ORM and controller arch pluggable. Subsidiary aim API improvements. There shall be an rc_bc plugin that aims at backwards compat with resources_controller.

It's work in progress!


When rc has parsed a path against a path_spec, you get an rc path, with this you can do all of the relative resource stuff you might want to do


Spec: Singleton: "account", Polymorphic: "commentable", Keyed: "comment"

request.path: '/account/posts/3/comments/2'

Rc::Path contains the records and the rc path_spec info

p.resource    # => <Comment: 2>
p.parents     # => [<User:1>, <Post:3>]
p.member      # => [:account, <Post: 3>, <Comment: 2>]  # suitable for passing to polymorphic_path
p.collection  # => [:account, <Post: 3>, :comments]     # as above

# go up the path
p.up            # => new Rc::Path for immediately enclosing 
p.up.resource   # => <Post: 3>
p.up.parents    # => [<User:1>]
p.up.member     # => [:account, <Post: 3>]
p.up.collection # => [:account, :posts]

# go to the comment path for another comment in same context
p.up.member << @comment # => [:account, <Post: 3>, <Comment: 5>]


Specify a RESTful resource

# two keyed resources
p = Rc.path_spec :forum, :post
=> #<Rc::PathSpec: [#<Rc::Spec::Keyed: /forums/:forum_id {name:forum}>, #<Rc::Spec::Keyed: /posts/:post_id {name:post}>]>

=> "/forums/:forum_id/posts/:post_id"

=> /^\/forums\/[^\/]+\/posts\/[^\/]+$/

Match, or expand an incomplete (e.g. with glob or polymorphic) path spec

g = Rc.path_spec '*', :post
=> #<Rc::PathSpec: [#<Rc::Spec::Glob: /*>, #<Rc::Spec::Keyed: /posts/:post_id {name:post}>]>

p = g.expand '/foo/bars/3/posts/4'
=> "/foo/bars/:bar_id/posts/:post_id"

g.expand '/foo/bars/3'
=> false

g.match? '/foo/1/posts/2'
=> true