Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10088 (new enhancement)

Opened 3 years ago

Last modified 2 years ago

[PATCH] Added support for additional query parameters on ActiveResource save requests

Reported by: trek Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveResource Version: edge
Severity: normal Keywords: activeresource save update create
Cc:

Description

Attached is a patch that adds the ability to send additional query parameters on an ActiveResource object save (and pass these parameters to either create or update appropriately).

    matz = Person.new(:name => "Matz")
    matz.new?
    #=> true
    save_options = {:referral_code => 'abc123'}
    matz.save(save_options)
    #=> POST to http://site.com/people.xml?referral_code=abc123
    #=> with matz xml data

Attachments

add_additional_query_parameters_on_active_resource_save.diff (3.6 kB) - added by trek on 11/06/07 20:35:09.
add_additional_query_parameters_on_active_resource_save.2.diff (3.6 kB) - added by trek on 11/06/07 20:36:19.
add_additional_query_parameters_on_active_resource_save.3.diff (3.6 kB) - added by trek on 11/06/07 20:36:56.
add_additional_query_parameters_on_active_resource_save.4.diff (3.6 kB) - added by trek on 11/06/07 20:38:08.
add_additional_query_parameters_on_active_resource_save_using_params_option.diff (2.4 kB) - added by trek on 11/27/07 03:58:19.
updated patch using :params => style seen in ARes finders

Change History

11/06/07 20:35:09 changed by trek

  • attachment add_additional_query_parameters_on_active_resource_save.diff added.

11/06/07 20:36:19 changed by trek

  • attachment add_additional_query_parameters_on_active_resource_save.2.diff added.

11/06/07 20:36:56 changed by trek

  • attachment add_additional_query_parameters_on_active_resource_save.3.diff added.

11/06/07 20:38:08 changed by trek

  • attachment add_additional_query_parameters_on_active_resource_save.4.diff added.

(in reply to: ↑ description ) 11/06/07 21:50:52 changed by trek

Replying to trek: all the diff files are identical, fwiw: random time out errors at the "office"

11/06/07 22:03:33 changed by hmasing

This looks great - create the object, work with it, then apply additional data. Particularly useful for authorization across systems, etc. I know that I could use this immediately in my multi-channel rest-based eCommerce system supporting my warehouse operations (in production).

+1 this one, please.

11/06/07 22:10:38 changed by trek

  • keywords changed from activeresource save update create to unverified activeresource save update create.

11/07/07 17:33:01 changed by markcc01

Excellent request -- thank you for requesting this!! +1 please!

(in reply to: ↑ description ) 11/10/07 15:04:16 changed by libersy

Replying to trek:

Attached is a patch that adds the ability to send additional query parameters on an ActiveResource object save (and pass these parameters to either create or update appropriately).

excellent... we need something like this. +1

even better would be being able to send the data either as part of POST data or GET data, buts for us as-is.

11/10/07 17:44:00 changed by trek

  • keywords changed from unverified activeresource save update create to verified activeresource save update create.

(follow-up: ↓ 9 ) 11/25/07 22:27:00 changed by david

I'm not sure I understand the use case. Could you explain how and when you're in need of this? If there's a convincing use case, we should at the very least adopt a similar approach as the finder and use something like record.save(:params => { :a => 1 }). Save might well need other options in the future and it's just not very clear that saving with a hash would lead to extra parameters on the URL.

11/25/07 22:30:20 changed by david

  • keywords changed from verified activeresource save update create to activeresource save update create.

(in reply to: ↑ 7 ) 11/26/07 00:08:41 changed by trek

Replying to david:

I'm not sure I understand the use case. Could you explain how and when you're in need of this? If there's a convincing use case, we should at the very least adopt a similar approach as the finder and use something like record.save(:params => { :a => 1 }). Save might well need other options in the future and it's just not very clear that saving with a hash would lead to extra parameters on the URL.

