-
Couldn't load subscription status.
- Fork 363
Support Singleton Routes #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Thanks for digging in on this, @kenmazaika. I usually work around this with something like class Account < ActiveResource::Base
def self.find_singleton
find :one, from: '/account.xml'
end
endNative support would be pretty nice, and subclassing sounds reasonable. Could just do |
|
I would be happy to make the change from to Are there any other changes that I should make, while I'm at it? |
|
@kenmazaika looking at my own code, it'd be hard to use this new subclass since I already rely on an abstract base class: This is a pretty common pattern. Subclassing |
|
I like the idea with the mixin the best. What about |
|
I'd be happy to change this to a mixin. So that I'll probably get around to making this change sometime next weekend. I'll add to this pull request, and make a comment to ping you guys to look at it. If there are any other changes that should happen, let me know. Thanks. |
|
Great! Thanks @kenmazaika |
|
Good job @kenmazaika. Looks good to me and tests are green, too. Could you please squash these 2 commits into 1 clean commit? |
|
@SweeD done. |
|
Looks good @kenmazaika! 👍 for merge when that's fixed, @SweeD |
Implement ActiveResource::Singleton as a mixin, instead of using ActiveResource::Singleton::Base as a subclass.
|
@kenmazaika Good job, thank you! :) |
When the Singleton module was originally introduced in [rails#4][], there was a suggestion about the implementation for `.find` that used the `:one` modifier option with the `:from` keyword argument. ```ruby class Account < ActiveResource::Base def self.find_singleton find :one, from: '/account.xml' end end ``` This commit modifies the `Singleton.find` method by inlining the private `find_singleton` method. By doing so, the implementation can invoke `super`, and rely on the built-in `instantiate_record` call provided by `Base`. This reduces the amount of duplication involved in transforming a response payload into a resource instance. [rails#4]: rails#4 [rails#4]: rails#4 (comment)
When the Singleton module was originally introduced in [rails#4][], there was a suggestion about the implementation for `.find` that used the `:one` modifier option with the `:from` keyword argument. ```ruby class Account < ActiveResource::Base def self.find_singleton find :one, from: '/account.xml' end end ``` This commit modifies the `Singleton.find` method by inlining the private `find_singleton` method. By doing so, the implementation can invoke `super`, and rely on the built-in `instantiate_record` call provided by `Base`. This reduces the amount of duplication involved in transforming a response payload into a resource instance. [rails#4]: rails#4 [rails#4]: rails#4 (comment)
When the Singleton module was originally introduced in [rails#4][], there was a suggestion about the implementation for `.find` that used the `:one` modifier option with the `:from` keyword argument. ```ruby class Account < ActiveResource::Base def self.find_singleton find :one, from: '/account.xml' end end ``` This commit modifies the `Singleton.find` method by inlining the private `find_singleton` method. By doing so, the implementation can invoke `super`, and rely on the built-in `instantiate_record` call provided by `Base`. This reduces the amount of duplication involved in transforming a response payload into a resource instance. [rails#4]: rails#4 [rails#4]: rails#4 (comment)
This is a version of this pull request, for the ActiveResource repo instead of the Rails repo:
rails/rails#5361
I was interfacing with a third party who exposed an API with a RESTful singleton route. In order to support this with ActiveResource I made this patch.
Initially I planned to patch ActiveResource::Base to support this, but very quickly it became convoluted with conditionals to determine which mode the model was in (singleton or standard). It was much easier to make a subclass, and have clients inherit from this.
I tried to be consistent with ActiveResource::Base. The one exception that is not consistent is find. ActiveResource::Base has the following code:
In the singleton case however, find does not have a concept of a scope (or rather, the scope is always :singleton). Instead of forcing users to type an option that is not configurable, I thought it made more sense to use a sensible default, and alter how the method processes arguments.
To test the change, I made this simple singleton route API with an associated ActiveResource implementation:
https://github.com/kenmazaika/weather
If this is a feature that is not in ActiveResource for a reason, feel free to close this ticket out and not merge it. I did find some other people looking for this functionality though:
https://rails.lighthouseapp.com/projects/8994/tickets/760-activeresource-with-map-resource
https://rails.lighthouseapp.com/projects/8994/tickets/4348-supporting-singleton-resources-in-activeresource
http://railsforum.com/viewtopic.php?id=23172
If there are changes that need to happen to get this merged in, please let me know. I'd be happy to make them.