This project is archived and is in readonly mode.
Supporting singleton resources in ActiveResource
Reported by Jamis Buck | April 8th, 2010 @ 07:24 PM
Currently, if you indicate that a resource is a singleton in your routes, there is no way (short of monkeypatching ActiveResource) to access that resource via ActiveResource.
This patch adds support for a "singleton" macro, that lets you declare that a particular model maps to a singleton resource:
class Status < ActiveResource::Base singleton # ... end
You can then access that singleton resource via a bare "find" call (without arguments):
current_status = Status.find current_status.description = "Code red!" current_status.save
The attached patch includes tests.
Comments and changes to this ticket
Interesting idea. I'll try to review this patch thoroughly today, but just reading through the patch:
attr_accessor_with_default(:collection_name) do #:nodoc: if singleton? element_name else ActiveSupport::Inflector.pluralize(element_name) end end
Does it really make sense to call :collection_name what is basically a singular name ? Not a big deal, I guess you didn't want to change too much code, it just disturbs me. :)
Sorry, I guess I'm not automatically notified of comments posted here. I'll have to figure that out.
Regarding the singularized collection name: I'm not attached to that implementation. I was just trying to get this working with a minimum of reimplementation. If there's a cleaner way to do it, that's fine. But ultimately, in a singleton resource, there really isn't any "collection"--there is only the singleton, so you could look at my implementation as simply aliasing the collection to the element name. This has the happy effect of "just working" with the existing activeresource internals; otherwise, we'd be looking at conditionals throughout the code that check for singleton or not and tweak the path accordingly.
- State changed from new to open
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
Tickets have moved to Github
The new ticket tracker is available at https://github.com/rails/rails/issues