Skip to content

Commit

Permalink
Add url_for example for links
Browse files Browse the repository at this point in the history
  • Loading branch information
remear committed Mar 4, 2016
1 parent 97ed37e commit 32bfdb0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 2 additions & 0 deletions docs/general/rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ link(:link_name) { "https://example.com/resource/#{object.id}" }

link(:link_name) { resource_url(object) }

link(:link_name) { url_for(controller: 'controller_name', action: 'index', only_path: false) }

```

### serializer_opts
Expand Down
18 changes: 9 additions & 9 deletions test/adapter/json_api/links_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ class LinksTest < ActiveSupport::TestCase
LinkAuthor = Class.new(::Model)
class LinkAuthorSerializer < ActiveModel::Serializer
link :self do
href "//example.com/link_author/#{object.id}"
href "http://example.com/link_author/#{object.id}"
meta stuff: 'value'
end
link(:author) { link_author_url(object.id) }
link(:link_authors) { link_authors_url }
link :other, '//example.com/resource'
link(:link_authors) { url_for(controller: 'link_authors', action: 'index', only_path: false) }
link(:posts) { link_author_posts_url(object.id) }
link :resource, 'http://example.com/resource'
link :yet_another do
"//example.com/resource/#{object.id}"
"http://example.com/resource/#{object.id}"
end
end

Expand All @@ -35,15 +35,15 @@ def test_toplevel_links
adapter: :json_api,
links: {
self: {
href: '//example.com/posts',
href: 'http://example.com/posts',
meta: {
stuff: 'value'
}
}
}).serializable_hash
expected = {
self: {
href: '//example.com/posts',
href: 'http://example.com/posts',
meta: {
stuff: 'value'
}
Expand Down Expand Up @@ -74,16 +74,16 @@ def test_resource_links
hash = serializable(@author, adapter: :json_api).serializable_hash
expected = {
self: {
href: '//example.com/link_author/1337',
href: 'http://example.com/link_author/1337',
meta: {
stuff: 'value'
}
},
author: 'http://example.com/link_authors/1337',
link_authors: 'http://example.com/link_authors',
other: '//example.com/resource',
resource: 'http://example.com/resource',
posts: 'http://example.com/link_authors/1337/posts',
yet_another: '//example.com/resource/1337'
yet_another: 'http://example.com/resource/1337'
}
assert_equal(expected, hash[:data][:links])
end
Expand Down

0 comments on commit 32bfdb0

Please sign in to comment.