Skip to content

Commit 0bbeaa9

Browse files
committed
Add ActiveResource::Base.collection_url
Introduce the `.collection_url` method to leverage the [collection_path][] method and to complement the pre-existing [element_url][] and [element_path][] pairing. [collection_path]: https://rubydoc.info/gems/activeresource/ActiveResource/Base#collection_path-class_method [element_path]: https://rubydoc.info/gems/activeresource/ActiveResource/Base#element_path-class_method [element_url]: https://rubydoc.info/gems/activeresource/ActiveResource/Base#element_url-class_method
1 parent 9c8a2ee commit 0bbeaa9

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

lib/active_resource/base.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,31 @@ def collection_path(prefix_options = {}, query_options = nil)
886886
"#{prefix(prefix_options)}#{collection_name}#{format_extension}#{query_string(query_options)}"
887887
end
888888

889+
# Gets the collection URL for the REST resources. If the +query_options+ parameter is omitted, Rails
890+
# will split from the +prefix_options+.
891+
#
892+
# ==== Options
893+
# * +prefix_options+ - A hash to add a prefix to the request for nested URLs (e.g., <tt>:account_id => 19</tt>
894+
# would yield a URL like <tt>/accounts/19/purchases.json</tt>).
895+
# * +query_options+ - A hash to add items to the query string for the request.
896+
#
897+
# ==== Examples
898+
# Post.collection_url
899+
# # => https://example.com/posts.json
900+
#
901+
# Comment.collection_url(:post_id => 5)
902+
# # => https://example.com/posts/5/comments.json
903+
#
904+
# Comment.collection_url(:post_id => 5, :active => 1)
905+
# # => https://example.com/posts/5/comments.json?active=1
906+
#
907+
# Comment.collection_url({:post_id => 5}, {:active => 1})
908+
# # => https://example.com/posts/5/comments.json?active=1
909+
#
910+
def collection_url(prefix_options = {}, query_options = nil)
911+
URI.join(site, collection_path(prefix_options, query_options)).to_s
912+
end
913+
889914
alias_method :set_primary_key, :primary_key= # :nodoc:
890915

891916
# Builds a new, unsaved record using the default values from the remote server so

test/cases/base_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,10 @@ def test_collection_path
773773
assert_equal "/people.json", Person.collection_path
774774
end
775775

776+
def test_collection_url
777+
assert_equal "http://37s.sunrise.i:3000/people.json", Person.collection_url
778+
end
779+
776780
def test_collection_path_with_parameters
777781
assert_equal "/people.json?gender=male", Person.collection_path(gender: "male")
778782
assert_equal "/people.json?gender=false", Person.collection_path(gender: false)
@@ -789,6 +793,22 @@ def test_collection_path_with_parameters
789793
assert_equal "/people.json?struct%5Ba%5D%5B%5D=2&struct%5Ba%5D%5B%5D=1&struct%5Bb%5D=fred", Person.collection_path(struct: { :a => [2, 1], "b" => "fred" })
790794
end
791795

796+
def test_collection_url_with_parameters
797+
assert_equal "http://37s.sunrise.i:3000/people.json?name=Test", Person.collection_url(name: "Test")
798+
assert_equal "http://37s.sunrise.i:3000/people.json?name=false", Person.collection_url(name: false)
799+
assert_equal "http://37s.sunrise.i:3000/people.json?name=", Person.collection_url(name: nil)
800+
801+
assert_equal "http://37s.sunrise.i:3000/people.json?name=Test", Person.collection_url("name" => "Test")
802+
803+
# Use includes? because ordering of param hash is not guaranteed
804+
assert Person.collection_url(name: "Test", student: true).include?("/people.json?")
805+
assert Person.collection_url(name: "Test", student: true).include?("name=Test")
806+
assert Person.collection_url(name: "Test", student: true).include?("student=true")
807+
808+
assert_equal "http://37s.sunrise.i:3000/people.json?name%5B%5D=bob&name%5B%5D=your+uncle%2Bme&name%5B%5D=&name%5B%5D=false", Person.collection_url(name: ["bob", "your uncle+me", nil, false])
809+
assert_equal "http://37s.sunrise.i:3000/people.json?struct%5Ba%5D%5B%5D=2&struct%5Ba%5D%5B%5D=1&struct%5Bb%5D=fred", Person.collection_url(struct: { :a => [2, 1], "b" => "fred" })
810+
end
811+
792812
def test_custom_element_path
793813
assert_equal "/people/1/addresses/1.json", StreetAddress.element_path(1, person_id: 1)
794814
assert_equal "/people/1/addresses/1.json", StreetAddress.element_path(1, "person_id" => 1)

0 commit comments

Comments
 (0)