This project is archived and is in readonly mode.

#4348 ✓stale
Jamis Buck

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

etc.

The attached patch includes tests.

Comments and changes to this ticket

  • Jamis Buck

    Jamis Buck April 8th, 2010 @ 08:28 PM

    • Tag changed from activeresource, singleton to activeresource, patch, singleton
  • Gaël Deest

    Gaël Deest April 9th, 2010 @ 09:54 AM

    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. :)

  • Jamis Buck

    Jamis Buck April 21st, 2010 @ 09:29 PM

    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.

  • Jacob Gyllenstierna

    Jacob Gyllenstierna December 17th, 2010 @ 09:17 PM

    Any chance of including this? This would be really useful.

  • rails

    rails March 18th, 2011 @ 12:00 AM

    • 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.

  • rails

    rails March 18th, 2011 @ 12:00 AM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages