ingoweiss / resource_awareness

Makes information about a Rails application's resources (as defined via the 'resource(s)' routing DSL methods) available via Rails.application.resources
http://github.com/ingoweiss/resource_awareness
15 stars 1 forks source link

Should be able to handle multiple resources with same controller #1

Open MDaubs opened 14 years ago

MDaubs commented 14 years ago

resource_awareness is exactly what I've been looking for for a long long time but I think it could be 1000% more useful if it supported multiple resource mappings per controller. Example - I have ClubsController and PeopleController. Clubs and People should both be able to create/edit Pages that belong to either a Club or Person. The PagesController should know (via resource_awareness) whether or not it was called from a Club or Person and which Club or Person it is (probably for assigning a polymorphic field).

ingoweiss commented 14 years ago

Ah, thanks for catching this! I guess this applies to the controller's 'resource' method. It would be trivial to change it to a 'resources' method that returns all resources handled by that controller. I do suspect that in the overwhelming majority of cases there is a one-to-one mapping, though, and I would hate to have to use resources.first in all those cases. So my hunch is that the 'resource' method should stay and return the first handled resource if the controller handles only one resource (what it should return if it handles multiple resources I'm not sure yet), and that I should add a 'resources' method returning an array, and maybe a query method such as 'multiple_resources?'. But I'll give it some more thought

ingoweiss commented 14 years ago

BTW I don't think ResourceAwareness will have much to say about what resource a multiple-resource controller was called from, though...

MDaubs commented 14 years ago

Hi Ingo - I think it would be convenient to have a clean way for a controller to find its resource in the case of multiple resources. I'm honestly not sure how it would be done or if it is something that should fall within the facet of ResourceAwareness. My thought was that a PagesController calling the resource method would be able to get enough information to determine if it needs to recall a Club or People model and if so which id and which parameter that id is (a Rails::Resource object). I deal with mostly "enterprise" apps where this many-to-many situation occurs often. I find myself writing repetitious code to bind resource controllers to numerous others. Now if Rails had a way to stuff data into a route object and then later retrieve that from the request object I'd be a happy camper... but I guess it's not very MVC for a controller to speak directly to the router. Thanks for commenting and all the best.

ingoweiss commented 14 years ago

Hi, I am going back and forth about what the controller methods should be and how they should behave. Until I make up my mind you can do the following in your controller:

def resources
  Rails.application.resources.find_all{|r| r.controller == self.class}
end

now if there are multiple resources, here is one way to find out which is the current one that works for nested resources (I bet in most cases they will be nested):

Rails::Resource objects have a 'path_parameters' array attribute. Just check whether the 'path_parameters' are a subset of the current request parameters. The resource for which this is the case is the current one