Introduce Base.query_format for URL encoding values
          #435
        
          
      
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Follow-up to #421
Problem
Not all HTTP APIs support
snake_casequery parameters. Active Resource's built-in finders (like.find(:all, params: { … }),.all(params: { … }),where(…)) do not support transforming keys prior to their encoding.If an API expects camelCase keys (like
?firstName=Matzrather than?first_name=Matz), Active Resource's does not provide a method or hook to override. The already definedBase.query_stringmethod used by the current implementation isprivate. If a consumer were to override it (despite depending on or overridingprivatemethods being discouraged), they would need to be responsible for transforming theHashand encoding it to aString.Proposal
This commit proposed the introduction of the
ActiveResource::Formats::UrlEncodedFormat. It's modeled after theXmlFormatandJsonFormat, and defines anencodemethod with the prior behavior (a call to Hash#to_query). Along with the new class, this commit also introduce a new.query_formatclass attribute (with getter and setter methods), modeled after the.formatclass attribute.Consumers can provide their own implementation with or without depending on the
ActiveResource::Formats::UrlEncodedFormat. For example, callers can camelCase keys:The URL encoding only applies to query parameters. Prefix options remain snake_case:
This commit also includes changes to the
README.mdexample code's query parameters to demonstrate that keys aresnake_caseby default.