I think there's definitely something to this, at the very least it's
worth investigating what kind of functionality we can add in. For a
while now there's been talk about how there really should be some
concept of resources that you could introspect to do these kind of
useful things.
--
Cheers
Koz
Definitely, and I think we've discussed this previously on #rails-contrib?
I'm not necessarily sold on having the concept of the 'resource'
you're looking at tied so tightly to routes. Perhaps there's another
class we should have which is available on the request?
request.resource or something with the necessary bits and bobs to
introspect successfully.
I think the best way forward is for the authors of the relevant
plugins (m_r and RC) to figure out if there's a subset of
functionality which they could share, and then figure out what changes
we'd need to make to rails to enable that functionality, or if we
should adopt it ourselves.
--
Cheers
Koz
Sounds great to me :)
--
Cheers
Koz
You guys are bound to have a bunch in common, basic core stuff. Of
that set of features there's probably some stuff that still won't be
fit for inclusion in the core, but odds are there's a subset of that
which is. Whether that functionality is user-visible stuff or just
nice hooks, I don't know.
You guys are the experts, I'm sure you'll come up with some awesome stuff :).
--
Cheers
Koz
(I'm the resources_controller guy, btw)
- crud: Hampton's proposal of generated cruddies looks really nice, is
easy to understand, and override (let's say you want to order your
messages by some param, you can just override messages). Handling non
standard names should be easy enough to handle
load User, Message => {:as => :tweet}
# or, it might be nicer if spread over two lines?
load User
load Message, :as => :tweet
- scoping: leveraging AR's assocs to provide scoping. Also nicely
covered by Hampton's proposal. However, singleton vs collection
resources need different handling. In Hampton's example, perhaps:
load User, Image => {:singleton => true}
# or
load User
load Image, :singleton => true
# doesn't generate images, and generates this:
def image
user.image
end
- polymorphism. Not obvious, as James points out. I use polymorphism
a lot, which is why I gave r_c the ability to load up the resources
differently depending on the route that invoked it. I guess people
might argue whether this is 'basic'. (From my perspective, I got sick
of writing the same comments controller over and over again.)
if this is regarded as a 'basic', then
load :commentable, Comment
could do the trick.
To implement this sort of thing, r_c gleans info from the route that
invoked the controller, this means we need to introspect the route
that the controller was invoked with.
So, it looks like Hampton's proposal covers the 'has_many' resource
case, and should cover the 'has_one' case with a little tweaking, and
could handle non standard names. It seems that 'has_many :as', and
'has_one :as' would require something of a different nature (route
introspection). Perhaps for that reason alone, perhaps polymorphism
should not be regarded as basic?
I would be happy with this, because that's what plugins are for.
(polymorphism is a specific case of being able to nest resources at
arbitrary points - this problem is solved by the same technique. the
one images controller can service /users/:id/images /cats/:id/images)
But, if you think polymorphism should be regarded as basic...
Michael Koziarski wrote:
> request.resource or something with the necessary bits and bobs to
> introspect successfully.
This would be a great abstraction. r_c currently emulates this
behaviour, by abstracting the resources from the path (using the
routing code). This enables the polymorphic and nesting cases
In Rails, routing does some funky awesomeness to figure out the
mapping of url to controllers and params, but it throws (most of) that
work away before the controller is invoked. Having a resource
abstraction would be the container for passing around RESTful
resources, while being agnostic about how they were generated, and
what they map to.
On this last point, Chris Cruft, and other rc_ers have made the point
to me that they sometimes have restful controllers which don't have
ActiveRecords behind them (or have AR enclosing resources, but not at
the endpoints), it would be nice to have an ordered array of abstract
resources handed to the controller, and that request.resource would
be.
(btw. It just so happens I'm giving a talk at RailsConfEurope about
r_c, and how rails could help /.*resource.*/ plugin authors by passing
essentially request.resource to the controller. I shall watch this
thread in earnest - maybe I'll have to rewrite the damn thing. Yay
for rewriting the damn things!
Cheers,
Ian
I've used m_r, r_c and others, and while promising at start, I had to
remove them because most of the time the defaults weren't the defaults
as I want them to be and they became too obtrusive. The problem, as
hampton already indicated, is that they try to do too much. In my case
especially with regards to action behaviour. Not so much with regards to
resource facilities.
What I do like about hamptons proposal is that he's indicating he wants
the most basic resource facilities.
However, instead of baking this in the ActionController I think
resources deserve to be first class citizens. This idea was actually
born by Dan Yoder after long discussions on the Waves project. The term
"resource controller" that most are using is a misnomer, the two should
be decoupled imo.
I.e., when I do map.resources :posts, I would like to see this routed to:
/app/resources/posts_resource.rb
This class would inherit or mixin a ResourceService (a term r_c uses).
There you would do the kind of magic that hampton suggests in his pastie
and that is happening in all the plugins that have been plugged in this
thread. You would think more in terms of the resources instead of in
terms of the controller action. This is the place where you set up the
resources required, where you define how a new resource is created, etc.
All this would stay out of your controllers.
Once the resources are set up you go to your controllers. Just like
views have access to the instance variables defined in a controller the
controller would have access to the instance variables defined in the
resource. This also makes controllers more re-usable for multiple
resources, and gives greater potential for DRYness. Which is the other
part that all these "resource controllers" are trying to solve. So
instead of having a controller for every resource you could have just a
few controllers (ideally just one). This again would highlight that
controllers are about action, orchestration, driving behaviour, and not
about managing resources.
Lawrence
Definitely, I think it's a great idea to come up with a nice
evolutionary enhancement to the resources code, along with some added
metadata for reflecting on what's going on.
> However, instead of baking this in the ActionController I think
> resources deserve to be first class citizens. This idea was actually
> born by Dan Yoder after long discussions on the Waves project. The term
> "resource controller" that most are using is a misnomer, the two should
> be decoupled imo.
I think that this kind of experimentation should definitely take place
in a plugin. The controller + routes thing we have now works really
well, and some minor enhancements could well improve that. Something
larger like you're talking about here should be left in plugins till
it's had a chance to prove itself and get fully baked.
--
Cheers
Koz
From what I gather hampton's suggestion isn't for a transparent
replacement, but some simple filters and helpers that get created for
you. ala
This discussion didn't really seem to reach a conclusion, hampton do
you have a patch ready to go for this? I think that the pastie above
is a good start for experimentation and something we should consider
including for the 2.2 release.
--
Cheers
Koz
Do you mean:
http://www.mbleigh.com/plugins/needy-controllers-drying-stylesheets-scripts-and-fetching
I hadn't seen it till now but at first glance it does seem pretty cool
--
Cheers
Koz