Skip to content

Commit

Permalink
Merge pull request #399 from igor-ladkin/encode-alpha-numeric-identif…
Browse files Browse the repository at this point in the history
…ers-for-custom-methods

Encode ID parameter for custom methods
  • Loading branch information
rafaelfranca authored Jan 24, 2024
2 parents 1517cad + e0fa690 commit 9077ec9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/active_resource/custom_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def delete(method_name, options = {})

private
def custom_method_element_url(method_name, options = {})
"#{self.class.prefix(prefix_options)}#{self.class.collection_name}/#{id}/#{method_name}#{self.class.format_extension}#{self.class.__send__(:query_string, options)}"
"#{self.class.prefix(prefix_options)}#{self.class.collection_name}/#{URI.encode_www_form_component(id.to_s)}/#{method_name}#{self.class.format_extension}#{self.class.__send__(:query_string, options)}"
end

def custom_method_new_element_url(method_name, options = {})
Expand Down
11 changes: 11 additions & 0 deletions test/cases/base/custom_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,15 @@ def test_paths_without_format
ensure
ActiveResource::Base.include_format_in_path = true
end

def test_custom_element_method_identifier_encoding
luis = { person: { id: "luís", name: "Luís" } }.to_json

ActiveResource::HttpMock.respond_to do |mock|
mock.get "/people/lu%C3%ADs.json", {}, luis
mock.put "/people/lu%C3%ADs/deactivate.json", {}, luis, 204
end

assert_equal ActiveResource::Response.new(luis, 204), Person.find("luís").put(:deactivate)
end
end

0 comments on commit 9077ec9

Please sign in to comment.