The specific case we kept running into when examining other REST-style APIs was "auth token" credentials for data updates on behalf of a user (e.g. "3rd party application identified by api key '35e70567abfb2ef6540ac07f84a7934c' is making an update request for item 55 on behalf of the user 'dhh' using dhh's auth_token '9ab42ec819cddf912'").

Whether this auth token should be a part of the created/updated item's parameters or should exist as a sibling to that data seems to vary from service to service.

So,

parameters =  {
:item => {:name => "I have a name", 
          :description => "Lorem ipsum dolor sit amet..."},
:auth_token => "9ab42ec819cddf912"
}

or

parameters =  {
:item => {:name => "I have a name",
          :description => "Lorem ipsum dolor sit amet..."
		      :auth_token => "9ab42ec819cddf912"}

are both potential request types.

Currently save (and its call to create/update) doesn't take any arguments so additional parameters can't be added. I agree that if save/create/update do indeed need to pass additional data in this format they should probably follow approach of the finders and pass :params => {:a => 1, :b => 2} as an argument.

If you think that this is a viable use case I can update the patch to reflect this style.

Some examples of what we ran into:

http://www.flickr.com/services/api/misc.userauth.html
"Users should be authenticated using the Authentication API. The auth_token and api_sig parameters should then be passed along with each request. For more details, please read the Auth API Spec."

http://upcoming.yahoo.com/services/api/token_auth.php
"Now that you have the proper token for the user, you may authenticate any method calls that require or support it by including the parameter token in your method calls, either via GET or POST. All methods should accept the token instead of the traditional username and password."

twitter is moving towards this for their REST API
http://groups.google.com/group/twitter-development-talk/web/api-documentation
"At the time of writing, the Twitter engineers are working on an additional authentication scheme similar to Google’s AuthSub or Flickr’s API Authentication. "

11/26/07 00:11:18 changed by trek

  • summary changed from [PATCH] Added support for additional query parameters on ActiveResrouce save requests to [PATCH] Added support for additional query parameters on ActiveResource save requests.

11/26/07 16:48:59 changed by david

Ah, okay. So you're making this addition to get ARes to work with other services not necessarily done in Rails. That's fine. I like that addition, then. But yeah, definitely want to use the :params => style.

11/27/07 03:58:19 changed by trek

  • attachment add_additional_query_parameters_on_active_resource_save_using_params_option.diff added.

updated patch using :params => style seen in ARes finders

(follow-up: ↓ 13 ) 01/28/08 07:54:11 changed by praxis

Has there been any (related) updates for this?

I have a similar case where I would want to pass username and password as params to a service that needs them in order to return the appropriate xml. Also, requests to the service don't use the .xml extension, but rather .ashx handlers that generate xml that will be returned.

http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/1ef867e7060c8cf3

(in reply to: ↑ 12 ; follow-up: ↓ 14 ) 01/28/08 13:00:03 changed by trek

Replying to praxis:

Has there been any (related) updates for this?

Hey praxis, no, no related updates. If this patch is something you'd like or need you should draw the attention of the rails core group: http://groups.google.com/group/rubyonrails-core

The patch has been verified, just needs to be applied.

(in reply to: ↑ 13 ) 11/24/08 20:15:47 changed by webficient

Replying to trek:

Replying to praxis:

Has there been any (related) updates for this?

Hey praxis, no, no related updates. If this patch is something you'd like or need you should draw the attention of the rails core group: http://groups.google.com/group/rubyonrails-core The patch has been verified, just needs to be applied.

Hi,

Note: there is a bug in the last version added on 11/27/07 (add_additional_query_parameters_on_active_resource_save_using_params_option.diff).

Line 796 should be:

new? ? create(options) : update(options)

...otherwise, your options are empty when passed to the create/update methods. It was correct in previous patches.

I'm going to submit a Lighthouse ticket to propose this patch gets into Rails but wanted to leave this comment here in case anyone else is trying to append tokens to posts/puts.