-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposts_representer.rb
More file actions
43 lines (31 loc) · 920 Bytes
/
posts_representer.rb
File metadata and controls
43 lines (31 loc) · 920 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class PostsRepresenter
attr_reader :decorator
delegate :to_json, :as_json, to: :decorator
def initialize(posts, options = {})
@options = options
authors = posts.map(&:author).uniq { |i| i.id }
resource = Resource.new(posts: posts,
authors: authors,
links: links
)
@decorator = PostsDecorator.new(resource)
end
protected
attr_reader :options
def links
{
'posts.author' => {
href: "#{options[:base_url]}/authors/{posts.author}", type: 'authors'
}
}
end
class Resource < OpenStruct; end
class PostsDecorator < Roar::Decorator
include Roar::Representer::JSON
hash :links
collection :posts, class: Post, decorator: PostExtendedRepresenter
nested :linked do
collection :authors, class: Author, decorator: AuthorBaseRepresenter
end
end
end