Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Supporting singleton resources in ActiveResource

#4348

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.

Reported by Jamis Buck · April 8th, 2010 @ 07:24 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Jamis Buck
    Jamis Buck
    • Tag changed from activeresource, singleton to activeresource, patch, singleton

    April 8th, 2010 @ 08:28 PM

  2. Gaël Deest
    Gaël Deest

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

    April 9th, 2010 @ 09:54 AM

  3. Jamis Buck
    Jamis Buck

    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.

    April 21st, 2010 @ 09:29 PM

  4. Jacob Gyllenstierna
    Jacob Gyllenstierna

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

    December 17th, 2010 @ 09:17 PM

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

    March 18th, 2011 @ 12:00 AM

  6. rails
    rails
    • State changed from open to stale

    March 18th, 2011 @ 12:00 AM