Permalink
Browse files

Get rid of nasty crap and do it well

  • Loading branch information...
1 parent 251e383 commit 1ef447b7cc320dc66c851eb65ece5f8d483329e5 @txus txus committed May 28, 2012
Showing with 20 additions and 39 deletions.
  1. +11 −38 lib/hypermodel/empty_collection.rb
  2. +9 −1 test/collection_test.rb
@@ -2,8 +2,6 @@
module Hypermodel
# Public: Represents an empty Collection.
- #
- # FIXME: LET THIS BURN IN HELL FOR ALL ETERNITY
class EmptyCollection
# Public: Initializes a Collection.
#
@@ -27,52 +25,27 @@ def as_json(*opts)
#
# Returns a Hash of the links of the collection. It will include, at least,
# a link to itself.
- #
- # TODO: Discover parents and include them in _links.
def links
+ url = @controller.request.url
+ url = url.split('.')[0..-2].join('.') if url =~ /\.\w+$/
+
+ parent_url = url.split('/')[0..-2].join('/')
+ parent_name = parent_url.split('/')[0..-2].last.singularize
+
_links = {
- self: { href: @controller.polymorphic_url(collection_hierarchy) }
+ self: { href: url },
+ parent_name => { href: parent_url }
}
{ _links: _links }
end
+ # Internal: Constructs an empty _embedded section of the response.
+ #
+ # Returns an empty collection.
def embedded
{ _embedded: { @name => [] } }
end
-
- #######
- private
- #######
-
- def collection_hierarchy
- params = @controller.params.select { |k, v| k =~ /_id/ }
-
- traverse = lambda do |name|
- klass = self.class.const_get(name.singularize.capitalize)
- assoc = klass.associations.detect do |name, metadata|
- types = [Mongoid::Relations::Embedded::In, Mongoid::Relations::Referenced::In]
- params["#{name}_id"] && types.include?(metadata.relation)
- end
-
- if assoc
- parent_id = params["#{assoc.first}_id"]
- parent_name = assoc.first
-
- id = params["#{name}_id"]
- object = id ? self.class.const_get(name.singularize.capitalize).find(id) : name
-
- parent = self.class.const_get(parent_name.capitalize).find(parent_id)
- [traverse[parent_name], object].flatten
- else
- id = params["#{name}_id"]
- object = self.class.const_get(name.capitalize).find(id)
- [object]
- end
- end
-
- traverse[@name]
- end
end
end
View
@@ -21,20 +21,25 @@ def setup
blog_id: @blog.id
)
@comments = @post.comments
+ end
+ def request!
post :index, { blog_id: @blog.id, post_id: @post.id, format: :json }
end
test "it returns a successful response" do
+ request!
assert response.successful?
end
test "returns the self link" do
+ request!
body = JSON.load(response.body)
assert_match %r{/comments}, body['_links']['self']['href']
end
test "returns the comments" do
+ request!
body = JSON.load(response.body)
comments = body['_embedded']['comments']
@@ -50,6 +55,7 @@ def setup
end
test "returns the parent post" do
+ request!
body = JSON.load(response.body)
assert_equal blog_post_url(@blog, @post), body['_links']['post']['href']
end
@@ -59,10 +65,12 @@ def setup
_post = blog.posts.create(title: 'hey') # Post with no comments
post :index, { blog_id: blog.id, post_id: _post.id, format: :json }
-
body = JSON.load(response.body)
assert response.successful?
assert_equal [], body['_embedded']['comments']
+
+ assert_equal blog_post_comments_url(blog.id, _post.id), body['_links']['self']['href']
+ assert_equal blog_post_url(blog.id, _post.id), body['_links']['post']['href']
end
end

0 comments on commit 1ef447b

Please sign in to comment.